var open_window = new GameViewer();

function GameViewer()
{
	// Our elements.
	this.bgDiv 	= "overlayblackout";
	this.frDiv 	= "popin";
	this.gmDiv	= "popmovie";
	this.flashdiv = "videoContentFrame";
	this.currentLang = "en";
	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,flashID)
	{
		// Insert the movie movie
		var write_the_flash = new flashInjection(movie,width,height);
		
		GetElement('videoContentFrame').innerHTML = write_the_flash.writeFlashMovie();
		
		GetElement(open_window.gmDiv).style.visibility = "visible";
		//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.fixMoviePos();

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

		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.filter = "alpha(opacity=50)";
		 }

		// 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.fixMoviePos = function()
	{
		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()
	{
		GetElement('videoContentFrame').innerHTML = "&nbsp;";

		// Let's get rid of that event listener
		RemoveEventHandler("resize", this.fixMoviePos);
		RemoveEventHandler("scroll", this.fixMoviePos);

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

		// Get rid of our movie.
		GetElement(open_window.bgDiv).style.display = "none";
		GetElement(open_window.bgDiv).style.src = "";
		GetPopup().style.display = "none";
		var bname = navigator.appName;
		if (bname == "Microsoft Internet Explorer")
		{window.location.reload()}
		
	}

	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;
	}
	
	function emptyDiv(divToClear)
	{
		var i;
		alert(divToClear.childNodes[0]);
		while (i=divToClear.childNodes[0])
		{
			if (i.nodeType == 1 || i.nodeType == 3)
			{
				divToClear.removeChild(i);
			}
		}
	}
	// 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);
	}
	
function flashInjection(url, width, height)
{
    var DEFAULT_WINDOW_SETTING = "window";
    var quality = "high";
    var id = "flash";
    var wmode = DEFAULT_WINDOW_SETTING;
    var script_access = "sameDomain";
    var allow_fullscreen = "false";
    var params = null;
    var has_flash = gotFlash();

    this.SetQuality = function(new_quality)
    {
        quality = new_quality;
    }

    this.SetID = function(new_id)
    {
        id = new_id;
    }

    this.SetTransparent = function(is_transparent)
    {
        wmode = is_transparent ? "transparent" : DEFAULT_WINDOW_SETTING;
    }

    this.SetFullScreen = function(fullscreen)
    {
        allow_fullscreen = fullscreen ? "true" : "false";
    }

    this.SetScriptAccess = function(new_script_access)
    {
        script_access = new_script_access;
    }

    this.SetParams = function(new_params)
    {
        params = new_params;
    }

    this.writeFlashMovie = function()
    {
        var embed_line = "";

        if(has_flash)
        {
            embed_line += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '">';
            embed_line += '<param name="allowScriptAccess" value="' + script_access + '" />';
            embed_line += '<param name="allowFullScreen" value="' + allow_fullscreen + '" />';
            embed_line += '<param name="movie" value="http://www.station.sony.com/movies/now_playing.swf?movieURL=http://starwarsgalaxies.station.sony.com/tradingcardgame/content/flvs/lootcards/'+url+'.flv" /><param name="quality" value="' + quality + '" />';
            embed_line += '<param name="wmode" value="' + wmode + '" />';

            if(params != null)
            {
                embed_line += '<param name="flashvars" value="' + params + '" />';
            }

            embed_line += '<embed src="http://www.station.sony.com/movies/now_playing.swf?movieURL=http://starwarsgalaxies.station.sony.com/tradingcardgame/content/flvs/lootcards/'+url+'.flv" quality="' + quality + '" ';

            if(params != null)
            {
                // Non-IE browsers need this
                embed_line += 'flashvars="' + params + '" ';
            }

            embed_line += 'wmode="' + wmode + '" width="' + width + '" height="' + height + '"FlashVars="" name="' + id + '" allowScriptAccess="' + script_access + '" allowFullScreen="' + allow_fullscreen + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
            embed_line += '</object>';
        }
        else
        {
            embed_line += '<p style="text-align: center; margin-top: 2em; margin-bottom: 2em; padding-top: 3em; padding-bottom: 3em; background: #333333">You don\'t appear to have Flash installed.</p>';
        }

        return(embed_line);
    }

    function gotFlash()
    {
        var flash_versions = 12;

        // Code swiped from http://www.dangrossman.info/2007/01/03/detecting-flash-and-java-with-javascript/
        if (navigator.plugins && navigator.plugins.length) {
            // Netscape style plugin detection
            for (x = 0; x <navigator.plugins.length; x++) {
                if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
                    return(true);
                }
            }
        }
        else if (window.ActiveXObject) {
            // ActiveX style plugin detection
            for (x = 2; x <= flash_versions; x++) {
                try {
                    oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
                    if (oFlash) {
                        return(true);
                    }
                }
                catch(e) { }
            }
        }
        
        return(false);
    }
}

}