// JavaScript Document

// MM functions for doing roll over images and preloading
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Used to work out the users current timezone and set
// the schedule accordingly
function selectTimeZone(el) {
	var rightNow = new Date(); // Get the current date on the users machine
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); // Use it to work out the time on Jan 1
	var temp = jan1.toGMTString(); // Convert that time to GMT
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60); // Work out the hour different between the two dates
	
	sel = document.getElementById(el); // Get the select element
	
	// If the time offset exists in North America,
	// set the timezone select to that option
	switch(std_time_offset) {
		case -5:
			break;
		case -8:
			sel.selectedIndex = 1;
			break;
		case -6:
			sel.selectedIndex = 2;
			break;	
		case -7:
			sel.selectedIndex = 3;
			break;	
		case -4:
			sel.selectedIndex = 4;
			break;	
		case -3.5:
			sel.selectedIndex = 5;
			break;
		default:
			sel.selectedIndex = 0;	
	}
	
	// Resubmit the form with the new timezone data
	// so that the new schedule will be displayed
	document.forms['formTimeZone'].submit();
}

// Reload the showcasereel at the end of any video being played
function reloadVideoShowcase(videofile) {
	video.src = 'media/videos/'+videofile;	
	showPlayButton();
}

// Reloads the showcasereel in the flashplayer
function reloadWaitBeforeLoad(flashfile, posterfile) {
	fp.hide(); // Hide the player to show the poster image again
	fp.play('media/videos/'+flashfile);	
	fp.pause(); // Pause the flash player
	fp.seek(0);		

	showPlayButton();	
}

// Reload the showcasereel in the flashplayer at
// the end of any video being played
function reloadFlashShowcase(flashfile, posterfile) {
	//showPlayButton();	
	
	// Flowplayer fires the ended event about a second
	// before the video actually ends, causing the video to
	// get cut off. This gives the video time to end before
	// loading the showcasereel back in
	this.setTimeout("reloadWaitBeforeLoad('"+flashfile+"', '"+posterfile+"');",1000);
}

// Shows the custom play button
function showPlayButton() {
	btn = document.getElementById('btn_playstop');
	btn.src = "images/btn_videoplay.png";
	btn.alt = "Play Video";				  
}

// Shows the custom stop button
function showStopButton() {
	btn = document.getElementById('btn_playstop');
	btn.src = "images/btn_videostop.png";
	btn.alt = "Stop Video";				  
}

// Checks which phase the button is in, and either starts or stops the video
function videoPlayStop() {
	btn = document.getElementById('btn_playstop');
	
	// If button said play...
	if(btn.alt == 'Play Video') {			
		showStopButton(); // Change button to custom stop
		fp.show();
		fp.play(); // start the Flash player if being used
		video.play(); // start the HTML5 video player if being used (IE doesn't like you calling on HTML5 
					  // functionality it can't use, so FP commands need to go first)
	} else {	
		showPlayButton(); // Change button to custom play
		fp.pause(); // Pause the flash player
		fp.seek(0); // Reset the flash player
		video.pause(); // Pause the HMTL5 video player
		video.currentTime = 0; // Reset the HTML5 video player (IE rules apply again)
	}
}

// Changes the main video window to another source
function changeVideo(videofile) {
	fp.show();
	fp.play('media/videos/'+videofile+'.flv'); // Loads and plays the new video file in the Flash player
	showStopButton(); // Change button to custom stop
	
	window.location = "#vp"; // Jump the browser window up to the main player window
	
	video.src = 'media/videos/'+videofile+'.mp4';
	video.play();
}

function setTimeZone() {
	var rightNow = new Date(); // Get the current date on the users machine
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); // Use it to work out the time on Jan 1
	var temp = jan1.toGMTString(); // Convert that time to GMT
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60); // Work out the hour different between the two dates

	// Work out timezone
	switch(std_time_offset) {
		case -5:
			timezone = 'ET';
			break;
		case -8:
			timezone = 'PT';
			break;
		case -6:
			timezone = 'CT';
			break;	
		case -7:
			timezone = 'MT';
			break;	
		case -4:
			timezone = 'AT';
			break;	
		case -3.5:
			timezone = 'NT';
			break;
		default:
			timezone = 'ET';	
	}
	
	// Save timezone to a cookie for use by PHP
	var cookie_date = new Date("December 31, 2099");
	document.cookie= "timezone=" + timezone + ";expires=" + cookie_date.toGMTString();
}

setTimeZone();

// Added to hide errors caused by VideJS in IE
// and IE's problem with calling on HTML 5 video
// functionality, when it was not being used
function noError(){return true;}
window.onerror = noError;
