//A List populated by server side control with an array for each popup. 
//The first item in this array is the ClientId of the popup-control 
//and the rest is the ClientIds of all controls that should have an onClick to open the popup.
var PopupList = new Array();

function showTextLayer(ctrlId) {
    if(document.getElementById(ctrlId) != null){
        document.getElementById(ctrlId).click();
    }
}

function addPopup(popupClientId, closeEventButtonClientId, openPopupClientIdArray) {
    var popIndex = PopupList.length                     //We work with one item in the PopupList-array on each LayerPopup-control.
    PopupList[popIndex] = new Array();                  //Create one Array on this item.
    
    PopupList[popIndex][0] = popupClientId;             //And add the first item - the popup ClientId.
    PopupList[popIndex][1] = closeEventButtonClientId;  //And add the second item - the ClientId of the button that should trigger the ServerSide event.
    PopupList[popIndex][3] = openPopupClientIdArray;    //And the rest is an array with the ClientIds for the controls that should open the Popup with client onClick.
}


addOnLoad('init_PopupButtons()');

var init_PopupButtons = function() {
    if(PopupList != null) {
        for (var i = 0; i < PopupList.length; i++) {
            if(PopupList[i] != null) {
	            var link;
	            var popupClientId;
                for (var j = 0; j < PopupList[i].length; j++) {
	                if(j == 0){
	                    popupClientId = PopupList[i][j]; //First item is Popup control ClientId.
                    }
	                else{
	                    if(j != 1) {    //The second item is the clientId of the button that should be triggered when the popup is closed. The rest is buttons that opens the Popup
                            link = document.getElementById(PopupList[i][j]);
                            if(link != null) {
			                    link.removeAttribute('href');
			                    link.setAttribute('PopupClientId', popupClientId.toString());
			                    link.onclick = function() { Popup.show(this, null); return false; };
                            }
                        }
                    }
                }
            }
        }
    }
}

var Popup = {
    triedObjects: false,
    leftOffset: null,
    wantedWidth: null,
    ran: false,

    mainObject: null,
    popupWrapperObject: null,
    popupBackgroundObject: null,
    popupContentObject: null,
    popupMainObject: null,
    popupCloseButtonObject: null,

    show: function(linkObj, popupClientId) {
        if (linkObj != null) popupClientId = linkObj.getAttribute('PopupClientId');

        if (Popup.readyToRun(false, popupClientId)) {
            Popup.hideSelects();
            Popup.popupWrapperObject.style.display = 'block';
            Popup.popupWrapperObject.style.visibility = 'visible';

            Popup.popupBackgroundObject.onclick = Popup.hide;
            Popup.popupCloseButtonObject.onclick = Popup.hide;
            Popup.popupCloseButtonObject.removeAttribute('href');

            Popup.popupBackgroundObject.style.height = (Popup.mainObject.offsetHeight) + 'px';
            
            
            $('.popup_main').center(500, 250); // by HAR
        }
    },

    hide: function() {
        var popupClientId;
        var closeClientId;
        if (PopupList != null) {
            for (var i = 0; i < PopupList.length; i++) {
                if (PopupList[i] != null) {
                    if (PopupList[i].length >= 1) {
                        popupClientId = PopupList[i][0]; //First item is Popup control ClientId. The rest is buttons.

                        if (Popup.readyToRun(false, popupClientId)) {
                            Popup.showSelects();
                            Popup.popupWrapperObject.style.display = 'none';
                            if (document.getElementById(closeClientId) != null)
                                document.getElementById(closeClientId).click();
                        }
                    }
                    if (PopupList[i].length >= 2)
                        closeClientId = PopupList[i][1]; //Second item is close event button.
                }
            }
        }
    },

    tryObjects: function(popupClientId) {
        Popup.triedObjects = true;
        div = document.getElementById(popupClientId);
        if (div != null) {
            divs = div.getElementsByTagName('div');
            if (div.className == 'popup_wrapper') {
                Popup.popupWrapperObject = div;
                for (var i = 0; i < divs.length; i++) {
                    if (divs[i].className == 'popup_background') Popup.popupBackgroundObject = divs[i];
                    else if (divs[i].className == 'popup_content') Popup.popupContentObject = divs[i];
                    else if (divs[i].className == 'popup_main') Popup.popupMainObject = divs[i];
                }
                links = div.getElementsByTagName('a');
                for (var i = 0; i < links.length; i++) {
                    if (links[i].className == 'popup_close') Popup.popupCloseButtonObject = links[i];
                }

                divs = document.getElementsByTagName('div');
                for (var i = 0; i < divs.length; i++) {
                    if (divs[i].className == "siteContainer")
                        Popup.mainObject = divs[i];
                }
            }
        }
    },

    readyToRun: function(dontshow, popupClientId) {
        if (!Popup.triedObjects) {
            Popup.tryObjects(popupClientId);
        }
        if (Popup.popupWrapperObject != null) {
            if (Popup.popupWrapperObject.id != popupClientId) {
                Popup.tryObjects(popupClientId);
            }
        }
        if (
			Popup.mainObject &&
			Popup.popupWrapperObject &&
			Popup.popupBackgroundObject &&
			Popup.popupContentObject &&
			Popup.popupMainObject &&
			Popup.popupCloseButtonObject
		) {
            return true;
        } else {
            if (!Popup.triedObjects) {
                Popup.tryObjects(popupClientId);
                if (!dontshow) Popup.show(null, popupClientId);
            } else {
                return false;
            }
        }
    },

    hideSelects: function() {
        selects = Popup.mainObject.getElementsByTagName('select');
        for (var i = 0; i < selects.length; i++) {
            if (selects[i].className.indexOf('popupoverride') < 0) {
                selects[i].originalDisplay = selects[i].style.display;
                selects[i].style.display = 'none';
            }
        }
    },

    showSelects: function() {
        selects = Popup.mainObject.getElementsByTagName('select');
        for (var i = 0; i < selects.length; i++) {
            if (selects[i].className.indexOf('popupoverride') < 0) {
                selects[i].style.display = selects[i].originalDisplay;
            }
        }
    }
}
