function OTPopoupApp()
{
	this.frameId = "ot_popup_app_frame";
	this.cookieName = "otstatus";
	this.ie6 = false;
	this.hidden = null;

	this.init = function(rootPath, instance, type, showAlways)
	{
		if(showAlways)
		{
			this.showAlways = true;			
		}
		else
		{
			this.showAlways = false;			
			
			this.cookieName += instance + type;

			if(this.readCookie(this.cookieName) == "true")
			{			
				return;
			}			
		}
		
		var frame = document.getElementById(this.frameId);
		if(frame != null)
		{
			throw "iframe already exists";		
		}
		
		this.hidden = new Array();
		this.hideObjects("object");
		this.hideObjects("embed");
		
		frame = document.createElement("IFRAME");
		frame.id = this.frameId;
		frame.src = rootPath + instance + "/overlay_" + type + ".html";
		
		if(this.ie6)
		{
			this.hideControls("select");
			frame.style.position = "absolute";  
			frame.style.setExpression("height", "(a = document.body.offsetHeight + document.body.offsetTop - documentElement.scrollTop)");
			frame.style.setExpression("top", "(a = documentElement.scrollTop)");
			frame.scrolling = "no";
		}
		else
		{
			frame.style.top = "0";
			frame.style.height = "100%";
			frame.style.position = "fixed";
			frame.style.left = "0";
			frame.style.zIndex = 100;
		}
		
		frame.allowTransparency  = "true";
		frame.style.width = "100%";
		frame.style.backgroundColor = "transparent"
		frame.style.border = "none"
		frame.style.margin = "0";
		frame.style.padding = "0";
		frame.frameBorder  = "no";
		
		this.container = document.getElementById("ot_popup_app");		
		this.container.appendChild(frame);
	}
	
	this.closeOverlay = function()
	{
		if(!this.showAlways)
		{
			this.createCookie(this.cookieName, "true", 30);
		}
		
		var frame = document.getElementById(this.frameId);
		if(frame == null)
		{
			throw "iframe not found";		
		}
		
		this.showObjects();
		this.container.removeChild(frame);
	}
	
	this.hideObjects = function(type)
	{
		var objects = document.getElementsByTagName(type);
		for(var i = 0; i < objects.length; i++)
		{
			var object = objects[i];
			if(object.style.visibility.toLowerCase() == "hidden")
			{
				continue;
			}
			
			var hide = true;
			if(object.wmode == undefined)
			{
				var embed;
				if(type == "object")
				{
					var params = object.getElementsByTagName("param");
					for(var j = 0; j <  params.length; j++)
					{
						var param = params[j];
						if(param.name && param.name.toLowerCase() == "wmode")
						{
							if(param.value && param.value.toLowerCase() == "transparent")
							{
								hide = false;
							}
							break;
						}
					}
					
					embed = object.getElementsByTagName("embed")[0];
				}
				else
				{
					embed = object;
				}
				
				if(embed)
				{
					var param = embed.attributes["wmode"];
					if(param && param.value && param.value.toLowerCase() == "transparent")
					{					
						hide = false;
					}
				}
			}
			else
			{
				if(object.wmode.toLowerCase() == "transparent")
				{
					hide = false;
				}
			}			
			
			if(hide)
			{				
				this.hidden.push(object);
				object.style.visibility = "hidden";
			}
		}
	}
	
	this.hideControls = function(type)
	{
		var objects = document.getElementsByTagName(type);
		for(var i = 0; i < objects.length; i++)
		{
			var object = objects[i];
			if(object.style.visibility.toLowerCase() != "hidden")
			{
				this.hidden.push(object);
				object.style.visibility = "hidden";
			}
		}	
	}
	
	this.showObjects = function()
	{
		var objects = this.hidden;
		for(var i = 0; i < objects.length; i++)
		{
			var object = objects[i];
			if(object && object.style)
			{
				object.style.visibility = "visible";
			}
		}
		
		this.objects = null;
	}
	
	this.createCookie = function(name, value, days) 
	{
		var expires = "";
		if (days) 
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}

		document.cookie = name + "=" + value + expires + "; path=/";
	}

	this.readCookie = function(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i = 0;i < ca.length; i++) 
		{
			var c = ca[i];
			while (c.charAt(0) == ' ')
			{
				c = c.substring(1, c.length);
			}
			
			if (c.indexOf(nameEQ) == 0) 
			
			return c.substring(nameEQ.length, c.length);
		}
		
		return null;
	}
}

if(window.otpopupapp == null)
{
	otpopupapp = new OTPopoupApp();
}
