/* 
 * Cross Browser Layer Class
 *
 * $Id: layer.js,v 1.8 2000/11/08 11:43:54 hirose31 Exp $
 *
 */

ua = new UserAgent();

/**
 * UserAgent Class
 */
function UserAgent() {
	this.fullVersion  = '';
	this.majorVersion = 0;
	this.agentName = '';
	this.agentInfo = '';
	this.isIE   = false;
	this.isIE4  = false;
	this.isIE5  = false;
	this.isIE55 = false;
	this.isNS   = false;
	this.isNS4  = false;
	this.isNS5  = false;

	var hasDom;

	this.agentName = navigator.appName;
	this.agentInfo = navigator.userAgent;
	hasDom = document.getElementById ? true : false;

	if (this.agentName == 'Microsoft Internet Explorer' && this.agentInfo.indexOf('MSIE') > -1) {
		// IE
		var pos1 = this.agentInfo.indexOf('MSIE ', 0) + 5;
		var pos2 = this.agentInfo.indexOf(';', pos1);

		this.isIE = true;
		this.fullVersion = this.agentInfo.substring(pos1, pos2);
		this.majorVersion = parseInt(this.fullVersion.substring(0, 1));

		if (this.majorVersion == 5) {
			this.isIE5 = true;
			if (this.fullVersion >= 5.5)
				this.isIE55 = true;
		} else if (this.majorVersion == 4) {
			this.isIE4 = true;
		}
	} else if (this.agentName == 'Netscape' && this.agentInfo.indexOf('Mozilla') > -1) {
		// Netscape
		this.isNS = true;

		this.fullVersion = navigator.appVersion.substring(0, navigator.appVersion.indexOf(' ', 0));
		this.majorVersion = parseInt(this.fullVersion.substring(0, 1));

		if (this.majorVersion == 5)
			this.isNS5 = true;
		else if (this.majorVersion == 4)
			this.isNS4 = true;

	} else {
		;
	}

	return this;
}

/**
 * CBLayer Class
 *
 * @param name			layer name (ID attribute).
 * @param x				x-coordinate in pixels.
 * @param y				y-coordinate in pixels.
 * @param z				z index.
 * @param w				width in pixels or null.
 * @param h				height in pixels or null.
 * @param visible		visibility in 'visible' or 'hidden'.
 * @param bgcolor		back ground color or null.
 * @param bgimage		back ground image or null.
 */
function CBLayer() {
	CBLayer.layerCount++;

    this.id = arguments[0] || 'CBLayer' + CBLayer.layerCount;
    this.x = arguments[1] || null;
    this.y = arguments[2] || null;
    this.z = arguments[3] || 0;
    this.w = arguments[4] || null;
    this.h = arguments[5] || null;
    this.visible = arguments[6] != 'hidden' ? true : false;
    this.bgColor = arguments[7] || null;
    this.bgImage = arguments[8] || null;

	this.obj = {};
	this.css = {};
	this.doc = {};

	if (ua.isIE) {
		this.obj = document.all(arguments[0]);
	} else if (ua.isNS) {
		if (ua.isNS4) {
			this.obj = document.layers[arguments[0]];
		} else if (ua.isNS5) {
			this.obj = document.getElementById(arguments[0]);
		}
	} else {
		//alert('CBLayer: browser is not IE nor Netscape.');
		return false;
	}
	this.setObject(this.obj);

	this.moveTo(this.x, this.y);
	this.setZIndex(this.z);

	if (this.w == null) {
		if (ua.isIE) {
			if (this.css.width) {
				this.w = parseInt(this.css.width);
			} else {
				this.w = this.obj.clientWidth;
			}
		} else if (ua.isNS4) {
			this.w = this.obj.document.width;
		} else if (ua.isNS5) {
			this.w = parseInt(this.css.width);
		}
	}
	if (this.h == null) {
		if (ua.isIE) {
			if (this.css.height) {
				this.h = parseInt(this.css.height);
			} else {
				this.h = this.obj.clientHeight;
			}
		} else if (ua.isNS4) {
			this.h = this.obj.document.height;
		} else if (ua.isNS5) {
			this.h = parseInt(this.css.height);
		}
	}
	this.setSize(this.w, this.h);
	this.setBgColor(this.bgColor);
	this.setBgImage(this.bgImage);
	this.setVisible(this.visible);

	return this;
}

CBLayer.layerCount = 0;
CBLayer.prototype.getComponent = function() { return this }
CBLayer.prototype.setObject = function(obj) {
	if (ua.isNS4) {
		this.doc   = obj.document;
		this.css   = obj;
	} else if (ua.isNS5) {
		this.doc = document;
		this.css = obj.style;
	} else if (ua.isIE) {
		this.doc = document;
		this.css = obj.style;
	}
	return true;
}

CBLayer.prototype.moveTo = function(x, y) {
	if (x != null) {
		this.x = parseInt(x);
		if (ua.isNS) {
			this.css.left = this.x;
		} else {
			this.css.pixelLeft = this.x;
		}
	}
	if (y != null) {
		this.y = parseInt(y);
		if (ua.isNS) {
			this.css.top = this.y;
		} else {
			this.css.pixelTop = this.y;
		}
	}
	return true;
}
CBLayer.prototype.moveBy = function(x, y) {
	return this.moveTo(this.x+x, this.y+y);
}
CBLayer.prototype.setX = function(x) {
	return this.moveTo(x,null);
}
CBLayer.prototype.setY = function(y) {
	return this.moveTo(null,y);
}
CBLayer.prototype.getX = function() { return this.x; }
CBLayer.prototype.getY = function() { return this.y; }
CBLayer.prototype.setVisible = function(b) {
	this.visible = b
	this.css.visibility = b ? 'inherit' : (ua.isNS4 ? 'hide' : 'hidden');
	return true;
}
CBLayer.prototype.getVisible = function() { return this.visible; }

CBLayer.prototype.setZIndex = function(z) {
	this.z = z;
	this.css.zIndex = z;
	return true;
}
CBLayer.prototype.getZIndex = function() { return this.z; }

CBLayer.prototype.setSize = function(w, h) {
	if (w == null) {
		w = this.w;
	} else {
		w = this.w = ( ((w||this.w)||-1) < 0 ? 0 : w );
	}
	if (h == null) {
		h = this.h;
	} else {
		h = this.h = ( ((h||this.h)||-1) < 0 ? 0 : h );	
	}
	if (ua.isIE || ua.isNS5) {
		this.css.width = w;
		this.css.height = h;
	}
	this.setClip([0,w,h,0]);
	return true;
}
CBLayer.prototype.setWidth = function(w) {
	return this.setSize(w, this.h);
}
CBLayer.prototype.setHeight = function(h) {
	return this.setSize(this.w, h);
}
CBLayer.prototype.getWidth  = function() { return this.w; }
CBLayer.prototype.getHeight = function() { return this.h; }

CBLayer.prototype.getContentWidth = function() {
	return ua.isNS ? this.doc.width : parseInt(this.obj.scrollWidth);
}
CBLayer.prototype.getContentHeight = function() {
	return ua.isNS ? this.doc.height : parseInt(this.obj.scrollHeight);
}
CBLayer.prototype.setClip = function(clip) {
	var cc = this.getClip();
	var c  = this.css.clip;

	for (var i in clip) {
		if (clip[i] == null) {
			clip[i] = cc[i];
		}
	}
	if (ua.isNS4) {
		c.top    = clip[0];
		c.right  = clip[1];
		c.bottom = clip[2];
		c.left   = clip[3];
	} else if (ua.isIE || ua.isNS5) {
		this.css.clip = "rect("+clip[0]+"px "+clip[1]+"px "+clip[2]+"px "+clip[3]+"px)";
	}
	return true;
}
CBLayer.prototype.getClip = function() {
	var c = this.css.clip;
	if (c) {
		if (ua.isNS4) {
			return [c.top,c.right,c.bottom,c.left];
		} else {
			if (c.indexOf("rect(") > -1) {
				c = c.split("rect(")[1].split(")")[0].split("px");
				for (var i in c) {
					c[i] = parseInt(c[i]);
				}
				return [c[0],c[1],c[2],c[3]];
			} else {
				return [0,this.w,this.h,0];
			}
		}
	} else {
		return [0,0,0,0];
	}
}

CBLayer.prototype.setBgColor = function(color) {
	if (color == null || color == 'null') {
		if (ua.isNS4) {
			color = null;
		} else {
			color = 'transparent';
		}
	}
	this.bgColor = color;
	if (ua.isNS4) {
		this.obj.bgColor = color;
	} else {
		this.css.backgroundColor = color;
	}
	return true;
}
CBLayer.prototype.getBgColor = function() { return this.bgColor }

CBLayer.prototype.setBgImage=function(path) {
	if (path == null || path == 'null') {
		return false;
	}
	this.bgImage = path;
	if (ua.isNS4) {
		this.obj.background.src = path == 'none' ? null : path;
	} else {
		this.css.backgroundImage = 'url('+path+')';
	}
	return true;
}
CBLayer.prototype.getBgImage=function() { return this.bgImage }

CBLayer.prototype.getPageWidth = function() {
	if (ua.isIE) {
		return document.body.clientWidth;
	} else if (ua.isNS) {
		return window.innerWidth;
	} else {
		return -1;
	}
}
CBLayer.prototype.getPageHeight = function() {
	if (ua.isIE) {
		return document.body.clientHeight;
	} else if (ua.isNS) {
		return window.innerHeight;
	} else {
		return -1;
	}
}

CBLayer.prototype.getPageOffsetX = function() {
	if (ua.isIE) {
		return document.body.scrollLeft;
	} else if (ua.isNS) {
		return window.pageXOffset;
	} else {
		return -1;
	}
}
CBLayer.prototype.getPageOffsetY = function() {
	if (ua.isIE) {
		return document.body.scrollTop;
	} else if (ua.isNS) {
		return window.pageYOffset;
	} else {
		return -1;
	}
}

