
Object.extend = function(destination, source, overwrite)
{
	for (var property in source)
	{
		if(destination[property] == undefined || overwrite)
		{
			destination[property] = source[property];
		}
	}
	return destination;
}

Function.prototype.handler = function(obj)
{
  var __method = this;
  return function(event)
  {                              
    event = event || window.event || new Array();
    
  	event.obj = this;
    
    if(!event.preventDefault)
    {
        event.preventDefault = function()
        {
            this.returnValue = false;
        }
    }
    
    if(!event.stopPropagation)event.stopPropagation = function()
    {
        event.cancelBubble = true;
    }
             
    return __method.call(obj, event);
  }
}

TunedHTMLElement = function (){}

TunedHTMLElement.prototype =
{
    addEvent: function(type, fn, obj)
    {
		var that = this;
		
        if(that.attachEvent)
        {
            that['e'+type+fn] = fn;
            that[type+fn] = 
			function()
			{
				that['e'+type+fn](window.event);
			}
			
            that.attachEvent('on'+type, that[type+fn]);
        } 
        else
        {
    	    that.addEventListener(type, fn, false);
        }
    },

    removeEvent: function(type, fn)
    {
        if(this.detachEvent)
        {
            this.detachEvent('on'+type, this[type+fn]);
            this[type+fn] = null;
        } 
        else
        {
            this.removeEventListener(type, fn, false);
        }
    },
    
    getX: function()
    {
    	return this.offsetLeft;
    },
    
    getY: function()
    {
    	return this.offsetTop;
    },
    
    setX: function(x)
    {
    	this.style.left = x+"px";
    },
    
    setY: function(y)
    {
    	this.style.top = y+"px";
    },
    
    getWidth: function()
    {
    	return this.offsetWidth;
    },
    
    getHeight: function()
    {
    	return this.offsetHeight;
    },
    
    setWidth: function(width)
    {
    	this.style.width = width+"px";
    },
    
    setHeight: function(height)
    {
    	this.style.height = height+"px";
    },
	
	setPos: function(x, y)
	{
		this.setX(x);
		this.setY(y);
	},
	
    findPos: function()
    {
    	var curleft = 0;
		var curtop = 0;
		var obj = this;
    	if (obj.offsetParent)
    	{
    		curleft = obj.offsetLeft
    		curtop = obj.offsetTop
    		while ((obj = obj.offsetParent))
    		{
    			curleft += obj.offsetLeft
    			curtop += obj.offsetTop
    		}
    	}
    	else
    	{
    	    return { 'x': obj.x, 'y': obj.y };
    	}
    	
    	return { 'x': curleft, 'y': curtop };
    },
    
    show: function()
    {
        this.style.display = "";
    },
    
    hide : function()
    {
        this.style.display = "none";
    }
}

function $(id)
{
	var element;
	if(typeof(id) == "string")
		element = document.getElementById(id);
	else
		element = id;
    
    if(!element)return element;
		
	Object.extend(element, TunedHTMLElement.prototype);	
	return element;
}

function EngineConstructor()
{
    if(typeof __construct == "function")__construct();
}

$(window).addEvent("load", EngineConstructor);


