/**
 * pop up dialong Class
 * 
 * @author bli
 */
function Popup() {
};
Popup.prototype = new Object();
Popup.prototype.divId = null;
Popup.prototype.title = null;
Popup.prototype.popupOptions = {};
Popup.prototype.ajaxOptions = {};
Popup.prototype.openedDialog = null;
Popup.prototype.isTempDiv = false;
Popup.opened = false;
Popup.divId=null;
Popup.prototype.openPopupIml = function() {    
	Popup.opened = true;	
	$("#" + this.divId).dialog(this.popupOptions);
	
};

Popup.prototype.close = function() {
	if (this.isTempDiv) {	    
	    if (BrowserDetect.browser == "Explorer")
	    {
	    	document.getElementById(this.divId).removeNode(true);  
	    }
	    else
	    {
	    	$("#" + this.divId).remove();
	    }
	    $("#" + this.divId).dialog('destroy');	    
	}
	$("#" + this.divId).dialog('close');
};

Popup.prototype.openPopup = function() {
	/*
	if(Popup.opened)
	{
		Popup.closePoup();
	}
	*/
	if (this.divId == null && !Popup.opened) {
		this.popupOptions['dialogClass'] = 'alert';
		var openDivId = guid();
		$("<div title='" + this.title + "' height='auto' id='" + openDivId + "'>" + "</div>").appendTo("body");
		this.divId = openDivId;
		Popup.divId = openDivId;
		this.isTempDiv = true;
	}else
	{
		this.divId=Popup.divId;
		this.isTempDiv = true;
	}
	if (this.ajaxOptions != null) {
		var thisPopup = this;
		this.ajaxOptions['success'] = function(data) {
			//$("#" + thisPopup.divId).html(data);
			document.getElementById(thisPopup.divId).innerHTML=data;
			thisPopup.openPopupIml();
			//setProductSlider(); 
			realignQuickView();
		};
		$.ajax(this.ajaxOptions);
	} else {
		this.openPopupIml();
	}
};

// -------------------- static methods --------------------------------
/**
 * Open a dialog, the content of dialog is a response via a ajax request.
 * 
 * @param url
 *            ajax url
 * @param data
 *            ajax param
 * @param afterOpen
 *            dialong opened callback
 */
Popup.openPopupAjax = function(url, data, afterOpen) {
	var popupOptions = {
		modal : false,
		width : 600,
		height : 'auto',
		resizable : false,
		autoOpen:true, 
		open : afterOpen
		

	};
	var ajaxOptions = {
		url : url,
		data : data
	};
	Popup.openOnePopup(ajaxOptions, popupOptions, null, null);
};

Popup.openPopupDiv = function(divId) {
	var popupOptions = {
		modal : true,
		width : 'auto'
	};
	Popup.openOnePopup(null, popupOptions, null, divId);
};

/**
 * @param ajaxOptions
 *            if you want to open a popup with a ajax response, specify the
 *            param as ajax option. else you can specify it to null
 * @param popupOptions
 *            the popup option, you can see the jquery-ui-dialog reference.
 * @param title
 *            this param is not use now, just set it null.
 * @param divId
 *            if you want to specify a div to popup, specify it. if you want
 *            creat a div automatic, set it to null
 */
Popup.openOnePopup = function(ajaxOptions, popupOptions, title, divId) {
	var pop = new Popup();
	pop.ajaxOptions = ajaxOptions;
	pop.popupOptions = popupOptions;
	pop.divId = divId;
	pop.title = title;
	pop.openPopup();
	Popup.popStack.push(pop);
};

Popup.closePoup = function() {	
	var pop = Popup.popStack.pop();
	if(pop!=null)
	{
		Popup.opened=false;
		pop.close();		
	}
};

Popup.popStack = new Array();

