/*Storing the overall minimum GUI size*/
var gui_w 			= 955;
var gui_h 			= 600;

/*Storing the minimum values for each div element*/
var vb_logo_w 		= 310;
var vb_logo_h 		= 50;
var navUI_w 		= 310;
var navUI_h 		= 30;
var mainUI_w 		= 310;
var mainUI_h 		= 520;
var statusUI_w 		= gui_w - mainUI_w;
var statusUI_h 		= 20;
var unity_w 		= gui_w - mainUI_w;
var unity_h 		= gui_h - statusUI_h;

/*Storing the initial x and y positions for each div element*/
var vb_logo_x 		= 0;
var vb_logo_y 		= 0;
var navUI_x 		= 0;
var navUI_y 		= vb_logo_h;
var mainUI_x 		= 0;
var mainUI_y 		= navUI_y + navUI_h;
var statusUI_x 		= mainUI_x + mainUI_w;
var statusUI_y 		= 0;
var unity_x 		= mainUI_x + mainUI_w;
var unity_y 		= statusUI_y + statusUI_h;

var Logo, NavUI, MainUI, StatusUI, Unity;// learnWrapper, learnContent;

function getWidth()
{
	if (self.innerWidth)
	{
		return self.innerWidth;
	}
	if (document.documentElement && document.documentElement.clientWidth)
	// Explorer 6 Strict Mode
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientWidth;
	}
}

function getHeight()
{
	if (self.innerHeight)
	{
		return self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientHeight;
	}
}

function initGUI()
{
	// Set shortcuts.
	Logo 			= document.getElementById("dLogo");
	NavUI			= document.getElementById("dNavUI");
	MainUI 			= document.getElementById("dMainUI");
	StatusUI 		= document.getElementById("dStatusUI");
	Unity			= document.getElementById("dUnity");
	//learnWrapper	= document.getElementById("learnWrapper");
	//learnContent	= document.getElementById("learnContent");
	
	//alert("IN INIT GUI!");
	setGUIPosition();
	setGUISize();
	pageloaded = true;
	//checkReady();
}

function setGUIPosition()
{
	Logo.style.position 		= 'absolute';
	NavUI.style.position 		= 'absolute';
	MainUI.style.position 		= 'absolute';
	StatusUI.style.position 	= 'absolute';
	Unity.style.position 		= 'absolute';		
	//learnWrapper.style.position = 'absolute';
	
	Logo.style.left 			= vb_logo_x + 'px';
	NavUI.style.left 			= navUI_x + 'px';
	MainUI.style.left 			= mainUI_x + 'px';
	StatusUI.style.left 		= statusUI_x + 'px';
	Unity.style.left 			= unity_x + 'px';
	//learnWrapper.style.left 	= unity_x + 'px';
	
	Logo.style.top 				= vb_logo_y + 'px';
	NavUI.style.top 			= navUI_y + 'px';
	MainUI.style.top 			= mainUI_y + 'px';
	StatusUI.style.top 			= statusUI_y + 'px';
	Unity.style.top 			= unity_y + 'px';
	//learnWrapper.style.top 		= unity_y + 'px';
	
	/*Unity.style.zIndex 			= '50';		
	learnWrapper.style.zIndex 	= '1000';*/
}

function setFlashWidth(newWidth)
{
	//alert("setFlashWidth called with: " + newWidth + ", mainUI_w: " + mainUI_w);
	if (newWidth <= 0)
		newWidth = 310;
		
	var doc_width 	= getWidth();	
	var doc_height 	= getHeight();
	
	mainUI_w = Math.ceil(mainUI_w * 310 / newWidth);
	navUI_w = mainUI_w;
	vb_logo_w = mainUI_w;
	unity_w = doc_width - mainUI_w;
	statusUI_w = unity_w;
	
	vb_logo_h = Math.ceil(50 * vb_logo_w / 310);
	navUI_h = Math.ceil(30 * navUI_w / 310);
	statusUI_h 	= Math.ceil(20 * navUI_w / 310);
	unity_h = doc_height - statusUI_h;
	
	unity_x = mainUI_x + mainUI_w;
	statusUI_x 	= mainUI_x + mainUI_w;
	
	navUI_y = vb_logo_h;
	mainUI_y = navUI_y + navUI_h;
	unity_y = statusUI_y + statusUI_h;
	
	mainUI_h = doc_height - (navUI_y + navUI_h);

	
	setGUIPosition();
	setGUISize();
	//alert("SET FLASH WIDTH FINISHED!");
}

function setGUISize()
{
	//alert("RESIZE CALLEd ... ");
	var doc_width 				= getWidth();	
	var doc_height 				= getHeight();
	
	/*Setting unchanging sizes... may move to different function due to static nature*/
	Logo.style.width 			= vb_logo_w + 'px';
	Logo.style.height 			= vb_logo_h + 'px';
	MainUI.style.width 			= mainUI_w + 'px';
	MainUI.style.height			= mainUI_h + 'px';
	NavUI.style.width			= navUI_w + 'px';
	NavUI.style.height 			= navUI_h + 'px';
	StatusUI.style.width 		= statusUI_w + 'px';
	StatusUI.style.height 		= statusUI_h + 'px';
		
	/* Set components' width*/
	/*if(doc_width < gui_w)
	{		
		Unity.style.width 		 = unity_w + 'px';
		//learnWrapper.style.width = unity_w + 'px';
	}
	else 
	{ */
		var mod_doc_width 		 = doc_width - mainUI_w;
		Unity.style.width 		 = mod_doc_width + 'px';
		StatusUI.style.width 	 = mod_doc_width + 'px';
		//learnWrapper.style.width = mod_doc_width + 'px';
	//}
	
	/* Set components' height*/
	/*if(doc_height < gui_h)
	{
		Unity.style.height 			= unity_h + 'px';
		//learnWrapper.style.height 	= unity_h + 'px';
	}
	else 
	{*/
		//don't need to establish height for Unity Window if width != '0px' because height doesn't change
		mainUI_h = doc_height - (navUI_y + navUI_h);
		MainUI.style.height 		= mainUI_h + 'px';
		Unity.style.height 			= (doc_height - statusUI_h) + 'px';	
		//learnWrapper.style.height 	= (doc_height - statusUI_h) + 'px';
	//}

	//learnContent.style.width = learnWrapper.style.width;
	//learnContent.style.height = learnWrapper.style.height;
}

function embedModule(moduleFilename) 
{
	AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
			'width', '100%',
			'height', '100%',
			'src', 'vb/flash/' + moduleFilename,
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'wmode', 'window',
			'devicefont', 'false',
			'id', moduleFilename,
			'bgcolor', '#ffffff',
			'name', moduleFilename,
			'menu', 'true',
			'allowScriptAccess','sameDomain',
			'movie', 'vb/flash/' + moduleFilename,
			'salign', 'tl'
			);
};

function embedUnity(src)
{
	initGUI();
	if (DetectUnityWebPlayer()) {
		document.write('<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="100%" height="100%"> \n');
		document.write('  <param name="src" value="' + src +'" /> \n');
		
		document.write('  <param name="logoimage" value="vb/images/load_logo.png" /> \n');
		document.write('  <param name="progressbarimage" value="vb/images/load_bar_fill.png" /> \n');
    	document.write('  <param name="progressframeimage" value="vb/images/load_bar.png" /> \n');

		document.write('  <param name="disableContextMenu" value="true" /> \n');
		document.write('  <embed id="UnityEmbed" src="' + src + '" width="100%" height="100%" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" disableContextMenu="true" logoimage="vb/images/load_logo.png" progressbarimage="vb/images/load_bar_fill.png" progressframeimage="vb/images/load_bar.png"/> \n');
		document.write('</object>');
		//alert("CALLED EMBED UNITY!");
		//document.getElementById('dUnity').innerHTML = '<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="100%" height="100%"> \n' + '  <param name="src" value="' + src +'" /> \n' + ' <param name="disableContextMenu" value="true" /> \n' + '  <embed id="UnityEmbed" src="' + src + '" width="100%" height="100%" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" disableContextMenu="true" /> \n' + '</object>';
		
		/*var obj = document.createElement("object");
		

		var param1 = document.createElement("param");
		param1.setAttribute("name", "src");
		param1.setAttribute("value", src);
		
		var param2 = document.createElement("param");
		param2.setAttribute("name", "disableContextMenu");
		param2.setAttribute("value", "true");
		
		var embed = document.createElement("embed");
		embed.setAttribute("id", "UnityEmbed");
		embed.setAttribute("src", src);
		embed.setAttribute("width", "100%");
		embed.setAttribute("height", "100%");
		embed.setAttribute("type", "application/vnd.unity");
		embed.setAttribute("pluginspage", "http://www.unity3d.com/unity-web-player-2.x");
		embed.setAttribute("disableContextMenu", "true");
		
		obj.appendChild(param1);
		obj.appendChild(param2);
		obj.appendChild(embed);
		
		obj.setAttribute("id", "UnityObject");
		obj.setAttribute("classid", "clsid:444785F1-DE89-4295-863A-D46C3A781394");
		obj.setAttribute("width", "100%");
		obj.setAttribute("height", "100%");
		
		document.getElementById('dUnity').appendChild(obj);
		alert(document.getElementById('dUnity').innerHTML);*/
		//document.getElementById('dUnity').innerHTML = '<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="100%" height="100%"> \n' + '  <param name="src" value="' + src +'" /> \n' + '  <embed id="UnityEmbed" src="' + src + '" width="100%" height="100%" type="application/vnd.unity" pluginspage="http://www.unity3d.com/unity-web-player-2.x" /> \n' + '</object>';
	}
	else {	
		var installerPath = GetInstallerPath();
		if (installerPath != "") {
			// Place a link to the right installer depending on the platform we are on. The iframe is very important! Our goals are:
			// 1. Don't have to popup new page
			// 2. This page still remains active, so our automatic reload script will refresh the page when the plugin is installed
			document.write('<div align="center" id="UnityPrompt"> \n');
			document.write('  <a href= ' + installerPath + '><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
			document.write('</div> \n');
			
			// By default disable ActiveX cab installation, because we can't make a nice Install Now button
//						if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1)
			if (0)
			{	
				document.write('<div id="InnerUnityPrompt"> <p>Title</p>');
				document.write('<p> Contents</p>');
				document.write("</div>");

				var innerUnityPrompt = document.getElementById("InnerUnityPrompt");
				
				var innerHtmlDoc =
					'<object id="UnityInstallerObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="320" height="50" codebase="http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0">\n' + 
					'</object>';
					
				innerUnityPrompt.innerHTML = innerHtmlDoc;
			}

			document.write('<iframe name="InstallerFrame" height="0" width="0" frameborder="0">\n');
		}
		else {
			document.write('<div align="center" id="UnityPrompt"> \n');
			document.write('  <a href="javascript: window.open("http://www.unity3d.com/unity-web-player-2.x"); "><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
			document.write('</div> \n');
		}
		
		AutomaticReload();
	}
}