function onBodyLoad(){
	onResize();
	bindAuth();
}


var currentSiteMarginLeft;
var currentSiteMarginTop;


function onResize(){

	
	if (document.layers || document.all || document.getElementById){
		
		var windowW, windowH, fullW, fullH, finalW, finalH;
		var isSafari = (navigator.appVersion.indexOf("Safari") != -1) ? true : false;
		
		//constants
		
		var minW = 720;
		var minH = 462;
		var maxW = 1014;
		var maxH = 650;
		
		var padLeft = 20;
		var padRight = 0;
		var padTop = 24;
		var padBottom = 0;
		
		
		
		ns4 = (document.layers)? true:false
		ie4 = (document.all)? true:false

		windowW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
		windowH = (ns4)? window.innerHeight : document.body.offsetHeight
		
		windowW = windowW-(padLeft*2);
		windowH = windowH-(padTop*2);

		//figure out the full dimensions, would be final without preserving scaling
		
		if(windowW < minW){
			fullW = minW;
		} else {
			fullW = windowW;
		}
		
		if(windowH < minH) {
			fullH = minH;
		} else {
			fullH = windowH;
		}
		
		//if ((windowW < maxW) || (windowH < maxH)){
			
			
		
			////preserve scaling
			//if (windowW/windowH < maxW/maxH){
				//finalW = fullW;
				//finalH = (maxH*finalW)/maxW;
				
				//currentSiteMarginLeft = padLeft +"px";
				//document.getElementById('pbsLogo').style.marginLeft = (0 - ((windowW)/2)-30)  + "px";
				
			//} else {
				//finalH = fullH;
				//finalW = (maxW*finalH)/maxH;
				
				//currentSiteMarginLeft = ((windowW-finalW)/2 + padLeft/2)+"px";
				//document.getElementById('pbsLogo').style.marginLeft = (0 - ((finalW)/2)-45)  + "px";
				
			//}
			
			////place the margins to keep it in the top left corner if overflow is occuring
			//document.getElementById('flashcontent').style.marginTop =  currentSiteMarginTop = (padTop-24)  + "px";
			
			//document.getElementById('flashcontent').style.width = finalW + "px";
			//document.getElementById('flashcontent').style.height = finalH + "px";
			
			
			//document.getElementById('pbsLogo').style.marginTop = padTop  + "px";
			////document.getElementById('flashcontent').style.marginLeft = ((windowW-maxW)/2)+"px";	
			
		//} else {
			////browser is big enough in both directions, so use max
			
		if(ie4)
		{
			document.getElementById('flashcontent').style.marginTop = currentSiteMarginTop = "240px";
			document.getElementById('flashcontent').style.marginLeft = currentSiteMarginLeft = (0 - padLeft) +"px";
			
			document.getElementById('flashcontent').style.width = maxW + "px";
			document.getElementById('flashcontent').style.height = maxH + "px";
			
			
			document.getElementById('pbsLogo').style.marginTop = padTop + "px";
			document.getElementById('pbsLogo').style.marginLeft = (0 - 530 - padLeft)  + "px";
		}else{
			
			document.getElementById('flashcontent').style.marginTop = currentSiteMarginTop = 0 +"px";
			document.getElementById('flashcontent').style.marginLeft = currentSiteMarginLeft = ((windowW-maxW)/2) + padLeft +"px";
			
			document.getElementById('flashcontent').style.width = maxW + "px";
			document.getElementById('flashcontent').style.height = maxH + "px";
			
			
			document.getElementById('pbsLogo').style.marginTop = padTop + "px";
			document.getElementById('pbsLogo').style.marginLeft = (0 - 530 - padLeft)  + "px";
		}
		//}
		
	}
}

function setMargins(fullscreen){
	
	if (fullscreen){
		document.getElementById('flashcontent').style.marginTop = 0;
		document.getElementById('flashcontent').style.marginLeft = 0;
	} else {
		document.getElementById('flashcontent').style.marginTop = currentSiteMarginTop;
		document.getElementById('flashcontent').style.marginLeft = currentSiteMarginLeft;
	}
}

function queryString(parameter) { 
	var loc = location.toString().substring(location.toString().indexOf('?')+1,location.toString().length);
	var param_value = false;

	var params = loc.split("&");
	
	for (i=0; i<params.length;i++) {
		param_name = params[i].substring(0,params[i].indexOf('='));
		
		if (param_name == parameter) {
		  param_value = params[i].substring(params[i].indexOf('=')+1)
		}
	}
	if (param_value) {
		return param_value;
	}
	else {
		return false; //Here determine return if no parameter is found
	}
}


function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function bindAuth(){
	$('body').bind('cbox_closed', function() {
		if (getCookie("pbskids.username") != ""){
			getFlashMovie("producerPlayer").handleAuthClosed(getCookie("pbskids.username"));
			//document.getElementById('loginholder').innerHTML = getLoginStrip(username); //this was made unnecessary because of new login bar implementation
		}
     });
}


function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}


function getLoginStrip(username){
	var strVar="";
	strVar += "<div id=\"pbskidsgologinbar\"><img src=\"\/go\/secretbox\/images\/avatars\/trophy.jpg\" border=\"0\" id=\"pbskidsgoavatar\"> ";
	strVar += "<span id=\"pbskidsgologinwelcome\">Hi, jonddg!<\/span>  ";
	strVar += "<a href=\"\/go\/secretbox\/\">Go to SECRET BOX<\/a> | <a href=\"\/go\/apps\/auth\/account_settings\/\">Settings<\/a> | <a href=\"\/go\/apps\/auth\/logout\/?destination=\/electriccompany\">Logout<\/a> <input type=hidden name=\"lasturi\" value='\/electriccompany\'><\/div>";
	strVar += "";
	return strVar.replace("[username]",username);
}




