/**
* Layer PopUp / Windows PopUp
*
* @example
* var opts  = {'url' : 'index.html', 'width' : '600px', 'height' : '500px', 'left': '100px' , 'top' : '300px', 'type' : 'layer' };
* $.popup(opts);
* $('.layerpopup').popup(opts);
*/

(function($){
$.fn.popup = function(options) {
    var popup, popup_options;
    var $this = $(this);
    var opts  = $.extend({},$.fn.popup.defaults, options);

	if (opts.url != '') {
		if(opts.center) {
			opts.top  = $(window).height()/2- opts.height.replace('px','')/2;
			opts.left = $(window).width()/2 - opts.width.replace('px','')/2;
		}
		if( opts.type == 'browser' ) {
			popup_options = "width=" + opts.width + ",height=" + opts.height;
			popup_options += ",left=" + opts.left + ",top=" + opts.top;
			popup_options += ",scrollbars="  + opts.scrollbars  + ",toolbar=" + opts.toolbar + ",menubars=" + opts.menubars;
			popup_options += ",locationbar=" + opts.locationbar + ",statusbar=" + opts.statusbar;
			popup_options += ",resizable="   + opts.resizable ;
			popup_options += ",titlebar=" + opts.titlebar;
		} else {
			popup =  $("<div id='"+ opts.id + "'></div>").css({
				'display' : 'none',
				'position': 'absolute',
				'z-index' : '9000',
				'width'   :  opts.width,
				'height'  :  opts.height,
				'left'    :  opts.left,
				'top'     :  opts.top
			});
			$.get(opts.url, function(strResult){
				if (strResult != ''){
					$(popup).html(strResult);
				}
			});
			$("body").append(popup);
		}
	}

    var methods = {
        'open': function() {
            if( opts.type == 'browser') {
                popup = window.open(opts.url, opts.id, popup_options);
                if(!popup) {
                    alert(opts.message);
                    return false;
                }else {
                    popup.focus();
                }
            } else {
                popup.show();
           }
        },
        'close': function() {
            if( opts.type == 'browser' ) {
                popup.close();
            } else {
				$('#'+opts.id).remove();
            }
        },
        'getPopup' : function() {
            return pupup;
        }
    };
    return methods;
}

$.fn.popup.defaults = {
    'url'    : '',
    'type'   : 'layer',
    'id'   : 'popup',
    'width'  : '300px',
    'height' : '300px',
    'scrollbars' : 'yes',
    'toolbar' : 'no',
    'menubars' : 'no',
    'locationbar' : 'no',
    'statusbar'   : 'no',
    'resizable'   : 'no',
    'titlebar'    : 'no',
    'left'    : '0px',
    'top'     : '0px',
    'message' : POPUP_BLOCK_MSG,
    'center'  : true
};

$.extend({
    popup : function (options) {
        return $.fn.popup(options);
    }
});

})(jQuery);