//$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 = "";

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

		/*
		var _z = parseInt(findObj(_layers[key].layerId).style.zIndex);
		writeLog("Layer[" + key + "] zIndex : " + o.style.zIndex);
		if (_z > zz)
		{
			zz_key = key;
			zz = _z;
		}
		*/
		findObj(_layers[key].layerId).style.zIndex = 1;
	}

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

	/*
	if (zz > z)
	{
		o.style.zIndex = zz;
		findObj(key).zIndex = z;
	}
	else
	{
		o.style.zIndex = zz + 1;
	}
	*/
	o.style.zIndex = 2;
	writeLog("Layer : " + id + " zIndex : " + o.style.zIndex);
}

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



	if (Layers[id] != null)
	{
		//writeLog('Layer[' + id + '] already exists');
	  /* 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 );
	var dom_event_handler = (document.addEventListener) ? true : false;
	var ie_event_handler = (document.attachEvent) ? true : false;

	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.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;

	this.title_bar.style.backgroundColor = title_bar_colour;

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

	this.newLayer.appendChild(this.title_bar);

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

	this.newLayer.appendChild(this.title_bar);


	/**

		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);
	}


	try
	{
		if (maximise_icon.length > 0)
		{

			this.maximise = document.createElement("img");
			this.maximise.src = maximise_icon;
			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_foo = function()
			{
				writeLog("[" + id + "]minimise : state " + Layers[id].state);
				if(Layers[id].state == 'normal')
				{
					findObj(id).style.height = 20 + "px";
					Layers[id].state = 'minimised';
				}
				else
				{
					findObj(id).style.height = Layers[id].layerHeight + "px";
					Layers[id].state = 'normal';
				}

			}

			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_foo = function()
			{
				if(onCloseFoo)
				{
					onCloseFoo();
				}
				o = findObj(id);
				p = o.parentNode;
				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 - 20) + "px";
	this.client_tr.style.width = width + "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";
	this.newLayer.style.zIndex = "10";

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

}

