
mWidgetLibrary = function(){}
mWidgetLibrary.prototype.version = 1;

mWidgetLibrary.prototype.hitch = function(object, method)
{
	return function ()
	{
		if ("function" == typeof method)
		{
			return method.apply(object, arguments);
		} else {
			return obj[method].apply(object, arguments);
		}
	}
}

mWidgetLibrary.prototype.addClass = function(el, className)
{
	if (this.hasClass(el, className))
	{
		return true;
	} else {
		el.className += " " + className;
		return true;
	}
	return false;
}

mWidgetLibrary.prototype.hasClass = function(el, className)
{
	var curClassString = el.className;
	var classes = curClassString.split(" ");
	for (var i = 0; i < classes.length; i++)
   {
   	if (className == classes[i])
   	{
   		return true;
   	}
   }
	return false;
}

mWidgetLibrary.prototype.removeClass = function(el, className)
{
	var curClassString = el.className;
	var classes = curClassString.split(" ");
	for (var i = 0; i < classes.length; i++)
	{
   	if (className == classes[i])
   	{
   		classes.splice(i, 1);
			el.className = classes.join(" ");
			return true;
   	}
	}
	return false;
}

// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/
// modified by Marty Mapes, 2008
// new usage: addEvent (button, "click", hitch(context, doMe), data)...
//            doMe (event, element, data) { ... }
mWidgetLibrary.prototype.addEvent = function(element, type, handler, data) 
{
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = this.eventGuid++;
		handler.$$data = data;

		// create a hash table of event types for the element
		if (!element.events) element.events = {};

		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = this.hitch(this, this.handleEvent);
};
// a counter used to create unique IDs
mWidgetLibrary.prototype.eventGuid = 1;

mWidgetLibrary.prototype.removeEvent = function (element, type, handler) 
{
	// delete the event handler from the hash table
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$data];
		delete element.events[type][handler.$$guid];
	}
};

mWidgetLibrary.prototype.handleEvent = function(event) 
{
	var returnValue = true;

	// grab the event object (IE uses a global event object)
	var e = event || this.fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);

	// get the element (from quirksmode)
	var el;
	if (e.target) { el = e.target; }
	else if (e.srcElement) { el = e.srcElement; }
	if (el.nodeType == 3) // defeat Safari bug
	{ el = el.parentNode; }
		
	// get a reference to the hash table of event handlers
	var handlers = el.events[e.type];

	// execute each event handler
	for (var i in handlers) {
		el.$$handleEvent = handlers[i];
		var data = handlers[i].$$data;
		if (el.$$handleEvent(e, el, data) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

mWidgetLibrary.prototype.fixEvent = function(event) 
{
	// add W3C standard event methods
	event.preventDefault = _preventDefault;
	event.stopPropagation = _stopPropagation;
	return event;
};

_preventDefault = function() {
	this.returnValue = false;
};

_stopPropagation = function() {
	this.cancelBubble = true;
};

/* usage:

sendRequest("yourscript.php", hitch(this, this.test), {x:1,y:"zero"});
function handleAjaxResults (req)
{
	alert (req.responseText);
}

 */

mWidgetLibrary.prototype.sendRequest = function(url,callback,postData) 
{
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	if (typeof postData == "object")
	{
		var rNameValuePairs = [];
		for (var i in postData)
		{
			rNameValuePairs.push(i + "=" + postData[i]);
		}
		postData = rNameValuePairs.join("&");
	}
	req.send(postData);
}

mWidgetLibrary.prototype.XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

mWidgetLibrary.prototype.createXMLHTTPObject = function() 
{
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

// by convention, every page that uses this library should begin with
// var MWL = new mWidgetLibrary(); 
