//CREated by Daniel Grant 13/11/2008 for modal popups and lightboxes
var ModalManager = 
{
    ModalArea : null, 
    Form : null,
    FormShadowMiddle : null,
    FormShadow : null,
    FormHeight : 0,
    FormOverflowHeight : 0,
    ModalAnimationPointer : null,
    FormAnimationPointer : null,
    TargetContainer : null,
    Opacity : 0,
    FrameSkip : 20,
    FrameInterval : 1,
    OnLoadedEvent : null,
    DisplayElementById : function()
    {
        
        try{document.body.onclick = null; } catch(ee){}
        if(arguments.length > 1)
        {
            ModalManager.Init($(arguments[0])[0], function()
            { 
                document.body.onclick = function() 
                {
                  ModalManager.Cancel();
                };
            }, arguments[1]);
        }
        else
        {
            ModalManager.Init($(arguments[0])[0], function()
            { 
                document.body.onclick = function() 
                {
                  ModalManager.Cancel();
                };
            });
        }
    },
    Init : function()
    {
        clearInterval(ModalManager.ModalAnimationPointer);
        ModalManager.ModalArea = $('modal_bg')[0];
        ModalManager.Form = $('modal_form')[0];
        ModalManager.FormShadow = $('modal_shadow')[0];
        ModalManager.FormShadowMiddle = $('midshadow')[0];
        do
        {
            while(ModalManager.FormShadowMiddle.tagName != "DIV")
                ModalManager.FormShadowMiddle = ModalManager.FormShadowMiddle.nextSibling;
                
            if(ModalManager.FormShadowMiddle.className == "right")
                break;
        }
        while(ModalManager.FormShadowMiddle = ModalManager.FormShadowMiddle.firstChild)
        
        
        ModalManager.FormOverflowHeight = 0;
        ModalManager.FormPanel = $('modal_panel')[0];
        ModalManager.TargetContainer = arguments[0]; //use the object to later reference
        //check height declared
        if(arguments.length > 2)
            ModalManager.FormHeight = arguments[2]
        else
            ModalManager.FormHeight = (ModalManager.TargetContainer.offsetHeight>0)?ModalManager.TargetContainer.offsetHeight : 500;
        ModalManager.ModalArea.style.display = "block";
        ModalManager.ModalArea.style.visibility = "visible";
        setOpacity(ModalManager.ModalArea,ModalManager.Opacity);
        ModalManager.ModalAnimationPointer = setInterval("ModalManager.FadeIn()",ModalManager.FrameInterval);   
        ModalManager.OnLoadedEvent = null; //set OnLoadevent To Empty
        if(arguments.length > 1)
        {
            ModalManager.OnLoadedEvent = arguments[1];
        }
    },
    Cancel : function()
    {
        clearInterval(ModalManager.ModalAnimationPointer);
        ModalManager.ModalAnimationPointer = setInterval("ModalManager.FadeOut()",ModalManager.FrameInterval); 
        document.body.onkeydown = null;
    },
    FadeIn : function()
    {
        if(ModalManager.Opacity < 75)
        {
            ModalManager.Opacity += ModalManager.FrameSkip;
            setOpacity(ModalManager.ModalArea,ModalManager.Opacity);
        }
        else if(ModalManager.Opacity < 90)
        {
            ModalManager.Opacity += 1;
            setOpacity(ModalManager.ModalArea,ModalManager.Opacity);
        }
        else 
        {
            //cancel the anim thread and stop
            clearInterval(ModalManager.ModalAnimationPointer);
            ModalManager.Form.style.display="block";
            ModalManager.Form.style.visibility="visible";
            //start the rollin in animation
            clearInterval(ModalManager.FormAnimationPointer);
            var elemnt = ModalManager.TargetContainer.parentNode;
            var blnIsChildElement = false;
            try{
                do{
                    if(elemnt==ModalManager.FormPanel)
                    {
                       blnIsChildElement = true;
                       break;
                    }
                }
                while((elemnt = elemnt.parentNode).tagName!='BODY')
                
                if(!blnIsChildElement)
                {
                    //evict all existing children and adopt this one
                    DOM_RemoveAllChildren(ModalManager.FormPanel);
                    ModalManager.FormPanel.appendChild(ModalManager.TargetContainer.cloneNode(true));
                }
            
            //else do nothing, just continue
            ModalManager.FormAnimationPointer = setInterval("ModalManager.AnimateRollIn()",ModalManager.FrameInterval);   
            } 
            catch (Exc)
            {
                ModalManager.Cancel();
            }
            
        }
    },
    FadeOut : function()
    {
        if(ModalManager.Opacity > 10)
        {
            ModalManager.Opacity -= ModalManager.FrameSkip;
            setOpacity(ModalManager.ModalArea,ModalManager.Opacity);
        }
        else if(ModalManager.Opacity > 0)
        {
            ModalManager.Opacity -= 1;
            setOpacity(ModalManager.ModalArea,ModalManager.Opacity);
        }
        else 
        {
            //cancel the anim thread and stop
            clearInterval(ModalManager.ModalAnimationPointer);
            ModalManager.ModalArea.style.display = "none";
            ModalManager.ModalArea.style.visibility = "";
            ModalManager.Form.style.display="none";
            ModalManager.Form.style.visibility="";
            ModalManager.FormOverflowHeight = 0;
            ModalManager.FormPanel.style.height = ModalManager.FormOverflowHeight+"px";   
            ModalManager.FormShadow.style.height = ModalManager.FormOverflowHeight+"px";  
        }
    },
    AnimateRollIn : function()
    {
        if((ModalManager.FormOverflowHeight+10) < ModalManager.FormHeight )
        {
            ModalManager.FormOverflowHeight += ModalManager.FrameSkip+5;
            ModalManager.FormPanel.style.height = ModalManager.FormOverflowHeight+"px";   
            //ModalManager.FormShadow.style.height = ModalManager.FormOverflowHeight+"px";  
        }
        else if(ModalManager.FormOverflowHeight < ModalManager.FormHeight )
        {
            ModalManager.FormOverflowHeight += 1;
            ModalManager.FormPanel.style.height = ModalManager.FormOverflowHeight+"px";   
            //ModalManager.FormShadow.style.height = ModalManager.FormOverflowHeight+"px";  
        }
        else
        {
            clearInterval(ModalManager.FormAnimationPointer);
            ModalManager.FormShadow.style.height = (ModalManager.FormOverflowHeight+166)+"px";
            //check shadow height
            if(ModalManager.FormOverflowHeight > 500)
            {
                ModalManager.FormShadowMiddle.style.height = (ModalManager.FormOverflowHeight-166)+"px";
            }
            else if(ModalManager.FormOverflowHeight < 500)
            {
                ModalManager.FormShadowMiddle.style.height = (ModalManager.FormOverflowHeight-166)+"px";
            }
            if(ModalManager.OnLoadedEvent!=null)
                if( typeof ModalManager.OnLoadedEvent=='function')
                    ModalManager.OnLoadedEvent(); //execute event handler
        }
    }
};    
