function Controller()
{
	this.ie6 = false;
	this.intervalId = null;
	this.lcname = null;
	this.hidden = null;
	
	this.showSignupForm = function()
	{
		var frame = document.getElementById("eh_signup_frame");
		if(frame != null || this.intervalId != null)
		{
			throw "iframe already exists";		
		}
		
		this.hidden = new Array();
		this.hideObjects("object");
		this.hideObjects("embed");
		
		frame = document.createElement("IFRAME");
		frame.id = "eh_signup_frame";
		frame.src = "http://apps.panda.org/earthhour/signup.php?source=" + this.signup_source + "&lc=" + this.lcname;
		
		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.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";
				
		document.body.appendChild(frame);
	}
	
	this.closeSignupForm = function()
	{		
		setTimeout("controller.closeSignupFormDelay()", 1);		
	}
	
	this.closeSignupFormDelay = function()
	{
		var frame = document.getElementById("eh_signup_frame");
		if(frame == null)
		{
			throw "iframe not found";		
		}
		
		this.showObjects();
		document.body.removeChild(frame);
	}
	
	this.init = function(path, width, height, target, link)
	{
		if(target == null)
		{
			target = "eh_widget";
		}
	
		var linkNode = null;
		
		if(link != null)
		{
			linkNode = document.getElementById(link);
		}
		
		if(swfobject.hasFlashPlayerVersion("9.0.28.0"))
		{
			var now = new Date();
			
			if(this.lcname == null)
			{
				this.lcname = "eh_close_lc_" + now.getTime();
			}
			
			var flashvars = {lc_name: this.lcname};
			
			var params = {menu: "false", scale: "noScale", allowscriptaccess : "always", wmode:"transparent"};
			swfobject.embedSWF(path, target, width, height, "9.0.28.0", "http://apps.panda.org/earthhour/swf/expressInstall.swf", flashvars, params);
			
			if(linkNode)
			{
				linkNode.href = "javascript:controller.showSignupForm()";
			}
		}
		else
		{
			if(linkNode)
			{
				linkNode.style.display = "none";
			}		
		}
	}
		
	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;
	}
}

if(window.controller == null)
{
	controller = new Controller();
}
