var open_window = new GameViewer();

function GameViewer()
{
	// Our elements.
	this.bgDiv 	= "overlayblackout";
	this.frDiv 	= "popin";
	this.gmDiv	= "popmovie";

	this.textAdURL	= "test";

	// Transitions go here.
	this.EFFECTS_IN = 0.05;
	this.EFFECTS_OUT = 0.15;

	var ourbody, pbackground;
	var movie_width, movie_height, window_min_width;
	var topbanner, topbanner_src, bottombanner, bottombanner_src;
	var blank_ad = "/blank.html";
	var have_text_ad = false;
	
	this.Launch = function(title, movie, width, height, min_width)
	{
		// Insert our movie
		

		//Effect.toggle(open_window.frDiv,'blind')
		if(document.getElementById(open_window.gmDiv).style.display == 'none')
			document.getElementById(open_window.gmDiv).style.display = 'block'
		//Effect.toggle(open_window.gmDiv,'appear')

		// Get our values for repetitive use.
		movie_width = width;
		movie_height = height;
		window_min_width = min_width;

		// Set up our background and append it to the body.
		ourbody = document.getElementsByTagName("body").item(0);
		pbackground = document.createElement("div");

		if(!(IsFirefoxMac()))
		{
			pbackground.setAttribute("id", open_window.bgDiv);
		}
		else
		{
			// Mac Firefox can't handle opacity stuff, so let's do a workaround.
			open_window.bgDiv = "overlayblackout_ffmac";
			pbackground.setAttribute("id", open_window.bgDiv);
			pbackground.style.backgroundImage = "url(/images/blackbox.png)";
			pbackground.style.backgroundRepeat = "repeat";
		}

		pbackground.style.display = 'none';
		ourbody.appendChild(pbackground);

		
		// Position stuff before we start displaying things
		this.CalibrateMoviePos();

		// Add an event handler for when the user resizes/scrolls the page
		SetEventHandler("resize", this.CalibrateMoviePos);
		//SetEventHandler("scroll", this.CalibrateMoviePos);

		HideSelects();
		GetElement(open_window.bgDiv).style.display = "block";
		
		//remove this once the solution is found
		var browserName=navigator.appName; 
		 if (browserName=="Microsoft Internet Explorer")
		 {
			GetElement(open_window.bgDiv).style.display = "none";
		 }

		// Set the title text.
		//GetElement("popmovietitle").innerHTML = "<span>SOE Presents:</span> " + title;

		if(!(IsFirefoxMac()))
		{
			GetPopup().style.display = "block";
		}
		else
		{
			// Firefox/Mac can't handle fading in Flash
			GetPopup().style.display = "block";
		}
	}

	this.CalibrateMoviePos = function()
	{
		// Let's set up the positioning of the background and the container,
		// prior to doing anything else.
		var popup_element = GetPopup();
		popup_element.style.width = (Math.max(movie_width, window_min_width) + 20) + "px";

		// This needs to be set, for centering purposes.
		GetElement(open_window.gmDiv).style.width = movie_width + "px";

		var background_element = GetElement(open_window.bgDiv);
		background_element.style.top = 0 + "px";
		background_element.style.left = 0 + "px";
		background_element.style.width = ScrollDimensionsInfo()[0] + "px";
		background_element.style.height = ScrollDimensionsInfo()[1] + "px";
		background_element.style.zIndex = 20;

		var popup_height = popup_element.clientHeight;
		var popup_width = popup_element.clientWidth;

		// Little trick to get the dimensions of a possibly hidden element
		var element_hidden = (popup_element.style.display == "none");

		popup_element.style.visibility = "hidden";
		popup_element.style.display = "block";
		popup_height = popup_element.clientHeight;
		popup_width = popup_element.clientWidth;

		if(element_hidden)
		{
			popup_element.style.display = "none";
		}

		popup_element.style.visibility = "visible";

		// Now calculate our location based on scrolling and window size
		var top = Round((InnerDimensionsInfo()[1] - popup_height) / 4);

		if(top < 0)
		{
			top = 0;
		}
		top += GetPageScrolledPosition()[1];

		var left = Round((InnerDimensionsInfo()[0] - popup_width) / 2);
		if(left < 0)
		{
			left = 0;
		}
		left += GetPageScrolledPosition()[0];

		popup_element.style.top = top + "px";
		popup_element.style.left = left + "px";
		popup_element.zIndex = 99;

		
		
}

	this.Cancel = function()
	{
		// Let's get rid of that event listener
		RemoveEventHandler("resize", this.CalibrateMoviePos);
		RemoveEventHandler("scroll", this.CalibrateMoviePos);

		// We have to scrap the flash from inside of the div.
		GetElement(open_window.gmDiv).style.display = "none";
		

		// Get rid of our movie.
		GetElement(open_window.bgDiv).style.display = "none";
		GetPopup().style.display = "none";

		// Show the select boxes again.
		ShowSelects();
	}

	function GetElement(id)
	{
		return(document.getElementById(id));
	}

	function GetPopup()
	{
		return(GetElement(open_window.frDiv));
	}
	
	//Round
	function Round(val) {
		var rnum = val;
		var rlength = 2; // The number of decimal places to round to
		if (rnum > 8191 && rnum < 10485) {
			rnum = rnum-5000;
			var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
			newnumber = newnumber+5000;
		} else {
			var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		}
		return newnumber;
	}
	
	// Browser related functions.
	function InnerDimensionsInfo()
	{
		var dimensions = new Array(2);

		if(self.innerWidth)
		{
			dimensions[0] = self.innerWidth;
			dimensions[1] = self.innerHeight;
		} else
		if(document.documentElement && document.documentElement.clientWidth)
		{
			dimensions[0] = document.documentElement.clientWidth;
			dimensions[1] = document.documentElement.clientHeight;
		} else
		if(document.body)
		{
			dimensions[0] = document.body.clientWidth;
			dimensions[1] = document.body.clientHeight;
		}

		return(dimensions);
	}

	function ScrollDimensionsInfo()
	{
		var dimensions = new Array(2);

		if(window.innerHeight && window.scrollMaxY)
		{
			dimensions[0] = document.body.scrollWidth;
			dimensions[1] = window.innerHeight + window.scrollMaxY;
		} else
		if(document.body.scrollHeight > document.body.offsetHeight)
		{
			dimensions[0] = document.body.scrollWidth;
			dimensions[1] = document.body.scrollHeight;
		} else
		{
			dimensions[0] = document.body.offsetWidth;
			dimensions[1] = document.body.offsetHeight;
		}

		return(dimensions);
	}

	function GetPageScrolledPosition()
	{
		var dimensions = new Array(2);

		if(self.pageYOffset)
		{
			dimensions[0] = self.pageXOffset;
			dimensions[1] = self.pageYOffset;
		}
		else if((document.documentElement) && (document.documentElement.scrollTop))
		{
			dimensions[0] = document.documentElement.scrollLeft;
			dimensions[1] = document.documentElement.scrollTop;
		}
		else if(document.body)
		{
			dimensions[0] = document.body.scrollLeft;
			dimensions[1] = document.body.scrollTop;
		}

		return(dimensions);
	}

	function ShowSelects()
	{
		var selects = document.getElementsByTagName("select");
		for(var i = 0; i != selects.length; i++)
		{
			selects[i].style.visibility = "visible";
		}
	}

	function HideSelects()
	{
		var selects = document.getElementsByTagName("select");
		for(var i = 0; i != selects.length; i++)
		{
			selects[i].style.visibility = "hidden";
		}
	}

	function SetEventHandler(eventname, handler)
	{
		if(window.addEventListener)
		{
			window.addEventListener(eventname, handler, false);
		}
		else if(window.attachEvent)
		{
			window.attachEvent("on" + eventname, handler);
		}
	}

	function RemoveEventHandler(eventname, handler)
	{
		if(window.removeEventListener)
		{
			window.removeEventListener(eventname, handler, false);
		}
		else if(window.detachEvent)
		{
			window.detachEvent("on" + eventname, handler);
		}
	}

	// Necessary for an ugly hack - FF/Mac can't handle Flash combined with CSS opacity
	function IsFirefoxMac()
	{
		var userAgent = navigator.userAgent.toLowerCase();
  		return((userAgent.indexOf('mac') != -1) && (userAgent.indexOf('firefox') != -1));
	}

	function InsertTextAd()
	{
		// Thanks to http://www.mikeonads.com/category/ad-networks/ for this!
		// It captures the document.write()s from the ad and inserts them properly.
		// First we override document.write()
		(function(){

			function SetAd(ad)
			{
				GetElement("popin_ad").innerHTML = ad;
			}

			if( typeof( Document)!='undefined' && typeof( HTMLDocument)!='undefined' ){
				// Gecko, Firefox, Opera
				HTMLDocument.prototype.write = function(s){
					SetAd(s);
					document.close();
				};
			}else if(document.uniqueID && document.expando){
				// Internet Explorer
				document.write = function(s){
					 SetAd(s);
					 document.close();
				}
			}else if(document.constructor){
				// Safari
				document.write  = function(s){
					SetAd(s);
				};
			}
		})();

		// Now insert the script stuff, and let the above code handle the output
		var script_element = document.createElement("script");
		script_element.setAttribute("type", "text/javascript");
		script_element.setAttribute("src", open_window.textAdURL);
		var ad_container = GetElement("popin_ad");
		ad_container.replaceChild(script_element, ad_container.firstChild);
	}
}