Event.observe(window, 'load', function() {

	var hasQuicktime = AC.Detector.isQTInstalled();

	Element.setStyle('paramount-scroller', {opacity: 0.01});
	
	iTunesDrawers = new AC.SectionBureau('itunes-drawers');
	
	var initialDrawerId = document.location.search.toQueryParams()['section'];
	var initialDrawer = null;
	
	var drawerNodes = $('itunes-drawers').getElementsByTagName('li');
	
	for(var i = 0; i < drawerNodes.length; i++) {
		
		var drawerBits = drawerNodes[i].childNodes;

		var drawerActualBits = [];
		
		for(var k = 0; k < drawerBits.length; k++) {
			if(drawerBits[k].nodeType == 1) {
				drawerActualBits.push(drawerBits[k]);
			}
		}
		
		
		var view = null;
		var handle = null;
		
		for(var j = 0; j < drawerActualBits.length; j++) {
			
			if(Element.hasClassName(drawerActualBits[j], 'view')) {
				view = drawerActualBits[j];
				view.hide();
			} else if(Element.hasClassName(drawerActualBits[j], 'trigger')) {
				handle = drawerActualBits[j];
			}
		}
		
		var options = {
			transitionDuration: 0.3,
			triggerEvent: 'mouseover',
			triggerDelay: 300
		}
		
		if (drawerNodes[i].id == 'tv') {
			
			var showContent = function(watchedMovie) {
				
				$('tv-background').show();
				
				if(!watchedMovie) {
				
					$('tv-headline').show();
					$('tv-platforms').show();
					if(hasQuicktime) {
						$('tv-intro-replay').show();
					}
				
				} else {
				
					setTimeout(function() {
						new Effect.Appear('tv-headline', {
							duration: 1.5});
					}, 200);
			
					setTimeout(function() {
						new Effect.Appear('tv-platforms', {
							duration: 0.8});
					}, 2200);
			
					if(hasQuicktime) {
						setTimeout(function() {
							new Effect.Appear('tv-intro-replay', {
								duration: 0.8});
						}, 2200);
					}
				}
				
				iTunesDrawers.getFirstDrawer().reportFinishedOpening()
			}
			
			options.afterOpen = function() {
				
				//check for cookie
				var hasCookie = true;
				var cookies = document.cookie.split(';');
				
				for(var i = 0; i < cookies.length; i++) {
					if (cookies[i].match(/tvIntro=yes/)) {
						hasCookie = true;
					}
				}
				
				
				if(!hasCookie && hasQuicktime) {
					//save cookie to bypass movie for the rest of today
					var tomorrow = new Date();
					tomorrow.setDate(tomorrow.getDate() + 1);
					document.cookie = 'tvIntro=yes; expires=' + tomorrow.toGMTString() + '; path=/; domain=apple.com';
				
					var movieUrl = $('tv-intro-replay').getAttribute('href');

					var tvIntro = AC.Quicktime.packageMovie('tvIntro',
						movieUrl, {
							width: 896,
							height: 432,
							showLogo: false,
							controller: false,
							autoplay: true,
							cache: true});
						
					$('appletv-ad').appendChild(tvIntro);
				
					this.controller = new AC.QuicktimeController(tvIntro, {
						onMoviePlayable: function() {
							this.reportFinishedOpening();
							this.controller.monitorMovie(tvIntro);
						
						}.bind(this),
					
						onMovieFinished: function() {
							this.controller.detachFromMovie();

							//need to force the movie to hide in ie
							tvIntro.style.display = 'none';

							//oddly enough this seems to be the most seamless 
							//way of stitching the movie to the image
							Element.show('tv-background')
							$('appletv-ad').replaceChild($('tv-background').cloneNode(false), tvIntro);
						
							//release reference for IE to aid in removal
							introMovie = null;
						
							showContent(true);
						
						}.bind(this)
					
					});

					//opera doens't have a javascript interface 
					//for quicktime, but it works perfectly without it
					//in this case, so let it report that it's done
					if(AC.Detector.isOpera()) {
						this.reportFinishedOpening();
					}
					
				} else {
					//no movie to load so simply report that we're open
					showContent(false);
				}
				
			}
			
			options.beforeClose = function() {
				
				if(this.controller != null) {
					this.controller.Rewind();
					
					//need to force the movie to hide in ie
					if(this.controller.movie != null) {
						this.controller.movie.style.display = 'none';
					}
					
					this.controller.detachFromMovie();
					
					this.controller = null;
				}
				
				var adContainer = $('appletv-ad');
				
				while(adContainer.hasChildNodes()) {
					adContainer.removeChild(adContainer.firstChild);
				}
				
				//hide all the images inside the drawer
				$('tv-headline').hide();
				$('tv-platforms').hide();
				$('tv-intro-replay').hide();
				
			}
			
			Event.observe('tv-intro-replay', 'mousedown', function(evt) {

				//on replay act just as if the drawer were being opened for 
				//the first time: clear the cookie
				var yesterday = new Date();
				yesterday.setDate(yesterday.getDate() - 1);
				document.cookie = 'tvIntro=yes; expires=' + yesterday.toGMTString() + '; path=/; domain=apple.com';

				
				iTunesDrawers.getFirstDrawer().beforeClose();
				iTunesDrawers.getFirstDrawer().afterOpen();
				
				Event.stop(evt);
			});
			
		} else if (drawerNodes[i].id == 'itunes-movie') {

			var startScroller = function() {
				
				var scroller = null;
				
				return(function() {
					
					if(!scroller) {
						
						var scrollContainer = $('paramount-scroller')
						
						new Effect.Appear(scrollContainer, {from: 0.01, to: 1.0, duration: 4.2});
						
						var scrollingItems = scrollContainer.getElementsByTagName('img');

						scroller = new AC.Scrollinator(scrollContainer,
							AC.Scrollinator.Strategies.Right,
							scrollingItems);
					}

					this.reportFinishedOpening();
				})
			}
			
			options.afterOpen = startScroller();
		}
		
		var drawer = new AC.SectionDrawer(view, handle, iTunesDrawers, options);
		iTunesDrawers.addDrawer(drawer);
		
		if(drawerNodes[i].id == initialDrawerId) {
			initialDrawer = drawer;
		}
	}
	
	if(initialDrawer == null) {
		initialDrawer = iTunesDrawers.drawers[0];
	}
	
	initialDrawer.open();
	
});
