﻿var CurrentClientID;
var resizeToWidth;
var resizeToHeight;
var transDuration = 500;
var currentPopup;
var currentContainer;
var currentCheckboxList;
var currentLabel;
var resizeCheckObj;


function PopupPosition(clientID) {
    var popupObject = $(clientID);

    //get the scroll offsets
    var y = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var x = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);

    //compensate for the object's parents offsets
    y -= popupObject.getParent().getCoordinates().top;
    x -= popupObject.getParent().getCoordinates().left;

    this.y = y;
    this.x = x;
}


function PopupPositionNoParent(clientID) {
    var popupObject = $(clientID);

    //get the scroll offsets
    var y = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var x = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);

    //compensate for the object's parents offsets
    //y -= popupObject.getParent().getCoordinates().top;
    //x -= popupObject.getParent().getCoordinates().left;

    this.y = y;
    this.x = x;
}



function SetupCSSOverlay() {

    if ($('CssOverlay') == null) {
        var popupContainer = $('popupContent');
        var cssOverlay = new Element('div', { 'class': 'CssOverlay', 'id': 'CssOverlay', 'style': 'display:none;position:absolute;background:#333333;zindex:400;' });
        cssOverlay.innerHTML = "&nbsp;";
        cssOverlay.injectTop(popupContainer);             
    }

    try {
        var overlay = $('CssOverlay');
        var position = new PopupPosition('CssOverlay');

        overlay.style.top = position.y + 'px';
        overlay.style.left = position.x + 'px';

        overlay.style.width = document.getCoordinates().width + 'px';
        overlay.style.height = document.getCoordinates().height + 'px';       
    }
    catch (e) {
        alert('Setup CSS Overlay - 2 - ' + e.message);
    }
    
}


function SetupPopupPosition(ClientID, finalWidth) {
    var item = $(ClientID + "_Container");
    var position = new PopupPosition(ClientID + "_Container");

    item.style.top = (position.y + 25) + 'px';
    item.style.left = (position.x + (((document.getCoordinates().width) / 2) - (finalWidth / 2))) + 'px';
}

window.onscroll = function() {
    if (CurrentClientID && CurrentClientID.length > 0) {
        currentPopup.fireEvent('movePopup');
    }
}


window.onresize = function() {
    if (CurrentClientID != null) {
        if (CurrentClientID.length > 0) {
            currentPopup.fireEvent('movePopup');
        }
    }
}

function AdjustPopupHeight(newHeight, addToCurrentHeight) {

    if (addToCurrentHeight) {
        var coords = currentContainer.getCoordinates();
        currentPopup.fireEvent('adjustHeight', newHeight + resizeToHeight);
    }
    else {
        currentPopup.fireEvent('adjustHeight', newHeight);
    }
}

function AutoAdjustHeight() {
    if (resizeCheckObj) {
        var coords = currentPopup.getElement('.ContainerHeight').getCoordinates();
        var height = resizeToHeight;
        if (coords.height + 25 < resizeToHeight && coords.height > 0) {
            AdjustPopupHeight(coords.height + 25);
        } else {
            AdjustPopupHeight(resizeToHeight)
        }
    }
}

function SetupPopup(ClientID) {
    SetupCSSOverlay();
    try {
        if (ClientID.indexOf("_mToolBox_mPopup") >= 0) {
            var popupContent = $('popupContent');
            var popup = $(ClientID).getParent();
            var parent = popup.getParent();
            var overlay = $('CssOverlay');

            popup.injectAfter(overlay);            
        }
    }
    catch (e) {
        //alert('Setup Popup - ' + e.message);
    }

    $(ClientID + '_Container').set('morph', { duration: transDuration
                            , transition: Fx.Transitions.Expo.easeOut
                            , link: 'chain'
    });

    $(ClientID).addEvents({
        'fadeOut': function() {
        },

        'hide': function() {
            currentPopup.fireEvent('fadeOut');
            currentPopup.fireEvent('resizeOut');
            currentPopup.fireEvent('hideContainer', '', (transDuration));
        },

        'fadeIn': function() {
            currentPopup.style.display = "block";

            /*currentPopup.set('morph', { duration: transDuration
            , transition: Fx.Transitions.Expo.easeOut
            , link: 'chain'
            });

            currentPopup.morph({ 'opacity': [0, 1] }); */
        },

        'resizeIn': function() {
            currentContainer.style.display = "block";
            currentContainer.morph({ 'width': [75, resizeToWidth], 'height': [75, resizeToHeight] });
        },

        'resizeOut': function() {
            currentContainer.morph({ 'width': [resizeToWidth, 0], 'height': [resizeToHeight, 0] });
        },

        'show': function() {
            currentPopup.fireEvent('hideContainer');
            currentPopup.fireEvent('resizeIn');
            currentPopup.fireEvent('fadeIn', '', transDuration);
            $('CssOverlay').style.display = "block";
            $("CssOverlay").setStyles({ 'opacity': 0.6 });

            if (resizeCheckObj) {                
                AutoAdjustHeight.delay(transDuration);            
            }
        },

        'hideContainer': function() {
            currentContainer.style.display = "none";
            $("CssOverlay").style.display = "none";
        },

        'hideContent': function() {
            currentPopup.style.display = "none";
        },

        'movePopup': function() {
            SetupCSSOverlay();
            var position = new PopupPosition(this.id + '_Container');
            currentContainer.morph({ 'top': [position.y + 25], 'left': [position.x + (((document.getCoordinates().width) / 2) - (resizeToWidth / 2))] });
        },

        'adjustHeight': function(newHeight) {
            currentContainer.morph({ 'height': [newHeight] });
        }
    });   
}

function ShowPopup(ClientID, width, height, adjustHeight) {

    CurrentClientID = ClientID;

    resizeToWidth = width;
    resizeToHeight = height;    
    
    currentPopup = $(CurrentClientID);
    currentContainer = $(CurrentClientID + '_Container');

    if (adjustHeight) {
        resizeCheckObj = true;
    } else {
        resizeCheckObj = false;
    }

    SetupCSSOverlay();
    SetupPopupPosition(ClientID, width);
    currentPopup.fireEvent('show');
}




function HidePopupPostback(sender, arguments) {
    //alert(arguments.EventTarget);
    if (arguments.EventTarget.indexOf('cmdSave') >= 0) {
        try {
            if (currentCheckboxList != null) {
                if (currentCheckboxList.length > 0 && currentLabel.length > 0) {
                    if (validateCheckboxList(currentCheckboxList, currentLabel)) {
                        currentCheckboxList = "";
                        currentLabel = "";
                    }
                    else {
                        currentCheckboxList = "";
                        currentLabel = "";

                        arguments.XMLHttpRequest.abort();
                        arguments.EnableAjax = true;

                        AdjustPopupHeight(20, true);
                        return false;
                    }
                }
            }
        }
        catch (e) {
            alert(e.message);
        }
        currentPopup.fireEvent('hide');
        CurrentClientID = "";
    }
}


function HidePopup() {
    resizeCheckObj = null;
    currentPopup.fireEvent('hide');
    CurrentClientID = "";
}

function setValidateObjects(elementId, label) {
    //alert('Setting Validate Objects');
    currentCheckboxList = elementId;
    currentLabel = label;    
}

function validateCheckboxList(elementId, label) {
    var elementRef = document.getElementById(elementId);
    var labelRef = $(label);
    var inputElementArray = elementRef.getElementsByTagName('INPUT');

    for (var i = 0; i < inputElementArray.length; i++) {
        if (inputElementArray[i].type == 'checkbox') {
            if (inputElementArray[i].checked == true) {
                return true;
            }
        }
    }
    labelRef.style.display = "block";
    return false;

}

