﻿var ModalObjectDict = new Object();    

/*
    Handle events for the Modal Objects;
*/
function HandleWindowResizeEventForModalObject(e)
{
    for(prop in ModalObjectDict)
    {
        if(ModalObjectDict[prop] != null)
        {
            ModalObjectDict[prop].Show();
        }
    }
}

function HandleScrollEventForModalObject(e)
{
    for(prop in ModalObjectDict)
    {
        if(ModalObjectDict[prop] != null)
        {
            ModalObjectDict[prop].Show();
        }
    }
}

function RegisterModalObject(key, obj)
{
    if(TotalModalObjects() == 0)
    {
        Event.observe(window, "resize", HandleWindowResizeEventForModalObject);
        Event.observe(window, "scroll", HandleScrollEventForModalObject);
    }

    ModalObjectDict[key] = obj;
}

function UnRegisterModalObject(key)
{
    //remove the element from the dictionary.
    ModalObjectDict[key] = null;

    if(TotalModalObjects() == 0)
    {
        Event.stopObserving(window, "resize", HandleWindowResizeEventForModalObject);
        Event.stopObserving(window, "scroll", HandleScrollEventForModalObject);
    }
}

function TotalModalObjects()
{
    var total = 0;
    for(prop in ModalObjectDict)
    {
        if(ModalObjectDict[prop] != null)
        {
            total++;
        }
    }
    return total;    
}


/*
    Base class which implements the ModalFullScreenBackground element
*/
var ModalObject =   Class.create(
                    {
                        initialize: function(id, zIndex)
                                    {
                                        this.Id = id;
                                        this.ZIndex = zIndex;
                                        
                                        RegisterModalObject(id, this);                                     
                                    },
                                    
                        ModalBackGroundCSSClass:  'modalBackground',
                        Id: '',
                        ModalBackGroundDiv: null,
                        ZIndex: 1000,
                        
                        
                        Show: function()
                              {
                                var ScrollBarOffset_x = document.viewport.getScrollOffsets()[0];
                                var ScrollBarOffset_y = document.viewport.getScrollOffsets()[1];
                                var screenHeight = document.viewport.getHeight();
                                var screenWidth = document.viewport.getWidth();
                              
                                if(this.ModalBackGroundDiv == null)
                                {
                                    this.ModalBackGroundDiv = document.createElement('div');
                                    this.ModalBackGroundDiv.setAttribute('id', this.Id + '_ModalBackGround');  
                                    this.ModalBackGroundDiv.setAttribute('ModalObjectId', this.Id);
                                    this.ModalBackGroundDiv.className = this.ModalBackGroundCSSClass;
                                    document.body.appendChild(this.ModalBackGroundDiv);     
                                    
                                    $(this.Id + '_ModalBackGround').setStyle("z-index: " + this.ZIndex);
                                }
                              
                                $(this.Id + '_ModalBackGround').setStyle("position: absolute;width: " + (screenWidth + 100) + "px;height: " + (screenHeight + 100) + "px;");
                                $(this.Id + '_ModalBackGround').setStyle("left : " + (ScrollBarOffset_x - 50)  + "px");
                                $(this.Id + '_ModalBackGround').setStyle("top : " + (ScrollBarOffset_y  - 50) + "px");
                               
                              },
                        
                        Hide: function()
                              {
                                UnRegisterModalObject(this.Id);
                              
                                if($(this.Id + '_ModalBackGround'))
                                    $(this.Id + '_ModalBackGround').remove();
                              },
                              
                        /*
                            This function will resize the object to the correct size
                        */
                        Resize: function(e)
                                {                                    
                                    //No implementation needed for the resize of the background
                                    
                                }
                    }
                    );




