/*
* LAYER OBJECT FUNCTIONS
* 
* Layer managment functions 
*
*/


/************************************************* HTML OBJECT MANAGEMENT FUNCTIONS **************************************************/		

//var to hold layer objects as associative array
var html_objs = new Object();





/********************************************************** HTML OBJECT  *************************************************************/	


//constructor class for dynamic layer
function htmlObj (window,id) 
{
	//set lyr id to id property
	this.window = window;
	this.id = id;
	
	//make shortcut to document object
	var d = document;
	
	//alert(this.id)
	//get ref to html element and element styles
	this.element = (document.getElementById) ? d.getElementById(this.id) : this.element = d.all[this.id];
	this.style = this.element.style;
	
	//call function to set default properties
	this.setDefaultProperties();
}


//default method (called from constructor) to set object base properties
htmlObj.prototype.setDefaultProperties = function() 
{
	//set initial values for event stack
	this.event_stack = new Array(0);	//array holding event actions waiting to execute
	this.event_stack_active = 0; //ref to stack activity (0 = off)
	this.event_stack_interval = 50;
	
	//set clip step
	this.clip_step = 15;
	
	//set initial value for mouseover status
	this.mouseover = false;
}


/********************************************************** HTML STANDARD METHODS  **************************************************/	


htmlObj.prototype.show = function() { this.style.visibility = 'visible'; }
htmlObj.prototype.hide = function() { this.style.visibility = 'hidden'; }
htmlObj.prototype.setStackingOrder = function(z) { this.style.zIndex = z; }
htmlObj.prototype.setBgColor = function(color) { this.style.backgroundColor = color; }
htmlObj.prototype.setBgImage = function(image) { this.style.backgroundImage = image; }
htmlObj.prototype.setWidth = function(width) { this.style.width = width; }
htmlObj.prototype.setHeight = function(height) { this.style.height = height; }

htmlObj.prototype.setClip = function(top,right,bottom,left) 
{ 
	var toggle_vis = (is_nav && (is_major < 7) && this.isVisible) ? true : false;
	if (toggle_vis) this.hide();
	this.style.clip = 'rect(' + top + 'px ' + right + 'px ' + bottom + 'px ' + left + 'px' + ')';
	if (toggle_vis) this.show();
}

/**************************************
* method to write to object (retstricted usage)
**************************************/

htmlObj.prototype.setBody = function() 
{
	var string_body = '';
	for (var i=0;i<arguments.length;i++) string_body += (arguments[i] + '\n');
	//set body string to html body
	window.alert(this.element.innerHTML);
	this.element.innerHTML = string_body;
	window.alert(this.element.innerHTML);
}


/*************************************
* methods to retrieve style properties.
**************************************/

//methods for retrieving info on layer object
htmlObj.prototype.getX = function() { return (is_ie) ? parseInt(this.style.pixelLeft) : parseInt(this.style.left); }
htmlObj.prototype.getY = function() { return (is_ie) ? parseInt(this.style.pixelTop) : parseInt(this.style.top); }
htmlObj.prototype.getWidth = function() { return parseInt(this.style.width); }
htmlObj.prototype.getHeight = function() { return parseInt(this.style.height); }
htmlObj.prototype.getStackingOrder = function() { return this.style.zIndex; }
htmlObj.prototype.isVisible = function() { return this.style.visibility == 'visible'; }

htmlObj.prototype.getClip = function ()
{
	//turn 'rect(tpx rpx bpx lpx)' string into array of values
	var clip_array = this.style.clip.match(/(\d+px)/g);
	for (var i=0;i<clip_array.length;i++) clip_array[i] = parseInt(clip_array[i]);
	return clip_array
}

/**************************************
* methods to handle events
**************************************/

htmlObj.prototype.addEventHandler = function (event_name, handler) 
{
	//get ref to current obj for use in nested function
	var obj = this; 
	
	if (is_ie)
	{
		//define a function that will invoke the specified handler
		this.element[event_name] = function () {
			var e = window.event;
			return handler(e, obj, e.clientX, e.clientY, e.screenX, e.screenY);
		}
	}
	else
	{
		//define a function that will invoke the specified handler
		this.element[event_name] = function (e) 
		{ 
			return handler(e, obj, e.pageX, e.pageY, e.screenX, e.screenY); 
		}
	}
}


htmlObj.prototype.removeEventHandler = function (event_name) 
{
	delete this.element[event_name];
}





//function to check whether x, y pos is within layer (i.e. is mouse over layer)
//n.b. width and height must be declared inline (i.e. style="width: Xpx; height: Ypx;")
htmlObj.prototype.isMouseOver = function (pos_x, pos_y) {
	
	//get x y positions of layer
	var crnt_x = this.getX();
	var crnt_y = this.getY();
	
	var offset = 0;	//offset to avoid overlapping
	
	if (((crnt_x + offset) > pos_x) || ((crnt_x + this.getWidth()) <= pos_x)) return false;
	else if (((crnt_y + offset) > pos_y) || ((crnt_y + this.getHeight()) <= pos_y)) return false;
	
	return true;

}

/**************************************
* methods to control event stack
**************************************/


htmlObj.prototype.addEvent = function (exec_string, exec_type, remove_group) 
{
	this.pauseEventStack();
	
	//remove group events if set
	if (remove_group) this.removeEventGroup (exec_type);
	
	//add item to index
	var n = this.event_stack.length;
	this.event_stack[n] = new Object();
	this.event_stack[n].event_exec = exec_string;	//execution string
	this.event_stack[n].event_type = exec_type;	//event type (allows for group cancelling)
	//window.alert(exec_string);
	this.startEventStack();
}


htmlObj.prototype.RemoveEvent = function (pos) 
{
	this.pauseEventStack();
	
	var temp_array = new Array();	//create temporary array
	
	//loop through event stack array
	var i, n = 0;
	for (i=0;i<this.event_stack.length;i++) 
	{
		//if not selected event, then add to temp array
		if (i != pos) { temp_array[n] = this.event_stack[i]; n++; } 
	}
	
	this.event_stack = temp_array;	//set event stack to temp array
	
	this.startEventStack();
}


htmlObj.prototype.removeEventGroup = function (exec_type) 
{
	this.pauseEventStack();
	
	var temp_array = new Array();	//create temporary array
	
	//loop through event stack array
	var i, n = 0;
	for (i=0;i<this.event_stack.length;i++) 
	{
		//if not selected event, then add to temp array
		if (this.event_stack[i].event_type != exec_type) { temp_array[n] = this.event_stack[i]; n++; }
	}
	
	this.event_stack = temp_array;	//set event stack to temp array
	
	this.startEventStack();
}


htmlObj.prototype.startEventStack = function () 
{
	if (this.event_stack_active == 0) 
		this.event_stack_active = window.setInterval('html_objs["'+this.id+'"].EventStackController()',this.event_stack_interval);
}


htmlObj.prototype.pauseEventStack = function () 
{
	if (this.event_stack_active != 0) 
		window.clearTimeout(this.event_stack_active);
	this.event_stack_active = 0;
}


htmlObj.prototype.EventStackController = function () 
{
	//if there are any events in the stack then execute top event
	if (this.event_stack.length > 0) {
		eval(this.event_stack[0].event_exec);	//execute item
		
		//remove item from event stack (less optimised version for non array.shift() browsers)
		//n.b. need to test on ie5x pc (poss could be supported on ie5.5)
		if (is_ie && (is_major < 6)) this.RemoveEvent(0);
		else this.event_stack.shift();
	}
	else
	{
		this.pauseEventStack();	//pause event stack if no events left to run
	}
}