/*
 * Web 2.0 static functionality
 * 
 * Members Properties:
 *		cursor.x
 *      cursor.y
 *		regex.numeric			ex: w2.regex.numeric.exec('120px') => 120;
 *		regex.query			
 *		xml.doc					
 *
 * Member Functions:
 *		getXmlHttpObject
 *		xml.load(source,callback)
 *		call(uri, params, async, callback)
 *		cookie.create(name,value,days)
 *		cookie.erase(name)
 */

// Legacy support
if (document.all && !document.getElementById)
{
  document.getElementById = function(id)
  {
    return document.all[id];
  }
}
else if (document.layers && !document.getElementById)
{
  document.getElementById = function(id)
  {
    return document.layers[id];
  }
}

// Lazy instantiation of w2 object
if (typeof(window.w2) == 'undefined') { var w2 = null; }
w2 = {
	// Cursor position coordinates
	cursor : {x:0,y:0,clientX:0,clientY:0},

	// Callback for maintaining current mouse position
	getCursorPos : function(e)
	{
		e = e || window.event;
		if (e.pageX || e.pageY)
		{
			w2.cursor.x = e.pageX;
			w2.cursor.y = e.pageY;
			w2.cursor.clientX = e.clientX;
			w2.cursor.clientY = e.clientY;
		} 
		else
		{
			var de = document.documentElement;
			var b = document.body;
			w2.cursor.x = e.clientX + 
				(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
			w2.cursor.y = e.clientY + 
				(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
			w2.cursor.clientX = e.clientX;
			w2.cursor.clientY = e.clientY;
		}
	},

	// Regular expression patterns
	regex : {numeric:/^[0-9]+/,query:/(\?.*)$/},

	// Create an XmlHttpRequest object
	getXmlHttpObject : function()
	{
		var returnObj = false;
		try
		{
			returnObj = new XMLHttpRequest();
		}
		catch (e)
		{
			try
			{
				returnObj = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				returnObj = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}

		return returnObj;
	},

	call : function(uri, params, async, callback)
	{
		var xmlhttp = w2.getXmlHttpObject();
		xmlhttp.open('POST', uri, async);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.setRequestHeader("Referer", location.href);

		// explicit timeout of 5 seconds
		var callTimeout = window.setTimeout(function(){
			xmlhttp.abort();
			// show default
			},5000);

		if (async)
		{
			if (callback != null)
			{
				xmlhttp.onreadystatechange = function() {
					callback(xmlhttp.responseText, callTimeout);
				}
			}
			xmlhttp.send(params);
		}
		else
		{
			xmlhttp.send(params);
			window.clearTimeout(callTimeout);
			if (callback != null) callback(xmlhttp.responseText);
			else document.write(xmlhttp.responseText);
		}
	},

	// Singleton reference to data source 
	xml : {
		doc:false,

		// Read in an external XML data source for parsing 
		load : function(source,callback)
		{
			if (document.implementation && document.implementation.createDocument)
			{
				w2.xml.doc = document.implementation.createDocument("", "", null);			
				w2.xml.doc.onload = function() { callback(); }

				// Safari doesn't support "load"
				if (w2.xml.doc.onload == null)
				{
					var xmlHttpObj = w2.getXmlHttpObject();
					xmlHttpObj.open('GET', source, true);

					xmlHttpObj.onreadystatechange = function()
					{	
						if (xmlHttpObj.readyState == 4)
						{
							var parser = new DOMParser();
							w2.xml.doc = parser.parseFromString(xmlHttpObj.responseText, "text/xml");
							callback();
						}
					}

					xmlHttpObj.send(null);
				}
			}
			else if (window.ActiveXObject)
			{
				w2.xml.doc = new ActiveXObject("Microsoft.XMLDOM");
				w2.xml.doc.onreadystatechange = function() {
					if (w2.xml.doc.readyState == 4)
					{
						callback();
					}
				}
			}

			if (w2.xml.doc.async) {
				w2.xml.doc.load(source);
			}
		}
	},
		
	client : {width:0,height:0},
	
	getClientSize : function()
	{
		if (typeof(window.innerWidth ) == 'number')
		{
			w2.client.width = window.innerWidth;
			w2.client.height = window.innerHeight;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			w2.client.width = document.documentElement.clientWidth;
			w2.client.height = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
			w2.client.width = document.body.clientWidth;
			w2.client.height = document.body.clientHeight;
		}
	},

	cookie : {
		create : function(name,value,days)
		{
			if (days)
			{
				var date = new Date();
				date.setTime(date.getTime() + (days*24*60*60*1000));
				var expires = "; expires=" + date.toGMTString();
			}
			else
			{
				var expires = "";
			}

			document.cookie = name + "=" + value + expires + "; path=/";
		},

		erase : function(name)
		{
			w2.createCookie(name,"",-1);
		}
	},

	getObjectSize : function(oid)
	{
		// TODO
		var size = {width:250,height:300};
		return size;
	},

	popup : {
		timer : {show:false,hide:false},
		delay : {
			show : function(oid, xOffset, yOffset, ms)
			{
				clearTimeout(w2.popup.timer.hide);
				clearTimeout(w2.popup.timer.show);
				w2.popup.timer.show = setTimeout("w2.popup.show('" + oid + "'," + xOffset + "," + yOffset + ")", ms);
			},
			
			hide : function(oid, ms)
			{
				clearTimeout(w2.popup.timer.show);
				clearTimeout(w2.popup.timer.hide);
				w2.popup.timer.hide = setTimeout("w2.popup.hide('" + oid + "')", ms);
			}
		},

		show : function(oid, xOffset, yOffset)
		{
			clearTimeout(w2.popup.timer.hide);
			var obj = document.getElementById(oid);
			if (obj == null) return;	

			// (re)initialize the current client dimensions
			w2.getClientSize();

			// get the width of the oid or use best guess
			var popupWidth = obj.style.width;
			var popupHeight = obj.style.height;	

			if (typeof(popupWidth) == 'undefined' || popupWidth == null)
			{
				var size = w2.getObjectSize(oid); 
				popupWidth = size.width;
				
				if (typeof(popupHeight) == 'undefined' || popupHeight == null)
				{
					popupHeight = size.height;
				}
			}

			// get the numeric value of the width
			popupWidth = w2.regex.numeric.exec(popupWidth)*1;
			popupHeight = w2.regex.numeric.exec(popupHeight)*1;

			// solidify offsets
			var xos = (xOffset != null) ? xOffset : 0 ;
			var yos = (yOffset != null) ? yOffset : 0 ;
			if (xos > 0 && yos == 0 && yos == null) yos = xos;

			// allow percentages of the popup size
			if (xos < 1 && xos > -1) xos = Math.round(xos * popupWidth);
			if (yos < 1 && yos > -1) yos = Math.round(yos * popupWidth);
			
			/*
			 * open the popup within the client window
			 */
			if ((w2.cursor.x + popupWidth + xos) > w2.client.width)
			{
				// open popup to the left 
				obj.style.left = w2.cursor.x - popupWidth - xos + "px";
				obj.className = 'left';

				// assign all children objects the same class
				for (i = 0; i < obj.childNodes.length; i++)
				{
					if (obj.childNodes[i].nodeType == 1)
					{
						obj.childNodes[i].className = 'left';
					}
				}
			}
			else
			{
				// open popup to the right
				obj.style.left = w2.cursor.x + xos +  "px";
				obj.className = 'right';

				for (i = 0; i < obj.childNodes.length; i++)
				{
					if (obj.childNodes[i].nodeType == 1)
					{
						obj.childNodes[i].className = 'right';
					}
				}
			}

			if ((w2.cursor.y - popupHeight - yos) < 0)
			{
				obj.style.top = (w2.cursor.y - popupHeight - yos) - (tsr.curor.clientY - popupHeight - yos) + "px";
			}
			else
			{
				obj.style.top = w2.cursor.y + yos + "px";
			}
	
			obj.style.visibility = 'visible';
		},

		hide : function(oid)
		{
			clearTimeout(w2.popup.timer.show);
			var obj = document.getElementById(oid);
			if (obj == null) return;
			obj.style.visibility = 'hidden';
		}
	}
}

// Attach standard event handlers to document
if (window.attachEvent)
{
	document.attachEvent("onmousemove",w2.getCursorPos);
}
else if (window.addEventListener)
{
	window.addEventListener("mousemove",w2.getCursorPos,false);
}
else if (window.captureEvents)
{
	window.onmousemove = w2.getCursorPos;
	window.captureEvents(Event.MOUSEMOVE);
}
