//$Log: layers.js,v $
//Revision 1.5  2008/07/12 16:23:09  agitpap
//sync for oldtimer
//
//Revision 1.4  2008/07/06 21:01:21  agitpap
//change layer function to use divs instead of tables
//
//Revision 1.3  2008/07/06 17:41:33  agitpap
//extend catch block
//
//Revision 1.2  2008/05/31 09:21:53  agitpap
//tidy
//
//Revision 1.1  2008/05/24 21:14:16  agitpap
//initial check-in
//

function getImgDir()
{
	return "../images/";
}


var Layers = [];

function bringToTop(_layers, id)
{
	var o = findObj(id);
	var z = parseInt(o.style.zIndex);
	var zz = 0;
	var zz_key = "";
	var shadow = null;

	for (key in _layers)
	{
		if (!key || id == _layers[key].layerId)
		{
			continue;
		}

		var shadow = _layers[key].shadowLayer;
		if (shadow != null)
		{
			shadow.style.zIndex = 2;
		}

		findObj(_layers[key].layerId).style.zIndex = 3;
	}

	var shadow = Layers[dragObj.id].shadowLayer;
	if (shadow != null)
	{
		shadow.style.zIndex = 4;
	}

	o.style.zIndex = 5;

	//writeLog("Layer : " + id + " zIndex : " + o.style.zIndex);
}

function layer(id, parentId,
		layerTitle,
		width, height,
		left, top,
  		clientAlign, clientVAlign,
		bg_img,
		bg_repeat,
		bg_colour,
		title_bar_colour,
		title_bar_text_colour,
		favicon,
		minimise_icon,
		maximise_icon,
		close_icon,
		onCloseFoo,
		makeShadow)
{

	if (Layers[id] != null)
	{
	  /* o = findObj(id);
	   if (o)
		alert('[' + id + ']Already exists : '
                + "\nWidth : " + o.style.width
                + "\nHeight : " + o.style.height
                + "\nleft : " + o.style.left
                + "\ntop : " + o.style.top
                + "\nzIndex : " + o.style.zIndex
                + "\nVisibility : " + o.style.visibility
            );
    */
		return;
	}


	Layers[id] = this;
    writeLog("New Layer Added : " + id );

	this.layerWidth = width;
	this.layerHeight = height;
	this.state = 'normal';
	this.layerId = id;
	this.newLayer = document.createElement("DIV");
	this.newLayer.id = id;
	this.newLayer.style.backgroundColor = '#' + bg_colour;
	this.newLayer.style.height = height + "px";
	this.newLayer.style.width = width + "px";
	this.newLayer.style.border = "1px solid black";
	this.newLayer.style.position = "absolute";
	this.newLayer.style.zIndex = 5;

    this.newLayer.style.left = left + "px";
	this.newLayer.style.top = top + "px";

	this.title_bar =  document.createElement("DIV");
	this.title_bar.style.height = "20px";
	this.title_bar.style.width = (width - 8) + "px";
	this.title_bar.style.color = '#' + title_bar_text_colour;


	this.title_bar.style.backgroundColor = '#' + title_bar_colour;

	try {
		this.title_bar.style.backgroundImage = "url(" + favicon + ")";
		this.title_bar.style.backgroundPosition = "2px 2px";
		this.title_bar.style.backgroundRepeat = "no-repeat";
	}
	catch (err)
	{}

	this.newLayer.appendChild(this.title_bar);

	this.title_bar.innerHTML = layerTitle + "&nbsp;";

	this.title_bar.id = id + "_titlebar";
	this.title_bar.style.textAlign = "right";

	this.newLayer.appendChild(this.title_bar);
	
	this.shadowLayer = document.createElement("DIV");
	this.shadowLayer.style.height = height + "px";
	this.shadowLayer.style.width = width + "px";
	this.shadowLayer.style.position = "absolute";
	this.shadowLayer.style.left = left + shadowWidth + "px";
	this.shadowLayer.style.top = top  + shadowWidth  + "px";
	this.shadowLayer.style.backgroundColor = "#333355";
	this.shadowLayer.style.visibility = "visible";
	this.shadowLayer.style.zIndex = 4;
	this.shadowLayer.id = id + "_shadow";
	this.shadowLayer.style.opacity = ".1";
	this.shadowLayer.style.filter = 'alpha(opacity=10)';

	/**

		Dragging ...................

	*/
	this.foo = function(){
				o = findObj(id);
	 			setDragObj(o);
	 			bringToTop(Layers, id);
			}

	if (dom_event_handler)
	{
		this.title_bar.addEventListener('mousedown',this.foo,true);
	}
	else if (ie_event_handler)
	{
		this.title_bar.attachEvent('onmousedown',this.foo);
	}

	this.minimise_foo = function()
	{
		writeLog("[" + id + "]minimise : state " + Layers[id].state);
		if(Layers[id].state == 'normal')
		{
			findObj(id).style.height = 26 + "px";
			findObj(id + "_client").style.display = "none";
			shadow = Layers[id].shadowLayer;
			if(shadow)
			{
				shadow.style.display = "none";
			}
			Layers[id].state = 'minimised';
		}
		else
		{
			findObj(id).style.height = Layers[id].layerHeight + "px";
			findObj(id + "_client").style.display = "block";
			shadow = Layers[id].shadowLayer;
			if(shadow)
			{
				shadow.style.display = "block";
			}
			Layers[id].state = 'normal';
		}

	}

	if (dom_event_handler)
	{
		this.title_bar.addEventListener('doubleclick',this.minimise_foo,true);
	}
	else if (ie_event_handler)
	{
		this.title_bar.attachEvent('doubleclick',this.minimise_foo);
	}

	try
	{
		if (maximise_icon.length > 0)
		{

			this.maximise = document.createElement("img");
			this.maximise.src = maximise_icon;
			this.maximise.style.height = "18px";
			this.maximise.style.width = "18px";
			this.title_bar.appendChild(this.maximise);

		}

		if (minimise_icon.length > 0)
		{

			this.minimise = document.createElement("img");
			this.minimise.src = minimise_icon;
			this.title_bar.appendChild(this.minimise);
			this.minimise.style.height = "18px";
			this.minimise.style.width = "18px";
			//this.minimise.style.cssFloat = "right";



			if (dom_event_handler)
			{
				this.minimise.addEventListener('click',this.minimise_foo,true);
			}
			else if (ie_event_handler)
			{
				this.minimise.attachEvent('onclick',this.minimise_foo);
			}

		}

		if (close_icon.length > 0)
		{

			this.close_layer = document.createElement("img");
			this.close_layer.src = close_icon;
			this.title_bar.appendChild(this.close_layer);
			this.close_layer.style.cssFloat = "top";
			this.close_layer.style.height = "18px";
			this.close_layer.style.width = "18px";

			this.close_layer_foo = function()
			{
				if(onCloseFoo)
				{
					onCloseFoo();
				}
				o = findObj(id);
				shadow = Layers[id].shadowLayer;
				p = o.parentNode;
				if (shadow != null)
				{
					p.removeChild(shadow);
				}
				p.removeChild(o);
				Layers[id] = null;
			}

			if (dom_event_handler)
			{
				this.close_layer.addEventListener('click',this.close_layer_foo,true);
			}
			else if (ie_event_handler)
			{
				this.close_layer.attachEvent('onclick',this.close_layer_foo);
			}
		}
	}
	catch (err) {}


	this.client_tr =  document.createElement("DIV");
	//this.client_tr.style.border = "1px solid black";

	if(clientAlign)
	{
		this.client_tr.style.textAlign = clientAlign;
	}

	if(clientVAlign)
	{
		this.client_tr.style.verticalAlign = clientVAlign;
	}

	//this.client_tr.style.height = (height - 16) + "px";
	this.client_tr.style.width = (width - 10) + "px";
	this.client_tr.id = id + "_client";
	this.newLayer.appendChild(this.client_tr);
	this.client_tr.style.overflow = "auto";

	if (bg_img.length > 0)
	{
		this.client_td.style.backgroundImage = "url(" + bg_img + ")";
		this.client_td.style.backgroundRepeat = bg_repeat;
	}

	if (bg_colour.length > 0)
	{
		this.client_tr.style.backgroundColor = "#" + bg_colour;
	}


	this.newLayer.style.visibility = "visible";
	findObj(parentId).appendChild(this.newLayer);

	if (this.shadowLayer != null)
	{
		findObj(parentId).appendChild(this.shadowLayer);
		this.shadowLayer.style.visibility = "visible";
	}

	//document.getElementsByTagName("body").item(0).appendChild(this.newLayer);

}

