// JavaScript Document
jQuery.extend( jQuery.easing, {
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
});
var homeMovies = new Array('','02_PR_CommunityActivities','TPR_master_final','09_PR_Location-Access_','10_PR_ExceptionalService','01_PR_GiftToChildren','06_PR_GrandCuisine','11_PR_JustForFun');
//the picture before flash overwrites
function loadFlash() {
	//track video views
	_gaq.push(['_trackEvent', 'Home-Videos', 'Viewed', homeMovies[currImgNum]]);
	var baseVideoPath='video/';
	//stop playing things. Prevent from being able to click another movie in
	$('#shade4').unbind('click');
	autoplay=false;
	clearTimeout(tt);
	$('#imgHolder').stop(true,false);
	if(!$('#shade #MooVWrapper').attr('ID')) {//checks if a video wrapper exists, if not add it.
		var movieContainer = '<div id="MooVWrapper"><video id="videoPlayer" width="608" height="342" controls autoplay><source src="'+baseVideoPath+homeMovies[currImgNum]+'.mp4" type="video/mp4" /></source><source src="'+baseVideoPath+homeMovies[currImgNum]+'.ogv" type="video/ogg; codecs=&quot;theora, vorbis&quot;" /></source><div id="videoWrapper"></div></video></div>';
		var oldContainer=$('#shade .noShade').html();
		$('#shade .noShade').html(oldContainer+movieContainer);
	}
	$('#shade').children('.bShade').fadeTo(500,0.8);
	//make the shadow opaque
	$('#shade .noShade').fadeTo(0,1);
	// Are we dealing with a browser that supports <video>? 
	var v = document.createElement("video"); 
    if ( !v.play ) { // If no, use Flash.
		//add a wrapper for the flash movie to be dropped in.
		var movieContainer = '<div id="MooVWrapper"><div id="videoWrapper"></div></div>';
		$('#shade .noShade').append(movieContainer);
		var t=setTimeout(function() {//because jquery somehow interferes with swfobject.
			var flashvars = {};
			flashvars.MM_ComponentVersion = "1";
			flashvars.skinName = baseVideoPath+"SkinOverPlayStopSeekFullVol.swf";
			flashvars.mooV = homeMovies[currImgNum]+'.mp4';
			flashvars.autoPlay = "true";
			flashvars.autoRewind = "false";
			var params = {};
			params.scale = "noscale";
			params.salign = "tl";
			params.quality = "high";
			params.swfversion = "9";
			params.bgcolor = "#000000";
			params.allowfullscreen = "true";
			params.wmode = "transparent";
			var attributes = {};
			attributes.id = "FLVPlayer";
			swfobject.embedSWF(baseVideoPath+"mp4Player2.swf", "videoWrapper", "610", "344", "9", false, flashvars, params, attributes);
		},550);
	} 
}
//sets current image for text display
var currImgNum = 1;
var autoplay=true;
var tt;
function animateTop2(direction) {
	if(autoplay) {//delay if inital animation loop is active
		//track image views
		_gaq.push(['_trackEvent', 'HomeImages-Autoplay', 'Viewed', 'Img: '+currImgNum]);
		var imgHold=7500;	
	} else {
		imgHold=0;
		//track image views
		_gaq.push(['_trackEvent', 'HomeImages-Clicked', 'Viewed', 'Img: '+currImgNum]);
	}
	if($('#shade .noShade #MooVWrapper').attr('id')) {//resets the video wrapper
		$('#shade4').click(function() {
			loadFlash();
		});
		$('#shade .noShade').fadeTo(0,0);
		$('#videoPlayer').get(0).pause();
		$('#MooVWrapper').remove();
		$('#shade').children('.bShade').fadeTo(250,0.3);
	}
	var picHolders = $('#imgHolder').children('div').length;
	tt=setTimeout(function() {
		if(direction=='Next') {
			//moves the position to compensate for a new item being added to the beggining
			$('#imgHolder').css('left', -1218);
			//the first image child
			var firstPicID =$('#imgHolder :nth-child(1)').attr('id');
			//detaches said image
			var firstPicContent = $('#'+firstPicID).detach();
			//addes it to the end
			$('#imgHolder').append(firstPicContent);
			//curImg for txt display
			if(currImgNum+1>=8) {
				currImgNum=1;
			} else {
				currImgNum+=1;
			}
		} else if(direction=='Previous') {
			//moves the position to compensate for a new item being added to the beggining
			$('#imgHolder').css('left', -2436);
			//the last image child
			var lastPicID =$('#imgHolder :nth-child('+picHolders+')').attr('id');
			//detaches said image
			var lastPicContent = $('#'+lastPicID).detach();
			//addes it to the beggining
			$('#imgHolder').prepend(lastPicContent);
			//curImg for txt display
			if(currImgNum-1<=0) {
				currImgNum=7;
			} else {
				currImgNum-=1;
			}
		}
		//changes text
		$('#slideshowNum').html(currImgNum+' of '+picHolders);
		$('#imgHolder').stop(true, false).animate({
			left: -1827
			}, 1500, 'easeOutQuint', function() {
			if(autoplay) {
				animateTop2('Next');
			}
  		});
  },imgHold);
}
$(document).ready(function() {
	//auto slideshow
	animateTop2('Next');	
	$('#navControls a').click(function() {//actiate slideshow controls
		autoplay=false;
		clearTimeout(tt);
		var direction=$(this).attr('title');
		animateTop2(direction);		 
	});
	//loads click function to the flash player
	$('#shade4').click(function() {
		loadFlash();
	});
});
