var ie = document.all;
//-----------------------------------------------------------------
dom = (document.getElementById) ? true : false; 
ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false; 
ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false; 
ns4 = (document.layers && !dom) ? true : false; 
ie4 = (document.all && !dom) ? true : false; 
nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false; 
//-----------------------------------------------------------------
function ObjRef(id) { 
	if (dom) return document.getElementById(id);
	return (ns4) ? document.layers[id] : (ie4) ? document.all[id] : (ns5||ie5) ? document.getElementById(id) : null; 
} 


function GetTarg(Ev, NoParent) {
	if (!Ev) return false;
	if (Ev.SpecialTarget) var Target = Ev.SpecialTarget;
	else var Target = (Ev && Ev.target) ? Ev.target : window.event.srcElement;
	if (TemporaryObjLink)	Target=TemporaryObjLink;
	if (NoParent) return Target;
	return (Target.ParentStop)?Target:GetFirstParent(Target);
}

function GetFirstParent(el) {
	if (!el) return false;
	Parent=el;
	while (Parent.offsetParent) {
		Parent=Parent.offsetParent;
		if (Parent.ParentLink) return Parent.ParentLink;
		if (Parent.ParentStop) return Parent;
	}
	return Parent;
}

var TemporaryObjLink=false;
var DDMove=false;
var DDMoveObj=false;
var DDResize=false;
var DDResizeObj=false;

/////////////////////////////////////////////////////////
/// DYNAMIC DIV


function DynamicObj(ID, ELEM, ELEM_TYPE)
{
	var Obj= ObjRef(ID);
	if(!Obj) {
		if (!ELEM) ELEM="DIV";
		Obj=document.createElement(ELEM);
		Obj.id=ID;
		if (ELEM_TYPE) Obj.type=ELEM_TYPE;
		Obj.style.display="none";
		document.body.appendChild(Obj);
	}
	this.el=Obj;
	this.id=ID;
	this.el.id=ID;
	this.style=this.el.style;
	this.el.ParentStop=true;
	this.el.Parent=this;
	this.MLeftX=-1;
	this.MRightX=-1;
	this.MTopY=-1;
	this.MBottomY=-1;
	this.UseBody=true;
	this.Visible=false;

	this.DockingObjects= new Array();
	this.Dockable = false;

	this.Interactive=false;
	
	this.HideUnder=false;
	this.HideTags= new Array();
	this.HideTags[0]="SELECT";
	this.SkipId= new Array();
	this.SkipId[0]=this.el.id;

	this.OnMoveStart="";
	this.OnMoveEnd="";
	this.OnMove="";
	this.OnResizeStart="";
	this.OnResizeEnd="";
	this.OnResize="";
	this.OnShow="";
	this.OnClose="";

	this.MoveDirR=true;
	this.MoveDirL=true;
	this.MoveDirT=true;
	this.MoveDirB=true;
	this.DoNotMove=false;
}


/////////////////////////////////////////////////////
/// Set properties



DynamicObj.prototype.SetSize = function(W, H) {
	this.style.width=W;
	this.style.height=H;
	if (this.OnResize) this.OnEvent(this.OnResize);
}

DynamicObj.prototype.GetWidth = function () {
	return parseInt(this.style.width);
}
DynamicObj.prototype.SetWidth = function (Width) {
	this.style.width=Width;
}
DynamicObj.prototype.GetHeight = function () {
	return parseInt(this.style.height);
}
DynamicObj.prototype.SetHeight = function (Height) {
	this.style.height=Height;
}

DynamicObj.prototype.SetPosition = function (L, T) {
	this.style.left=L;
	this.style.top=T;
	if (this.HideUnder&&!DDMove) this.HideTagsUnder();
}

DynamicObj.prototype.GetLeft = function () {
	return parseInt(this.style.left);
}

DynamicObj.prototype.GetTop = function () {
	return parseInt(this.style.top);
}


DynamicObj.prototype.SetRelativePosition = function (ObjID, OffsetLeft, OffsetTop, OnCorn) {
	if (ObjID.el) ObjID=ObjID.el;
	if (typeof(ObjID)=="object") var OffsetObj=ObjID;
	else 	var OffsetObj=ObjRef(ObjID);
	if (!OffsetObj) return false;
	var FirstObj=OffsetObj;
	var leftpos=0;
	var toppos=0;
	do {
		OffsetObj = OffsetObj.offsetParent;
		leftpos += OffsetObj.offsetLeft;
		toppos += OffsetObj.offsetTop;
	} while(OffsetObj.tagName != "BODY");

	if (OnCorn==1||OnCorn==3) leftpos+=parseInt(FirstObj.offsetWidth);
	if (OnCorn==2||OnCorn==4) toppos+=parseInt(FirstObj.offsetHeight);

	this.SetPosition(FirstObj.offsetLeft + leftpos + OffsetLeft, FirstObj.offsetTop + toppos + OffsetTop);
}

DynamicObj.prototype.SetStyle = function (Key, Value) {
	this.style[Key]=Value;
}

DynamicObj.prototype.SetValue = function (Key, Value) {
	this[Key]=Value;
}



///////////////////////////////////////////////////////
////// Showing

DynamicObj.prototype.Show = function () {
	this.style.display="";
	this.Visible=true;
	if (this.HideUnder) this.HideTagsUnder();
	if (this.OnShow) this.OnEvent(this.OnShow);
}

DynamicObj.prototype.Close = function () {
	this.style.display="none";
	this.Visible=false;
	if (this.HideUnder) this.ShowTagsUnder();
	if (this.OnClose) this.OnEvent(this.OnClose);
}




////////////////////////////////////////////////////


DynamicObj.prototype.FalseHandler = function () {
	return false;
}

DynamicObj.prototype.OnEvent = function (Code, Ev) {
	if (!Code) return false;
	if (typeof(Code)=="object") {
		for(i in Code) {
			eval(Code[i]);
		}
	}
	if (typeof(Code)=="string") eval(Code);
}

//////////////////////////////////////////////////////////



DynamicObj.prototype.IsObjectUnder = function (oObj, X, Y, ObjMatch) {
	if(!oObj.offsetParent) return false;

	var objLeft		 =	 oObj.offsetLeft;
	var objTop		 =  oObj.offsetTop;
	var objHeight  =  oObj.offsetHeight;
	var objWidth   =  oObj.offsetWidth;
	var objParent	 =	 oObj.offsetParent;

	while (objParent.tagName.toUpperCase() != "BODY") {
		objLeft		+= objParent.offsetLeft;
		objTop		+= objParent.offsetTop;
		objParent = objParent.offsetParent;
	}

	if (!ObjMatch) {
		if (X >= objLeft && X <= objLeft + objWidth &&
			Y >= objTop && Y <= objTop + objHeight) {
			return true;
		}
	}
	if (ObjMatch) {
		if ((ObjMatch.offsetLeft + ObjMatch.offsetWidth) <= objLeft);
		else if ((ObjMatch.offsetTop + ObjMatch.offsetHeight) <= objTop);
		else if (ObjMatch.offsetTop >= (objTop + objHeight));
		else if (ObjMatch.offsetLeft >= (objLeft + objWidth));
		else {
		  return true;
		}
	}

	return false;
}

DynamicObj.prototype.HideTagsUnder = function (OnMove) {
	if (!ie) return;
	for (var i = 0; i< this.HideTags.length; i++) {
		Tag=this.HideTags[i];
		Tag=Tag.toUpperCase();
		TagsC:
		for (j = 0; j < document.all.tags(Tag).length; j++) {
			obj = document.all.tags(Tag)[j];
			for (z=0; z < this.SkipId.length; z++) {
				if (obj.id==this.SkipId[z]) continue TagsC;
			}
			if (this.IsObjectUnder(obj, false, false, this.el)) obj.style.visibility="hidden";
			else if (OnMove) obj.style.visibility="";
		}
	}
}

DynamicObj.prototype.ShowTagsUnder = function () {
	if (!ie) return;
	for (var i = 0; i< this.HideTags.length; i++) {
		Tag=this.HideTags[i];
		Tag=Tag.toUpperCase();
		TagsC:
		for (j = 0; j < document.all.tags(Tag).length; j++) {
			obj = document.all.tags(Tag)[j];
			for (z=0; z < this.SkipId.length; z++) {
				if (obj.id==this.SkipId[z]) continue TagsC;
			}
			obj.style.visibility="";
		}
	}
}

