/**
  * hXUL._aux
  * wrapper for external dependencies (frameworks).
  *
  * This is the minimal auxiliary adapter. It contains self-sufficient
  * implementations of all dependencies.
  *
  */

if (typeof(hXUL._aux) == "undefined") {
	hXUL._aux = {};
}

/*
 * bind(func, self) : function 
 * from http://blog.ianbicking.org/prototype-and-object-prototype.html
 */

hXUL._aux.bind = function(func, self) {
	if(typeof (func)=="string"){
		func=self[func];
	}
	var im_func = null;
    if (typeof(func.im_func) == 'function') {
        im_func = func.im_func;
    } else {
        im_func = func;
    }
    func = function () {
        return func.im_func.apply(func.im_self, arguments);
    }
    func.im_func = im_func;
    func.im_self = self;
	return func;

};

// addEvent adapated from http://ejohn.org/projects/flexible-javascript-events/
// and  Andy Smith's (http://weblogs.asp.net/asmith/archive/2003/10/06/30744.aspx)
hXUL._aux.addEvent = function(obj, type, fn) {
	if(!obj) { return; }	
	if (obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else if(obj.addEventListener) {		
		obj.addEventListener( type,fn, false );
	} else {
		var originalHandler = obj["on" + type]; 
		if (originalHandler) { 
		  obj["on" + type] = function(e){originalHandler(e);fn(e);}; 
		} else { 
		  obj["on" + type] = fn; 
		} 
	}
}

hXUL._aux.getEventSource = function(e) {
	if(!e) e = window.event;
	if(e.target)
		var n = e.target;
	else
		var n = e.srcElement;
	if(!n) return null;
	if(n.nodeType == 3) n = n.parentNode; // safari weirdness		
	return n;
}

// Cancels the default execution of an event.
hXUL._aux.preventEvent = function(e) {
	if (!e) e = window.event;
	if (e.preventDefault) e.preventDefault();
	else e.returnValue = false;
	return false;
}

// Cancels the propagation of the event
hXUL._aux.stopPropagation = function(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

hXUL._aux.hasClass = function(element, className) {
	if(element && element.className) {
		if((' ' + element.className + ' ').indexOf(' ' + className +' ') != -1) {
			return true;
		}
	}
	return false;		
}

hXUL._aux.getScreenCoordinates = function (element) {
	var curleft = curtop = 0;
	if (element.offsetParent) {
		curleft = element.offsetLeft;
		curtop  = element.offsetTop;
		while (element = element.offsetParent) {
			curleft += element.offsetLeft;
			curtop  += element.offsetTop;
		}
	}
	return {x:curleft,y:curtop};
}

hXUL._aux.getMouseCoordinates = function (e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return {x: posx, y: posy};
}
