var _popup = false;
jQuery(window).load(function() {     
    _popup = new wttPopup();
});

function wttPopup(container) {
    this.container = (container) ? container : '#popup';
    this.hide = function() {
        jQuery(this.container).fadeOut();
    };
    this.show = function() {
        jQuery(this.container).fadeIn();
    };
    this.init = function() {
        this.hide(); 
        jQuery('a.popup').each(function() {
            jQuery(this).click(function() {
                _popup.loadDiv(jQuery(this).attr('href'));
                return false;
            });
        });
        jQuery('a.popup-ajax').each(function() {          
            jQuery(this).click(function() {
                _popup.loadUrl(jQuery(this).attr('href'), 'html');     
                return false;
            });
        });
        jQuery('a.closepopup').each(function() {
            jQuery(this).click(function () {
                _popup.hide();
                return false;
            });
        });
    }; 
    this.setHTML = function(html) {
        jQuery(this.container + '-content').html(html);
    }
    this.loadDiv = function(what) {
        this.loading();
        this.setHTML(jQuery(what).html());
        this.show();
    };                                        
    this.loadUrl = function(url, type) {     
        this.loading();
        type = (type) ? type : 'html';                       
        jQuery.ajax({
            url : url,
            success : function (res) {
                if(type == 'html') {             
                    _popup.setHTML(res);
                }
            }
        });
        this.show();
    };
    this.loadForm = function(form, type) {
        this.loading(); 
        type = (type) ? type : 'html';                                   
        jQuery.ajax({
            url : jQuery(form).attr('action'),
            type : 'POST',
            data : jQuery(form).serialize(),
            dataType : type,
            success : function (res) {
                if(type == 'html') {
                    _popup.setHTML(res);
                }
            }
        });        
    };
    this.loading = function() {
        this.setHTML('<div class="header">Loading</div><div class="content">Loading your content, one moment please...</div>');
    }
    this.init();
}
