function Layer(id)
{
	//Variables
	this.id = id;	
	this.isVisible = false;	
	//this._doc = document;
	//this._elem = this._doc.getElementById(this.id);
	
	//Opacity and fade vars
	this.opac = 0.0;		//Current opacity
	this.fadeTimer = null;	//Timer used when fading
	this.opacChange = 0.1; 	//Amount to change opacity during fade
	this.maxOpac = 0.99; 	//The maximum opacity allowed for the layer, defaults to 0.99 (Flicker 'bug' if fading to 1.0)
	this._funcPtrOnFadeDone = null;	//Function pointer to fade done callback
	
	this._strTest = null;

//	SetOpacity(opac);
}

Layer.prototype.SetId = function(pId)
{
	this.id = pId;
}
	
Layer.prototype.GetId =	function()
{		
	return this.id;
}

Layer.prototype.SetTop = function(top)
{	
	document.getElementById(this.id).style.top = parseInt(top)  + "px";
}

Layer.prototype.GetTop = function()
{
	return parseInt(document.getElementById(this.id).style.top);
}

Layer.prototype.SetLeft = function(left)
{		
	document.getElementById(this.id).style.left = parseInt(left) + "px";
}

Layer.prototype.GetLeft = function()
{
	return parseInt(document.getElementById(this.id).style.left);
}

Layer.prototype.SetWidth = function(width)
{
	document.getElementById(this.id).style.width = parseInt(width) + "px";
}

Layer.prototype.GetWidth = function()
{		
	return parseInt(document.getElementById(this.id).style.width);
}

Layer.prototype.SetHeight = function(height)
{		
	//if (parseInt(height) < 0)
	//	alert("Layer:SetHeight: height cannot be negative, id:" + this.GetId());
	document.getElementById(this.id).style.height = parseInt(height) + "px";
}

Layer.prototype.GetHeight = function()
{		
	return parseInt(document.getElementById(this.id).style.height);
}

Layer.prototype.Center = function()
{
	if (isNaN(parseInt(document.getElementById(this.id).style.width)))
	{
		alert("Layer:Center - Width must be set before centering. (Id: " + this.GetId() + ")");
	}
	
	document.getElementById(this.id).style.left = (self.innerWidth/2 - parseInt(document.getElementById(this.id).style.width)/2) + "px";
}

Layer.prototype.CenterVert = function()
{
	if (isNaN(parseInt(document.getElementById(this.id).style.height)))
	{
		alert("Layer:CenterVert - Height must be set before centering. (Id: " + this.GetId() + ")");
	}
	
	document.getElementById(this.id).style.top = (self.innerHeight/2 - parseInt(document.getElementById(this.id).style.height)/2)  + "px";
}

Layer.prototype.SetZIndex = function(zIndex)
{		
	document.getElementById(this.id).style.zIndex = parseInt(zIndex);
}

Layer.prototype.GetZIndex = function()
{		
	if (isNaN(parseInt(document.getElementById(this.id).style.zIndex)))
	{
		alert(this.GetId() + ": GetZIndex: zIndex not set");
		return;
	}
	else
		return parseInt(document.getElementById(this.id).style.zIndex);
}

Layer.prototype.SetVisible = function(visible)
{	
	if (visible)
	{
		//alert ("Showing: " + this._elem);
		//this._elem.style.visibility = "visible";
		document.getElementById(this.id).style.visibility = "visible";
		this.isVisible = true;
	}
	else
	{
		//alert ("Hiding: " + this._elem);
		//this._elem.style.visibility = "hidden";
		document.getElementById(this.id).style.visibility = "hidden";
		this.isVisible = false;
	}
}

Layer.prototype.IsVisible = function()
{	
	//alert(this.id);
	return this.isVisible;
}

Layer.prototype.SetBGColor = function(color)
{
	//HTML { background-color : sColor }  
	//Scripting object.style.backgroundColor [ = sColor ] 
	
	//transparent is default
	document.getElementById(this.id).style.backgroundColor = color;
}

//---------------------------------------------------------------------------------------------------------------------

Layer.prototype.SetOpacity = function(opacity)
{		
	this.opac = Math.min(0.99, parseFloat(opacity));
	
	//this._strTest = this._strTest.concat(this.opac + " | ");
	//window.status = this._strTest;
	
	document.getElementById(this.id).style.MozOpacity = this.opac;	
}

Layer.prototype.SetMaxOpacity = function(opacity)
{	
	if ( parseFloat(opacity) >= 1.0)
	{
		alert ("Layer::Fade: Max opacity must be less than 1.0! Setting to 0.99.");
		this.maxOpac = 0.99;
	}
	if ( parseFloat(opacity) <= 0.0)
	{
		alert ("Layer::Fade: Max opacity must be more than 0.0! Setting to 0.99.");
		this.maxOpac = 0.99;
	}
	
	this.maxOpac = parseFloat(opacity);
}

Layer.prototype.Fade = function(opacityChange, speed)
{
	this._strTest = "";
	
	if ( parseFloat(opacityChange) > 0.5 || parseFloat(opacityChange) < -0.5)
	{
		alert ("Layer::Fade: opacityChange must be less than or eqal to 0.5 and more than or equal to -0.5! Layer Id: " + this.GetId());
		return;
	}
	
	this.opacChange = parseFloat(opacityChange);	
	var _this = this;
	
	if (parseFloat(this.opacChange) > 0.0)
	{
		this.SetOpacity(0.0);
		this.SetVisible(true);
		this.fadeTimer = setInterval( function() { _this.fadeIn() }, speed);
	}
	
	if (parseFloat(this.opacChange) < 0.0)
	{
		this.SetOpacity(this.maxOpac);
		this.SetVisible(true);
		this.fadeTimer = setInterval( function() { _this.fadeOut() }, speed);
	}
}

Layer.prototype.fadeIn = function()
{	
	this.SetOpacity( this.opac + this.opacChange );
		
	if ( this.opac >= this.maxOpac )
	{		
		clearInterval(this.fadeTimer);
		this.fadeTimer = null;
		this.SetOpacity(this.maxOpac);
		
		if ( this._funcPtrOnFadeDone != null )
		{
			//Call callback function
			setTimeout( this._funcPtrOnFadeDone, 10);
			this._funcPtrOnFadeDone = null;			
		}
	}
}

Layer.prototype.fadeOut = function()
{
	this.SetOpacity( this.opac + this.opacChange );	//opacChange will be negative
		
	if ( this.opac <= 0 )
	{		
		clearInterval(this.fadeTimer);
		this.fadeTimer = null;
		this.SetOpacity(0.0);
		this.SetVisible(false);
		
		if ( this._funcPtrOnFadeDone != null )
		{
			//Call callback function
			setTimeout( this._funcPtrOnFadeDone, 10); 
			this._funcPtrOnFadeDone = null;
		}
	}
}

Layer.prototype.SetOnFadeDone = function(funcPtrCallback)
{
	//alert (funcPtrCallback);
	this._funcPtrOnFadeDone = funcPtrCallback;
}
	
//---------------------- Show -------------------------------------------

Layer.prototype.Show = function(show)
{
	if (show)
	{		
		//this.SetTop(50);
		//this.SetOpacity(100);
		document.getElementById(this.id).style.visibility = "visible";
		this.isVisible = true;
	}
	else
	{			
		document.getElementById(this.id).style.visibility = "hidden"
		this.SetTop(-800);
		//this.SetOpacity(0);
		this.isVisible = false;
	}
}

//---------------------------------------------------------------------------------------------------------------------
//Menu functions
/*
Layer.prototype.IsMenu = function()
{
	return this.isMenu;
}

Layer.prototype.SetMenu = function(pMenu)
{
	//alert(this.id + " " + pMenu);
	this.isMenu = true;
	this.menu = pMenu;
}

Layer.prototype.GetMenu = function()
{
	//alert(isMenu);
	if (this.isMenu)
		return this.menu;
	else
		return null;
}
	
Layer.prototype.BuildMenu = function(doc)
{		
	if (!this.isMenu)
	{
		alert('Cannot build menu. ' + this.id + ' is not a menu.');
		return;
	}
	//alert(menu);
	//menu.AlertMenuItems();
		
//		alert('Building menu: ' + id);
	doc.writeln("<div id=\"" + this.id + "\" style=\"position: absolute\">");
	doc.writeln("<table width=\"100\" bordercolorlight=\"#000000\" bordercolordark=\"#FFFFFF\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"background: transparent url(../images/bg_menu_1.gif) repeat-y\">");
	doc.writeln("<tr><td>");
	doc.writeln("<table border=\"0\">");
	doc.writeln("<tr>");
	doc.writeln("<td border=\"0\" width=\"100\" align=\"left\" nowrap>");
		
	for(var i = 0; i < this.menu.NumItems(); i++)
	{
		doc.writeln("<a href=\"javascript:MenuPressed('" + this.menu.GetMenuItem(i) + "')\">")
		doc.writeln(this.menu.GetMenuItem(i).replace(/_/g," "))
		doc.writeln("</a><br>")
	}
		
	doc.writeln("</td></tr>");
	doc.writeln("</table>");
	
	doc.writeln("");
	
	doc.writeln("</td></tr>");
	doc.writeln("</table>");
	doc.writeln("</div>");
}

Layer.prototype.UpdateMenuPos = function()
{		
	if (!this.isMenu)
	{
		alert('Cannot update menu. ' + this.id + ' is not a menu.');
		return;
	}
	else
		this.menu.UpdatePosition(this.id);
}*/
