/**
 * @author Zach
 */
Function.prototype.bind = function (object) {
		var method = this;
		return function () {
	        method.apply(object, arguments);
    	};
}
function IsIE()
{
	if(navigator.appName.indexOf("Microsoft") != -1)
	{
		return true;
	}
	return false;
}

function IsOpera()
{
	if(navigator.appName.indexOf("Opera") != -1)
	{
		return true;
	}
	return false;
}

function FS_Overlay()
{
	this.dOverlay = null;
	this.dBackground = null;
	this.dImage = null;
	this.dCloseBtn = null;
	this.dLoading = null;
	this.nWaitTimer = null;
	this.bVideo = false;
	
	this.FS_OverlayInit = function()
	{
		dTemp2 = new Image();
		dTemp2.src = "gallery/karting/Karting1.jpg";
		dTemp = document.createElement("img");
		dTemp.setAttribute("src", "gallery/karting/Karting1.jpg");
		if(dTemp2.height > 400)
		{
			nHeight = dTemp2.height;
			nWidth = dTemp2.width;
			nHeightPct = 400 / nHeight;
			nHeight *= nHeightPct;
			nWidth *= nHeightPct;
			dTemp.style.height = nHeight + "px";
			dTemp.style.width = nWidth + "px"; 
		}
		this.dBackground = document.createElement("div");
		this.dBackground.style.position = "fixed";
		this.dBackground.style.display = "none";
		this.dBackground.style.top = "0px";
		this.dBackground.style.left = "0px";
		this.dBackground.style.backgroundColor = "#474747";
		this.dBackground.style.height = "100%";
		this.dBackground.style.width = "100%";
		this.dBackground.style.opacity = .67;
		this.dBackground.style.filter = "alpha(opacity=67)";
		this.dBackground.style.zIndex = 999;
		this.dBackground.onclick = this.FS_Hide.bind(this);
		
		this.dLoading = new Image();
		this.dLoading.src = "images/loading.gif";
		this.dLoading.style.position = "relative";
		this.dLoading.style.top = "50%";
		//this.dLoading.style.marginTop = "-" + (this.dLoading.naturalHeight / 2) + "px";
		
		this.dOverlay = document.createElement("div");
		this.dOverlay.style.position = "absolute";
		this.dOverlay.style.display = "none";
		this.dOverlay.style.padding = "16px 16px";
		this.dOverlay.style.marginLeft = "auto";
		this.dOverlay.style.marginTop = "auto";
		this.dOverlay.style.top = "50%";
		this.dOverlay.style.left = "50%";
		this.dOverlay.style.backgroundColor = "#FFFFFF";
		
		this.dOverlay.style.zIndex = 1000;
		
		this.dImage = document.createElement("div");
		this.dImage.style.border = "1px solid black";
		this.dImage.style.padding = "10px";
		this.dImage.style.position = "relative";
		this.dImage.style.width = "600px";
		this.dImage.style.height = "400px";
		
		if(document.all)
		{
			this.dImage.style.width = "620px";
			this.dImage.style.height = "420px";
		}
		
		this.dCloseBtn = document.createElement("div");
		this.dCloseBtn.onclick = this.FS_Hide.bind(this);
		this.dCloseBtn.style.position = "relative";
		this.dCloseBtn.style.fontFamily = "arial";
		this.dCloseBtn.style.textAlign = "center";
		this.dCloseBtn.style.width = "100px";
		this.dCloseBtn.style.padding = "10px 0px;";
		this.dCloseBtn.style.border = "1px solid black";
		this.dCloseBtn.style.marginLeft = "auto";
		this.dCloseBtn.style.marginRight = "auto";
		this.dCloseBtn.style.marginTop = "30px";
		this.dCloseBtn.appendChild(document.createTextNode("Close"));
		
		this.dCloseBtn.onmouseover = function()
		{
			this.style.textDecoration = "underline";
		}
		
		this.dCloseBtn.onmouseout = function()
		{
			this.style.textDecoration = "none";
		}
		
		this.dImage.appendChild(dTemp);
		this.dOverlay.appendChild(this.dImage);
		this.dOverlay.appendChild(this.dCloseBtn);
		document.body.appendChild(this.dBackground);
		document.body.appendChild(this.dOverlay);
	}
	
	this.FS_ShowImage = function(szAddr)
	{ 
		var nSomething = 0;
		var nHeight = 0;
		var nWidth = 0;
		var fHeightPct = 0.0;
		var fWidthPct = 0.0;
		
		if (this.nWaitTimer == null)
		{
			this.dTemp = new Image();
			this.dTemp.src = szAddr;
		}
		
		if (!this.dTemp.complete) // If the image isn't done...
		{
			this.dBackground.style.display="block";
			this.dImage.innerHTML = "";
			this.dImage.appendChild(this.dLoading);
			this.dOverlay.style.display = "block";
			this.dOverlay.style.width = "600";
			this.dOverlay.style.height = "400";
			this.dOverlay.style.marginLeft = this.dOverlay.offsetWidth / -0.5;
			this.dOverlay.style.marginTop = this.dOverlay.offsetHeight / -0.5;
			this.nWaitTimer = setTimeout(this.FS_ShowImage.bind(this, szAddr), 250);
		}
		else
		{
			this.dOverlay.style.width = "auto";
			this.dOverlay.style.height = "auto";
			this.dOverlay.style.margin = "0px 0px 0px 0px";				
			
			if (IsOpera())
			{
				nWidth = this.dTemp.width;
				nHeight = this.dTemp.height;
			}
			else if(IsIE())
			{
				nWidth = this.dTemp.getAttribute("width");
				nHeight = this.dTemp.getAttribute("height");
			}
			else
			{
				nHeight = this.dTemp.naturalHeight;
				nWidth = this.dTemp.naturalWidth;
			}
			
			if(nWidth > 600)
			{
				fWidthPct = 600 / nWidth;
				
				nWidth *= fWidthPct;
				nHeight *= fWidthPct;
			}
			
			if (nHeight > 400) {
				fHeightPct = 400 / nHeight;
				
				nHeight *= fHeightPct;
				nWidth *= fHeightPct;
			}
			this.dTemp.style.height = nHeight + "px";
			this.dTemp.style.width = nWidth + "px";
			
			this.dImage.innerHTML = "";
			this.dImage.appendChild(this.dTemp);
			//this.dImage.appendChild(this.dCloseBtn);
			this.dOverlay.style.display = "block";
			this.dOverlay.style.width = (this.dOverlay.offsetWidth - 32) + "px";
			this.dOverlay.style.height = (this.dOverlay.offsetHeight - 32) + "px";
			this.dOverlay.style.marginLeft = ((this.dOverlay.offsetWidth / 2) * -1) + "px";
			this.dOverlay.style.marginTop = ((this.dOverlay.offsetHeight / 2) * -1) + "px";
			this.dBackground.style.display = "block";
			
			this.dTemp = null;
			this.nWaitTimer = null;
		}
	}
	
	this.FS_ShowVideo = function(szVidURL, nWidth, nHeight, flashvars, bHTML)
	{
		var fWidthPct = 0.0;
		var fHeightPct = 0.0;
		var params = {};
		var dFlash = null;
		if (nWidth && nHeight) 
		{
			if(bHTML == true)
			{
				dFlash = document.createElement("object");	
			}
			else
			{
				dFlash = document.createElement("div");
			}
			var bReturn;
			
			if(nWidth > 600 || nWidth < 350)
			{
				fWidthPct = 600 / nWidth;
				
				nWidth *= fWidthPct;
				nHeight *= fWidthPct;
			}
			
			if(nHeight > 400 || nHeight < 250)
			{
				fHeightPct = 400 / nHeight;
				
				nWidth *= fHeightPct;
				nHeight *= fHeightPct;
			}
			
			if(flashvars != undefined)
			{
				params.flashvars = flashvars;
			}
			
			dFlash.setAttribute("id", "dFlash");
			dFlash.setAttribute("name", "dFlash");
			
			if(bHTML == true)
			{
				dFlash.setAttribute("data", szVidURL);
				dFlash.setAttribute("type", "text/html");
				dFlash.setAttribute("border", "0");
				dFlash.style.height = (nHeight + 30) + "px";
				dFlash.style.width = (nWidth + 30) + "px";
				dFlash.style.border = "0px";
				
				this.dImage.style.overflow = "hidden";
			}
			
			this.dImage.innerHTML = "";
			this.dImage.appendChild(dFlash);
			this.dImage.style.width = nWidth + "px";
			this.dImage.style.height = nHeight + "px";
			
			if(swfobject != undefined && bHTML != true)
			{
				swfobject.embedSWF(szVidURL, "dFlash", nWidth, nHeight, "9.0.0", "includes/expressInstall.swf", {}, params);
			}
			
			this.dOverlay.style.width = "auto";
			this.dOverlay.style.height = "auto";
			this.dOverlay.style.display = "block";
			if(IsIE())
			{
				this.dOverlay.style.width = (nWidth + 24) + "px";
				this.dOverlay.style.height = (this.dOverlay.offsetHeight - 32) + "px";
			}
			else
			{
				this.dOverlay.style.width = (this.dOverlay.offsetWidth - 32) + "px";
				this.dOverlay.style.height = (this.dOverlay.offsetHeight - 32) + "px";
			}
			this.dOverlay.style.marginLeft = ((this.dOverlay.offsetWidth / 2) * -1) + "px";
			this.dOverlay.style.marginTop = ((this.dOverlay.offsetHeight / 2) * -1) + "px";
			this.dBackground.style.display = "block";
			this.bVideo = true;
		}
	}
	
	this.FS_StopPlay = function()
	{
		dFlashObj = document.getElementById("dFlash");
		if(dFlashObj.tagName == "object") 
		{
			if(IsIE())
			{
				if(dFlashObj.isPlaying())
				{
					dFlashObj.StopPlay();
				}
			}
			else
			{
				if(dFlashObj.IsPlaying())
				{
					dFlashObj.StopPlay();
				}
			}
		}
	}
	
	
	this.FS_Hide = function()
	{
		if(this.bVideo)
		{
			this.FS_StopPlay();
			this.bVideo = false;
		}
		this.dBackground.style.display ="none";
		this.dOverlay.style.display = "none";
		this.dImage.style.innerHTML = "";
		this.dOverlay.style.width = "0px";
		this.dOverlay.style.height = "0px";
		this.dOverlay.style.margin = "0px 0px 0px 0px";
	}
}

var dObj;
var g_FSOverlay = new FS_Overlay();
