﻿//-------- Region Globals --------
var m_arrDefaultHighlightRGB = [255, 128, 128];
//-------- Region Handlers --------


//-------- Region Tranformats --------

function DoChangeObjectSize(a_oObject, a_arrStartSize, a_arrEndSize, a_nSteps, a_nIntervals, a_nBias) 
{ 
    if (a_oObject.SizeChangeInterval)
    {
        window.clearInterval(a_oObject.SizeChangeInterval);
    }
    var nActualStep = 0;
    a_oObject.SizeChangeInterval = window.setInterval(
        function() 
        { 
          a_oObject.CurrentSize = [GetBias(a_arrStartSize[0], a_arrEndSize[0], a_nSteps, nActualStep, a_nBias), GetBias(a_arrStartSize[1], a_arrEndSize[1], a_nSteps, nActualStep, a_nBias)];
          a_oObject.style.width = a_oObject.CurrentSize[0] + "px"; 
          a_oObject.style.height = a_oObject.CurrentSize[1] + "px";
          nActualStep++;
          if (nActualStep > a_nSteps) 
          {
            window.clearInterval(a_oObject.SizeChangeInterval);
          }
        } 
        , a_nIntervals);
}

function DoChangeObjectWidth(a_oObject, a_nStartWidth, a_nEndWidth, a_nSteps, a_nIntervals, a_nBias) 
{ 
    if (a_oObject.WidthChangeInterval)
    {
        window.clearInterval(a_oObject.WidthChangeInterval);
    }
    var nActualStep = 0;
    a_oObject.WidthChangeInterval = window.setInterval(
        function() 
        { 
          a_oObject.CurrentWidth = GetBias(a_nStartWidth, a_nEndWidth, a_nSteps, nActualStep, a_nBias);
          a_oObject.style.width = a_oObject.CurrentWidth + "px"; 
          nActualStep++;
          if (nActualStep > a_nSteps) 
          {
            window.clearInterval(a_oObject.WidthChangeInterval);
          }
        } 
        , a_nIntervals);
}

function DoChangeObjectHeight(a_oObject, a_nStartHeight, a_nEndHeight, a_nSteps, a_nIntervals, a_nBias) 
{ 
    if (a_oObject.HeightChangeInterval)
    {
        window.clearInterval(a_oObject.HeightChangeInterval);
    }
    var nActualStep = 0;
    a_oObject.HeightChangeInterval = window.setInterval(
        function() 
        { 
          a_oObject.CurrentHeight = GetBias(a_nStartHeight, a_nEndHeight, a_nSteps, nActualStep, a_nBias);
          a_oObject.style.height = a_oObject.CurrentHeight + "px"; 
          nActualStep++;
          if (nActualStep > a_nSteps) 
          {
            window.clearInterval(a_oObject.HeightChangeInterval);
          }
        } 
        , a_nIntervals);
}

function DoChangeObjectColor(a_oObject, a_arrStartRGB, a_arrEndRGB, a_nSteps, a_nIntervals, a_nBias) 
{
    if (a_oObject.ColorChangeInterval) 
    {
        window.clearInterval(a_oObject.ColorChangeInterval);
    }
    var nActualStep = 0;
    a_oObject.ColorChangeInterval = window.setInterval(
	    function() 
	    {
		    a_oObject.CurrentColorRGB = [
			    GetBias(a_arrStartRGB[0], a_arrEndRGB[0], a_nSteps, nActualStep, a_nBias),
			    GetBias(a_arrStartRGB[1], a_arrEndRGB[1], a_nSteps, nActualStep, a_nBias),
			    GetBias(a_arrStartRGB[2], a_arrEndRGB[2], a_nSteps, nActualStep, a_nBias)
			    ];
		    a_oObject.style.backgroundColor = "rgb("+a_oObject.CurrentColorRGB[0]+","+a_oObject.CurrentColorRGB[1]+","+a_oObject.CurrentColorRGB[2]+")";
		    nActualStep++;
		    if (nActualStep > a_nSteps) 
		    {
		        window.clearInterval(a_oObject.ColorChangeInterval);
		    }
	    }
	    , a_nIntervals);
}    

function DoChangeObjectPosition(a_oObject, a_arrStartPosition, a_arrEndPosition, a_nSteps, a_nIntervals, a_nBias) 
{
    if (a_oObject.PositionChangeInterval) 
    {
        window.clearInterval(a_oObject.PositionChangeInterval);
    }
    var nActualStep = 0;
    a_oObject.PositionChangeInterval = window.setInterval(
	    function() 
	    {
		    a_oObject.CurrentPosition = [
			    GetBias(a_arrStartPosition[0],a_arrEndPosition[0],a_nSteps,nActualStep,a_nBias),
			    GetBias(a_arrStartPosition[1],a_arrEndPosition[1],a_nSteps,nActualStep,a_nBias)
			    ];
		    a_oObject.style.left = a_oObject.CurrentPosition[0] + "px";
		    a_oObject.style.top = a_oObject.CurrentPosition[1] + "px";
		    nActualStep++;
		    if (nActualStep > a_nSteps) 
		    {
		        window.clearInterval(a_oObject.PositionChangeInterval);
		    }
	    }
	    , a_nIntervals);
}    

//-------- Region Event Handlers --------

function ChangeObjectSizeEvent(evt) 
{ 
	oEvent = new MouseEvent(evt);
	oCurrentSourceNode = oEvent.Source;

    if (!oCurrentSourceNode.currentWidth) oCurrentSourceNode.CurrentSize = [GetWidth(oCurrentSourceNode), GetHeight(oCurrentSourceNode)]; 
    DoChangeObjectSize(oCurrentSourceNode, oCurrentSourceNode.CurrentSize, 250,10,10,0.5); 
} 

function RestoreObjectSizeEvent(evt) 
{ 
	oEvent = new MouseEvent(evt);
	oCurrentSourceNode = oEvent.Source;  

    if (!oCurrentSourceNode.currentWidth) return; 
    DoChangeObjectSize(oCurrentSourceNode, oCurrentSourceNode.currentWidth, 150, 10, 10, 0.5); 
} 

function ChangeObjectColorEvent(evt) 
{
	oEvent = new MouseEvent(evt);
	oCurrentSourceNode = oEvent.Source; 
	
	if (oCurrentSourceNode.OriginalColor == null)
	{
        oCurrentSourceNode.OriginalColor = GetObjectOriginalStyleColor(oCurrentSourceNode);
	}
	if (!oCurrentSourceNode.CurrentColorRGB) 
	{
	    oCurrentSourceNode.CurrentColorRGB = oCurrentSourceNode.OriginalColor;
	}
	DoChangeObjectColor(oCurrentSourceNode, oCurrentSourceNode.CurrentColorRGB, m_arrDefaultHighlightRGB, 4, 20, 1);
}

function RestoreObjectColorEvent(evt) 
{
	oEvent = new MouseEvent(evt);
	oCurrentSourceNode = oEvent.Source;  
		
	if (!oCurrentSourceNode.CurrentColorRGB) return;
	if (oCurrentSourceNode.OriginalColor)
	{
	    DoChangeObjectColor(oCurrentSourceNode, oCurrentSourceNode.CurrentColorRGB, oCurrentSourceNode.OriginalColor, 12, 20, 1);
	}
}    

//-------- Region Transformation Functions --------

function ChangeObjectSize(a_oObject, a_arrSize) 
{ 
    if (!a_oObject.CurrentSize) a_oObject.CurrentSize = [150, 150]; 
    DoChangeObjectSize(a_oObject, a_oObject.CurrentSize, a_arrSize, 2, 10, 10, 0.5); 
} 

function ChangeObjectWidth(a_oObject, a_nWidth) 
{ 
    if (!a_oObject.CurrentWidth) a_oObject.CurrentWidth = GetWidth(a_oObject); 
    DoChangeObjectWidth(a_oObject, a_oObject.CurrentWidth, a_nWidth, 2, 10, 10, 0.5); 
}

function ChangeObjectHeight(a_oObject, a_nHeight) 
{ 
    if (!a_oObject.CurrentHeight) a_oObject.CurrentHeight = GetHeight(a_oObject); 
    DoChangeObjectHeight(a_oObject, a_oObject.CurrentHeight, a_nHeight, 2, 10, 10, 0.5); 
} 

function RestoreObjectSize(a_oObject) 
{   
    if (!a_oObject.CurrentSize) return; 
    DoChangeObjectSize(a_oObject, a_oObject.CurrentSize, [150, 150], 10, 10, 0.5); 
} 

function ChangeObjectColor(a_oObject, a_arrColorRGB) 
{
	if (!a_oObject.CurrentColorRGB) a_oObject.CurrentColorRGB = [255,204,204];
	DoChangeObjectColor(a_oObject, a_oObject.CurrentColorRGB, a_arrColorRGB, 4, 20, 1);
}

function RestoreObjectColor(a_oObject) 
{
	if (!a_oObject.CurrentColorRGB) return;
	DoChangeObjectColor(a_oObject, a_oObject.CurrentColorRGB, [255,204,204], 12, 20, 1);
}    

function MoveObject(a_oObject, a_arrPosition) 
{
    a_oObject.CurrentPosition = [GetX(a_oObject), GetY(a_oObject)];
    DoChangeObjectPosition(a_oObject, a_oObject.CurrentPosition, a_arrPosition, 20, 20, 0.5);
}           
  

//-------- Region Tools --------

function GetX(a_oObject) 
{
    if (a_oObject.style.left != "")
    {
	    return parseInt(a_oObject.style.left);
	}
	return GetObjectOffsetLeft(a_oObject);
}

function GetObjectOffsetLeft(a_oObject)
{
    if (a_oObject)
    {
        var nOffset = 0;
        if (a_oObject.offsetLeft != null)
        {
            nOffset = nOffset + a_oObject.offsetLeft;
        }
        if (a_oObject.offsetParent != null)
        {
             nOffset = nOffset + GetObjectOffsetLeft(a_oObject.offsetParent);
        }
        return nOffset;
    }
    return 0;
}

function GetY(a_oObject) 
{
    if (a_oObject.style.top != "")
    {
	    return parseInt(a_oObject.style.top);
	}
	return GetObjectOffsetTop(a_oObject);
}

function GetObjectOffsetTop(a_oObject)
{
    if (a_oObject)
    {
        var nOffset = 0;
        if (a_oObject.offsetTop != null)
        {
            nOffset = nOffset + a_oObject.offsetTop;
        }
        if (a_oObject.offsetParent != null)
        {
             nOffset = nOffset + GetObjectOffsetTop(a_oObject.offsetParent);
        }
        return nOffset;
    }
    return 0;
}

function SetX(a_oObject, a_nX) 
{
    //a_oObject.offsetLeft = a_nX;
	a_oObject.style.left = a_nX + "px";
}

function SetY(a_oObject, a_nY) 
{
    //a_oObject.offsetTop = a_nY;
	a_oObject.style.top = a_nY + "px";
}	

function GetWidth(a_oObject) 
{
    if (a_oObject.style.width != "")
    {
	    return parseInt(a_oObject.style.width);
	}
    if (a_oObject.clientWidth != null || a_oObject.clientWidth == 0)
    {
        return a_oObject.clientWidth;
    }
	return 0;
}

function GetHeight(a_oObject) 
{
    if (a_oObject.style.height != "")
    {
	    return parseInt(a_oObject.style.height);
	}
    if (a_oObject.clientHeight != null || a_oObject.clientHeight == 0)
    {
        return a_oObject.clientHeight;
    }
	return 0;
}	

function SetWidth(a_oObject, a_nWidth) 
{
    //a_oObject.clientWidth = a_nWidth;
	a_oObject.style.width = a_nWidth + "px";
}

function SetHeight(a_oObject, a_nHeight) 
{
    //a_oObject.clientHeight = a_nHeight;
	a_oObject.style.height = a_nHeight + "px";
}			

function SetOpacity(a_oObject, a_nValue) 
{
	if (a_oObject.filters) 
	{
		try 
		{
			a_oObject.filters["alpha"].opacity = a_nValue * 100;
		} 
		catch (e) { }
	}
	else if (a_oObject.style.opacity) 
	{
		a_oObject.style.opacity = a_nValue;
	}
}

function SetZIndex(a_oObject, a_nValue)
{
    a_oObject.style.zIndex = a_nValue;
}	

function GetSeparateRGBStyle(a_strColor)
{
    var arrRGBColor = new Array();
    if (a_strColor && a_strColor.indexOf("rgb") != -1)
    {
        var strSubstring = a_strColor.substring(4, a_strColor.length - 1);
        arrRGBColor = strSubstring.split(",");
        for (var i = 0; i < arrRGBColor.length; i++)
        {
            arrRGBColor[i] = parseInt(arrRGBColor[i]);
        }
        return arrRGBColor;
    }
    else if (a_strColor && a_strColor.indexOf("#") != -1)
    {
        if (a_strColor.length == 4)
        {
            arrRGBColor[0] = Hex2Decimal(a_strColor.substr(1,1) + a_strColor.substr(1,1));
            arrRGBColor[1] = Hex2Decimal(a_strColor.substr(2,1) + a_strColor.substr(2,1));
            arrRGBColor[2] = Hex2Decimal(a_strColor.substr(3,1) + a_strColor.substr(3,1));
            return arrRGBColor;
        }
        else if (a_strColor.length == 7)
        {
            arrRGBColor[0] = Hex2Decimal(a_strColor.substr(1,1) + a_strColor.substr(2,1));
            arrRGBColor[1] = Hex2Decimal(a_strColor.substr(3,1) + a_strColor.substr(4,1));
            arrRGBColor[2] = Hex2Decimal(a_strColor.substr(5,1) + a_strColor.substr(6,1));   
            return arrRGBColor;   
        }
    }
    return null;
}

function GetPageCssStyle(a_strId)
{
    for (var i = 0; i < document.styleSheets.length; i++)
    {
        if (document.styleSheets[i].id == a_strId || document.styleSheets[i].title == a_strId)
        {
            return document.styleSheets[i];
        }
    }
    return null;
}

function GetCssStyleRule(a_oCssStyle, a_strSelectorText)
{
    if (a_oCssStyle.cssRules) 
    {
        arrCssRules = a_oCssStyle.cssRules;
    }
    else if (a_oCssStyle.rules) 
    {
        arrCssRules = a_oCssStyle.rules;
    }
    
    for (var i = 0; i < arrCssRules.length; i++)
    {
        if (arrCssRules[i].selectorText == a_strSelectorText)
        {
            return arrCssRules[i].style;
        }
    }  
    return null;
}  


function GetObjectOriginalStyleColor(a_oObject)
{
    if (a_oObject)
    {
	    if (a_oObject.style.backgroundColor)
	    {
	        return GetSeparateRGBStyle(a_oObject.style.backgroundColor);
	    }
	    else
	    {
	        oStyle = GetCssStyleRule(GetPageCssStyle("GfxStyle"), ".NodeContainer");
	        return GetSeparateRGBStyle(oStyle["backgroundColor"]);
	    }
	 }
	 return null;
}

function GetBias(a_nStartValue, a_nEndValue, a_nSteps, a_nCurrentStep, a_nBias) 
{ 
    return Math.ceil(a_nStartValue + (Math.pow(((1 / a_nSteps) * a_nCurrentStep), a_nBias) * (a_nEndValue - a_nStartValue))) 
} 	