/*============================================================================||
||       __  ___  _______    ___  ____  __         ___                        ||
||      /  \/  /_/__  __/_ _/ __\/_/  \/ /   _____/   \                       ||
||     / /\_/ / ___/ / __ `/ /  __/ __/  \  / ___/  `_/                       ||
||    / /  / / __// / /_/ / /__/ / / / /\ \/ __// /\ \                        ||
||   /_/  /_/____/_/\__,_/\___/_/_/ /_/ /_/____/_/ /_/                        ||
||                                                                            ||
||============================================================================||
|| XElement																	  ||
|| Copyright Tim Jones. All Rights Reserved.                                  ||
||============================================================================*/

var XElement = {};

//=============================================================================
XElement.Element = function(SrcElement) {
	var self			= this;

	// public
		self.SrcElement	= SrcElement;		//The actual element on the page

	// private
		var Visible		= true;

	//-------------------------------------------------------------------------
	// Gets the X Coordinate of the SrcElement
	//
	// Return:
	//		Integer - X Coordinate of SrcElement
	//-------------------------------------------------------------------------
	self.GetX = function() {
		var X = self.SrcElement.style.left.replace("px", "");
			X = X.replace("%", "");
	
		return parseInt(X);
	}

	//-------------------------------------------------------------------------
	// Gets the Y Coordinate of the SrcElement
	//
	// Return:
	//		Integer - Y Coordinate of SrcElement
	//-------------------------------------------------------------------------
	self.GetY = function() {
		var Y = self.SrcElement.style.top.replace("px", "");
			Y = Y.replace("%", "");

		return parseInt(Y);
	}

	//-------------------------------------------------------------------------
	// Shows the current element
	//-------------------------------------------------------------------------
	self.Show = function() {
		if(self.SrcElement) {
			self.SrcElement.style.display = "block";
		}

		Visible = true;
	}

	//-------------------------------------------------------------------------
	// Shows the current element
	//-------------------------------------------------------------------------
	self.ShowAt = function(X, Y) {
		if(self.SrcElement) {
			self.SrcElement.style.display = "block";
		}

		self.SrcElement.style.left  = X + "px";
		self.SrcElement.style.right = Y + "px";

		self.SrcElement.style.position = "absolute";

		Visible = true;
	}

	//-------------------------------------------------------------------------
	// Hides the current element
	//-------------------------------------------------------------------------
	self.Hide = function() {
		if(self.SrcElement) {
			self.SrcElement.style.display = "none";
		}

		Visible = false;
	}

	//-------------------------------------------------------------------------
	// Returns visibility of element based upon style.display
	//
	// Return:
	//		bool - Visibility of element
	//-------------------------------------------------------------------------
	self.IsVisible = function() {
		return Visible;
	}

	//=========================================================================
	self.OnCleanup = function() {
	}
}

//=============================================================================
XElement.AddElement = function(ID, Content) {
	var NewElement		= document.createElement('div');

	NewElement.setAttribute("id", ID);
	NewElement.innerHTML = Content;

	document.body.appendChild(NewElement);

	return NewElement;
}

//-----------------------------------------------------------------------------
XElement.GetByID = function(ID) {
	return document.getElementById(ID);
}

//-----------------------------------------------------------------------------
XElement.AddChild = function(ID, Parent, Content) {
	var NewElement		= document.createElement('div');

	NewElement.setAttribute("id", ID);
	NewElement.innerHTML = Content;

	Parent.appendChild(NewElement);

	return NewElement;
}

//-----------------------------------------------------------------------------
XElement.RemoveChild = function(Element, Parent) {
	XElement.Purge(Element);

	Parent.removeChild(Element);

	Element = null;
}

//-----------------------------------------------------------------------------
XElement.RemoveAllChildren = function(Element) {
	XElement.Purge(Element);

	while(Element.hasChildNodes()) {
		XElement.Purge(Element.firstChild);
		
		var T = Element.firstChild;

		Element.removeChild(Element.firstChild);

		T = null;
	}
}

//-----------------------------------------------------------------------------
// Function by: Douglas Crockford
//-----------------------------------------------------------------------------
// http://www.crockford.com/javascript/memory/leak.html
//-----------------------------------------------------------------------------
XElement.Purge = function(d) {
	if(d == null) return;

    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            XElement.Purge(d.childNodes[i]);
        }
    }
}

//-----------------------------------------------------------------------------
XElement.SetDimension = function(NewValue, Value) {
	var Post = "px";

	if(NewValue[Value.length - 1] == "%" || NewValue[Value.length - 1] == "x") {
		return NewValue;
	}

	if(Value) {
		if(Value[Value.length - 1] == "%") {
			Post = "%";
		}
	}

	return NewValue + Post;
}

//-----------------------------------------------------------------------------
XElement.SetX = function(Element, X) {
	Element.style.left = XElement.SetDimension(X, Element.style.left);
}

//-----------------------------------------------------------------------------
XElement.SetY = function(Element, Y) {
	Element.style.top = XElement.SetDimension(Y, Element.style.top);
}

//-----------------------------------------------------------------------------
XElement.GetX = function(Element) {
	if(Element.style && Element.style.left) {
		var X = Element.style.left.replace("px", "");
			X = X.replace("%", "");

		return parseInt(X);
	}else
	if(Element.offsetLeft) {
		return Element.offsetLeft;
	}

	return 0;
}

//-----------------------------------------------------------------------------
XElement.GetY = function(Element) {
	if(Element.style && Element.style.top) {
		var Y = Element.style.top.replace("px", "");
			Y = Y.replace("%", "");

		return parseInt(Y);
	}else
	if(Element.offsetTop) {
		return Element.offsetTop;
	}

	return 0;
}

//-----------------------------------------------------------------------------
XElement.GetPosX = function(Element) {
	var X = 0;

	if(Element.offsetParent) {
		while(Element.offsetParent) {
			X += Element.offsetLeft;
			Element = Element.offsetParent;
		}
	}else if(Element.x) {
		X += Element.x;
	}else if(Element.offsetLeft) {
		X += Element.offsetLeft;
	}

	return X;
}

//-----------------------------------------------------------------------------
XElement.GetPosY = function(Element) {
	var Y = 0;

	if(Element.offsetParent) {
		while(Element.offsetParent) {
			Y += Element.offsetTop;
			Element = Element.offsetParent;
		}
	}else if(Element.y) {
		Y += Element.y;
	}else if(Element.offsetTop) {
		Y += Element.offsetTop;
	}

	return Y;
}

//-----------------------------------------------------------------------------
XElement.SetWidth = function(Element, Width) {
	Element.style.width = XElement.SetDimension(Width, Element.style.width);
}

//-----------------------------------------------------------------------------
XElement.SetHeight = function(Element, Height) {
	Element.style.height = XElement.SetDimension(Height, Element.style.height);
}

//-----------------------------------------------------------------------------
XElement.GetWidth = function(Element) {
	if(Element.style && Element.style.width) {
		var Width = Element.style.width.replace("px", "");
			Width = Width.replace("%", "");

		return parseInt(Width);
	}else
	if(Element.offsetWidth) {
		return Element.offsetWidth;
	}else
	if(Element.innerWidth) {
		return Element.innerWidth;
	}

	return 0;
}

//-----------------------------------------------------------------------------
XElement.GetHeight = function(Element) {
	if(Element.style && Element.style.height) {
		var Height = Element.style.height.replace("px", "");
			Height = Height.replace("%", "");

		if(Height > 0) {	
			return parseInt(Height);
		}
	}

	if(Element.offsetHeight) {
		return Element.offsetHeight;
	}else
	if(Element.innerHeight) {
		return Element.innerHeight;
	}

	return 0;
}

//=============================================================================
