﻿//(C) Michael Roberg 2009
//This script may not be used without permission, to obtain permission please contact Michael at michaelr@smartcom.co.za


var ctlCornerPeeler_GrowTimeoutID = null;
var ctlCornerPeeler_GrowState = null;
var ctlCornerPeeler_CurrentGrowIndex = 0;


function ctlCornerPeeler_Initialize(BackgoundImageID, CornerImageID, WrapperID, width, height) {
    
    var Corner = document.getElementById(CornerImageID);
    var Background = document.getElementById(BackgoundImageID);
    var Wrapper = document.getElementById(WrapperID);


    Corner.style.display = 'inline';
    Background.style.display = 'inline';
    Corner.style.visibility = 'visible';
    Background.style.visibility = 'visible';    

    //apply alpha blending so that png24 shadow effect can be supported by IE6
    if (navigator.appVersion.indexOf('MSIE 6.0') != -1) {

        Corner.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + Corner.src + "',sizingMethod='scale')";
        Corner.src = 'clear.gif';
        
    }

    Wrapper.onmouseover = function() { ctlCornerPeeler_CornerGrow(BackgoundImageID, CornerImageID, 30, ctlCornerPeeler_CurrentGrowIndex,width,height); }
    Wrapper.onmouseout = function() { ctlCornerPeeler_ReverseCornerGrow(BackgoundImageID, CornerImageID, 30, ctlCornerPeeler_CurrentGrowIndex, width, height); }

    ctlCornerPeeler_AnimateCornerStandby(BackgoundImageID, CornerImageID, width, height, 0, 1);
    
}

function ctlCornerPeeler_AnimateCornerStandby(BackgoundImageID, CornerImageID, width, height, growIndex, growDirectionIndex) {
   
    var GrowSpeed = 50;
    var GrowIncrement = 1;
    var CornerMaxGrow = 5;
    var CornerMinGrow = 0;
    var Corner = document.getElementById(CornerImageID);
    var Background = document.getElementById(BackgoundImageID);
    


    if (Corner != null && growDirectionIndex != 0 && ctlCornerPeeler_GrowState != 2) {

        ctlCornerPeeler_GrowState = 2;

        Corner.style.width = width + 'px';
        Corner.style.height = height + 'px';
        Corner.style.left = '-' + width + 'px';        
 
        Background.style.width = width + 'px';
        Background.style.height = (height -12) + 'px';
        Background.style.left = '-' + Background.style.width;

        if (growIndex > CornerMaxGrow) { growDirectionIndex = -1; }
        if (growIndex < CornerMinGrow) { growDirectionIndex = 1; }
        
        if(growDirectionIndex == 1){
        
            width = width + GrowIncrement;
            height = height + GrowIncrement;
            growIndex++;
        
        }
        else
        {        
            width = width - GrowIncrement;
            height = height - GrowIncrement;
            growIndex--;
        }

        ctlCornerPeeler_GrowState = 0;
        ctlCornerPeeler_GrowTimeoutID = setTimeout("ctlCornerPeeler_AnimateCornerStandby('" + BackgoundImageID + "','" + CornerImageID + "'," + width + "," + height + "," + growIndex + "," + growDirectionIndex + ")", GrowSpeed);
    }

}

function ctlCornerPeeler_ReverseCornerGrow(BackgoundImageID, CornerImageID, MaxCornerGrow, growIndex, width, height) {
    
    clearTimeout(ctlCornerPeeler_GrowTimeoutID);
    ctlCornerPeeler_CurrentGrowIndex = MaxCornerGrow - ctlCornerPeeler_CurrentGrowIndex;
    ctlCornerPeeler_AnimateCornerGrow(BackgoundImageID, CornerImageID, MaxCornerGrow, ctlCornerPeeler_CurrentGrowIndex, 0,width,height);
    
}


function ctlCornerPeeler_CornerGrow(BackgoundImageID, CornerImageID, MaxCornerGrow, growIndex, width, height) {
    
    clearTimeout(ctlCornerPeeler_GrowTimeoutID);
    ctlCornerPeeler_CurrentGrowIndex = MaxCornerGrow - ctlCornerPeeler_CurrentGrowIndex;
    if (ctlCornerPeeler_CurrentGrowIndex == MaxCornerGrow) { ctlCornerPeeler_CurrentGrowIndex = 0; }
    ctlCornerPeeler_AnimateCornerGrow(BackgoundImageID, CornerImageID, MaxCornerGrow, ctlCornerPeeler_CurrentGrowIndex, 1, width, height);
    
}


function ctlCornerPeeler_AnimateCornerGrow(BackgoundImageID, CornerImageID, MaxCornerGrow, growIndex, GrowDirection, standbyWidth, standbyHeight) {

    
    var GrowSpeed = 10;
    var GrowIncrement = 10;
    var Corner = document.getElementById(CornerImageID);
    var Background = document.getElementById(BackgoundImageID);
    

    if (Corner != null && Background != null && ctlCornerPeeler_GrowState != 1 ) {

        var width = parseInt(Corner.style.width.replace('px', ''));
        var height = parseInt(Corner.style.height.replace('px', ''));

        if (Corner != null && growIndex < MaxCornerGrow) {

            ctlCornerPeeler_GrowState = 1;

            if (GrowDirection == 1) {

                width = width + GrowIncrement;
                height = height + GrowIncrement;
                growIndex++;

            }
            else {

                width = width - GrowIncrement;
                height = height - GrowIncrement;
                growIndex++;

            }

            ctlCornerPeeler_CurrentGrowIndex = growIndex;

            Corner.style.width = width + 'px';
            Corner.style.height = height + 'px';

            Corner.style.width = width + 'px';
            Corner.style.height = (height) + 'px';
            Corner.style.left = '-' + width + 'px';

            if (GrowDirection == 1) {
                Background.style.width = width + 'px';
                Background.style.height = (height - (12 + (growIndex * 1.49))) + 'px';
                Background.style.left = '-' + Background.style.width;
            }
            else {
                Background.style.width = width + 'px';
                Background.style.height = (height - (12 + ((MaxCornerGrow - growIndex) * 1.49))) + 'px';
                Background.style.left = '-' + Background.style.width;
            }

            ctlCornerPeeler_GrowState = 0;
            ctlCornerPeeler_GrowTimeoutID = setTimeout("ctlCornerPeeler_AnimateCornerGrow('" + BackgoundImageID + "','" + CornerImageID + "'," + MaxCornerGrow + "," + growIndex + "," + GrowDirection + "," +  standbyWidth + "," + standbyHeight + ")", GrowSpeed);

        }
        else if (GrowDirection == 0) {

        ctlCornerPeeler_AnimateCornerStandby(BackgoundImageID, CornerImageID, standbyWidth, standbyHeight, 0, 1);
        
        }

        
       
    }

}

