//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()');

function GetPopupLeftPos(){
    leftPos = popupLeftPos;
    if(typeof leftPos == 'undefined')
         leftPos = 150;//popupLeftPos should be specified in Master. If it's not, we use default 150;
    return leftPos;
}

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,
	footWrapperObject: null,
	popupWrapperObject: null,
	popupBackgroundObject: null,
	popupContentBackgroundObject: null,
	popupContentBottomObject: null,
	popupContentObject: null,
	popupMainObject: null,
	popupCloseButtonObject: null,
	
	show: function(linkObj, popupClientId) {
	    if(linkObj != null) popupClientId = linkObj.getAttribute('PopupClientId');
	    
		if (!Popup.leftOffset) {
		    alignRight = GetPopupLeftPos();
			if (alignRight) {
				Popup.leftOffset = alignRight;
			} else {
				Popup.leftOffset = 10;
			}
		}
		
		if (Popup.readyToRun(false, popupClientId)) {
            Popup.hideSelects();
			
			Popup.popupWrapperObject.style.display = 'block';
			
			if (!(yoffset = window.pageYOffset)) {
				yoffset = document.documentElement.scrollTop;
			}

			Popup.popupMainObject.style.top = 4+yoffset+'px'
			Popup.popupContentBackgroundObject.style.top = Popup.popupMainObject.style.top;
						
			//Make sure the html-object is in the MAIN-div
		    Popup.popupWrapperObject.parentNode.removeChild(Popup.popupWrapperObject)
		    Popup.mainObject.appendChild(Popup.popupWrapperObject);
            
			Popup.popupBackgroundObject.onclick = Popup.hide;
			Popup.popupCloseButtonObject.onclick = Popup.hide;
			Popup.popupCloseButtonObject.removeAttribute('href');
		
			Popup.popupBackgroundObject.style.height = (Popup.mainObject.offsetHeight+Popup.footWrapperObject.offsetHeight)+'px';
		
			Popup.popupMainObject.style.marginLeft = (Popup.leftOffset)+'px';
			Popup.popupContentBackgroundObject.style.marginLeft = (Popup.leftOffset)+'px';
		
			Popup.popupContentBottomObject.style.marginLeft = (Popup.leftOffset)+'px';
			Popup.popupContentBottomObject.style.top = (Popup.popupMainObject.offsetHeight+Popup.popupMainObject.offsetTop-36)+'px';
		
			Popup.popupContentBackgroundObject.style.height = (Popup.popupMainObject.offsetHeight-36)+'px';
		
			Popup.popupWrapperObject.style.visibility = 'visible';
		}
	},
	
	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_background') Popup.popupContentBackgroundObject = divs[i];
			        else if (divs[i].className == 'popup_content_bottom') Popup.popupContentBottomObject = 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 == "main" || divs[i].className == "main_no_menu" ||divs[i].className == "main_no_ads" || divs[i].className == "main_start_page" || divs[i].className == "main_no_ads_s") Popup.mainObject = divs[i];
                    else if (divs[i].className == 'foot_wrapper') Popup.footWrapperObject = 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.footWrapperObject &&
			Popup.popupWrapperObject &&
			Popup.popupBackgroundObject &&
			Popup.popupContentBackgroundObject &&
			Popup.popupContentBottomObject &&
			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;
			}
		}
	}
}