doc.scripts['createPopClick'] = initializePopClick;

function initializePopClick()
{
    var popVariants = 
    {
        Click : function(popup) 
            { 
                var popClickDisambiguate = /(^| )popClick_?(.*?)(_| |$)/;
	    
                //get the element id and disambiguate generated dnn-ids
                var uniqueId = popups[i].className.match(popClickDisambiguate)[2];
                uniqueId = uniqueId.replace(/:/g, "_")
        	    
                var popClickArgument = /(^| )popClick_.*?_(.*?)=(.*?)(_| |$)/;
                var argument = popup.className.match(popClickArgument);
        	    	    
                //get element in match
                var clickElement = doc.id(uniqueId); 
                var eventFunction;
                if (argument)
                {
                    eventFunction = function () 
                    {
                        var argumentValue = argument[3];
                        if(argument[2] == "checked" || argument[2] == "disabled")
                        {
                            argumentValue = eval(argument[3]);
                        }
                        if(clickElement[argument[2]] == argumentValue)
                        {
                            popWindow.createVeil();
                            popWindow.createLoadBox();
                            popWindow.setInnerHTML(popup.innerHTML);
                            popWindow.setClass(popup.className);
                        };
                    }
                }
                doc.setFunction(clickElement, "click", eventFunction );
            },
        Load : function(popup) 
            { 
                if(!popup.innerHTML.match("^[\n\t\r ]*$"))
                {
                    popWindow.createVeil();
                    popWindow.createLoadBox();
                    popWindow.setInnerHTML(popup.innerHTML);
                    popWindow.setClass(popup.className);
                }
            }
    }
    
	var popups = doc.css("pop[a-zA-Z0-9_\.:-=]*?");
	for(var i in popups)
	{
	    for(var variant in popVariants)
	    {
	        if (popups[i].className.match("pop" + variant))
	        {
	            popVariants[variant](popups[i]);
	        }
	    }
	}
}

var popWindow = 
{
    createVeil : function () 
        { 
            this.hideControls(true);
            doc.setFunction(window, "resize", function () { popWindow.veil.position(); });
            doc.setFunction(window, "scroll", function () { popWindow.veil.position(); });
            if(this.veil.isSet()) return;
            this.veil.isSet.value = true;
            this.veil.element = document.createElement("div");
            this.veil.element.className = "veil";
            doc.body().appendChild(this.veil.element);
	        this.veil.position()
	    },
	
	veil : { 
	        element : {},
	        isSet : function() 
	            { 
	                if(typeof(this.isSet.value) == "undefined")
	                    this.isSet.value = false; 
	                return this.isSet.value;
	            } ,
	        position : function ()
	            {
	                if (!this.element || !this.isSet()) return;
	                var body = doc.body();
	                this.element.style.height = doc.sniff.IE() ? window.screen.height + "px" : window.innerHeight + "px";
	                if(doc.sniff.IE6())
	                {
	                    this.element.style.top = doc.scroll.y();
	                }
	                else
	                {
	                    this.element.style.width = body.offsetWidth ? body.offsetWidth + "px" : window.width + "px";
	                }
	            }
	        },
	
	loadBox : { 
	        element : {},
	        isSet : function() 
	            { 
	                if(typeof(this.isSet.value) == "undefined")
	                    this.isSet.value = false; 
	                return this.isSet.value;
	            } ,
	        topOffset : function()
	            {
	                if(typeof(this.topOffset.value) == "undefined")
	                    this.topOffset.value = doc.style(this.element, "top") + "";
	                return this.topOffset.value;
	            },
	        messageFrame : {},
	        position : function()
                {
                    if (!this.element || !this.isSet()) return;
                    
                    //center in window (all browsers)
                    var loadWindowWidth = doc.measure.width(this.element);
                    var windowWidth = doc.window.width();
                    var leftOffset = windowWidth / 2 - loadWindowWidth / 2;
                    this.element.style.left = leftOffset + "px";
                    
                    //calculate offsetheight in IE6 to emulate position: fixed
                    var loadWindowHeight = doc.measure.height(this.element);
                    var windowHeight = doc.window.height();
                    
                    if (loadWindowHeight >= windowHeight - (windowHeight * 0.1))
                    {
                        this.messageFrame.style.height = (windowHeight - (windowHeight * 0.3)) + "px";
                        this.messageFrame.style.overflow = "auto";
                    }
                    if(doc.sniff.IE6())
                    {
                       var topOffset = windowHeight / 2 - doc.measure.height(this.element) / 2;
                       this.element.style.top = (doc.scroll.y() + 0) + topOffset + "px";
                    }
                }
	        },
	
	messageBox : {},
	
    createLoadBox : function ()
        {
            if(this.loadBox.isSet()) return;
            doc.setFunction(window, "resize", function () { popWindow.loadBox.position(); });
            doc.setFunction(window, "scroll", function () { popWindow.loadBox.position(); });
            this.loadBox.isSet.value = true;
            this.hideControls(true);
            this.loadBox.element = document.createElement("div");
            this.loadBox.element.className = "loadBox";
            this.loadBox.messageFrame = document.createElement("div");
            this.loadBox.element.appendChild(this.loadBox.messageFrame);
            var okButton = doc.create('button');
            okButton.innerHTML = "OK";
            doc.setFunction(okButton, "click", this.destroyAll);
            this.loadBox.element.appendChild(okButton);
            doc.body().appendChild(this.loadBox.element);
            this.loadBox.position();
        },
        
    setInnerHTML : function(innerMessage)
        {
            this.loadBox.messageFrame.innerHTML = innerMessage;
            this.loadBox.position();
        },
    setClass : function(messageClass)
        {
            messageClass = messageClass.replace(/hidden\w*/, '');
            messageClass = messageClass.replace(/pop[a-zA-Z0-9_\.:-=]*/, '');
            this.loadBox.messageFrame.className = messageClass;
            this.loadBox.position();
        },
        
    hideControls : function (disable)
        {
            if(doc.sniff.IE6())
            {
                var dropDowns = doc.tag('select');
                for(var i = 0; i < dropDowns.length; i++)
                {
                    if(!this.hidden)
                    {
                        dropDowns[i].oldStyle = doc.style(dropDowns[i], "display");
                    }
                    dropDowns[i].style.display = !!disable ? "none" : dropDowns[i].oldStyle;
                }
                this.hidden = disable;
            }
        },
        
    destroyLoadBox : function ()
        {
            doc.body().removeChild(this.loadBox.element);
            this.loadBox.isSet.value = false;
        },
        
    destroyVeil : function ()
        {
            this.hideControls(false);
            doc.body().removeChild(this.veil.element);
            this.veil.isSet.value = false;
        },
        
    destroyAll : function ()
        {
            popWindow.destroyLoadBox();
            popWindow.destroyVeil();
        }
   
}