var js_popups = new Array();

var js_popup = null;

function js_Popup(id) {
    this.id = id;
    this.link = new js_PopupLink(id + "_link");
    this.location = "above_right"; // What direction relative to mouse position / link?
    this.easyClose = true;
    this.clicked = false;

    this.init = function() {
        this.myInit();
        this.locate();
    }

    this.myInit = function() {}
    
    /*
     * Locate is called with init = true to move popup near one of
     * it's links.
     */
    this.locate = function() {
    	var popup = document.getElementById(this.id);
        var link = document.getElementById(this.link.id);
		if (this.location == "below_right") {
	    	popup.style.top = (link.offsetTop + 4) + "px";
	    	popup.style.left = (link.offsetLeft + 8) + "px";
		} else if (this.location == "above_right") {
	    	//popup.style.top = (link.offsetTop - popup.clientHeight - 10 - (js_isIE() ? 4 : 10)) + "px";
	    	popup.style.bottom = "50px";
	    	popup.style.left = link.offsetLeft + "px";
		} else if (this.location == "above_left") {
	    	//popup.style.top = (link.offsetTop - popup.clientHeight - 10 - (js_isIE() ? 4 : 10)) + "px";
	    	//popup.style.left = (link.offsetLeft - popup.clientWidth) + "px";
	    	popup.style.bottom = "50px";
	    	popup.style.right = "185px";
		} else if (this.location == "below_left") {
	    	popup.style.top = (link.offsetTop + 20) + "px";
	    	popup.style.left = (link.offsetLeft - popup.offsetWidth - 2) + "px";
		} else if (this.location == "below_left_close") {
	    	popup.style.top = (link.offsetTop + 4) + "px";
	    	popup.style.left = (link.offsetLeft - popup.offsetWidth + 8) + "px";
		}
    }
    
    this.hide = function() {
        var popup = document.getElementById(this.id);
        popup.style.visibility = "hidden";
        popup.style.zIndex = -10;
    	this.link.helpText(true);  
    	this.link.styleClass(true);  
        if (js_popup == this) { 
            js_popup = null;
        }
    }
    
    this.show = function() {
        var popup = document.getElementById(this.id);
        if ((js_popup != null) && (js_popup != this) && js_popup.easyClose) {
            js_popup.hide();
        }
        popup.style.visibility = "visible";
        popup.style.zIndex = 20;
    	this.link.helpText(false);  
    	this.link.styleClass(false);  
        js_popup = this;
    }
        
    // Called when popup link clicked:
    this.update = function() {
        if (this.visible()) {
            this.hide();
        } else {
            this.locate(false);
            this.show();
        }
        this.clicked = false;
        this.myUpdate();
    }
    
    this.myUpdate = function() {}

    this.visible = function() {
    	return (document.getElementById(this.id).style.visibility == "visible");
    }
    
    this.click = function() {
		this.clicked = true;
    }   
}

function js_initPopups() {
    for (var i = 0; i < js_popups.length; i++) {
		js_popups[i].init();
    }
}

function js_onmouseup_popup(e) {
    if (js_busy) return;
    if ((js_popup != null) && js_popup.easyClose) {
        if (js_popup.clicked) {
            js_popup.clicked = false;
        } else {
            js_popup.hide();
        }
    }
}

function js_PopupLink(id) {
    this.id = id;
    this.helpText_open = "";
    this.helpText_closed = "Klik for mere info";
    
    this.helpText = function(closed) {
      	var link = document.getElementById(this.id);
      	var text = closed ? this.helpText_closed : this.helpText_open;
      	if (text != null) {
	        link.title = text;
      	}
    }
    
    this.styleClass = function(closed) {
      	var link = document.getElementById(this.id);
      	var style = closed ? "popup_link" : "active_popup_link";
        link.className = style;
    }
}