//*************************************************************************************/
// File:  dhtml.js
// Purpose: Dynamic HTML toolkit.
// Please don't distribute without prior written consent. 
// Programmers:
// - Stacy Haven
// - stacy.haven@dhtmlsolutions.com
// - Copyright (c)2004 Stacy Haven / DHTMLSolutions.com.  All rights reserved.
//*************************************************************************************/
//*************************************************************************************/

//Page Browser Object
function PageBrowserObject(){
	//User Agent
	this.bAgent=navigator.userAgent;
	//Windows
	this.bWin=(this.bAgent.search(/win/i)>-1)?true:false;
	//Mac
	this.bMac=(this.bAgent.search(/mac/i)>-1)?true:false;
	//IE 4
	this.bIE4=(this.bAgent.search(/msie 4\./i)>-1)?true:false;
	//IE 5
	this.bIE5=(this.bAgent.search(/msie 5\./i)>-1)?true:false;
	//IE 6
	this.bIE6=(this.bAgent.search(/msie 6\./i)>-1)?true:false;
	//IE 7
	this.bIE7=(this.bAgent.search(/msie 7\./i)>-1)?true:false;
	//IE 4.0+
	this.bIE4plus=(document.all)?true:false;
	//Netscape 4.0
	this.bNS4=(document.layers)?true:false;
	//Netscape 6.0
	this.bNS6=(!document.layers && (this.bAgent.indexOf('Netscape6')!=-1))?true:false;
	//Netscape 7.0
	this.bNS7=(!document.layers && (this.bAgent.indexOf('Netscape')!=-1) && !this.bNS6)?true:false;
	//OSX Safari
	this.bSaf=(!document.layers && this.bAgent.search(/applewebkit/i)>-1 && !this.bNS6 && !this.bNS7 && this.bAgent.search(/gecko/i)>-1)?true:false;
	//Mozilla1.1
	this.bMOZ=(!document.layers && this.bAgent.search(/netscape/i)==-1 && !this.bNS6 && !this.bNS7 && !this.bSaf && this.bAgent.search(/gecko/i)>-1)?true:false;
	//IE 4.0+ or NS 4.0+ or NS 6+
	this.bVer4=(this.bNS4||this.bNS6||this.bNS7||this.bMOZ||this.bIE4plus)?true:false;
	//Function to show all values in the Page Browser Object.
	this.SeeAll=function(){var bData="";for(a in this)if(this[a])bData+=a+"="+this[a]+"\n";alert(bData);};
}

PBO=new PageBrowserObject();

var LayerobjAddOn = [];

//*************************************************************************************/
//	Layerobj - This will create a layer at the specified location. with
//					the specified content and with the specified attributes.
//		Arguments
//			c = object with the following properties
//				l = left coordinate;
//				t = top coordinate;
//				w = width;
//				h = height;
//			oType = object type (DIV, IFRAME) (NS4 will always be a Layer)
//			oText = object text;
//			aIn = args In;
//		Return
//			Object Handle
//			false if browser isn't supported
//*************************************************************************************/
function Layerobj(c,oType,oText,aIn,parent){

	var oID = aIn.objArgs.id;
	var nL = null;
	if(parent){
		this.parent = parent;
	}else{
		this.parent = document.body;
	}

	nL = document.createElement(oType);	
	nL.style.position = "absolute";
	if(aIn && aIn.objArgs)
		for (a in aIn.objArgs)
			nL.setAttribute(a,aIn.objArgs[a]);
	if(aIn && aIn.styleArgs)
		for (a in aIn.styleArgs)
			nL.style[a]=aIn.styleArgs[a];
	this.parent.appendChild(nL);
	this.oType = oType.toLowerCase();
	if(this.oType.indexOf("iframe") > -1)
		this.window=window.frames[oID];
	this.layer=document.getElementById(oID);
	if(aIn && aIn.eventArgs)
		for (a in aIn.eventArgs)
			this.layer[a] = aIn.eventArgs[a];
	if(aIn && aIn.objHandlers)
		for (a in aIn.objHandlers)
			this[a]=aIn.objHandlers[a];
	this.lID = oID;
	this.pos = {};	
	this.posx = {};
	if (PBO.bIE4plus){
		this.posLeft = "pixelLeft";
		this.posTop = "pixelTop";
		this.posWidth = "pixelWidth";
		this.posHeight = "pixelHeight";
		this.posPX = 0;
	}else if(PBO.bNS6 || PBO.bNS7 || PBO.bMOZ || PBO.bSaf){
		this.posLeft = "left";
		this.posTop = "top";
		this.posWidth = "width";
		this.posHeight = "height";
		this.posPX = "px";
	}
	this.MoveToLoc(c.l,c.t);
	this.ResizeTo(c.w,c.h);
	if(oText && this.oType!="iframe")
		this.WriteTo(oText,2,true);
	if(oType.toLowerCase() != "iframe"){
		this.pos = this.GetObjectData(0);
		this.posx = this.GetObjectData(1);
	}
	for(var x=0;x<LayerobjAddOn.length;x++){
		LayerobjAddOn[x](this);
	}
//	this.CreateLayer = this.CreateLayer;
//	this.createComplete(arguments,this);
}

//*************************************************************************************/
//	CreateLayer - This will create a sub layer at the specified location relative
//					to the base layer with the specified content and with the 
//					specified attributes.
//		Arguments
//			c = object with the following properties
//				l = left coordinate;
//				t = top coordinate;
//				w = width;
//				h = height;
//			oType = object type (DIV, IFRAME) (NS will always be a Layer)
//			oText = object text;
//			aIn = args In;
//		Return
//			Object Handle
//			false if browser isn't supported
//*************************************************************************************/
Layerobj.prototype.CreateLayer = function(c,oType,oText,aIn){
	var layer = new Layerobj(c,oType,oText,aIn,this.layer);
	return layer;
}

//*************************************************************************************/
//	Write to Object - This function will write anything to a DHTML object
//		Arguments
//			textIn = the text or HTML that you want to write to the object.
//			type = 1 is prepend, 2 is append, 3 is replace (default)
//			init = Set the pos property object.
//		Returns
//			true of complete
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.WriteTo = function(textIn,type,init){
	var layer = null;
	if(this.window && this.oType == "iframe"){
		if(!document.getElementById(this.lID).contentWindow.document.body)
			document.getElementById(this.lID).contentWindow.document.write("<body marginheight=0 marginwidth=0 leftmargin=0 topmargin=0></body>");
		layer = document.getElementById(this.lID).contentWindow.document.body;
	}else
		if(PBO.bIE4plus || PBO.bNS6 || PBO.bNS7 || PBO.bMOZ || PBO.bSaf)
			layer = this.layer;
	if(type == 1){
		layer.innerHTML = textIn + layer.innerHTML;
	}else if(type == 2){
		layer.innerHTML += textIn;
	}else if(type == 3 || type == null){
		layer.innerHTML = textIn;
	}
	if(!init){
	this.pos = this.GetObjectData(0);
	this.posx = this.GetObjectData(1);
	}
	this.isBusy = false;
	return true;
}

//*************************************************************************************/
//	Move To - This will move any DHMTL object to the specified location.
//		Arguments
//			l = the left position
//			t = the top position
//		Return
//			true of complete
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.MoveToLoc = function(l,t){
	if(l!=null){
		document.getElementById(this.lID).style[this.posLeft]=l+this.posPX;
	};
	if(t!=null){
		document.getElementById(this.lID).style[this.posTop]=t+this.posPX;
	}
	this.posx.l-=this.pos.l-l;
	this.posx.t-=this.pos.t-t;
	this.pos.l=l;
	this.pos.t=t;
	return true;
}

//*************************************************************************************/
//	Show It - Either shows the layer or not.
//		Arguments
//			oIn - object that you want to change state of.
//			on - boolean of whether to show or not to show the layer.
//		Returns
//			None
//*************************************************************************************/
Layerobj.prototype.ShowIt = function(on){
    document.getElementById(this.lID).style["visibility"]=(on)?'visible':'hidden';
}

//*************************************************************************************/
//	Move By - This will move any DHMTL object by the amount specified.
//		Arguments
//			l = the amount to move to the left
//			t = the amount to move to the top
//		Return
//			true of complete
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.MoveBy = function(l,t){
	document.getElementById(this.lID).style[this.posLeft]=(l+this.pos.l)+this.posPX;
	document.getElementById(this.lID).style[this.posTop]=(t+this.pos.t)+this.posPX;
	this.pos.l+=l;
	this.posx.l+=l;
	this.pos.t+=t;
	this.posx.t+=t;
	return true;
}

//*************************************************************************************/
//	Resize Object To - This will alter the size of any DHTML object.
//		Arguments
//			h = the new height of the object
//			w = the new width of the object
//		Return
//			true of complete
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.ResizeTo = function(w,h){
	(w!=null)?document.getElementById(this.lID).style[this.posWidth]=w+this.posPX:"";
	(h!=null)?document.getElementById(this.lID).style[this.posHeight]=h+this.posPX:"";
	this.pos.w=w;
	this.pos.h=h;
	return true;
}

//*************************************************************************************/
//	Set Clip - This will set the clip rect on the DHTML object.
//		Arguments
//			t = top of the rect
//			r = right side of the rect
//			b = bottom of the rect
//			l = left of the rect
//		Return
//			true of complete
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.SetClip = function(t,r,b,l){
	document.getElementById(this.lID).style.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
	this.posx.w=r-l;
	this.posx.h=b-t;
	return true;
}

//*************************************************************************************/
//	Resize Object By - This will alter the size of any DHTML object by the amount specified.
//		Arguments
//			h = addition to the height of the object
//			w =addition to the new width of the object
//		Return
//			true of complete
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.ResizeBy = function(w,h){
	var obj = document.getElementById(this.lID).style;
	(w!=0)?obj[this.posWidth]=(parseInt(obj[this.posWidth])+w)+this.posPX:"";
	(h!=0)?obj[this.posHeight]=(parseInt(obj[this.posHeight])+h)+this.posPX:"";
	this.pos.w+=w;
	this.pos.h+=h;
	return true;
}


//*************************************************************************************/
//	Get Object Data - This will return the information about an object.
//				(t for top, l for left, w for width, h for height)
//		Arguments
//		Return
//			Object with these properties
//				t = top coordinate
//				l = left coordinate
//				w = width of object
//				h = height of object
//		    else
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.GetObjectData = function(abs,oIn){
	var oOut={t:0,l:0,w:0,h:0};
	if(oIn)
		var el=oIn;
	else
		var el=this.layer;
	if(abs && this.layer){
		gotil = null;
	}else{
		gotil = this.parent;	
	}
	if(PBO.bIE4plus || PBO.bNS7 || PBO.bNS6 || PBO.bMOZ || PBO.bSaf){
		oOut.l=el.offsetLeft;
		oOut.t=el.offsetTop;
		var newp=el.offsetParent;
		while(newp != null && newp != gotil){
			oOut.l+=newp.offsetLeft;
			oOut.t+=newp.offsetTop;
			newp=newp.offsetParent;}
		if(PBO.bMac && PBO.bIE4plus){
			oOut.t+=parseInt(document.body.topMargin);
			oOut.l+=parseInt(document.body.leftMargin);
		}
		if(!abs){
			oOut.h=(el.height)?el.height:el.offsetHeight;
			oOut.w=(el.width)?el.width:el.offsetWidth;
		}
	}
	return oOut;
}

//*************************************************************************************/
//Relatively Position Object - This will position any object relativly to any other object this function will store
//						the positioning info about the reference object in code so that it doesn't have to be reevaluated
//						everytime. 
//
//		Arguments
//		Return
//			Object with these properties
//				refO = Reference object handle
//				hJ = horizontal justification
//				hI = horizontal indent
//				vJ = vertical justification
//				vI = vertical indent
//				resize = This will force the reevaluation of the location of the reference object.
//		    else
//			false if browser not supported
//*************************************************************************************/
Layerobj.prototype.RelPosObject = function(refO,hJ,hI,vJ,vI,resize){
	var addW,addH;
	if ((!refO.oData) || (resize))
		refO.oData=this.GetObjectData(0,refO);
	var oD=this.pos;
	var refOD=refO.oData;
	if (hJ==0)addW=hI;
	if (hJ==1)addW=Math.round((parseInt(refOD.w)-oD.w)/2)+hI;
	if (hJ==2)addW=Math.round((parseInt(refOD.w)-oD.w))+hI;
	if (vJ==0)addH=vI;
	if (vJ==1)addH=Math.round((parseInt(refOD.h)-oD.h)/2)+vI;
	if (vJ==2)addH=Math.round(parseInt(refOD.h)-(oD.h))-vI;
	return this.MoveToLoc(refOD.l+addW,refOD.t+addH);
}

//*************************************************************************************/
//Get Attribute - This returns the value of an attribute of a DHMTL Toolkit instance.
//
//		Arguments
//			attribute = name of the attribute to retrieve
//		Return
//			value of attribute
//*************************************************************************************/
Layerobj.prototype.getAttribute = function(attribute){
	return this[attribute];
}

//*************************************************************************************/
//Set Attribute - This sets an attribute of a DHMTL Toolkit instance.
//
//		Arguments
//			attribute = name of the attribute to retrieve
//			value = the value that you want to set to the attribute
//		Return
//			value of attribute
//*************************************************************************************/
Layerobj.prototype.setAttribute = function(attribute,value){
	return this[attribute]=value;
}

