/*
site.js
*/

/*******************************
 Move Website Functions
 ******************************/
MN.moveSite = {
	"adjustTotal" : 45, //Total padding needed on bottom
	"aspectRatio" : 2.188, //Aspect ratio - 1280x585
	"adjustHtmlControls" : 59, // HTML controls height
	"host" : "http://cms001.slc1.qcn3.movenetworks.com", //Publish host
	"user" : "movemkt", //Publish user
	"currentClip" : 0,
	"counter" : 5,
	"counterTimer" : 0,
	"homeClips" : null,
	"windowLoad" : function() {
		//Suckerfish for main menu
		var sfEls = document.getElementById("mainmenu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
		
		//Resize player
		//MN.moveSite.resizePlayer();
		
		//Display and set width for mainmenu bar for active page
		/*var mainline = document.getElementById("activeitemline");
		if(mainline){
			mainline.style.display = "block";
			//menuwidth - secondarymenuwidth - (listitemx - menux)
			var lineWidth = 984 - 250 - (MN.moveSite.findPosX(mainline.parentNode) - MN.moveSite.findPosX(mainline.parentNode.parentNode));
			mainline.style.width = lineWidth + "px";
		}*/
	
	},
	
	//Function provided on http://blog.firetree.net/2005/07/04/javascript-find-position/
	"findPosX" : function(obj){
		var curleft = 0;
		if(obj.offsetParent)
			while(1) 
			{
			  curleft += obj.offsetLeft;
			  if(!obj.offsetParent)
				break;
			  obj = obj.offsetParent;
			}
		else if(obj.x)
			curleft += obj.x;
		return curleft;
	},
	
	//Function provided on http://blog.firetree.net/2005/07/04/javascript-find-position/
	"findPosY" : function(obj){
		var curtop = 0;
		if(obj.offsetParent)
			while(1)
			{
			  curtop += obj.offsetTop;
			  if(!obj.offsetParent)
				break;
			  obj = obj.offsetParent;
			}
		else if(obj.y)
			curtop += obj.y;
		return curtop;
	},
	
	//Validate Request more Info
	"validateForm" : function(form){
	
		if (form.company.value == "") {
			alert( "Please enter your company's name." );
			form.company.focus();
			return false;
		}
		
		if (form.first_name.value == "") {
			alert( "Please enter your first name." );
			form.first_name.focus();
			return false;
		}
		
		if (form.last_name.value == "") {
			alert( "Please enter your last name." );
			form.last_name.focus();
			return false;
		}
		
		if (form.title.value == "") {
			alert( "Please enter your title." );
			form.title.focus();
			return false;
		}
		
		if (form.phone.value == "") {
			alert( "Please enter your phone number." );
			form.phone.focus();
			return false;
		}
		
		if (form.mobile.value == "") {
			alert( "Please enter your mobile phone's number." );
			form.mobile.focus();
			return false;
		}
		
		if (form.email.value == "") {
			alert( "Please enter your email address." );
			form.email.focus();
			return false;
		}
		
		if (form.URL.value == "") {
			alert( "Please enter your company's URL." );
			form.URL.focus();
			return false;
		}
		
		if (form.street.value == "") {
			alert( "Please enter your company's street address." );
			form.street.focus();
			return false;
		}
		if (form.city.value == "") {
			alert( "Please enter your company's city." );
			form.city.focus();
			return false;
		}
		if (form.state.value == "") {
			alert( "Please enter your company's state." );
			form.state.focus();
			return false;
		}
		if (form.country.value == "") {
			alert( "Please enter your company's country." );
			form.country.focus();
			return false;
		}
		if (form.zip.value == "") {
			alert( "Please enter your company's zip code." );
			form.zip.focus();
			return false;
		}
	
		return true;
	},
	
	//******************Sizing Functions*************************
	"resizePlayer" : function(){
		if(MN.$('homepage')){
			MN.moveSite.fullBrowserResize();
			//Hack to check the video again after a resize for nonIE browsers to compensate if the scrollbar appears
			if(MN.nonIE){
				setTimeout('MN.moveSite.fullBrowserResize();',100);	
			}
		}
	},
	
	"fullBrowserResize" : function(){
		var win = MN.GetWindowSize();
		var width = document.width;
		if(!MN.nonIE){
			width = win[0];
		}
		if(width < 864){
			width = 864;	
		}
		
		var htmlheight = 0;
		if(movePlayer && movePlayer.p && !movePlayer.p.overlaysSupported()){
			htmlheight = MN.moveSite.adjustHtmlControls;
		}
		
		var height = Math.round(width / (MN.moveSite.aspectRatio));
		log("Height: " + height);
		log("comb: " + (height + MN.moveSite.adjustTotal + htmlheight) + " > " + win[1]);
		if(height + MN.moveSite.adjustTotal + htmlheight > win[1]){
			height = win[1] - MN.moveSite.adjustTotal - htmlheight;
			width = Math.round(height * (MN.moveSite.aspectRatio));
			log("Height: " + height + " Width: " + width);
		}
		
		MN.$('mn_container').style.width = width + "px";
		MN.$('mn_container').style.height = (height + htmlheight) + "px";
		MN.$('mn_player_container').style.width = width + "px";
		MN.$('mn_player_container').style.height = height + "px";	
	},
	
	"loadPublishData" : function(){
		if(CatalogListing && CatalogListing.categoryList && CatalogListing.categoryList[0].clipList){
			MN.moveSite.homeClips = CatalogListing.categoryList[0].clipList;
		}
	},
	
	"getStartVideo" : function(){
		if(MN.moveSite.homeClips != null && MN.moveSite.homeClips.length > 0){
			return MN.moveSite.getClipUrl(MN.moveSite.homeClips[0].id);
		}
		else{
			return "http://stream.sv1.qcn3.movenetworks.com/vod/4F2C563B/88919F/output.qmx"; //Default start video	
		}
	},
	
	"getPreloadVideo" : function(){
		if(MN.moveSite.homeClips != null && MN.moveSite.homeClips.length > 1){
			return MN.moveSite.getClipUrl(MN.moveSite.homeClips[1].id);
		}
		else{
			return "";	
		}
	},
	
	"getClipUrl" : function(id){
		return MN.moveSite.host + "/cms/publish/vod/vodclip/" + MN.moveSite.user + "/" + id + ".qvt";
	},
	
	//Show the waiting room at the end of a clip
	"showWaitingRoom" : function(){
		if(MN.$('homepage')){
			MN.moveSite.playNextClip();
			//Load next clip if available
			/*if(MN.moveSite.homeClips != null && MN.moveSite.homeClips.length > 1){
				//Get clip
				var nextClip = MN.moveSite.currentClip + 1;
				if(nextClip >= MN.moveSite.homeClips.length){
					nextClip = 0; //Reset back to beginning	
				}
				
				//Load info
				var clip = MN.moveSite.homeClips[nextClip];
				
				//Title
				MN.$('mn_clipTitle').innerHTML = clip.name;
				
				//Description
				MN.$('mn_clipInfo').innerHTML = clip.description;
				
				//Thumbnail
				MN.$('mn_clipThumb').innerHTML = "<img src=\""+clip.thumbnail.thumb_url+"\" />";
				
				//Start Countdown
				MN.moveSite.counter = 5;
				MN.$('mn_clipCount').innerHTML = MN.moveSite.counter;
				MN.moveSite.counterTimer = setInterval('MN.moveSite.clipCountdown()', 1000);
				
				//Show
				MN.$('mn_waiting').style.display = "block";
			}
			else{
				MN.$('mn_waiting').style.display = "block";
				MN.$('mn_upcoming').style.display = "none";
				MN.$('mn_continue').style.display = "none";
			}*/
		}
	},
	
	"hideWaitingRoom" : function(){
		if(MN.$('homepage')){
			MN.$('mn_waiting').style.display = "none";
			//Stop countdown if necessary
			clearTimeout(MN.moveSite.counterTimer);
		}
	},
	
	//Decrement countdown
	"clipCountdown" : function(){
		MN.moveSite.counter--;
		if(MN.moveSite.counter <= 0){
			clearTimeout(MN.moveSite.counterTimer);
			MN.moveSite.hideWaitingRoom();
			MN.moveSite.playNextClip();
		}else{
			MN.$('mn_clipCount').innerHTML = MN.moveSite.counter;
		}
	},
	
	//Calls into player to replay clip
	"replayClip" : function(){
		clearTimeout(MN.moveSite.counterTimer);
		MN.$('mn_movie').ReplayContent();
	},
	
	//Play next clip
	"playNextClip" : function(){
		clearTimeout(MN.moveSite.counterTimer);
		MN.moveSite.currentClip++;
		if(MN.moveSite.currentClip >= MN.moveSite.homeClips.length){
			MN.moveSite.currentClip = 0;	
		}
		var nextClip = MN.moveSite.currentClip + 1;
		if(nextClip >= MN.moveSite.homeClips.length){
			nextClip = 0; //Reset back to beginning	
		}
	//	MN.$('mn_movie').PreloadContent(MN.moveSite.getClipUrl(MN.moveSite.homeClips[nextClip].id));
	//	MN.$('mn_movie').PlayContent(MN.moveSite.getClipUrl(MN.moveSite.homeClips[MN.moveSite.currentClip].id));
		movePlayer.play(MN.moveSite.getClipUrl(MN.moveSite.homeClips[MN.moveSite.currentClip].id));
	},
	
	//Show in page log, used for IE debugging
	"jLog" : function(text){
		MN.$('myLog').innerHTML += text + "<br />";
	}
}

MN.Event.Observe(window,"load",MN.moveSite.windowLoad);