/*
author: patrik_bergman@yahoo.com
creates a cross-browser object with a wide range of methods and properties
*/


function LayerDynObj(id, left, top, width, height, visibility, zIndex, clip, bgColor, background, htmlContent, contentResize) {
	this.id		= id;	
	this.obj	= id.substr(0,id.length-3);
	
	this.zIndex 	= this.initzIndex = (zIndex != null) ? zIndex : 0;		
	this.visible 	= (visibility) ? true : false;
	
	this.x = this.initx = left;
	this.y = this.inity = top;
	
	//actual width/height of layer-object
	this.w = this.initW = width;
	this.h = this.initH = height;
	//inner width/height
	this.contentW;
	this.contentH;
	
	this.clipRect 	= new Rect(0, 0, 0, 0);			
	var arrClip = (clip != null) ? clip.match(/\d+/g) : null;		
	this.clipRect.top 		= this.clipRect.initTop		= (arrClip == null) ? 0 : parseInt(arrClip[0]);	
	this.clipRect.right  	= this.clipRect.initRight	= (arrClip == null) ? width : parseInt(arrClip[1]);	
	this.clipRect.bottom 	= this.clipRect.initBottom	= (arrClip == null) ? height : parseInt(arrClip[2]);	
	this.clipRect.left   	= this.clipRect.initLeft	= (arrClip == null) ? 0 : parseInt(arrClip[3]);
	
	this.bgColor = (bgColor != null) ? bgColor : null;
	this.background = (background != null) ? background : null;
   	    
	//dynamically create <div>- or <layer>-tag 					
	if (nav.ns4) {
		this.css = this.elm = this.event = document.layers[this.id] = new Layer(width);	
		this.doc = this.css.document;
		
		this.css.zIndex 	= this.zIndex;
		this.css.visibility = (this.visible) ? "show" : "hide";
		
		this.css.left 	= this.x;
		this.css.top 	= this.y;
					
		if (this.bgColor != null) this.css.bgColor = this.bgColor;
		if (this.background != null) this.css.background.src = this.background;
        
		this.css.clip.top 		= this.clipRect.top;
		this.css.clip.right 	= this.clipRect.right;
		this.css.clip.bottom 	= this.clipRect.bottom;	
		this.css.clip.left 		= this.clipRect.left;
		
		this.innerHtml = "";
		if (htmlContent != null){
			this.css.document.open();
			this.css.document.write(htmlContent);
			this.css.document.close();
			this.innerHtml = htmlContent;
		}
				
		//content height/width
		this.contentW = this.css.document.width;
 		this.contentH = this.css.document.height;	
	}  
	else if (nav.ns6) {
		var elm = document.createElement("DIV");
		elm.style.position = "absolute";
		elm.id = this.id;
		
		this.elm = this.event = elm;
		this.css = this.elm.style;
		
		document.body.appendChild(this.elm);
		
		this.css.left = this.x;
		this.css.top = this.y;
		this.css.width = this.w;
		this.css.height = this.h;
		this.css.backgroundColor = (this.bgColor == null) ? "" : this.bgColor;
		this.css.backgroundImage = (this.background == null) ? "" : "url('" + this.background + "')";
		this.css.clip = "rect(" + this.clipRect.top + "px " + this.clipRect.right + "px " + this.clipRect.bottom + "px " + this.clipRect.left + "px)";
		this.css.visibility = (this.visible) ? "visible" : "hidden";
		this.css.zIndex = this.zIndex;
		this.elm.innerHTML = htmlContent;
		
		//i don't get it, this code gets/sets the content width/height
		tw = this.elm.style.width
       	this.elm.style.width = "auto";
      	var w = this.elm.offsetWidth;
      	this.elm.style.width = tw;
		var th = this.elm.style.height
		this.elm.style.height = "auto";
       	var h = this.elm.offsetHeight;
       	this.elm.style.height = th;
		this.contentW = w;
		this.contentH = h;	
	}
	else if (nav.ie) {
		var str = '' + 
		'<DIV id="' + this.id + '" style="position:absolute; left:' + this.x + '; top:' + this.y +
        '; width:' + this.w + '; height:' + this.h + 
        '; visibility:' + (this.visible == true  ? "visible" : "hidden") + 
		(this.bgColor == null ? "" : ("; background-color:" + this.bgColor )) +
		(this.background == null ? "" : ("; background-image:url('" + this.background + "')")) +
		'; clip:rect('+this.clipRect.top+'px '+this.clipRect.right+'px '+this.clipRect.bottom+'px '+this.clipRect.left+'px);';
        
        str += ' z-index:' + this.zIndex + ';';
        str += ' overflow:hidden;">\n';
		if (htmlContent != null)
			this.innerHtml = htmlContent;
			str += htmlContent;
		str += '\n</DIV>\n';
		
      	document.body.insertAdjacentHTML("BeforeEnd", str);
			 
		this.elm = this.event  = document.all[id];
		this.css = document.all[id].style;
		this.doc = document;
		
		//actual width/height of div-object
		this.w = (nav.ie4)? this.css.pixelWidth : this.elm.offsetWidth;
		this.h = (nav.ie4)? this.css.pixelHeight : this.elm.offsetHeight;
	
		//width/height of div-content
		this.contentW = this.elm.scrollWidth;
 		this.contentH = this.elm.scrollHeight;		
	}
	
	this.elm.parent = this;
	
	//resize object to the size of content   
	if (contentResize) {
	//	LayerDynObj_resizeTo(width, height, saveTmpVals, changeClip)
		this.resizeTo(this.contentW, this.contentH, false, true); 
	}	
}

function LayerDynObj_show() {
	this.css.visibility = (nav.ns4)? "show" : "visible";
  this.visible = true;
}

function LayerDynObj_hide() {
	this.css.visibility = (nav.ns4)? "hide" : "hidden";
  this.visible = false;
}

function LayerDynObj_writeHtml(html, contentResize) {
	this.innerHtml = html;
	if (nav.ns4) {	
		this.doc.open();
		this.doc.write(html);
		this.doc.close();
	//	this.css.resizeTo(this.css.document.width, this.css.document.height); ???
		this.contentW = this.css.document.width;  
 		this.contentH = this.css.document.height; 
	}
	else if (nav.ie || nav.ns6) {
		this.event.innerHTML = html;
		if (nav.mac) {
			this.elm.offsetWidth;
			this.elm.offsetHeight;
		}	
		this.contentW = this.elm.scrollWidth;
		this.contentH = this.elm.scrollHeight; 	
	}
  if (contentResize) this.resizeTo(this.contentW, this.contentH, false, true);
}
 
function LayerDynObj_deleteObject(){
    this.hide();
	this.writeHtml("");
	if (is.ns4)
		delete this.css;
}

function LayerDynObj_moveTo(x,y,changeInitVals) {	
//	if (changeInitVals == null || changeInitVals == true) {
	if (changeInitVals == true) {
		this.x = this.initx = x; //need to change initvals when using the scroll
		this.y = this.inity = y;
	} else {	
		this.x = x;
		this.y = y;
	}
		
	if (nav.ns4 || nav.ns6) { 
		this.css.left = this.x;
		this.css.top = this.y;
	}	
	else if (nav.ie) { 
		this.css.pixelLeft = this.x;
		this.css.pixelTop = this.y;
	} 
}

function LayerDynObj_moveBy(x,y) {	
	this.x += (x == null) ? 0 : x;
	this.y += (y == null) ? 0 : y;
	if (nav.ns4 || nav.ns6) { 
		this.css.left = this.x;
		this.css.top = this.y;
	}	
	else if (nav.ie) { 
		this.css.pixelLeft = this.x;
		this.css.pixelTop = this.y;
	}
}

function LayerDynObj_changeClip(top, right, bottom, left) {
	this.clipRect.top 	 = parseInt(top);
	this.clipRect.right  = parseInt(right);
	this.clipRect.bottom = parseInt(bottom);
	this.clipRect.left   = parseInt(left);	
	if (nav.ns4) { 
		this.css.clip.top 		= this.clipRect.top; 
		this.css.clip.right 	= this.clipRect.right; 
		this.css.clip.bottom 	= this.clipRect.bottom; 
		this.css.clip.left 		= this.clipRect.left;
	} else { 
		this.css.clip = "rect("+ this.clipRect.top +"px "+ this.clipRect.right +"px "+ this.clipRect.bottom +"px "+ this.clipRect.left +"px)";
	}
}

function LayerDynObj_resizeTo(width, height, saveTmpVals, changeClip) {
	if (saveTmpVals) {				//used to keep track of previous width/height
		this.tmpWidth 	= this.w;
		this.tmpHeight	= this.h;
	}
	this.w = width;
	this.h = height;
			
	if (nav.ie) { 	
//	this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
//	this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
		this.css.pixelWidth = width; 
		this.css.pixelHeight = height;
		if (changeClip != false) this.changeClip(0,width,height,0);
  } else if (nav.ns4 || nav.ns6) {
		this.css.height = this.h = height;
		this.css.width = this.w = width;
		if (changeClip != false) this.changeClip(0,width,height,0);
	}
}

function LayerDynObj_resizeBy(dwidth, dheight, saveTmpVals, changeClip) {
    this.resizeTo(this.w + dwidth, this.h + dheight, saveTmpVals, changeClip);
}

function LayerDynObj_changeZIndex(z) {
	this.zIndex = z;
	this.css.zIndex = this.zIndex;
}

function LayerDynObj_setBgColor(color) {	
	this.css.bgcolor = color;
	if (nav.ns4) { 
		this.css.bgColor = this.css.bgcolor;
	} else if (nav.ie || nav.ns6) { 
		this.css.backgroundColor = this.css.bgcolor;
	} 
}

function LayerDynObj_setBackground(background) {	
	this.background = background;
	if (nav.ns4) { 
		this.css.background.src = this.background;
	} else if (nav.ie || nav.ns6) { 
		this.css.backgroundImage = "url('" + this.background + "')";
	} 
}

LayerDynObj.prototype.moveTo 		= LayerDynObj_moveTo;
LayerDynObj.prototype.moveBy 		= LayerDynObj_moveBy;
LayerDynObj.prototype.writeHtml 	= LayerDynObj_writeHtml;
LayerDynObj.prototype.deleteObject 	= LayerDynObj_deleteObject;
LayerDynObj.prototype.show 			= LayerDynObj_show;
LayerDynObj.prototype.hide 			= LayerDynObj_hide;
LayerDynObj.prototype.changeClip 	= LayerDynObj_changeClip;
LayerDynObj.prototype.resizeTo 		= LayerDynObj_resizeTo;
LayerDynObj.prototype.resizeBy 		= LayerDynObj_resizeBy;
LayerDynObj.prototype.changeZIndex 	= LayerDynObj_changeZIndex;
LayerDynObj.prototype.setBgColor 	= LayerDynObj_setBgColor;
LayerDynObj.prototype.setBackground	= LayerDynObj_setBackground;

