/*
animate.js
This Javascript file contains:
1. Suckerfish dropdown for IE
2. Animates the div containing the Move Player to fill the screen and disable the other elements on the screen and vice versa.
3. Sets size of activeline that connects the active tier 1 page with its sub-nav on the right.
*/


/*******************************
 Player functions for Move video on Move Networks site
 ******************************/
MN.mvod = {
	"flashMovie" : null, //Flash movie object
	"flashRef" : null, //Reference element for the movie
	"movieBack" : null, //Dark background turned on when full screen
	"content" : null, //Container div for content
	"flashWidth" : 480,
	"flashHeight" : 492,
	"flashX" : 0,
	"flashY" : 0,
	"setFull" : false, //Flag that Full Screen is set
	"subpageTopY" : 0, //Offset for movie object on a subpage
	"ratio" : 16/9, //Movie aspect ratio
	"topControls" : 26, //Space for controls above the movie
	"bottomControls": 45, //Space for playback controls
	"winWidth" : 0, //Viewport width
	"winHeight" : 0, //Viewport height
	"pageWidth" : 0, //Page width
	"pageHeight" : 0, //Page height
	
	
	"windowLoad" : function() {
		//Set Mvod variables and events
		//MN.mvod.flashMovie = document.getElementById("flashcontent");
		//MN.mvod.flashRef = document.getElementById("flashspace");
		//MN.mvod.movieBack=document.getElementById('darkenScreenObject');
		//MN.mvod.content = document.getElementById('content');
		
		if(MN.mvod.movieBack){
			MN.mvod.movieBack.onclick = MN.mvod.switchToSmallPlayer;	
		}
		
		MN.mvod.setSizes();
		
		//Set subpage variables
		if(pageType == "subpage"){
			MN.mvod.flashWidth = 695;
			MN.mvod.flashHeight = 456;
			MN.mvod.topControls = 0;
			
			//MN.mvod.flashY = MN.mvod.findPosY(MN.mvod.flashRef);
			//var contentY = MN.mvod.findPosY(MN.mvod.content);
			//var offsetTop = MN.mvod.flashY - contentY + 10;
			//MN.mvod.subpageTopY = offsetTop;
			//MN.mvod.flashMovie.style.top = offsetTop + "px";
		}
		
	},
	
	"windowResize" : function() {
		
		MN.mvod.setSizes();
		
		if(MN.mvod.setFull){
			MN.mvod.setFullSize();
		}
	},
	
	"windowScroll" : function() {
		//Nate's hackfix to minimize or eliminate player artifacts caused by excessive scrolling
        //document.body.style.backgroundColor = '#343b40';
        //document.body.style.backgroundColor = '#343b41'; //the correct bgcolor

		/*if(MN.mvod.setFull){
			MN.mvod.setFullSize();	
		}*/
		
        //Ben's hackfix for the Vista 'player can leave my browser window' problem
        if (navigator.userAgent.indexOf("Windows NT 6.0") > -1) {
                $('playerDiv').style.visibility = 'hidden';
                setTimeout("$('playerDiv').style.visibility = 'visible'",1); //wait a millisecond, else it doesn't work
        }
	},
	
	"setSizes" : function() {
		var sizes = MN.mvod.getPageSize();
		MN.mvod.winWidth = sizes[2] - 16;
		MN.mvod.winHeight = sizes[3];	
		MN.mvod.pageWidth = sizes[0];
		MN.mvod.pageHeight = sizes[1];
	},
	
	"maximizePlayerDiv" : function(){
		MN.mvod.setFullSize();
	},
	
	"thisMovie" : function(movieName){
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
    	return (isIE) ? window[movieName] : document[movieName];
	},
	
	"switchToSmallPlayer" : function(){
		MN.mvod.thisMovie("movie").switchToSmallPlayer();
	},
	
	"pauseMovie" : function(){
		if(MN.mvod.thisMovie("movie")){
			MN.mvod.thisMovie("movie").pauseDueToShowcase();
		}
	},
	
	"minimizePlayerDiv" : function(){
		MN.mvod.setNormalSize();
	},
	
	"setInstallFlashSize" : function(){
		MN.mvod.flashRef.style.height = "350px";
		MN.mvod.flashMovie.style.height = "350px";
	},
	
	"installPlayer" : function(){
		window.location.href = "/install-move-player?home";
	},
	
	
	"playShowcase" : function(){
		if(!MN.QMPInstall.CanPlay()){
			window.location.href = "/install-move-player?showcase";
		}
		else{
			MN.mvod.pauseMovie();
			MN.Cookie.Set("mn_os", MN.QMPInstall.OS());
			MN.Cookie.Set("mn_browser", MN.QMPInstall.Browser());
			MN.Cookie.Set("mn_canplay", "1");
			MN.Cookie.Set("mn_type", MN.QMPInstall.controller.mimeStr || MN.QMPInstall.controller.ocxCLSIDs[0]);
			
			var params = MN.GetPageParams();
			var urlParams = "?admin=true";
			if (params.start && params.stop)
			{
				urlParams = "&start=%s&stop=%s".format(params.start,params.stop);
			}
			
			MN.mvod.openPopupWin('http://www.movenetworkshd.com/showcase.html' + urlParams, screen.availWidth, screen.availHeight);
		}
	},
	
	"setFullSize" : function(){
		if(MN.mvod.flashMovie != null){
			/*MN.mvod.content.style.zIndex = "50";
			MN.mvod.flashX = MN.mvod.findPosX(MN.mvod.flashRef);
			MN.mvod.flashY = MN.mvod.findPosY(MN.mvod.flashRef);
			var contentY = MN.mvod.findPosY(MN.mvod.content);
		
			//Get the player height
			var playerHeight = MN.mvod.winHeight - MN.mvod.flashY + 15;
			if(pageType == "subpage"){
				playerHeight = MN.mvod.winHeight - 60;	
			}
			if(playerHeight < 270){
				playerHeight = 270;	
			}
			
			//Adjust width based on height
			var playerWidth = Math.round(playerHeight * MN.mvod.ratio);
			if(playerWidth > MN.mvod.winWidth){
				//Size down if necessary to fit the window
				playerWidth = MN.mvod.winWidth - 20; //Add 20 pixel padding for each side
			}
			if(playerWidth < 480){
				playerWidth = 480;	
			}
			
			//Re-adjust height
			playerHeight = Math.round(playerWidth / MN.mvod.ratio) + MN.mvod.topControls + MN.mvod.bottomControls;
			
			//Set scroll distance from the top
			var scrolls = MN.mvod.getScrollXY();
			var scrolly = 0 + scrolls[1];
			var offsetTop = -77 - MN.mvod.topControls + scrolly; //Cover main menu
			if(pageType == "subpage"){
				offsetTop = scrolly - contentY + ((MN.mvod.winHeight - playerHeight)/2);
			}
			var offsetLeft = 10 - MN.mvod.flashX + ((MN.mvod.winWidth - playerWidth)/2);
			if(MN.mvod.flashMovie.className == "videoBlog"){
				offsetLeft += 35;
			}
			
			//Set Flash Movie parameters
			MN.mvod.flashMovie.style.left = offsetLeft + "px";
			MN.mvod.flashMovie.style.top = offsetTop + "px";
			MN.mvod.flashMovie.style.width = playerWidth + "px";
			MN.mvod.flashMovie.style.height = playerHeight + "px";
			MN.mvod.flashMovie.style.zIndex = "50";
			MN.mvod.setFull = true;
			
			//Dark Background
			MN.mvod.movieBack.style.display='block';
			
			//Reset dark to original location
			MN.mvod.movieBack.style.left = 0 + "px";
			MN.mvod.movieBack.style.top = 0 + "px";
			
			//Find original offset
			var backX = MN.mvod.findPosX(MN.mvod.movieBack);
			var backY = MN.mvod.findPosY(MN.mvod.movieBack);
		
			//Set offset position and dimensions
			MN.mvod.movieBack.style.left = (0 - backX) + "px";
			MN.mvod.movieBack.style.top = (0 - backY) + "px";
			MN.mvod.movieBack.style.width= MN.mvod.pageWidth + "px";
			MN.mvod.movieBack.style.height= MN.mvod.pageHeight + "px";*/
			
		}
	},
	

	"setNormalSize" : function(){
		if(MN.mvod.flashMovie != null){
			/*MN.mvod.content.style.zIndex = "20";
			MN.mvod.flashMovie.style.left = 10 + "px";
			MN.mvod.flashMovie.style.top = 0 + "px";
			if(pageType == "subpage"){
				MN.mvod.flashMovie.style.top = MN.mvod.subpageTopY + "px";	
			}
			MN.mvod.flashMovie.style.width = MN.mvod.flashWidth + "px";
			MN.mvod.flashMovie.style.height = MN.mvod.flashHeight + "px";
			MN.mvod.setFull = false;
			MN.mvod.movieBack.style.display = "none";*/
		}
	},
	
	"openPopupWin" : function(url,width,height){
		if(width == null) width = 950;
		if(height == null) height = 700;
		//var topedge = (screen.availHeight - height)/2;
		//var leftedge = (screen.availWidth - width)/2;
		var topedge = 0;
		var leftedge = 0;
		
		newwin = window.open(url,"","width="+width+", height="+height+", scrollbars=no, resizable=yes, screenX=" + leftedge + ", screenY=" + topedge + ", left=" + leftedge + ", top=" + topedge);
		newwin.focus();
	},
	
	"getPageSize" : function(){
		// Returns array with page width, height and window width, height and scroll x,y
		// Core code from - quirksmode.com
		// Edit for Firefox by pHaez
		
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		//	console.log(self.innerWidth);
		//	console.log(document.documentElement.clientWidth);
	
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		//	console.log("xScroll " + xScroll)
		//	console.log("windowWidth " + windowWidth)
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		//	console.log("pageWidth " + pageWidth)
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,yScroll) 
		return arrayPageSize;
	},
	
	"getScrollXY" : function() {
		var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	},
	
	//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;
	},
	
	"fullScreenAlert" : function(){
		if(!MN.Cookie.Get('fsalert')){
			MN.Cookie.Set('fsalert', '1');
			alert("Press the 'ESC' key to exit fullscreen");
			return true;
		}
		else{
			return true;	
		}
	}
}

MN.Event.Observe(window,"load",MN.mvod.windowLoad);
MN.Event.Observe(window, 'resize',MN.mvod.windowResize);
MN.Event.Observe(window, 'scroll',MN.mvod.windowScroll);