/**
	THIS FILE REQUIRES jquery.js
*/
var COVERFLOW = {
	loadContent: function(){
		// start ajax magic
		// Load in the correct XML file based on date 'movieCarouselDate' in the prepareDatedFiles.php file
		$jq.ajax({
			type: "GET",
			url: "_xml/movies/movies_carousel_"+movieCarouselDate+".xml",
			dataType: "xml",
			success: COVERFLOW.parseXML,
			error: function(){
				alert('Error loading XML, File with this date doesnt exist');
			}
		});	
	},
	/* PARSE THE XML */
	parseXML: function(xml){
		$jq(xml).find('movie').each(function(){
			// variables
			var movie = $jq(this),
				movieIMG = movie.attr('url'),
				movieTitle = movie.attr('title'),
				movieLink = movie.attr('link'),
				buttonText = movie.attr('buttonText'),
				sponsorLink = movie.attr('sponsorLink'),
				sponsorImg = movie.attr('sponsorImage'),
				logoImg = movie.attr('logoImage'),
				
				showTime = movie.find('showtime').text(),
				description = movie.find('description').text(),
				slideNumber = 0;
				
			//create each image
			$jq('<img alt="'+movieTitle+'" src="'+movieIMG+'"/>').appendTo('#coverFlowContainer');
			
			// create each overlay
			$jq('<div class="image-overlay"><div class="overlayContent"><div class="overlayHeaderBG"></div><div class="overlayHeader"><h2>'+movieTitle+'</h2><h3>'+showTime+'</h3></div><div class="overlayBodyBG"></div><div class="overlayBody"><h4>'+description+'</h4></div></div></div>').appendTo('#heroArea');
			
			$jq('.image-overlay').each(function(index, element){
				$jq(element).attr("id", "coverflowOverlay"+index);
			});				
		});
		
		
		// Hide coverflow and show loading image
		$jq("div.cover-flow").hide();
		$jq("#heroArea").append("<img id='loaderGif' src='_images/anim-loader.gif' />");
		$jq("#loaderGif").css({
			'left' : '45%',
			'top' : '50%',
			'position' : 'absolute'
		});
		
		/* THIS IS NEEDED FOR SAFARI/WEBKIT */
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
			setTimeout(initCoverFlow,8000);
		} else {
			setTimeout(initCoverFlow,5000);
		}
		
		// fade out loading gif and fade in cover flow
		function initCoverFlow() {
			$jq("#loaderGif").fadeOut('normal', function(){
				$jq("div.cover-flow").fadeIn('normal');
			});
			
			// ie6
			if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 9){
				// do nothing if its IE6
			} else {
				$jq("div.cover-flow img").each(function(){
					$jq(this).reflect({/* Put custom options here */});
					$jq(this).next("canvas").css({
						"width" : "100%"
					});
					
					$jq(this).css({
						"height" : "100%",
						"width" : "100%"
					});
				});		
			}	
			// start coverflow
			COVERFLOW.init();
			
			// set timer for auto rotating
			autoRotateID = setInterval(COVERFLOW.autoControls,8000);
			$jq('#main-content').children().hover(function(){
				clearInterval(autoRotateID);
			}, function(){
				autoRotateID = setInterval(COVERFLOW.autoControls,8000);	
			});			
			
		}
		
	},
	
	resetClicks:function(){
		// This controls how many slides to scroll based on which one you click
		$jq('div.cover-flow').children().click(function(){
			//console.log(parseInt($jq(this).css('left'), 10));
			switch(parseInt($jq(this).css('left'), 10)){
				case 75:
					COVERFLOW.goPrevious(3);
					break;
				case 131:
					COVERFLOW.goPrevious(2);
					break;
				case 219:
					COVERFLOW.goPrevious(1);
					break;
				case 775:
					COVERFLOW.goNext(3);
					break;
				case 675:
					COVERFLOW.goNext(2);
					break;
				case 520:
					COVERFLOW.goNext(1);
					break;
				case 340:
					// do the show thing
					break;
			}
			clearInterval(autoRotateID);
		});
	},
	
	init: function () {
		// setup fadeToggle function
		$jq.fn.fadeToggle = function(speed, easing, callback) {
		  return this.animate({opacity: 'toggle'}, speed, easing, callback); 
		};
	
		// activate pagination controls
		COVERFLOW.pagiControls();
		
		//Coordinates of each displayed image
		animObj = new Array();
		animObj[0] = {'top': '0px', 'left': '340px', 'width': '233px', 'height': '346px'}; // Main
		// right side
		animObj[1] = {'top': '68px', 'left': '520px', 'width': '180px', 'height': '267px'}; // Right 1
		animObj[2] = {'top': '148px', 'left': '675px', 'width': '118px', 'height': '175px'}; // Right 2
		animObj[3] = {'top': '208px', 'left': '775px', 'width': '71px', 'height': '105px'}; // Right 3
		// left side
		animObj[4] = {'top': '68px', 'left': '219px', 'width': '180px', 'height': '267px'}; // Left 1
		animObj[5] = {'top': '148px', 'left': '131px', 'width': '118px', 'height': '175px'}; // Left 2
		animObj[6] = {'top': '208px', 'left': '75px', 'width': '71px', 'height': '105px'}; // Left 3
		 
		//animObj[7] = {'top': 208, 'left': 75, 'width': 71, 'height': 105}; // (offscreen)
		
		// Next and Prev buttons click events		
		$jq('#coverflowPrevious').click(function(){
			clearInterval(autoRotateID);
			COVERFLOW.goPrevious(1);
		});
		$jq('#coverflowNext').click(function(){
			clearInterval(autoRotateID);
			COVERFLOW.goNext(1);
		});

		// grab array of all images
		this.imageArray = $jq('div.cover-flow').children(); //Array of all images
		
		
		if (COVERFLOW.imageArray.length > 4 && COVERFLOW.imageArray.length < 7) {
			this.origLength = COVERFLOW.imageArray.length;
			//Clone more images to make full carousel
			$jq(COVERFLOW.imageArray).each(
				function () {
					$jq(this).clone().appendTo('div.cover-flow');				
				});	
			this.imageArray = $jq('div.cover-flow').children(); //re-set var to get new length
		}
		
		this.maxNode = (COVERFLOW.imageArray.length) - 1; //Highest node index in imageArray
		this.current = 0; //Current array node of image in forefront
		
		//Position first 7 images, hide rest offscreen
		var highestZ = 10;		
		$jq(COVERFLOW.imageArray).each(function () {
			var index = $jq(COVERFLOW.imageArray).index($jq(this));	
			if (index < 4) {
				if (index == COVERFLOW.maxNode) {
					$jq(this).css(animObj[4]);
					$jq(this).css('z-index', highestZ);
				} else {
					$jq(this).css(animObj[index]);
					$jq(this).css('visibility', 'visible');
					$jq(this).css('z-index', highestZ);
				}
				highestZ = highestZ - 2;
			} else if (index == COVERFLOW.maxNode) {
				$jq(this).css(animObj[4]);
				$jq(this).css('z-index', highestZ);
			} else {
				$jq(this).css(animObj[5]);
				$jq(this).css('z-index', 1 );					
			}
			$jq(this).css('cursor', 'pointer');
			$jq(this).css('visibility', 'visible');		
			$jq('div.cover-flow div:eq(0)').addClass('current-slide');
			
		});	
									
		//Display initial caption and overlay info, make main image clickable
		//CLick event for each slides informative overlay
		$jq('.current-slide').click(function(){
			$jq('#coverflowOverlay0').fadeToggle("normal");
		});
		
		
		// Set positioning for image 3 in ie6
		if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 8){
			$jq('div.cover-flow img:eq(4)').css(animObj[6]);
		} else {
			// position 5th image in right place on initial load- far left
			$jq('div.cover-flow div:eq(4)').css(animObj[6]);	
		}				
				
		//$jq('#coverflowCaption').html($jq(COVERFLOW.imageArray[0]).attr("alt"));
		// make cursor a pointer
		$jq(COVERFLOW.imageArray[0]).css('cursor', 'pointer');
		
		// activate click events
		COVERFLOW.resetClicks();				
	},
	
	goPrevious: function (steps) {
		//COVERFLOW.pagination();
		//Disable click handlers until animation finishes
		$jq('#coverflowPrevious').unbind();
		$jq('#coverflowNext').unbind();
		$jq('div.cover-flow').children().unbind();	
		$jq('div.cover-flow').children().css('cursor', 'default');	
		$jq('.image-overlay').hide(); //hide all caption elements		

		// Set intitial positions
		var prevImg1, prevImg2, nextImg1, nextImg2, nextImg3, nextImg4;
		
		prevImg1 = COVERFLOW.current - 1;
		if (prevImg1 < 0) { prevImg1 = COVERFLOW.maxNode; }
		
		prevImg2 = prevImg1 - 1;
		if (prevImg2 < 0) { prevImg2 = COVERFLOW.maxNode; } 
		
		nextImg1 = COVERFLOW.current + 1;
		if (nextImg1 > COVERFLOW.maxNode) { nextImg1 = 0; }
		
		nextImg2 = nextImg1 + 1;
		if (nextImg2 > COVERFLOW.maxNode) { nextImg2 = 0; }
		
		nextImg3 = nextImg2 + 1;
		if (nextImg3 > COVERFLOW.maxNode) { nextImg3 = 0; }
				
		nextImg4 = nextImg3 + 1;
		if (nextImg4 > COVERFLOW.maxNode) { nextImg4 = 0; }
 
		$jq(COVERFLOW.imageArray[prevImg1]).css('visibility', 'visible');
		$jq(COVERFLOW.imageArray[prevImg1]).css('z-index', 10);
		$jq('div.cover-flow').children().removeClass('current-slide');
		$jq(COVERFLOW.imageArray[prevImg1]).addClass('current-slide').animate(animObj[0], 180, 'swing', function ()  {						
			var slideCounter;
			if (typeof $jq('div#coverflowOverlay' + prevImg1).attr("id") != "undefined") {
				slideCounter = prevImg1;
			}else {
				slideCounter = prevImg1 - COVERFLOW.origLength;
			}
			
			$jq('.current-slide').click(function(){
				$jq('#coverflowOverlay' +  slideCounter).fadeToggle("normal");
			});
													
			//$jq('#coverflowCaption').html($jq(this).attr("alt"));			
			/*$jq(this).click(function () {
				window.location = $jq('#coverflowOverlay' + slideCounter + ' .more-button a').attr("href");
			});*/
			
			$jq(this).css('cursor', 'pointer');			
			$jq('#coverflowPrevious').click(function () {COVERFLOW.goPrevious(1);});
			$jq('#coverflowNext').click(function () {COVERFLOW.goNext(1);});
			if(steps == 2){ COVERFLOW.goPrevious(1); }		
			if(steps == 3){ COVERFLOW.goPrevious(2); }
			if(steps == 4){ COVERFLOW.goPrevious(3); }
			if(steps == 5){ COVERFLOW.goPrevious(4); }
			if(steps == 6){ COVERFLOW.goPrevious(5); }
			if(steps == 7){ COVERFLOW.goPrevious(6); }
			if(steps == 8){ COVERFLOW.goPrevious(7); }	
		 });
		
		// Below is all animations
		$jq(COVERFLOW.imageArray[COVERFLOW.current]).css('z-index', 8);
		$jq(COVERFLOW.imageArray[COVERFLOW.current]).animate(animObj[1], 180, 'swing', function(){
			$jq(this).css('cursor', 'pointer');			
			//$jq(this).click(function(){COVERFLOW.goNext(1);});
		});
		if (COVERFLOW.maxNode > 2) {
			if (COVERFLOW.maxNode > 3) {
				$jq(COVERFLOW.imageArray[nextImg1]).css('z-index', 6);
				$jq(COVERFLOW.imageArray[nextImg1]).animate(animObj[2], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goNext(2);});
				});

				$jq(COVERFLOW.imageArray[nextImg2]).css('z-index', 4);
				$jq(COVERFLOW.imageArray[nextImg2]).animate(animObj[3], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goNext(3);});
				});

				$jq(COVERFLOW.imageArray[nextImg3]).css('z-index', 2);
				$jq(COVERFLOW.imageArray[nextImg3]).animate(animObj[6], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goPrevious(1);});
				});

				$jq(COVERFLOW.imageArray[nextImg4]).css('z-index', 3);
				$jq(COVERFLOW.imageArray[nextImg4]).animate(animObj[5], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goPrevious(2);});
				});

				$jq(COVERFLOW.imageArray[prevImg2]).css(animObj[5]); // left of center forreals	(sometimes)	
				$jq(COVERFLOW.imageArray[prevImg2]).css('z-index', 8);
				$jq(COVERFLOW.imageArray[prevImg2]).animate(animObj[4], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goPrevious(1);});
				});
			} else {
				$jq(COVERFLOW.imageArray[nextImg1]).css('z-index', 6);
				$jq(COVERFLOW.imageArray[nextImg1]).animate(animObj[2], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goNext(2);});
				});

				$jq(COVERFLOW.imageArray[nextImg2]).css('z-index', 2); // left of center
				$jq(COVERFLOW.imageArray[nextImg2]).animate(animObj[4], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){COVERFLOW.goPrevious();});
				});				
			}		
		} else {
			$jq(COVERFLOW.imageArray[nextImg1]).css('z-index', 2);
			$jq(COVERFLOW.imageArray[nextImg1]).animate(animObj[4], 180, 'swing', function(){
				$jq(this).css('cursor', 'pointer');			
				//$jq(this).click(function(){COVERFLOW.goPrevious();});
			});
		}

		COVERFLOW.current = COVERFLOW.current - 1;
		if (COVERFLOW.current < 0) { COVERFLOW.current = COVERFLOW.maxNode; }	
		COVERFLOW.pagination();
		COVERFLOW.resetClicks();
	},
	
	goNext: function(steps) {
		//Disable click handlers until animation finishes
		$jq('#coverflowPrevious').unbind();
		$jq('#coverflowNext').unbind();
		$jq('div.cover-flow').children().unbind();
		$jq('div.cover-flow').children().css('cursor', 'default');
		$jq('.image-overlay').hide(); //hide all caption elements		
				
		var prevImg1, prevImg2, nextImg1, nextImg2, nextImg3, nextImg4;
		
		prevImg1 = COVERFLOW.current - 1;
		if (prevImg1 < 0) { prevImg1 = COVERFLOW.maxNode; }
		
		prevImg2 = prevImg1 - 1;
		if (prevImg2 < 0) { prevImg2 = COVERFLOW.maxNode; }
		
		nextImg1 = COVERFLOW.current + 1;
		if (nextImg1 > COVERFLOW.maxNode) { nextImg1 = 0; }
		
		nextImg2 = nextImg1 + 1;
		if (nextImg2 > COVERFLOW.maxNode) { nextImg2 = 0; }
		
		nextImg3 = nextImg2 + 1;
		if (nextImg3 > COVERFLOW.maxNode) { nextImg3 = 0; }
				
		nextImg4 = nextImg3 + 1;
		if (nextImg4 > COVERFLOW.maxNode) { nextImg4 = 0; }
		
		$jq(COVERFLOW.imageArray[nextImg1]).css('z-index', 10);
		$jq('div.cover-flow').children().removeClass('current-slide');
		$jq(COVERFLOW.imageArray[nextImg1]).addClass('current-slide').animate(animObj[0], 180, 'swing', function ()  {
			var slideCounter;
			if (typeof $jq('div#coverflowOverlay' + nextImg1).attr("id") != "undefined") {
				slideCounter = nextImg1;
			}else {
				slideCounter = nextImg1 - COVERFLOW.origLength;
			}
			
			$jq('.current-slide').click(function(){
				$jq('#coverflowOverlay' +  slideCounter).fadeToggle("normal");
			});			
			
			//$jq('#coverflowOverlay' +  slideCounter).fadeIn("slow");									
			//$jq('#coverflowCaption').html($jq(this).attr("alt"));			
			/*$jq(this).click(function () {
				window.location = $jq('#coverflowOverlay' + slideCounter + ' .more-button a').attr("href");
			});*/
			$jq(this).css('cursor', 'pointer');
			$jq('#coverflowPrevious').click(function () { COVERFLOW.goPrevious(1); });
			$jq('#coverflowNext').click(function () { COVERFLOW.goNext(1); });	
			if(steps == 2){ COVERFLOW.goNext(1); }		
			if(steps == 3){ COVERFLOW.goNext(2); }
			if(steps == 4){ COVERFLOW.goNext(3); }
			if(steps == 5){ COVERFLOW.goNext(4); }
			if(steps == 6){ COVERFLOW.goNext(5); }
			if(steps == 7){ COVERFLOW.goNext(6); }
			if(steps == 8){ COVERFLOW.goNext(7); }	
		});

		$jq(COVERFLOW.imageArray[COVERFLOW.current]).css('z-index', 8);
		$jq(COVERFLOW.imageArray[COVERFLOW.current]).animate(animObj[4], 180, 'swing', function(){
			$jq(this).css('cursor', 'pointer');			
			//$jq(this).click(function(){ COVERFLOW.goPrevious(); });
		});	

		if (COVERFLOW.maxNode > 2) {
			if (COVERFLOW.maxNode > 3) {
				$jq(COVERFLOW.imageArray[prevImg1]).css('z-index', 2);
				$jq(COVERFLOW.imageArray[prevImg1]).animate(animObj[5], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
					//$jq(this).click(function(){COVERFLOW.goPrevious();});
				});

				$jq(COVERFLOW.imageArray[nextImg2]).css('z-index', 8);
				$jq(COVERFLOW.imageArray[nextImg2]).animate(animObj[1], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){ COVERFLOW.goNext(1); });
				});

				$jq(COVERFLOW.imageArray[nextImg3]).css('z-index', 6);
				$jq(COVERFLOW.imageArray[nextImg3]).animate(animObj[2], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){ COVERFLOW.goNext(2); });
				});

				$jq(COVERFLOW.imageArray[nextImg4]).css(animObj[6]);
				$jq(COVERFLOW.imageArray[nextImg4]).css('z-index', 1);
				$jq(COVERFLOW.imageArray[nextImg4]).animate(animObj[3], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){ COVERFLOW.goNext(3); });
				});

				//$jq(COVERFLOW.imageArray[prevImg2]).css('z-index', 2);
				$jq(COVERFLOW.imageArray[prevImg2]).animate(animObj[6], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
					//$jq(this).click(function(){COVERFLOW.goPrevious();});
				});
				$jq(COVERFLOW.imageArray[prevImg2]).css('z-index', 1);

			} else {
				$jq(COVERFLOW.imageArray[nextImg2]).css('z-index', 8);
				$jq(COVERFLOW.imageArray[nextImg2]).animate(animObj[1], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){ COVERFLOW.goNext(1); });
				});

				$jq(COVERFLOW.imageArray[prevImg1]).css('z-index', 1);
				$jq(COVERFLOW.imageArray[prevImg1]).animate(animObj[2], 180, 'swing', function(){
					$jq(this).css('cursor', 'pointer');			
				//	$jq(this).click(function(){ COVERFLOW.goNext(2); });
				});			
			}		
		
		} else {
			$jq(COVERFLOW.imageArray[prevImg1]).css('z-index', 1);
			$jq(COVERFLOW.imageArray[prevImg1]).animate(animObj[1], 180, 'swing', function(){
				$jq(this).css('cursor', 'pointer');			
				//$jq(this).click(function(){ COVERFLOW.goNext(1); });
			});
		}

		COVERFLOW.current = COVERFLOW.current + 1;
		if (COVERFLOW.current > COVERFLOW.maxNode) { COVERFLOW.current = 0; }
		COVERFLOW.pagination();
		COVERFLOW.resetClicks();
	},
	/* 
		This handles the classes for each active pagination item
	*/
	pagination: function(){
		var currentActive = this.current;
		// This adds or removes the active-slide class to each pagination item
		// by checkign the current slide number
		switch(currentActive){
			case 0:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(0)').addClass('active-slide');
				break;
			case 1:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(1)').addClass('active-slide');
				break;
			case 2:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(2)').addClass('active-slide');
				break;
			case 3:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(3)').addClass('active-slide');
				break;
			case 4:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(4)').addClass('active-slide');
				break;
			case 5:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(5)').addClass('active-slide');
				break;
			case 6:
				$jq('#movies-hero-controls ul li').removeClass('active-slide');
				$jq('#movies-hero-controls ul li:eq(6)').addClass('active-slide');
				break;
			default:
				//alert('hello')
		}
		
		
	},
	
	pagiControls: function(){
		// This controls the actions for when you click a pagination bubble
		$jq('#movies-hero-controls ul li').each(function(index, element){
			$jq(element).click(function(e){
				//alert('2')
				e.preventDefault();
				
				clearInterval(autoRotateID);
				
				if($jq(element).hasClass('active-slide')){
					// do nothing
				} else {
					
					var theIndex = index;
					var activeIndex = $jq('#movies-hero-controls ul li').index($jq('#movies-hero-controls ul li.active-slide'));
					
					//console.log(theIndex - activeIndex);
					
					if(theIndex - activeIndex < 0){
						//clicked to left of active one	
						COVERFLOW.goPrevious(Math.abs(theIndex - activeIndex));				
					}else{
						//clicked to right of active one 
						COVERFLOW.goNext(theIndex - activeIndex);
					}
					
				}
						
			});
		});
		
	},
	
	autoControls: function(){
		// checks to see if the last slide is active.. if it is then stop auto rotate
		//var autoRotateID = 0;
		//if($jq('#movies-hero-controls ul li:eq(6)').hasClass('active-slide')){
		//	clearInterval(autoRotateID);
		//} else {
			COVERFLOW.goNext(1);
		//}
	}	
};



$jq(function(){
	//activate coverflow
	COVERFLOW.loadContent();
}); // END: dom ready
