/*
 * Copyright (c) 2008, Aleajecta
 * All rights reserved.
 * 
 * The Service and any necessary software used in connection with the 
 * Service ("Software") contain proprietary and confidential information 
 * that is protected by applicable intellectual property and other laws.
 * You agree not to modify, rent, lease, loan, sell, distribute or create
 * derivative works based on the Service or the Software, in whole or in part.
 */
/**
 * @author Greenseed
 */
HTMLElement.prototype.centerAbsolute = function(horizontal,vertical,parentIndex)
{
    if(!this.parentNode)
        return;

    var horizontal = (typeof horizontal == "undefined" ? true : horizontal);
    var vertical = (typeof vertical == "undefined" ? true : vertical);
    var parentIndex = (typeof parentIndex == "undefined" ? 0 : parentIndex);
    
    var ParentNode;
    for(ParentNode = this.parentNode; parentIndex; ParentNode = ParentNode.parentNode,parentIndex--);

    if(horizontal)
        this.style.left = String((ParentNode.offsetWidth - this.offsetWidth) /2) + "px";
        
    if(vertical)
        this.style.top = String((ParentNode.offsetHeight - this.offsetHeight) /2) + "px";
};
HTMLElement.prototype.mouseReallyOut = function(offSet)
{
    var _OffSet = (typeof offSet == 'undefined') ? 0 : offSet;
    var _OffSetLeft = this.offsetLeft;
    var _OffSetTop = this.offsetTop;
    
    //IE Miss 24 pixel and FF miss 16 pixel == ScrollBar
    var _EleParent = this.offsetParent;
    if(_EleParent)
    {
        do
        {
            _OffSetLeft += _EleParent.offsetLeft;
            _OffSetTop += _EleParent.offsetTop;
        }while(_EleParent = _EleParent.offsetParent) 
    }

    //alert(Mouse.coord['x'] + " | " +(this.x))
    if(Mouse.coord['x']+(_OffSet/2) <= _OffSetLeft+4 || Mouse.coord['x']-(_OffSet/2) >= _OffSetLeft + this.offsetWidth + (_OffSet/2)) 
    {
        return true;
    }
    else if (Mouse.coord['y']+(_OffSet/2) <= _OffSetTop+4 || Mouse.coord['y']-(_OffSet/2) >= _OffSetTop + this.offsetHeight + (_OffSet/2)) 
    {
        return true;
    }
	return false;
};

HTMLElement.prototype.alphaPlayIntervalId = -1;
HTMLElement.prototype.sizePlayIntervalId = -1;
HTMLElement.prototype.alphaPlay = function(end,speed,refresh)
{
    if(typeof moronIE === "undefined")var moronIE = parent.moronIE;
    if(moronIE)                                    //Bypass IE for moment cause DirectX Filter will broke the PNG Filter
        return;

    var currentValue = parseFloat(this.style.opacity);;
    if(this.alphaPlayIntervalId < 0)
    {
        speed = currentValue > end ? -(speed) : speed;
        if(moronIE)
        {
            var ieSrapMemoryLost = this;
            ieSrapMemoryLost.alphaPlayIntervalId = this.alphaPlayIntervalId = setInterval(function(){ieSrapMemoryLost.alphaPlay(end,speed,refresh);},refresh);
        }
        else
            this.alphaPlayIntervalId = window.setInterval(function(owner,end,speed,refresh){owner.alphaPlay(end,speed,refresh);},refresh,this,end,speed,refresh);
    }
    if( (speed > 0 && currentValue < end) || (speed < 0 && currentValue > end) )
    {
        this.style.opacity = String(currentValue + speed);
        this.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+(currentValue + speed)*100+")";
    }
    else
    {
       this.style.opacity = String(end);
       window.clearInterval(this.alphaPlayIntervalId);
       this.alphaPlayIntervalId = -1;
    }
};
HTMLElement.prototype.sizePlay = function(end,slope,speed,refresh)
{
    var currentHeightValue = parseFloat(this.style.height);
    var currentWidthValue = parseFloat(this.style.width);
    if(this.sizePlayIntervalId < 0)
    {
        speed = currentHeightValue > end ? -(speed) : speed;
        if(typeof moronIE === "undefined")var moronIE = parent.moronIE;
        if(moronIE)
        {
            var ieSrapMemoryLost = this;
            this.sizePlayIntervalId = ieSrapMemoryLost.sizePlayIntervalId = setInterval
            (
                function()
                {
                    ieSrapMemoryLost.sizePlay(end,slope,speed,refresh);
                },
                refresh
            );
        }
        else
            this.sizePlayIntervalId = setInterval(function(owner,end,slope,speed,refresh){owner.sizePlay(end,speed,slope,refresh);},refresh,this,end,speed,slope,refresh);
    }
    if( (speed > 0 && currentHeightValue < end) || (speed < 0 && currentHeightValue > end) )
    {
        this.style.height = String(Math.round(currentHeightValue + speed)) + "px";
        this.style.width = String(Math.round(currentWidthValue + (speed*slope))) + "px"; 

        this.parentNode.centerAbsolute(true,false,0);
    }
    else
    {
       this.style.height = String(end) + "px";
       this.style.width = String(Math.round(end*slope)) + "px";
       this.parentNode.centerAbsolute(true,false,0);
       clearInterval(this.sizePlayIntervalId);
       this.sizePlayIntervalId = -1;
    }
};
HTMLElement.prototype.classChange = function(startName,endName,timeout)
{
    if(this.classTimeoutId && startName)
        return;

    if (startName) 
    {
        this.className = startName;
    }
    else 
    {
        this.className = endName;
        this.classChangeTimeoutId = null;
        return;
    }
    if(moronIE) 
    {
        var ieSrapMemoryLost = this;
        this.classTimeoutId = ieSrapMemoryLost.classTimeoutId = setTimeout(function()
        {
            ieSrapMemoryLost.classChange(null, endName, timeout);
        }, timeout);
    }
    else
    {
        this.classTimeoutId = window.setTimeout(function(owner,endName,timeout)
        {
            owner.classChange(null,endName,timeout);
        },
        timeout,this,endName,timeout);
    }
};
