// index.js

//var g_cookies=new CookieJar("PA3V");
var g_orient="";
var g_isiPad   = navigator.userAgent.match(/iPad/i) != null;
var g_isIE8   = (jQuery.browser.version<=8);
var g_ActiveTrailer=0;
var g_Scale=1.0;

var g_Animations=[];
var g_Scene=new ra_Animation("scene",1199,651,1,1000,"g_Scene");
g_Animations[g_Animations.length]=g_Scene;

var g_SceneDistortion=new ra_Animation("distortion",1199,651,7,500,"g_SceneDistortion");
g_Animations[g_Animations.length]=g_SceneDistortion;

/*var g_Shadow=new ra_Animation("shadow",755,420,141,2000,"g_Shadow");
g_Animations[g_Animations.length]=g_Shadow;

var g_Frames=new ra_Animation("frames",280,190,60,2000,"g_Frames");
g_Animations[g_Animations.length]=g_Frames;*/

var g_Timecode=new ra_Animation("timecode",135,60,151,150000,"g_Timecode"); // 150 seconds
g_Animations[g_Animations.length]=g_Timecode;

var g_LeftGirl=new ra_Animation("leftgirl",142,155,27,4000,"g_LeftGirl"); // 1.3 seconds
g_Animations[g_Animations.length]=g_LeftGirl;

var g_RightGirl=new ra_Animation("rightgirl",109,60,42,4400,"g_RightGirl"); // 1.3 seconds
g_Animations[g_Animations.length]=g_RightGirl;

var g_Tankflash=new ra_Animation("tankflash",116,86,8,64,"g_Tankflash");
g_Animations[g_Animations.length]=g_Tankflash;

function getOrientation()
	{
	var orient="landscape"
	if ( orientation == 0 || orientation == 180 )   
                orient = 'portrait';
    else if ( orientation == 90 || orientation == -90 )   
                orient = 'landscape';
	return orient;
	}
	
function setupScene()
	{	
	//g_Scene.addFrame("images/animations/scene."+((g_isiPad) ? "jpg":"png"));
	g_Scene.addFrame("images/scene.jpg");
	g_Scene.setPosition("*",0,0);
	g_Scene.setLayer("*",0);
	}

function cbLastDistortionFrame(number){
	g_SceneDistortion.play(0);
	if(!isMute){
		playSound("static",10000);
		if(g_isIE8)
			playSound("static.mp3",10000);
	}
}
	
	
function setupDistortion()
	{
	var i,e=7;
	for(i=1;i<=e;i++)		
		g_SceneDistortion.addFrame("images/animations/"+((g_isiPad) ? "i":"")+"distortion0"+i.toString()+".jpg");
		
	g_SceneDistortion.hide();
	g_SceneDistortion.setPosition("*",0,0);
	g_SceneDistortion.setLayer("*",2);
	// g_SceneDistortion.setCallback_EndOfSequence(cbLastDistortionFrame);
	g_SceneDistortion.setLoop(1,(e-1),100);
	}
	
/*function cbLastShadowFrame(number){
	g_SceneDistortion.play(0);
	if(!isMute){
		playSound("static",10000);
		if(g_isIE8)
			playSound("static.mp3",10000);	
	}
}
	
function setupShadow()
	{
	var i,e=141;
	for(i=0;i<=e;i++){
		if(i<10){
			g_Shadow.addFrame("images/animations/"+((g_isiPad) ? "shadow_ipad/":"")+"shadow0000"+i.toString()+".png");
		}
		else if(i>=10 && i<100){
			g_Shadow.addFrame("images/animations/"+((g_isiPad) ? "shadow_ipad/":"")+"shadow000"+i.toString()+".png");
		}
		else{
			g_Shadow.addFrame("images/animations/"+((g_isiPad) ? "shadow_ipad/":"")+"shadow00"+i.toString()+".png");
		}
	}
		
	g_Shadow.hide();
	g_Shadow.setPosition("*",444,0);
	g_Shadow.setLayer("*",3);
	g_Shadow.setCallback_EndOfSequence(function()
												   {
													g_Shadow.stop();
													g_Shadow.hide();
												   });
	//g_Shadow.setLoop(1,(e-1),100);
	}
	
function setupFrames()
	{
	var i,e=60;
	for(i=0;i<=e;i++){
		if(i<10){
			g_Frames.addFrame("images/animations/frames/frames0000"+i.toString()+".jpg");
			var img = new Image();
			img.src = "images/animations/frames/frames0000"+i.toString()+".jpg";
		}
		else if(i>=10 && i<100){
			g_Frames.addFrame("images/animations/frames/frames000"+i.toString()+".jpg");
			var img = new Image();
			img.src = "images/animations/frames/frames000"+i.toString()+".jpg";
		}
		else{
			g_Frames.addFrame("images/animations/frames/frames00"+i.toString()+".jpg");
			var img = new Image();
			img.src = "images/animations/frames/frames00"+i.toString()+".jpg";
		}
	}
		
	g_Frames.hide();
	//g_Frames.setPosition("*",313,32);
	g_Frames.setPosition("*",314,41.5);
	g_Frames.setLayer("*",2);
	g_Frames.setCallback_EndOfSequence(function(){
		g_Frames.stop();
		g_Frames.showFrame(60);
	});
	//g_Frames.setLoop(1,(e-1),100);
	}*/
	
function setupLeftGirl()
	{
	var i,n;
	for(i=0;i<=13;i++)
		{
		n="000"+i.toString(); n=n.substring(n.length-3);
		g_LeftGirl.addFrame("images/animations/leftgirl"+n+".png");
		}
	for(i=12;i>=0;i--)
		{
		n="000"+i.toString(); n=n.substring(n.length-3);
		g_LeftGirl.addFrame("images/animations/leftgirl"+n+".png");
		}
		
	g_LeftGirl.hide();
	g_LeftGirl.setPosition("*",322,270);
	g_LeftGirl.setLayer("*",1);
		
	g_LeftGirl.setLoop(1,26,6000);	
	}
	
function setupRightGirl()
	{
	var i,n;
	for(i=0;i<=21;i++)
		{
		n="000"+i.toString(); n=n.substring(n.length-3);				
		g_RightGirl.addFrame("images/animations/rightgirl"+n+".png");
		}
	for(i=20;i>=0;i--)
		{
		n="000"+i.toString(); n=n.substring(n.length-3);				
		g_RightGirl.addFrame("images/animations/rightgirl"+n+".png");
		}
	
	
	
	g_RightGirl.hide();
	g_RightGirl.setPosition("*",773,350);	
	g_RightGirl.setLayer("*",1);
	
	g_RightGirl.setLoop(1,41,6000);	
	}
	
	
function setupTankflash()
	{
	var flash="images/animations/fishtanknew01.png";
	var noflash="images/animations/fishtanknew00.png";
	var i;

		g_Tankflash.addFrame(noflash);
		for(i=1;i<8;i++)
			{		
			g_Tankflash.addFrame(flash);
			g_Tankflash.addFrame(noflash);
			}

	
	g_Tankflash.hide();
		g_Tankflash.setPosition("*",0,194);
		g_Tankflash.setDuration(0,500);
		g_Tankflash.setDuration("1-3",52);
		g_Tankflash.setDuration("4-5",64);
		g_Tankflash.setDuration("6-7",52);
		g_Tankflash.setCallback("1,3,5",function(){
			if(!isMute){
				playSound("tanklight",50);
				//if(g_isIE8)
					//playSound("Turning_-Cadu_Vis-998.mp3",50);
			}
		});
	

	g_Tankflash.setLayer("*",1);
	
	
	g_Tankflash.setCallback_EndOfSequence(function()
												   {
													g_Tankflash.stop();
													g_Tankflash.hide();
												   });
	
	
	
	
	}
	
function startTankflash(){	
	if(g_Tankflash.isPlaying()==false)
		g_Tankflash.play(0);
}

function startGirls(){
	g_LeftGirl.play(0);
	g_RightGirl.play(0);
}


function stopTimecode(){
	g_Timecode.stop();
	g_Timecode.hide();
}
	
function startTimecode(){
	g_Timecode.play(0);
}

function stopTimecode(){
	g_Timecode.stop();
	g_Timecode.hide();
}

function cbLastTimecodeFrame(frame){
	g_Timecode.play(0);
}

function cbStartDistortion1(frame){
	startDistortion();
}

function cbStopDistortion1(frame){
	stopDistortion();
}

/*function cbStartShadow(frame){
	g_Shadow.play(0);
	if(!isMute){
		playSound("cymbolbassrumble");
		if(g_isIE8)
			playSound("CymbolBassRumble.mp3");
	}
}

function cbStopShadow(frame){
	g_Shadow.hide();
	g_Shadow.stop();
	
	cbStartFrames();
}

function cbStartFrames(frame){
	g_Frames.play(0);
}

function cbRestartFrames(frame){
	g_Frames.play(0);
	g_Frames.stop();
}*/

function cbRandomTankFlash(){
	startTankflash();
}
	
function setupTimecode(){
	var i,n;
	
	for(i=0;i<=150;i++){
		n="000"+i.toString(); n=n.substring(n.length-4);		
		g_Timecode.addFrame("images/animations/timecode/timecode"+n+".png");
	}
		
	g_Timecode.hide();
	
	g_Timecode.setPosition("*",1000,480);
	g_Timecode.setLayer("*",3);
	g_Timecode.setCallback_EndOfSequence(cbLastTimecodeFrame);
	
	g_Timecode.setCallback(10,startAmbianceSound);
	
	g_Timecode.setCallback(25,cbRandomTankFlash);
	//g_Timecode.setCallback(39,cbStartShadow);
	g_Timecode.setCallback(47,cbRandomTankFlash);
	
	g_Timecode.setDuration("58-75",125); // play frames faster
	g_Timecode.setCallback(58,cbStartDistortion1);
	g_Timecode.setCallback(75,cbStopDistortion1);
	
	g_Timecode.setCallback(79,startAmbianceSound);
	g_Timecode.setCallback(86,cbRandomTankFlash);
	
	//g_Timecode.setCallback(100,cbStartFrames);
	
	g_Timecode.setCallback(122,cbRandomTankFlash);
	
	g_Timecode.setDuration("130-149",125); // play frames faster
	g_Timecode.setCallback(130,cbStartDistortion1);
	g_Timecode.setCallback(149,cbStopDistortion1);
	
	//g_Timecode.setCallback(140,cbRestartFrames);
}
	
function startDistortion(){
	g_SceneDistortion.play(0);
	if(!isMute){
		playSound("static",10000);
		if(g_isIE8)
			playSound("static.mp3",10000);
	}
}
	
function stopDistortion(){
	stopSound("static");
	if(g_isIE8)
		stopSound("static.mp3");
	g_SceneDistortion.stop();
	g_SceneDistortion.hide();
}
	
function setupAnimations(){
	setupScene();
	setupDistortion();	
	//setupShadow();	
	//setupFrames();
	setupTimecode();
	setupLeftGirl();
	setupRightGirl();
	setupTankflash();	
}
	
function scaleAnimations(scale){
	var i,obj;
	for(i=0;i<g_Animations.length;i++){
		obj=g_Animations[i];
		obj.setScale(scale);
		obj.refresh();
	}
}
	
function resizeAnimations(){
	var new_width=$(window).width();
	var new_height=$(window).height();
	var scale;
	if((new_width/new_height)>(1199/651)){
		scale=new_width/1199;
	}
	else{
		scale=new_height/651;
	}
	
	if(g_isiPad){
		g_Scale=scale;
		g_Timecode.setPosition("*",940,490);
	}
	else{
		g_Scale=scale;
		g_Timecode.setPosition("*",980,510);
	}
	scaleAnimations(g_Scale);
	g_Scene.showFrame(0);
	
	centreModalBox();
}
	
function centreModalBox(){
	// reposition trailer overlay
	var bw=g_VideoInfo.width;
	var bh=g_VideoInfo.height;	
	//
	
	var cx=($(window).width()>>1)-(bw>>1);
	var cy=($(window).height()>>1)-(bh>>1);
	cy=20;
	
	$("#modal_over").css({left:cx+"px",top:cy+"px",width:bw+"px",height:bh+"px"});		
	g_VideoInfo.cx=cx;
	g_VideoInfo.cy=cy;
	
	positionPlayerControls();
}
	
function positionPlayerControls(){
	var vc=$("#vid-container");
	var vctop=parseInt(vc.css("top"));
	var vcheight=parseInt(vc.css("height"));
	
	$("#trailer_close_button").css({"top":(vctop-10)+"px","left":(g_VideoInfo.width-10)+"px"});	
	
	if(g_VideoInfo.type=="html5"){
		if(isiPad){
			$("#trailer_links").css({"top":"375px","left":"0px"});
		}
	}
	else{		
		$("#trailer_links").css({"top":"535px","left":"0px"});
	}
}
	
function hideAnimations(){
	var i;
	for(i=0;i<g_Animations.length;i++)		
		g_Animations[i].hide();	
}
	
function stopAnimations(){
	var i;
	for(i=0;i<g_Animations.length;i++){
		g_Animations[i].stop();
	}

	// stop sounds
	var SoundList=["static","buildup_ambience2","buildup_ambience3","cinematicboomnorm","cymbolbassrumble","distortedradioreciever","eeriepiano01","erhcinematicdeepbassrumble","fmflip2"]
	for(i=0;i<SoundList.length;i++)
		stopSound(SoundList[i]);

	g_Scene.showFrame(0);

	stopAmbianceSound();	
}
	
function startAnimations(){
	hideAnimations();
	g_Scene.show();
	g_Scene.showFrame(0);

	startTimecode();
	startGirls();
	resizeAnimations();
}
	
function startAmbianceSound(){
	if(!isMute){
		if(!g_isIE8){
			var sd = document.getElementById('ambiance');
			sd.play();
			sd.volume = 0.3;
		}
	}
}

function stopAmbianceSound(){
	if(!g_isIE8){
		var sd = document.getElementById('ambiance');
		sd.pause();
	}
}
	
function attachTrailerLinkEvent(){
	$(".trailer_link").click(function () {
		var remove_obj=$(".trailer_link");
		remove_obj.removeClass("trailer_active");
		$(this).addClass("trailer_active");
							 
		var id=$(this).attr("id");
		var n=parseInt(id.substring(id.length-1))-1;							 
		playTrailer(n);
	});
}
var isFirstTrailer = true;
var g_VideoInfo={width:928,height:522,type:"flash",cx:0,cy:0};
var html5_player; 
function showTrailerOverlay(start){
	if(!isFirstTrailer){
		if(filmCreditsOn){
			$('#filmCredits').fadeOut();
			filmCreditsOn = false;
		}
		if( reviewsOn ){
			$('#reviews_iframe').fadeOut();
			reviewsOn = false;
		}
		if(ticketsOn){
			$('#buy_tickets_pop').fadeOut();
			ticketsOn = false;
		}
	}
	else{
		isFirstTrailer = false;	
	}
	
	if(isNaN(start))
		start=1;
	
	stopAnimations();
	doModal("divTrailer",cb_videoComplete);
	
	var obj=document.getElementById("div_html5_video");
	if(g_VideoInfo.type=="html5"){
		if(isiPad){
			g_VideoInfo.width=640;
			g_VideoInfo.height=360;
		}
		else{
			obj.style.width = "928px";
			obj.style.height = "522px";
			
			$('#vid-container').css('width','928px');
			$('#vid-container').css('height','522px');	
		}
		g_VideoInfo.type="html5";
		$("#div_html5_video").show();
		
		if(obj!=null){
			
			html5_player = obj;
			
			obj.play();
			obj.addEventListener('playing',function(){
				_gaq.push(['_trackEvent', 'Video: Trailer','Begin']);
			},false);
			obj.addEventListener('durationchange',onDurationChangeHTML5,false);
			obj.addEventListener('timeupdate',onCurrentTimeChangeHTML5,false);
			obj.addEventListener('ended',onEnded,false);
			
			
		}
		
	}
	else{
		//alert("Begin");
		_gaq.push(['_trackEvent', 'Video: Trailer','Begin']);
	}
	
	$("#trailer_close_button").click(cb_videoComplete);
	centreModalBox();
}

function pauseTrailer(){
	if(player != null){
		player.pause();
	}
	else if(g_VideoInfo.type=="html5"){
		player = document.getElementById('div_html5_video');
		player.pause();	
	}
}
	
$(document).ready (function(){
	setupAnimations();	
	setTimeout("showTrailerOverlay()",1000);
	$("#scene").click(function(){														
		// startTankflash();
	});
	$('#btn_mute').click(onClickMute);
});

var isMute = false;

function onClickMute(){
	if(!isMute){
		isMute = true;
		muteAllSounds();
		$('#btn_mute_img').attr('src','img/btn_mute_crossed.png');	
	}
	else{
		isMute = false;
		$('#btn_mute_img').attr('src','img/btn_mute.png');
	}
}

function muteAllSounds(){
	$('audio').each(function(index,elem){
		elem.pause();
	});
}

// flash video player script
function cb_videoComplete(){
	is25 = false;
	is50 = false;
	is75 = false;
	isEnded = false;
	pauseTrailer();
	doModal();
	startAnimations();
}
	
//Strobe Player JS Bridge Functions
var player = null;
var video_duration = null;

var is25 = false;
var is50 = false;
var is75 = false;
var isEnded = false;

function onJavaScriptBridgeCreated(playerId){
	//alert("js bridge");
	//if (player == null) {
		
		player = document.getElementById(playerId);
				
		// Add event listeners that will update the 
		player.addEventListener("currentTimeChange", "onCurrentTimeChange");
		player.addEventListener("durationChange", "onDurationChange");
		player.addEventListener("ended", onEnded);
				
	//}
}
	
function onCurrentTimeChange(time, playerId){
	if((time/video_duration) > 0.25 && !is25){
		//alert("25% watched");
		is25 = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','25% Watched']);
	}
	if((time/video_duration) > 0.5 && !is50){
		//alert("50% watched");
		is50 = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','50% Watched']);
	}
	if((time/video_duration) > 0.75 && !is75){
		//alert("75% watched");
		is75 = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','75% Watched']);
	}
	if((time/video_duration) > 0.99 && !isEnded){
		//alert("ended");
		isEnded = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','Complete']);
	}
}

function onCurrentTimeChangeHTML5(){
	
	var time = html5_player.currentTime;
	
	if((time/video_duration) > 0.25 && !is25){
		
		is25 = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','25% Watched']);
	}
	if((time/video_duration) > 0.5 && !is50){
		
		is50 = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','50% Watched']);
	}
	if((time/video_duration) > 0.75 && !is75){
		
		is75 = true;
		_gaq.push(['_trackEvent', 'Video: Trailer','75% Watched']);
	}		
}

function onEnded(){
	_gaq.push(['_trackEvent', 'Video: Trailer','Complete']);
	if(g_VideoInfo.type=="html5"){
		$("#div_html5_video").css('display','none');
		$("#btn_replay").css('display','block');
		$("#btn_replay").click(replayVideo);
	}
}

function replayVideo(){
	$("#div_html5_video").css('display','block');
	$("#btn_replay").css('display','none');
	$("#btn_replay").unbind('click');
	html5_player.play();
}
		
function onDurationChange(time, playerId){
	video_duration = time;
}

function onDurationChangeHTML5(e){
	video_duration = html5_player.duration;
}
	
var tn=1;
	
attachTrailerLinkEvent();
           
function embedSWF(n)
{
	var video=["","videos/"+videofile+".flv"];
	//var video=["http://downloads.paramount.com/mp/PA3/web_video/pa3_trailer_theatrical_640.mp4","http://downloads.paramount.com/mp/PA3/Trailer2/ParanormalActivity3_Trailer2_h264_640.mp4"];
	n=1;
				
	var parameters =
		{};
    		
	// Embed the player SWF:    		
	swfobject.embedSWF( "StrobeMediaPlayback.swf?src="+video[n]+"&autoPlay=true&controlBarAutoHide=true&skin=skin.xml&javascriptCallbackFunction=onJavaScriptBridgeCreated"
			, "vid-container"
			, 928
			, 522
			, "10.0.0"
			, {}
			, parameters
			, { allowFullScreen: "true", allowScriptAccess:"always"}
			, { name: "strobeMediaPlayback", id:"strobeMediaPlayback",styleclass:"smp-video-player" }
	);
}
			
var isiPad   = navigator.userAgent.match(/iPad/i) != null;

var isiPhone = navigator.userAgent.match(/iPhone/i) != null;

var isiPod   = navigator.userAgent.match(/iPod/i) != null;

var flashplayer = swfobject.getFlashPlayerVersion();

if(!(isiPad || isiPhone || isiPod || flashplayer.major == "0")){			
	g_VideoInfo.type="flash";
}
else{
	g_VideoInfo.type="html5";
	
}
	
//g_VideoInfo.type="html5"; //for debugging
		
if(g_VideoInfo.type=="flash")
	embedSWF();		
		
g_ActiveTrailer=1;
	

function playTrailer(n){
	
	if(g_currentModalInfo.source!=null)	
		doModal();	
	
	
	if(g_VideoInfo.type=="flash"){
		/*g_cookies.Set("trailer",n.toString());
		g_cookies.Save();*/
		document.location.reload();		
	}
	else{		
		$("#div_html5_video").hide();	
		
		var obj=null;
		
		obj=document.getElementById("div_html5_video");
		if(obj!=null)
			obj.pause();
			
		g_VideoInfo.width=640;
		g_VideoInfo.height=360;
		
		showTrailerOverlay(n);
	}	
}
