/*******************************
*
* elcLayer.js
* 
* generic layer class
*
* Author: DoublePrime
* References: ELCI Clinique layer.js created by Razorfish
* Version: 0.1
*
* Comments
*
*******************************/
DEBUG = (window.location.search.indexOf('debug=1') > -1) ? true : false;

var xShift = 0;
var yShift = 0;

function elcLayer(id) {
	if (bw.ns4) this.layerAlias = getLayerAlias(id);
	this.obj=bw.dom?document.getElementById(id):bw.ie4?document.all[id]:bw.ns4?eval(this.layerAlias):0;
	this.css=bw.dom?document.getElementById(id).style:bw.ie4?document.all[id].style:bw.ns4?this.obj:0;
	if (bw.ie4) {this.x=this.css.pixelLeft;this.y=this.css.pixelTop;this.h=this.css.clientHeight;this.w=this.css.clientWidth;}
	if (bw.ns4) {this.x=this.css.left;this.y=this.css.top;this.h=this.obj.document.height;this.w=this.obj.document.width;}
	if (bw.ie5 || bw.ns6) {
		this.x=findAbsoluteOffset(this.obj, cFindOffsetFromLeft);
		this.y=findAbsoluteOffset(this.obj, cFindOffsetFromTop);
		this.h=this.obj.offsetHeight;
		this.w=this.obj.offsetWidth;
	}

	function getLayerAlias(name, docAlias) {
		var i, layer, docLayers, layerAlias;
		if (!docAlias) docAlias = "document";
		docLayers = eval(docAlias + ".layers");
		for (i=0; i<docLayers.length; i++) {
			layerAlias = docAlias + ".layers." + docLayers[i].name;
			layer = eval(layerAlias);
			if(layer.name == name) return layerAlias;
			if(layer.document.layers.length > 0) {
				layerAlias = getLayerAlias(name, layerAlias + ".document");
				if(layerAlias!=null) return layerAlias;
			}
		}
		return null;
	}

//	if (arguments[1]) this.moveBy(xShift,yShift);
	if (DEBUG) {
		var str = this.statistics();
		alert( 'New Layer instantiated: ' + id + '\n' + str );
	}
}

elcLayer.prototype.moveTo = function(x,y) {
	this.x=x;
	this.y=y;
	this.css.left=this.x;
	this.css.top=this.y;
}

elcLayer.prototype.moveBy = function(x,y){
	this.x+=x;
	this.y+=y;
	this.css.left=this.x;
	this.css.top=this.y;
}

elcLayer.prototype.setZIndex = function(z){
	this.z=z;
	this.css.zIndex=z;
}

elcLayer.prototype.clipValues = function(which){
	if(bw.ns4){
		if(which=="t") return this.css.clip.top;
		if(which=="r") return this.css.clip.right;
		if(which=="b") return this.css.clip.bottom;
		if(which=="l") return this.css.clip.left;
	} else {
		var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px");
		if(which=="t") return Number(clipv[0]);
		if(which=="r") return Number(clipv[1]);
		if(which=="b") return Number(clipv[2]);
		if(which=="l") return Number(clipv[3]);
	}	
	
}

elcLayer.prototype.clipTo = function(t,r,b,l){
	if (bw.ns4){
		this.css.clip.top=t;
		this.css.clip.right=r;
		this.css.clip.bottom=b;
		this.css.clip.left=l;
	} else {
		this.css.clip="rect("+t+","+r+","+b+","+l+")";
	}
}

elcLayer.prototype.clipBy = function(t,r,b,l){
	this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l);
}

elcLayer.prototype.hide = function(){
	this.css.visibility=(bw.ns4)?'hide':'hidden';
}

elcLayer.prototype.show = function(){
	this.css.visibility=(bw.ns4)?'show':'visible';
}

elcLayer.prototype.vis = function(){
	if(this.css.visibility=="hidden" || this.css.visibility=="hide") return true;
}

elcLayer.prototype.writeThis = function(txt){
	if (bw.ns4){
		this.obj.document.write(txt);
		this.obj.document.close();
	} else {
		this.obj.innerHTML = txt;
	}
}

elcLayer.prototype.replaceImage = function(imgName,imgSrc) {
	var wsmlImgSrc = wsmlMakeResourceUrl( imgSrc );
	if (bw.ns4) {
		this.obj.document.images[imgName].src = wsmlImgSrc;
	} else {
		document[imgName].src = wsmlImgSrc;
	}
}

elcLayer.prototype.statistics = function() {
	var vitals = 'Layer stats\n' + 'x : ' + this.x + '\n' + 'y : ' + this.y + '\n' + 'w : ' + this.w + '\n' + 'h : ' + this.h + '\n' + 'z : ' + this.z + '\n';
	return vitals;
}

function findAbsoluteOffset(node, leftTopOrRight) {
	var offset = 0;
	var currObject = node;
	
	while (currObject) {
		if (leftTopOrRight == cFindOffsetFromLeft || leftTopOrRight == cFindOffsetFromRight)
			offset += currObject.offsetLeft;
		else if (leftTopOrRight == cFindOffsetFromTop)
			offset += currObject.offsetTop;
		currObject = currObject.offsetParent;
	}

	if (leftTopOrRight == cFindOffsetFromRight)
		return offset + node.offsetWidth;
	else
		return offset;
}

var cFindOffsetFromLeft = 1;
var cFindOffsetFromTop = 2;
var cFindOffsetFromRight = 3;
