/*
	Dependencies:
--------------------------------------------
	wun.common.js
	wun.shadow.js
	jQuery 1.3.2 (or later - http://jquery.com/)
*/

var undefined;

var wun = (wun == undefined) ? {} : wun;

/* 	
	wun.popup css sample:
--------------------------------------------	

	.wunPopup {
	    background-color: #fff
	    border: 1px solid #6d6d6d;
	    width: 550px;
	    z-index: 26;
	}
	
	.wunPopup .wunPopupContent {
		padding: 50px 30px;
	}
	
	.wunPopup .wunPopupButton {
		background-image: url(../images/popup_close.gif);    
		background-color: #000;
	    background-repeat: no-repeat;
	    background-position: center center;
	    cursor: pointer;
	    height: 39px;
	    left: 458px;
	    position: absolute;
	    top: -9px;
	    width: 102px;
	}
*/

wun.popup = function(options) {

    this.defaultOptions = {
        isModal: false,
        useShadow: true,
        scrolling: false,
        fit: false,
        fixed: true,
        valign: undefined,
        align: undefined,
        cssClass: 'wunPopup',
        clearOnHide: false
    };

    this.elements = {};
    this.events = {};
    this.options = {};

    this.create = function() {

        if (wun.shadow != undefined) {
            this.shadow = new wun.shadow();

            jQuery(this.shadow.elements.div).bind('click', { sender: this }, function(event) {
                if (!event.data.sender.options.isModal) {
                    event.data.sender.hide();
                }
            });
        }

        this.elements.popup = jQuery('<div></div>')
        	.addClass(this.options.cssClass)
        	.css('position', this.isIE6() ? 'absolute' : 'fixed');

        this.elements.div1 = jQuery('<div></div>')
        	.addClass('wunWrap1');

        this.elements.div2 = jQuery('<div></div>')
        	.addClass('wunWrap2');

        this.elements.div3 = jQuery('<div></div>')
        	.addClass('wunWrap3');

        this.elements.btn = jQuery('<div></div>')
        	.addClass('wunPopupButton')
        	.css('position', 'absolute');

        this.elements.reldiv = jQuery('<div></div>')
        	.css('position', 'relative');

        this.elements.frame = jQuery('<iframe frameborder="0"></iframe>')
        	.attr("allowTransparency", "true")
        	.css('width', '100%')
        	.css('height', '100%');


        this.elements.flash = jQuery('<div></div>');
        this.elements.content = jQuery('<div></div>');
        this.elements.domAppend = jQuery('<div></div>');
        this.elements.domImport = jQuery('<div></div>');


        this.elements.reldiv.append(this.elements.btn);
        this.elements.popup.append(this.elements.reldiv);

        this.elements.div2.append(this.elements.div3);
        this.elements.div1.append(this.elements.div2);
        this.elements.popup.append(this.elements.div1);

        this.hide();

        if (jQuery('#aspnetForm').html() != null)
            jQuery('#aspnetForm').append(this.elements.popup);
        else
            jQuery(document.body).append(this.elements.popup);

        jQuery(this.elements.btn).bind('click', { sender: this }, function(event) {
            event.data.sender.hide();
        });
    };

    this.isIE6 = function() {
        return (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7);
    };

    //show popup content as an iframe
    this.setSource = function(url) {
        var now = new Date();
        this.setChild(this.elements.frame);
        this.elements.frame.scrolling = this.options.scrolling;
        this.elements.frame.attr('src', (url.indexOf("?") == -1 ? url + "?nocache=" + now.getTime() : url + "&nocache=" + now.getTime()));
        this.elements.frame.show();
        this.position();
    };

    //show popup content as an image
    this.setImage = function(url) {
        this.setChild(this.elements.content);
        this.elements.content.css('overflow', (this.options.scrolling ? 'auto' : 'visible'));
        this.elements.content.html(jQuery('<img></img>').attr('src', url));

        jQuery(this.elements.content.children().first()).bind('load', { sender: this }, function(event) {
            event.data.sender.position();
        });
    };

    //show popup content by embedding a flash through swfobject
    this.setFlash = function() {
        var now = new Date();
        var id = now.getTime();
        this.setChild(this.elements.flash);
        this.elements.flash.append(jQuery('<div></div>').attr('id', id));
        swfobject.embedSWF(arguments[0], id, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
        this.position();
    };

    //show popup content by setting the innerHTML of the popup
    this.setContent = function(content) {
        this.setChild(this.elements.content);
        this.elements.content.css('overflow', (this.options.scrolling ? 'auto' : 'visible'));
        this.elements.content.html(content);
        this.position();
    };

    //show popup content by moving an existing html element into the popup
    this.append = function(jQueryElem) {
        jQuery(this.elements.domAppend).append(jQueryElem);
        this.setChild(this.elements.domAppend);
        this.position();
    };

    //show popup content by cloning an existing html element into the popup
    this.importElement = function(element, newId) {
        domElement = (typeof element == 'string') ? document.getElementById(element) : element;
        this.elements.domImport = domElement.cloneNode(true);
        if (newId != undefined) this.elements.domImport.id = newId;
        this.setChild(this.elements.domImport);
        this.position();
    };

    /*
    options {
        align:          (string),
        cssClass:       (string),
        fitToWindow:    (boolean),
        fixed:          (boolean),
        scrolling:      (string),
        valign:         (string),
        useShadow:      (boolean),
        zIndex:         (int),
        clearOnHide:    (boolean)
    }
    */
    this.applyOptions = function(options) {
        if (options != undefined) {
            this.options.fit = (options.fit != undefined) ? options.fit : this.defaultOptions.fit;
            this.options.scrolling = (options.scrolling != undefined) ? options.scrolling : this.defaultOptions.scrolling;
            this.options.align = (options.align != undefined) ? options.align : this.defaultOptions.align;
            this.options.valign = (options.valign != undefined) ? options.valign : this.defaultOptions.valign;
            this.options.fixed = (options.fixed != undefined) ? options.fixed : this.defaultOptions.fixed;
            this.options.useShadow = (options.useShadow != undefined) ? options.useShadow : this.defaultOptions.useShadow;
            this.options.isModal = (options.isModal != undefined) ? options.isModal : this.defaultOptions.isModal;
            this.options.cssClass = (options.cssClass != undefined) ? options.cssClass : this.defaultOptions.cssClass;
            this.options.clearOnHide = (options.clearOnHide != undefined) ? options.clearOnHide : this.defaultOptions.clearOnHide;
            
            if (options.zIndex != undefined) this.setDepth(options.zIndex);
        }
    };

    this.isVisible = function() {
        return this.elements.popup.is(':visible');
    };

    this.show = function() {
        this.elements.popup.show();
        this.position();

        if (this.options.useShadow)
            this.shadow.show();

        /*jQuery(window).bind('scroll', { sender: this }, function(event) {
        if ( event.data.sender.isVisible() && event.data.sender.isIE6() )
        event.data.sender.position();
        });*/

        jQuery(window).bind('resize', { sender: this }, function(event) {
            if (event.data.sender.isVisible()) {
                event.data.sender.position();
            }
        });
    },

    this.hide = function() {
        if (this.events.onClose != undefined) this.events.onClose();
        this.elements.popup.hide();
        this.elements.flash.empty();
        this.elements.frame.attr('src', '');
        this.shadow.hide();
    };

    this.toggle = function() {
        if (this.isVisible())
            this.hide();
        else
            this.show();
    };

    this.setOnClick = function(fn) {
        this.elements.popup.click(fn);
    };

    this.setOnPosition = function(fn) {
        this.events.onPosition = fn;
    };

    this.setOnClose = function(fn) {
        this.events.onClose = fn;
    };

    this.setChild = function(elem) {
        if (this.activeChild != undefined)
            this.activeChild.hide();

        this.elements.div3.append(elem.show());
        this.activeChild = elem;
    };

    this.setDepth = function(index) {
        this.elements.popup.css('z-index', (typeof index == 'string' ? index : index.toString()));
    };

    this.position = function() {
        if (this.options.fit)
            this.activeChild.css('height', document.body.parentNode.offsetHeight * 0.7 + 'px');

        if (this.options.fixed) {

            scrolled = wun.common.getScrolled();
            bodyNode = document.body;
            htmlNode = bodyNode.parentNode;

            if (this.isIE6()) {


                switch (this.valign) {
                    case "top":
                        this.elements.popup.css('top', scrolled.y + 'px').css('bottom', '');
                        break;
                    case "middle":
                        this.elements.popup.css('top', scrolled.y + (((htmlNode.offsetHeight / 2) - (this.elements.popup.height() / 2))) + 'px').css('bottom', '');
                        break;
                    case "bottom":
                        this.elements.popup.css('bottom', (0 - scrolled.y) + 'px').css('top', '');
                        break;
                    default:
                        this.elements.popup.css('top', scrolled.y + (((htmlNode.offsetHeight / 2) - (this.elements.popup.height() / 2))) + 'px').css('bottom', '');
                }
            }
            else {

                switch (this.valign) {
                    case "top":
                        this.elements.popup.css('top', '0px').css('bottom', '');
                        break;
                    case "middle":
                        this.elements.popup.css('top', Math.floor((htmlNode.offsetHeight - this.elements.popup.height()) / 2) + 'px').css('bottom', '');
                        break;
                    case "bottom":
                        this.elements.popup.css('bottom', '0px').css('top', '');
                        break;
                    default:
                        this.elements.popup.css('top', Math.floor((htmlNode.offsetHeight - this.elements.popup.height()) / 2) + 'px').css('bottom', '');
                }
            }

            switch (this.align) {
                case "left":
                    this.elements.popup.css('left', '0px').css('right', '');
                    break;
                case "center":
                    this.elements.popup.css('left', Math.floor((bodyNode.offsetWidth / 2) - (this.elements.popup.width() / 2)) + 'px').css('right', '');
                    break;
                case "right":
                    this.elements.popup.css('right', '0px').css('left', '');
                    break;
                default:
                    this.elements.popup.css('left', Math.floor((bodyNode.offsetWidth / 2) - (this.elements.popup.width() / 2)) + 'px').css('right', '');
            }
        }

        if (this.events.onPosition != undefined) this.events.onPosition();
    };

    if (options != undefined)
        this.applyOptions(options);
    else
        this.applyOptions(this.defaultOptions);

    this.create();
};
