/*
	Dependencies:
--------------------------------------------
	jQuery 1.3.2 (or later - http://jquery.com/)
	wun.shadow.js [optional]
*/

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) {
    
    //default options
    this.options = {
        align: undefined,
        cssClass: 'wunPopup',
        fit: false,
        fixed: true,
        isModal: false,
        scrolling: false,
        useShadow: true,
        valign: undefined,
        zIndex: undefined
    };

    this.elements = {};
    this.events = {};

    this.create = function() {
        
        //modify viewport as required
        jQuery(document.body).css('height', 'auto').parent().css('height', '100%');
        
        //set options
        if (options != undefined) jQuery.extend(this.options, options);
        
        //set shadow object if not undefined
        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();
                }
            });
        }
        
        //create popup elements and attach to html document
        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>');

        if (!this.options.isModal) {
            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();
        });
        
        //set z-index
        this.setDepth();
    };

    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.empty().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();
    };

    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() {
        this.elements.popup.hide();
        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.setChild = function(elem) {
        if (this.activeChild != undefined)
            this.activeChild.hide();

        this.elements.div3.append(elem.show());
        this.activeChild = elem;
    };

    this.setDepth = function() {
        if (this.options.zIndex != undefined) {
            this.elements.popup.css('z-index', (typeof this.options.zIndex == 'string' ? this.options.zIndex : this.options.zIndex.toString()));
        }
    };
    
    this.getScrolled = function() {
        // chrome : this.getBodyNode().scrollTop/scrollLeft
        // others : this.getHtmlNode().scrollTop/scrollLeft
        
        return {
            x: (document.body.scrollLeft != 0) ? document.body.scrollLeft : document.body.parentNode.scrollLeft,
            y: (document.body.scrollTop != 0) ? document.body.scrollTop : document.body.parentNode.scrollTop
        };
    };

    this.position = function() {
        if (this.options.fit)
            this.activeChild.css('height', document.body.parentNode.offsetHeight * 0.7 + 'px');

        if (this.options.fixed) {

            var scrolled = this.getScrolled();
            var bodyNode = document.body;
            var 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();
    };

    this.create();
};
