/*******************************************************************
	Browser-Independent Layer Handler Version 2.2
	
	©2004-7 A+R Media Studio
	http://www.AandRmediastudio.com
	
	written by Rudy Dominguez (rudy@AandRmediastudio.com)
	
	These scripts are free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
*******************************************************************/

var bilh = new BILH();

function BILH(){
	this.ver = navigator.appVersion
	this.dom = (document.getElementById)? 1 : 0
	
	this.ie5 = ( (this.ver.substr(this.ver.indexOf("MSIE") >= 0)) && (parseInt(this.ver.substr(this.ver.indexOf("MSIE") + 4)) >= 5) && this.dom)? 1 : 0;
	this.ie4 = (document.all && !this.dom)? 1 : 0;
    this.ns6 = (this.dom && parseInt(this.ver) >= 6)? 1 : 0;
	this.ns5 = (this.dom && parseInt(this.ver) >= 5)? 1 : 0;
	this.ns4 = (document.layers && !this.dom)? 1 : 0;
		
	this.refs = new Object();
	
	this.useMenu = true;
	this.current = "";
	this.timerID = null;
	this.navImgID = "";
	this.defaultMenu = "";
	this.xRef = (this.dom && !this.ns4)? ((this.ie5 || this.ie4)? "event.clientX" : "e.clientX") : "e.pageX";
	this.yRef = (this.dom && !this.ns4)? ((this.ie5 || this.ie4)? "event.clientY" : "e.clientY") : "e.pageY";
	this.offsetXRef = (this.dom && !this.ns4)? ((this.ie5 || this.ie4)? "document.body.scrollLeft" : "pageXOffset") : "0";
	this.offsetYRef = (this.dom && !this.ns4)? ((this.ie5 || this.ie4)? "document.body.scrollTop" : "pageYOffset") : "0";
	this.dragLayer = null;
	this.dragStart = {x: 0, y: 0};
	this.dragging = false;
	this.navImgID = "";
}

BILH.prototype.register = function() {
	var layerID, i;
	
	if (arguments.length > 0) 
		for (i=0; i < arguments.length; i++) {
			if (typeof (arguments[i]) == "string") {
				layerID = arguments[i];
				
				var l = (this.dom)? document.getElementById(layerID) : (this.ie4)? document.all[layerID] : (this.ns4)?  document[layerID] : 0;
				if (l) {
					this.refs[layerID] = new Object();
					
					this.refs[layerID].name = layerID;
					this.refs[layerID].layer = l;
					this.refs[layerID].style = (this.dom || this.ie4)? l.style : ((this.ns4)?  document[layerID] : 0);
				}
			}
		}
		
	return;
}

BILH.prototype.show = function(layerID) {
	if (typeof(this.refs[layerID]) != "undefined") {
		this.refs[layerID].style.visibility = (this.ns4)? "show" : "visible";
		if (this.useMenu) this.current = layerID;
	}
	
	return;
}

BILH.prototype.hide = function(layerID) {
	if (typeof(this.refs[layerID]) != "undefined") {
		this.refs[layerID].style.visibility = (this.ns4)? "hide" : "hidden";
		
	}
	
	return;
}

BILH.prototype.toggle = function(layerID) {
	if (typeof(this.refs[layerID]) != "undefined") {
		((this.refs[layerID].style.display != "inline") && (this.refs[layerID].style.display != ""))? this.show(layerID) : this.hide(layerID);
	}
	
	return;
}

BILH.prototype.hideMenu = function(layerID) {
	if (this.useMenu) {
		this.timerID = setTimeout("bilh.hideMenuAll();", 250);
	}
	
	return;
}

BILH.prototype.hideMenuAll = function(hideDefault) {
	var item;
	
	if (re && (this.navImgID != "")) re.setOff(this.navImgID);
	if (re && (typeof(this.navLinkObj) != "undefined")) {
		re.setOff(this.navLinkObj);
	}
					  
	if (this.useMenu) {
		if (typeof(swap) != "undefined") stuntSwap(0); 
		
		for (item in this.refs) {
			//alert(item);
			this.hide(this.refs[item].name);
		}

		if ((this.defaultMenu != "") && !hideDefault) this.show(this.defaultMenu);
	}
	
	return;
}

BILH.prototype.showMenu = function(layerID, navImgID, navLinkObj) {
	if (this.useMenu) {
		clearTimeout(this.timerID);
		
		if (typeof(navImgID) != "undefined") this.hideMenuAll(true);
		if (typeof(swap) != "undefined") stuntSwap(1); 
		
		if ((layerID != "") && (typeof(this.refs[layerID]) != "undefined")) this.show(layerID);
		
		if (re && re.useOnState && (typeof(navImgID) != "undefined")) {
			re.setOn(navImgID, navLinkObj);
			this.navImgID = navImgID;
			this.navLinkObj = navLinkObj;
		}

		if (re && re.useOverState && (typeof(navImgID) != "undefined")) {
			re.setOver(navImgID, navLinkObj);
			this.navImgID = navImgID;
			this.navLinkObj = navLinkObj;
		}
	}
	
	return;
}

BILH.prototype.setContent = function(layerID, str){	
	if (layerID != "") {
		if (document.all) {
			this.refs[layerID].layer.innerHTML = str;
		} else {
			// even if NS 4, store the text as innerHTML for
			// later retrieval by getContent()	
			this.refs[layerID].layer.innerHTML = str;
	
			if (document.layers){	
				with(this.refs[layerID].layer) {
					open();
					write(str);
					close();
				}
			}
		}
		
		this.show(layerID);
		
		return true;
	} else {
		return false;	
	}
}

BILH.prototype.getContent = function(layerID){	
	return (layerID != "")? this.refs[layerID].layer.innerHTML : "";
}

BILH.prototype.setTop = function(layerID, y){	
	if (layerID != "") {
		this.refs[layerID].style.top = y;
		return true;
	} else {
		return false;	
	}
}

BILH.prototype.getTop = function(layerID){	
	return (layerID != "")? this.refs[layerID].style.top : 0;
}

BILH.prototype.setLeft = function(layerID, x){	
	if (layerID != "") {
		this.refs[layerID].style.left = x;
		return true;
	} else {
		return false;	
	}
}

BILH.prototype.getLeft = function(layerID){	
	return (layerID != "")? this.refs[layerID].style.left : 0;
}

BILH.prototype.isRegistered = function(layerID) {
	return (typeof(this.refs[layerID]) != "undefined");	
}

BILH.prototype.toString = function() {
	return "[object BILH]";	
}

/* DRAG_N_DROP FUNCTIONALITY 1.0*/
BILH.prototype.layerCount = function() {
	var i = 0;
	
	for (var o in this.refs) {
		i++;	
	}
	
	return i;
}
BILH.prototype.initDrag = function(baseObj) {
	this.bxo = baseObj.offsetLeft;
	this.byo = baseObj.offsetTop;
	var p = baseObj.offsetParent;
	
	while(p) {
		this.bxo += p.offsetLeft;
		this.byo += p.offsetTop;	
		p = p.offsetParent;
	}
}

BILH.prototype.setDragLayer = function(e, baseObj) {
	var mx = event.clientX - this.bxo + ((document.documentElement.scrollLeft)? document.documentElement.scrollLeft : document.body.scrollLeft);
	var my = event.clientY - this.byo + ((document.documentElement.scrollTop)? document.documentElement.scrollTop : document.body.scrollTop);
	var count = 0;// + baseObj.offsetTop
	
	window.status =  document.documentElement.scrollTop + "; mouse: " + mx + ", " + my + "; ";
	
	for (var i in this.refs) {
		count++;
		var xx = parseInt(this.refs[i].layer.offsetLeft);
		var yy = parseInt(this.refs[i].layer.offsetTop);
		
		p = this.refs[i].layer.offsetParent;
		
		while (p && p != baseObj) {
			xx += 	p.offsetLeft;
			yy += 	p.offsetTop;	
			p = p.offsetParent;
		}
	

		//var xx = baseObj.offsetLeft + parseInt(this.refs[i].layer.offsetLeft);
		//var yy = baseObj.offsetTop + parseInt(this.refs[i].layer.offsetTop);
		//window.status += ">: " + + "; ";
		window.status += "layer: " +  xx + "-" + (xx + parseInt(this.refs[i].style.width) + ", " + yy) + "-" + (yy + parseInt(this.refs[i].style.height)) + "; ";	
		if ( (mx > xx) && (mx < xx + parseInt(this.refs[i].style.width)) && (my > yy) && (my < yy + parseInt(this.refs[i].style.height)) ) {
			this.dragLayer = this.refs[i];
			this.dragLayer.style.zIndex = 100;
			this.dragLayer.onSelectStart = function() {
				return false;
			}
			this.dragStart = {x: mx, y: my};
	
			return;
		}

	}
}

BILH.prototype.clearDragLayer = function() {
	if (this.dragLayer != null) {
		this.dragLayer.style.zIndex = 1;
	}
	this.dragLayer = null;
	this.dragging = false;
}

BILH.prototype.moveDragLayer = function(e, baseObj, yOnly) {
	var mx = event.clientX - this.bxo + ((document.documentElement.scrollLeft)? document.documentElement.scrollLeft : document.body.scrollLeft)
	var my = event.clientY - this.byo + ((document.documentElement.scrollTop)? document.documentElement.scrollTop : document.body.scrollTop);
//	window.status = "mouse: " + mx + ", " + my + ";";
	
	if (this.dragLayer != null) {	
		if (!yOnly) {
			var x = mx - this.dragStart.x;
			this.dragLayer.style.left = x;//(x > 0)? x : 0;
		}
		var y = my - this.dragStart.y;
		this.dragLayer.style.top = y;//(y > 0)? y : 0;
		
		this.dragging = true;
		
		window.status = "mouse: " + mx + ", " + my + "; layer: " + this.dragStart.x  + ", " + this.dragStart.y + "; ";	
	} 
}

