/*dependencies
    <dependencies/>
dependencies*/



/* example implementation

// this assumes an expanding image like sabretubularstructures.com
function StretchImage(ImgPath) {

    var img = document.getElementById("imgStretch");
    var div = document.getElementById("divStretch");
    img.src = ImgPath;

    var width = 500;
    var height = 500;
    
    var divExpander = new Expander(div, height + 60, width + 30, 45, 1, true);
    ExpanderHelper.CloseExpander = function () { divExpander.Expand(false); }
    divExpander.Expand(true);
    return false;

}

EXAMLE HTML MARKUP
<td colspan = "2" style ="text-align:center;">
    <div style ="position:relative;">
        <div style = "position:absolute;left:0;width:0px;height:0px;display:none;border:solid 1px black;background-color:#eeeeee;overflow:hidden;" id = "divStretch">
            <table style = "width:100%;">
                <tbody>
                    <tr>
                        <td style = "height:20px;" align = "right"><img src = "http://www.SabreIndustriesInc.com/img/SHAREDIMG/icons/action_delete.gif" alt = "Close" onclick = "ExpanderHelper.CloseExpander();" style = "cursor:pointer;" /></td>
                    </tr>
                    <tr>
                        <td align = "center"><img id = "imgStretch" src = "" alt = "" title = "" /></td>
                    </tr>
                    <tr>
                        <td style = "height:20px;"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</td>
*/
function ExpanderHelper() { }
ExpanderHelper.CloseExpander = function () { }

function Expander(MovingObject, Height, Width, HeightIncrement, Speed, CompleteTaskBeforeStartingNewTask) {

    var self = this;
    var stretchIntvl;
    var iterationCount = 0;
    var iterationMax = 0;
    var widthIncrement = 0;
    var debug = false;
    this.Debug = function (value) { if (value == null) { return debug; } else { debug = value; } }
    this.WidthIncrement = function(value) { if (value == null) { return widthIncrement; } else { widthIncrement = value; } }
    
    this.Complete = function (bExpand) { if (!bExpand) { MovingObject.style.display = "none"; } }

    function Initialize() {
    
        if(Speed == null || isNaN(Speed)) { Speed = 1; }

        var percentage = parseFloat(Height / HeightIncrement);
        widthIncrement = Math.ceil(parseFloat(Width / percentage));
    

        var widthIterations = Math.ceil(parseFloat(Width / widthIncrement));
        var heightIterations = Math.ceil(parseFloat(Height / HeightIncrement));

        iterationMax = (heightIterations > widthIterations ? heightIterations : widthIterations);
        
    }
    
    function Increment(bool) {
        var height, width;
        try {

            var popup = MovingObject;

            var widthEnd = Width, tallEnd = Height, widthAdd = widthIncrement, heightAdd = HeightIncrement;

            if (!bool) { widthEnd = widthAdd; widthAdd *= -1; tallEnd = heightAdd; heightAdd *= -1; }
            else { popup.style.display = "block"; }

            if(typeof(Shared) == "function") {
            
                width = parseInt(Shared.GetStyle(popup, "width")) + widthAdd;
                height = parseInt(Shared.GetStyle(popup, "height")) + heightAdd;
                
            } else {
            
                width = parseInt(popup.style.width) + widthAdd;
                height = parseInt(popup.style.height) + heightAdd;
                
            }

            if (bool && height > tallEnd) { height = tallEnd; }
            else if (!bool && height < tallEnd) { height = tallEnd; }

            if (bool && width > widthEnd) { width = widthEnd; }
            else if (!bool && width < widthEnd) { width = widthEnd; }

            popup.style.width = width.toString() + "px";
            popup.style.height = height.toString() + "px";

            iterationCount++;
            if (width == widthEnd || iterationCount > iterationMax) {

                iterationCount = 0;
                clearInterval(stretchIntvl);
                stretchIntvl = null;
                self.Complete(bool);

            }

        } catch (x) {

            if (iterationCount >= iterationMax) { clearInterval(stretchIntvl); stretchIntvl = null; }
            if (debug) {
                alert(height.toString() + "px");
                alert(width);
                alert(x.message);
                alert(Popup().style.height);
                alert(Popup().style.width);
                mm = 0;
            }
        }

    }

    this.Expand = function(bShow) {

        if (CompleteTaskBeforeStartingNewTask && stretchIntvl != null) { return; }
        stretchIntvl = setInterval(function() { Increment(bShow); }, Speed);
    
    }
    
    Initialize();
}
