/* Copyright (C) 2009, Daniel Gründl - http://dgfire.de/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 	  
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
function olEvent() {
	/* public : version number - INCREMENT THIS ONE, IF YOU MADE CHANGES! */
	this.version = '090702';

	// on elem,event name,func to call
	this.observe = function (elem, name, func) {
		var capture = (arguments[3]) ? arguments[3] : false;
		if (elem.addEventListener)
			elem.addEventListener(name, func, capture);
		else if (elem.attachEvent)
			elem.attachEvent('on' + name, func);
	};

	this.stop = function (e) {
		if (e.preventDefault) { 
		  e.preventDefault(); 
		  e.stopPropagation(); 
		} else
		  e.returnValue = false;
	};	

	this.remove = function (elem, name, func) {
		var capture = (arguments[3]) ? arguments[3] : false;
		if (elem.removeEventListener)
			elem.removeEventListener(name, func, capture);
		else if (elem.detachEvent)
			elem.detachEvent('on' + name, func);		
	};

	this.getX = function (e) {
		return (e.pageX) ? e.pageX : e.clientX + document.body.scrollLeft;
	};
	
	this.getY = function (e) {
		return (e.pageY) ? e.pageY : e.clientY + document.body.scrollTop;
	};
	

	this.setPosition = function(el, x, y){
		if(x<0 || y<0)
			this.center(el);
		else {
			el.style.position = 'absolute';
			el.style.left = x +'px';
			el.style.top = y +'px';
			el.style.display = 'block';
		}
	}
	
	this.center = function(el){
		el.style.display = 'block';
		el.style.position = 'absolute';
		var w = document.body.scrollLeft+document.body.clientWidth;
		var x = (w-el.offsetWidth) >> 1;

		var y = document.body.scrollTop + ((document.body.clientHeight-el.offsetHeight)>>1)

		if (x < 0) x = 0;
		if (y < 0) y = 0;
		el.style.left = x +'px';
		el.style.top = y +'px';
	};
	
	this._centerSomehowBroken = function(el){
		el.style.display = 'block';
		el.style.position = 'absolute';
		var w = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
		var h = (window.innerHeight) ? window.innerHeight : document.body.offsetHeight;
		var x = (w-el.offsetWidth) >> 1;
		var y = (h-el.offsetHeight) >> 1;
		if (x < 0) x = 0;
		if (y < 0) y = 0;
		el.style.left = x +'px';
		el.style.top = y +'px';
	};
	
	return this;
}
