/*
 * jQuery esc_popupwin plugin
 *
 * Copyright (c) 2010 Christopher Darling (www.christopherdarling.co.uk)
 * Licensed under the GPL license and MIT:
 *   http://www.opensource.org/licenses/GPL-license.php
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id$
 * Version: 0.1
 * 
 */
 
 (function($) {
    $.fn.esc_popupwin = function(o) {
        return this.each(function() {
            new $esc_popupwin(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
    };

    /**
     * The esc_popupwin object.
     *
     * @constructor
     * @name $esc_popupwin
     * @param Object e The element to create the esc_popupwin for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/esc_popupwin
     */
    $.esc_popupwin = function(e, o) {
        this.options    = $.extend({}, defaults, o || {});
        
        this.link = $(e);
        this.popupwin;
        
        var self = this;

        self.setup();
    };

    // Create shortcut for internal use
    var $esc_popupwin = $.esc_popupwin;

    $esc_popupwin.fn = $esc_popupwin.prototype = {
        esc_popupwin: '0.1'
    };

    $esc_popupwin.fn.extend = $esc_popupwin.extend = $.extend;

    $esc_popupwin.fn.extend({
        
        /**
         * Setups the esc_popupwin.
         *
         * @name setup
         * @type undefined
         * @cat Plugins/esc_popupwin
         */
        setup: function() {
            var self = this;
            
            self.popupwin = $('#popupwinframe');
            if( self.popupwin.length == 0 ) {
                $('body').append("<div id=\"popupwinframe\"></div>");
                self.popupwin = $('#popupwinframe');
            }
            //self.popupwin.hide();
            
            self.link.click(function() {
                self.popupwin.hide();
                var link_url = $(this).attr("href");
                $.ajax({
                    type: "GET",
                    url: link_url,
                    dataType: "html",
                    cache: true,
                    success: function(html) {
                        self.popupwin
                            .html(html);
                            /*
                            .bind("focusout blur", function() { 
                                console.log("Focus out");
                                self.closeWindow(); 
                            });
                            */
                        self.showWindow();
                        self.applyJS();
                        
                    }
                });
                
                return false;
            });
        },
        
        applyJS: function() {
            var self = this;
                        
            $('#popupwinframe a.btn-close').click(function() { 
                self.closeWindow(); 
                return false; 
            });
            $('#popupwinframe .Actions > a').click(function() { 
                self.closeWindow(); 
                return false; 
            });
            
            if( $.browser.msie && $.browser.version.substr(0,1) < 8 ) {

            } else {
                // not IE6
                
                // Replace the checkboxes
                // $('#popupwinframe input[type=checkbox]').cd_checkbox();
                
                // Replace the select boxes
            	if (jQuery.isFunction(jQuery.fn.selectbox)) {
            		$('#popupwinframe select').selectbox();
            	}
            	$('#popupwinframe form input[type=password], #popupwinframe form input[type=text], #popupwinframe form textarea').watermark();
                
                if( !self.popupwin.children('.popupwin').hasClass('RegisterPage') 
                	&& !self.popupwin.children('.popupwin').hasClass('AccountsettingsPage') 
                	&& !self.popupwin.children('.popupwin').hasClass('UniversityToursPage') 
                    && !self.popupwin.children('.popupwin').hasClass('LoginForm') ) {
                       // alert("here");
                    $('#popupwinframe .Actions input[type=submit]').click(function() {
                        var action = $('#popupwinframe form').attr("action");
                        $.ajax({
                            type: "POST",
                            url: action,
                            data: $('#popupwinframe form').serialize(),
                            dataType: "html",
                            cache: false,
                            success: function(html) {
                            	$(html).filter('script').each(function(){
						            $.globalEval(this.text || this.textContent || this.innerHTML || '');
						        });

                                if( $('#popupwinframe > .popupwin').hasClass('popupwin-large') ) {
                                	
                                	if($(html).hasClass('popupwin-large')) {
                                		self.popupwin
		                                    .html(html);
                                	} else {
                                		self.popupwin.find('.popupwin-content > div')
	                                    	.html(html);
                                	}
                                } else {
	                                self.popupwin.find('.popupwin-content > div')
	                                    .html(html);
                                }
                                
                                self.applyJS();
                            }
                        });
                        return false;
                    });
                }
    
                $('#popupwinframe a.popupwinlink').esc_popupwin();
            }
        },
        
        closeWindow: function() {
            var self = this;
            self.popupwin.fadeOut(500).html("");
        },
        showWindow: function() {
            var self = this;
            var win = $(window);
            self.popupwin.css({
                "left": ((win.width() - self.popupwin.width()) / 2),
                "top" : ((win.height() - self.popupwin.height()) / 2)
            }).fadeIn(500);
        },
        formAjaxSubmit: function(_clicked) {
            var self = this;
            var action = $('#popupwinframe form').attr("action");
            $.ajax({
                type: "POST",
                url: action,
                data: $('#popupwinframe form').serialize(),
                dataType: "html",
                cache: false,
                success: function(html) {
                    var container = '.popupwin-content';
                    if( $('#popupwinframe > .popupwin').hasClass('popupwin-large') ) {
                        container = '.popupwin-content > div';
                    }
                    self.popupwin.find(container)
                        .html(html);
                    self.applyJS();
                }
            });
        }
    });

    $esc_popupwin.extend({
        /**
         * Gets/Sets the global default configuration properties.
         *
         * @name defaults
         * @descr Gets/Sets the global default configuration properties.
         * @type Hash
         * @param Hash d A set of key/value pairs to set as configuration properties.
         * @cat Plugins/esc_popupwin
         */
        defaults: function(d) {
            return $.extend(defaults, d || {});
        }
    });

})(jQuery);

