/*
 *   Drag & Drop
 *   Implementation Help Document from http://www.webreference.com/programming/javascript/mk/column2/
 */ 
window.dragObject = null;
window.ptMouseOffset = new Point(0,0);
window.dropTargets = [];
window.document.onmousemove = function()
{
	window.BROWSERSAFE_EVENT = arguments[0] || window.event;
	
	var mouseX = 0, mouseY = 0;

	if(window.BROWSERSAFE_EVENT.pageX || window.BROWSERSAFE_EVENT.pageY)
	{
		mouseX = window.BROWSERSAFE_EVENT.pageX;
		mouseY = window.BROWSERSAFE_EVENT.pageY;
	}
	else
	{	
		mouseX = window.BROWSERSAFE_EVENT.clientX + window.document.documentElement.scrollLeft - window.document.documentElement.clientLeft + 2;
		mouseY = window.BROWSERSAFE_EVENT.clientY + window.document.documentElement.scrollTop  - window.document.documentElement.clientTop + 2;
		
		window.BROWSERSAFE_EVENT.cancelBubble = true;
	}

	if(window.dragObject)
	{
		window.dragObject.style.left = INT2PXSTR(mouseX - window.ptMouseOffset.x);
		window.dragObject.style.top = INT2PXSTR(mouseY - window.ptMouseOffset.y);
		return false;
	}
}
window.document.onmouseup = function()
{
	if(window.dragObject == null) { return; }
	if(window.dragObject.releaseCapture) { window.dragObject.releaseCapture(); }
	window.dragObject = null;
}


/*
 *   Iframe
 * 
 */
function IFrameDocument(iFrameEl)
{
	if(iFrameEl.contentDocument)
	{
		// For NS6
		return iFrameEl.contentDocument; 
	}
	else if(iFrameEl.contentWindow)
	{
		// For IE5.5 and IE6
		return iFrameEl.contentWindow.document;
	}
	else if(iFrameEl.document)
	{
		// For IE5
		return iFrameEl.document;
	}

	return null;
}

function IFrameBody(iFrameEl)
{
	var doc = null;

	if(iFrameEl.contentDocument)
	{
		// For NS6
		doc = iFrameEl.contentDocument; 
	}
	else if(iFrameEl.contentWindow)
	{
		// For IE5.5 and IE6
		doc = iFrameEl.contentWindow.document;
	}
	else if(iFrameEl.document)
	{
		// For IE5
		doc = iFrameEl.document;
	}
	else
	{
		return null;
	}

	return doc.body;
}


/*
 *   String Functions & Override
 * 
 */
function TextToHTML(sText)
{
	return sText.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">");
}
function EscapeHTML(sHtml)
{
	return sHtml.replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g,"&lt;");
}
function TrimStr(s)
{
	return s.replace(/\s{1,}/g, "");
}
function TrimStrRT(s)
{
	return s.replace(/(\s{2,})/g, " ").replace(/^\s(.*)$/, "$1").replace(/^(.*)\s$/, "$1");
}

/*
 *  MACRO Functions    
 * 
 */
function BROWSERSAFE_ELEMENTSTRING(str)
{
	return (Browser.IE) ? str.toUpperCase() : str.toLowerCase(); 
}

function PXSTR2INT(pixel)
{
	pixel = pixel.replace("px", "");
	return (pixel == null || pixel.length == 0) ? 0 : parseInt(pixel, 10);
}

function INT2PXSTR(pixel)
{
	return (pixel + "px");
}

function NO_CACHE_URL(url)
{
	return (url+"?"+Math.random(1000)*100);
}

/*
 *	 TextPixel Ruller Object &
 *   Geometric Functions
 * 
 */
var pixelRuller =
{
	span: null,
	init: function()
	{
		this.span = $NE(null, BROWSERSAFE_ELEMENTSTRING("span"), null, null);
		$ES(this.span, "visibility", "hidden");
	},
	measureTextPixel: function(c, s)
	{
		var width = 0;
		
		if(this.span != null)
		{
			this.span.className = c;
			this.span.innerHTML = s;
			
			width = this.span.offsetWidth;
		}
		
		return width;
	}
}

function GetElementRect()
{
	var el = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	
	var left   = 0;
	var top    = 0;
	var width  = el.offsetWidth;
	var height = el.offsetHeight;
	
	while(el.offsetParent)
	{
		left += el.offsetLeft;
		top  += el.offsetTop;
		
		el = el.offsetParent;
	}

	left += el.offsetLeft;
	top  += el.offsetTop;
	
	return new Rect(left, top, left+width, top+height);
}

function GetElmentLeftTop()
{
	var el = (typeof arguments[0] == "string") ? $E(arguments[0]) : arguments[0];
	
	var left = 0;
	var top  = 0;

	while(el.offsetParent)
	{
		left += el.offsetLeft;
		top  += el.offsetTop;
		
		el = el.offsetParent;
	}

	left += el.offsetLeft;
	top  += el.offsetTop;

	return new Point(left, top);
}
