/*
	Popup helper functions
*/

function initializeEnlargePopupClose() {
	var enlargePopupClose = jQuery("#enlargePopupClose");
	var enlargePopupBackground = jQuery("#enlargePopupModalBackground");
	
	enlargePopupClose.click(function() {
		enlargePopupBackground.click();
	});
	
	repositionEnlargePopupClose();
	showEnlargePopupClose();
}

function showEnlargePopupClose() {
	var enlargePopupClose = jQuery("#enlargePopupClose");
	enlargePopupClose.fadeIn("slow");
}

function hideEnlargePopupClose() {
	var enlargePopupClose = jQuery("#enlargePopupClose");
	enlargePopupClose.fadeOut("slow");
}

function repositionEnlargePopupClose() {
	var enlargePopupMain = jQuery("#enlargePopupMain");
	var enlargePopupClose = jQuery("#enlargePopupClose");

	var mainTop = enlargePopupMain.css("top");
	var mainLeft = enlargePopupMain.css("left");
	var mainWidth = enlargePopupMain.css("width");
	
	var top = new Number(mainTop.replace("px", ""));
	var left = new Number(mainLeft.replace("px", ""));
	var width = new Number(mainWidth.replace("px", ""));
	
	enlargePopupClose.css({
		top: (top - 15) + "px",
		left: (left + width - 15) + "px"
	});
}

function openQuickviewPopup(url) {
	qvPopup = new Popup(
		"quickview",
		"qvPopupMain", 
		url, 
		true, 
		true,
		"qvPopupModalBackground",
		null,
		function() {
			reloadScript(fluidScriptLocation);
		},
		null,
		false,
		null
	);
	qvPopup.open();
}

function openQuickview(url) {
	document.location = url;
}

/*
 * Javascript Popup Class
 *
 * Dependency: JQuery 1.4.2
 * 
 */


// popupId - the ID of the popup (used for popup registration and retrieval)
// mainDivId - the main popup div that will contain the contents
// contentURL - the target of the AJAX call that will produce the popup content
// fadeIn - if set to true, the popup will shown by fading to opaque
// fadeOut - if set to true, the popup will closed by fading to transparent
// modalBackgroundDivId (optional) - the background of the modal popup
// openCallback (optional) - a function that is executed after the popup is opened
// closeCallback (optional) - a function that is executed after the popup is closed
// hideCallback (optional) - a function that is executed after the popup is hidden
// hideIfBackgroundClicked - if set to true, clicking the background will just hide the popup instead of closing it
// resizeCallback (optional) - a function that is executed after the window is resized or scrolled
function Popup(popupId, mainDivId, contentURL, fadeIn, fadeOut, modalBackgroundDivId, openCallback, closeCallback, hideCallback, hideOnBgClick, resizeCallback) {

	this.id = popupId;
	
	this.main = {
		id: mainDivId,
		div: null
	};
	
	this.background = {
		id: modalBackgroundDivId,
		div: null,
		opacity: null,
		filter: null
	};
	
	this.hideOnBgClick = hideOnBgClick;
	
	this.contentURL = contentURL;
	
	this.behavior = {
		openCallback: openCallback,
		closeCallback: closeCallback,
		hideCallback: hideCallback,
		resizeCallback: resizeCallback,
		fadeIn: fadeIn,
		fadeOut: fadeOut
	};

	//Resizes the background to cover the entire browser client window.
	this.setBackgroundToFullScreen = function() {
		if (this.background.div) {
			this.background.div.css({
				top: getTop() + "px",
				left: getLeft() + "px",
				height: "100%",
				width: "100%"
			});
		}
	};

	// Sets the background div's opacity to it's original settings.
	this.resetBackgroundOpacity = function() {
		this.background.div.css({
			opacity: this.background.opacity,
			filter: this.background.filter
		});	
	};

	// Resizes the popup.  Used in the window resize and scroll events.
	this.resizePopup = function() {
		this.main.div.center();
		this.setBackgroundToFullScreen();
		if (this.behavior.resizeCallback){
			this.behavior.resizeCallback();
		}
	};	
	
	// Set main popup div
	this.main.div = jQuery("#" + mainDivId);
	this.main.div.center();
	this.setContent("");

	// Set background div
	if (modalBackgroundDivId) {
		this.background.id = modalBackgroundDivId;
		this.background.div = jQuery("#" + modalBackgroundDivId);
		this.background.opacity = this.background.div.css("opacity");
		this.background.filter = this.background.div.css("filter");
		this.background.div.click(
			jQuery.proxy(this.closeOrHide, this)
		);
		this.setBackgroundToFullScreen();
	}

	// Set event handlers
	jQuery(window).bind("resize", jQuery.proxy(this, "resizePopup"));
	jQuery(window).bind("scroll", jQuery.proxy(this, "resizePopup"));
	
} // end Popup class

// Opens a modal popup with the contents taken from the given URL.
Popup.prototype.open = function() {
	if (!this.hideOnBgClick) {
		// Place popup at the top of the popup stack
		pushPopup(this);
	}

	// Show popup
	this.show();

	// Load popup contents
	jQuery.ajax({
		url: this.contentURL,
		context: this,
		dataType: "html",
		success: function(data) {
			// Set main popup content
			this.setContent(data);
			// Call open callback function if specified
			if (this.behavior.openCallback){
				this.behavior.openCallback();
			}
		}
	});		
}
// end function open()

// Closes the popup div and removes it from the popup stack.
Popup.prototype.closeAndPop = function() {
	this.close();
	popPopupWithID(this.id);
}
// end function closeAndPop()

//Closes or hides the popup div depending on its hideOnBgClick attribute.
//Displays the next popup in the stack if there is any.
Popup.prototype.closeOrHide = function() {
	if (this.hideOnBgClick) {
		this.hide(this.behavior.hideCallback);
		var popup = getCurrentPopup();
		if (popup != null) {
			popup.show();
		}
	}
	else {
		this.close();
		popPopupWithID(this.id);
	}
}
// end function closeOrHide()

// Closes the popup div.
Popup.prototype.close = function() {
	// Hide popup
	this.hide();
	
	// Clear main popup content
	this.setContent("");

	// Reset event handlers
	jQuery(window).unbind("resize", jQuery.proxy(this, "resizePopup"));
	jQuery(window).unbind("scroll", jQuery.proxy(this, "resizePopup"));
	
	// Call close callback function if specified
	if (this.behavior.closeCallback) {
		this.behavior.closeCallback();
	}		
}
// end function close()

// Sets the HTML content section of the main div.
// newContent - the new html content of the popup.  if set to "", the "loading" class name will also be set for
//              the main div section.  otherwise, the "loading" class name will be removed. 
Popup.prototype.setContent = function(newContent) {
	if (newContent == "") {
		this.main.div.addClass("loading");
	}
	else {
		this.main.div.removeClass("loading");
	}
	this.main.div.html(newContent);
	// end if (newContent == "")
}	
//end function setContent()
	
//Replaces the current contents of the popup with the new content from the given URL.
//newContentURL - the target of the AJAX call that will produce the new popup content
//changeCallback (optional) - a function that is executed after the new content is shown
//method (optional) - request method: GET or POST.  if not specified, the default is GET.
Popup.prototype.changeURL = function(newContentURL, changeCallback, method) {
	// Set request method
	var requestMethod = "GET";
	if (!method) {
		requestMethod = method;
	}
	
	// Clear main popup content
	this.setContent("");

	// Load popup contents
	jQuery.ajax({
		url: newContentURL,
		context: this,
		dataType: "html",
		type: requestMethod,
		success: function(data) {
			// Set main popup content
			this.setContent(data);
			// Call change callback function if specified
			if (changeCallback){
				changeCallback();
			}
		}
	});		
}
//end function changeURL()

// Reveals a temporarily hidden popup div.
// showCallback (optional) - a function that is executed after the popup is redisplayed
Popup.prototype.show = function(showCallback) {
	// Show background div
	if (this.background.div) {
		if (this.behavior.fadeIn) {
			// Fade in background
			this.background.div.fadeIn("slow", 
				jQuery.proxy(this.resetBackgroundOpacity(), this)
			);
		}
		else {
			// Don't fade in background
			this.background.div.show();
		}
		// end if (this.behavior.fadeIn) for background div
	}
	// end if (this.background.div)

	// Show main popup div
	if (this.behavior.fadeIn) {
		// Fade in main popup div
		this.main.div.fadeIn("slow");
	}
	else {
		// Don't fade in main popup div
		this.main.div.show();
	}
	// end if (this.behavior.fadeIn) for main div

	// Call show callback function if specified
	if (showCallback) {
		showCallback();
	}	
}
// end function show()

// Temporarily hides the popup div.
// hideCallback (optional) - a function that is executed after the popup is temporarily hidden
Popup.prototype.hide = function(hideCallback) {
	if (this.behavior.fadeOut) {
		// Fade out popup
		this.background.div.fadeOut("slow", 
				jQuery.proxy(this.resetBackgroundOpacity(), this)
		);
		this.main.div.fadeOut("slow");
	}
	else {
		// Do not fade out popup
		this.background.div.hide();
		this.main.div.hide();			
	}
	// end if (this.behavior.fadeOut)
	
	// Call hide callback function if specified
	if (hideCallback) {
		hideCallback();
	}		
}
// end function hide()

// Closes the topmost popup and removes it from the popup stack.
function closeCurrentPopup() {
	var popup = popPopup();
	
	if (popup) {
		popup.close();
	}
}
// end function closeCurrentPopup()

// Retrieves the topmost popup in the popup stack.
function getCurrentPopup() {
	var popup = null;
	var popupStack = getPopupStack();
	
	if (popupStack) {
		if (popupStack.length > 0) {
			return popupStack[popupStack.length - 1];
		}
	}
	// end if (popupStack) 
	
	return popup;
}
// end function closeCurrentPopup()

// Gets a popup reference from the popup stack based on the given ID.
function getPopup(popupId) {
	var popup = null;
	var popupStack = getPopupStack();

	if (popupStack) {
		for (i = 0; i < popupStack.length; i++) {
			if (popupStack[i].id == popupId) {
				// Popup found
				popup = popupStack[i];
				break;
			}
		}
		// end for
	}
	// end if (popupStack)
	
	return popup;
}
// end function getPopup(popupId)

//Gets a popup reference from the popup stack based on the given ID.
function getNextPopup(popupId) {
	var popup = null;
	var popupStack = getPopupStack();

	if (popupStack) {
		for (i = popupStack.length - 1; i >= 0; i--) {
			if (popupStack[i].id == popupId) {
				// Popup found
				if (i > 0) {
					popup = popupStack[i - 1];
				}
				break;
			}
		}
		// end for
	}
	// end if (popupStack)
	
	return popup;
}
// end function getNextPopup(popupId)

// Pushes the given popup into the popup stack.
function pushPopup(popup) {
	var popupStack = getPopupStack();

	// Create new popup stack if stack is not found
	if (!popupStack) {
		popupStack = new Array();
	}
	
	// Push current popup into stack
	popupStack.push(popup);

	// Replace stack
	jQuery("body").removeData("popupStack");
	jQuery("body").data("popupStack", popupStack);
}
// end function pushPopup(popup)

// Removes and returns the topmost popup in the popup stack.
function popPopup() {
	var popupStack = getPopupStack();
	var popup = null;
	
	if (popupStack) {
		// Pop and return the topmost popup
		popup = popupStack.pop();
	}

	// Replace stack
	jQuery("body").removeData("popupStack");
	jQuery("body").data("popupStack", popupStack);
	
	return popup;
}
// end function popPopup()

// Removes a specific popup from the popup stack.
function popPopupWithID(popupId) {
	var popupStack = getPopupStack();

	if (!popupStack) {
		// If popup stack is not found
		return null;
	}
	else {
		// Find popup with given id
		var popup = null;
		var newPopupStack = Array();
		for (i = 0; i < popupStack.length; i++) {
			var current = popupStack[i]; 
			if (current.id == popupId) {
				// Popup found
				popup = current;
			}
			else {
				// Push current popup into new popup stack
				newPopupStack.push(current);
			}
		}

		// Replace stack
		jQuery("body").removeData("popupStack");
		jQuery("body").data("popupStack", newPopupStack);
		
		return popup;
	}
	// end if (!popupStack)
}
// end function popPopup(popupId)

// Retrieves the popup stack
function getPopupStack() {
	return jQuery("body").data("popupStack");	
}
// end function getPopupStack() 

