/* Page Events */

SN.Events.Page = new Object();
SN.Events.Page.convertEvent = function(e) {
	if (!e) {
		e = window.event;
		if (e.srcElement) {
			e.target = e.srcElement;
		}
	}
	return e;
};
SN.Events.Page.loaded = false;
window.inheritFrom = SN.Events.EventDispatcher;
window.inheritFrom();


window.onload = function() {
	window.dispatchEvent('onload', {}); // What event info could we pass??
	SN.Events.Page.loaded = true;
};

document.onmousedown = function(e) {
	e = SN.Events.Page.convertEvent(e);
	if (!window.dispatchEvent('onmousedown', e)) {
		// Cancel text selection
		document.body.focus();
		// prevent text selection in IE
		document.onselectstart = function () {
			return false;
		};
		
		e.target.ondragstart = function() {
			return false;
		};
		
		return false;
	}
	return true;
};

document.onmouseup = function(e) {
	e = SN.Events.Page.convertEvent(e);
	window.dispatchEvent('onmouseup', e);
	document.onselectstart = null;
	e.target.ondragstart = null;
	
	return false;
};

window.mouseDown = null;


SN.Events.Page.isLeftClick = function(e, el) {
	return e.target == el && (e.button == 0 || (e.button == 1 && window.event));
};

SN.Events.Page.addLoadEvent = function(FN) {
	if (SN.Events.Page.loaded) {
		alert("The page is already loaded!");
		return;
	}
	window.addEventListener('onload', FN);
};

SN.Events.Page.addClickEvent = function(el, FN) {
	var init = function(e) {
		e = SN.Events.Page.convertEvent(e);
		if (SN.Events.Page.isLeftClick(e, el)) {
			FN(e); // What we wish to perform!
		}
	};
	if (!SN.Browser.isIE) {
		el.addEventListener('click', init, false);
	} else {
		el.attachEvent('onclick', init);
	}
};

SN.Events.Page.addMouseDownEvent = function(el, FN) {
    window.addEventListener('onmousedown', function down(e) {
        if (SN.Events.Page.isLeftClick(e, el)) {
            window.mouseDown = el;
            FN(e); // What we wish to perform!
            return false;
        }
        return true;
    });
};
SN.Events.Page.addMouseUpEvent = function(el, FN) {
    window.addEventListener('onmouseup', function up(e) {
        if (window.mouseDown == el) {
            window.mouseDown = null;
            FN(e);
            return false;
        }
        return true;
    }); // :D
};

// Add other Page Events here...
