var plg_popup = new function(){

	var $_objects = null;

	this.headerTPL = $('<div class="head oo"><a href="#" class="btn_close jQ_close"></a><h2></h2></div>');
	this.popupTPL = $('<div class="pop_bg pngfix"><a href="#" class="btn_close jQ_close"></a><div class="pop_bg_a pngfix"><div class="content clearfix"></div></div></div>');
	this.buttonTPL = $('<div class="clearfix mt10"><button type="submit" class="btn btn_d fr" value=""><font><span style="padding:0 26px 0 20px"></span></font></button></div>');

	this.preparePopup = function(){
		//return $('<div class="popup"><div class="top"></div><div class="content oo"></div><div class="bottom"></div></div>');
		return this.popupTPL.clone();
	};

	var visible = $([]);

	var timeoutId;

	this.getVisible = function(){
		return visible;
	};

	this.setContent = function(popup,content, replace){
		
		var mode = replace==true ? 'html':'append';
		
		if(popup.find(".content:first-child").length > 0){
			if(popup.find(".content .head.oo").length > 0){
				if(popup.find(".content .head + *").length > 0){
					popup.find(".content .head + *").eq(0).replaceWith(content);
				} else {
					if(popup.find(".content ~ *").length > 0){
						popup.find(".content:first-child").replaceWith(content);
					} else {
						popup.find(".content:first-child")[mode](content);
					}
				}
			} else {
				popup.find(".content")[mode](content);
			}
		} else {
			popup.find(".content")[mode](content);
		}
		return popup;
	};

	this.removeContent = function(popup) {
		popup.find(".content *").remove();
		return popup;
	}

	this.setTitle = function(popup,content){
		popup = $(popup);
		if(popup.find(".head h2").length > 0){
			popup.find(".head h2").html(content);
		} else {
			popup.find('.content').prepend(this.headerTPL.clone()).find('h2').html(content);
		}
		return popup;
	};

	this.delButton = function(popup,index){
		popup = $(popup);
		var buttons = popup.find('.content.oo button');

		if(index != undefined){
			buttons.eq(index).remove();
		} else {
			buttons.remove();
		}
	}

	this.addButton = function(popup,text,click){
		popup = $(popup);

			var btn = this.buttonTPL.clone().click(click);
				btn.find('span').html(text);

			popup.find('.content').append(btn);
		return popup;
	};

	this.show = function(popup, timeout, onHideCallback ){
		$.unblockUI();
		$.blockUI( {message: popup} );
		$(popup).find('.jQ_close').unbind('click').click(plg_popup.hide);
		var top = parseInt ( ( parseInt( screen.height ) - parseInt( $(popup).get(0).offsetHeight )) / 3 );
		$(".blockMsg").css({border:'0px', left:'400px', top: top+'px', cursor:'default'});
		$_objects = $('object, embed');

		if( $_objects !== null )
			$_objects.css({visibility:'hidden'});

		visible = $(popup);
		
		if( ( timeout = parseInt(timeout) ) > 0 ){
			this.hide( onHideCallback, timeout );
		}
	};

	this.hide = function( callback, delay ){
		
		if( ( delay = parseInt(delay) ) > 0 ){
			var self = this;
			timeoutId = window.setTimeout(function(){
				self.hide(callback);
			}, delay);
			return;
		}
		
		$.unblockUI();

		if( $_objects !== null )
			$_objects.css({visibility:'visible'});

		$_objects = null;

		if( typeof callback == 'function' ){
			callback();
		}

		$(window).trigger('plg_popup_hide');
		
		$(plg_popup).trigger('-onPopupHide');

		if( timeoutId != null ){
			window.clearTimeout(timeoutId);
			timeoutId = null;
		}

		visible = $([]);

	return false;
	};

	this.showConfirm = function(title,text,yesText,yesCallback,noText,noCallback){
		var confirm = this.preparePopup();
		this.setTitle(confirm,title);
		this.setContent(confirm,text);
		this.addButton(confirm,noText,noCallback);
		this.addButton(confirm,yesText,yesCallback);
		this.show(confirm);
	return confirm;
	}

	$.blockUI.defaults.fadeOut = false;
	$.extend( $.blockUI.defaults.overlayCSS, {backgroundColor: '#000', cursor:'default'} );

}

