/*
    Dependencies:
--------------------------------------------
    wun.common.js
    wun.shadow.js
*/

var undefined;

var wun = ( wun == undefined ) ? {} : wun;

/* 	
	wun.popup CSS:
--------------------------------------------	
	.wunPopup 
	{
		background-color: #ffffff;
		display: none;
		height: 300px;
		width: 400px;
		z-index: 26;
		position: fixed;
	    !_position: absolute;
	}
	
	.wunPopupButton 
	{
		background-image: url(../images/popup_close.gif);
		background-repeat: no-repeat;
		cursor: pointer;
		height: 24px;
		left:400px;
		width: 27px;
		position: absolute;
	}
*/

wun.popup = {
    popup: undefined,
    reldiv: undefined,
    btn: undefined,
    frame: undefined,
    image: undefined,
    fit: false,
    scrolling: 'no',
    create: function() {
        wun.shadow.create();
        //wun.common.windowEvents.addEvent();

        this.popup = document.createElement('div');
        this.btn = document.createElement('div');
        this.reldiv = document.createElement('div');
        this.frame = document.createElement('iframe');

        this.popup.className = "wunShadowPopup";
        this.btn.className = "wunPopupButton";
        this.reldiv.setAttribute("style", "position: relative;");

        this.frame.setAttribute("allowTransparency", "true");
        this.frame.style.width = "100%";
        this.frame.style.height = "100%";
        this.frame.className = "wunPopupFrame";
        this.frame.frameBorder = 0;
        //this.frame.setAttribute("scrolling", "no");

        this.reldiv.appendChild(this.btn);
        this.popup.appendChild(this.reldiv);
        this.popup.appendChild(this.frame);
        wun.common.getBodyNode().appendChild(this.popup);

        this.btn.onclick = function() { wun.popup.hide(); }
    },
    
    initiated: function() {
        if (this.popup == undefined) this.create();
        return this.popup != undefined;
    },


    //params:  url(string), options { fitToWindow:(boolean), scrolling:(string)}
    setSource: function(url) {
        if (this.initiated()) {
            this.popup.appendChild(this.frame);

            if (arguments[1] != undefined) {
                if (arguments[1].fitToWindow != undefined) this.fit = arguments[1].fitToWindow;
                if (arguments[1].scrolling != undefined) this.scrolling = arguments[1].scrolling;
            }

            var now = new Date();
            this.frame.scrolling = this.scrolling;
            this.frame.src = url.indexOf("?") == -1 ? url + "?nocache=" + now.getTime() : url + "&nocache=" + now.getTime();
        }
    },

    //params: content(string), fitToWindow(boolean-optional)
    setContent: function(content) {
        if (this.initiated()) {
            this.popup.innerHTML = "<div class='wunShadowPopupText'>" + content + "</div>";
            if (arguments[1] != undefined) this.fit = arguments[1];
        }
    },
    
    //params: url(string), height(int), width(int), fitToWindow(boolean-optional)
    setImage: function(url) {
        if (this.initiated()) {
            this.popup.innerHTML = "";
            this.image = document.createElement('img');
            this.image.src = url;
            this.popup.appendChild(this.image);
            if (arguments[1] != undefined) this.image.height = arguments[1];
            if (arguments[2] != undefined) this.image.width = arguments[2];
            if (arguments[3] != undefined) this.fit = arguments[3];
            
            this.position();
        }
    },

    setOnclick: function(func) {
        if (this.initiated()) this.popup.onclick = func;
    },

    visible: function() {
        if (this.initiated()) {
            return (this.popup.display != 'none');
            this.showOrHideAllDropDowns('visible');
        }
    },

    show: function() {
        if (this.initiated()) {
            this.popup.style.display = 'block';
            this.showOrHideAllDropDowns('hidden');
            this.position();
            window.onscroll = function() { wun.popup.position() };
        }
    },

    hide: function() {
        if (this.initiated()) {
            this.popup.style.display = 'none';
            this.showOrHideAllDropDowns('visible');
            this.frame.src = '';
            window.onscroll = function() { };
        }
    },
    
    toggle: function() {
        if (this.initiated()) {

            if (this.visible())
                this.hide();
            else
                this.show();
        }
    },
    
    showOrHideAllDropDowns: function(newState) {
        
        var elements = document.documentElement.getElementsByTagName('select');
     
        for (var i=0; i<elements.length; i++) {
            elements[i].style.visibility = newState;
        }
    },

    position: function() {
        if (this.fit)
            this.popup.style.height = wun.common.getHtmlNode().offsetHeight * 0.7 + "px";
        this.popup.style.top = wun.common.getScrolled().y + (((wun.common.getHtmlNode().offsetHeight / 2) - (this.popup.offsetHeight / 2))) + "px";
        this.popup.style.left = ((wun.common.getBodyNode().offsetWidth / 2) - (this.popup.offsetWidth / 2)) + "px";

        /*
        this.popup.style.height = wun.common.getHtmlNode().offsetHeight * 0.7 + "px";
        this.popup.style.top = ((wun.common.getHtmlNode().offsetHeight / 2) - (this.popup.offsetHeight / 2)) + "px";
        this.popup.style.left = ((wun.common.getBodyNode().offsetWidth / 2) - (this.popup.offsetWidth / 2)) + "px";*/
    }
};
