/*******************************************************************************
 *	jQuery smooth popup based on concept by Adian Mato Gondelle
 *	("yEnS", www.yensdesign.com)
 *	
 *	Based on popup div with id="popup", "button" (really any element) with
 *	id="popup-button" and close "button" (really any element) with
 *	id="popup-close".
 *	   
 ******************************************************************************/  

var popupStatus = 0; // 0 = disabled, 1 = enabled
var popup2Status = 0;

// Fade in popup
function loadPopup(popupIndex)
{
	if(popupStatus == 0 && popupIndex == 1)
	{
		$("#popup").fadeIn("slow");
		popupStatus = 1;
	}
	if(popup2Status == 0 && popupIndex == 2)
	{
		$("#popup2").fadeIn("slow");
		popup2Status = 1;
	}
}

// Fade out popup
function disablePopups()
{
	if(popupStatus == 1)
	{
		$("#popup").fadeOut("slow");
		popupStatus = 0;
	}
	if(popup2Status == 1)
	{
		$("#popup2").fadeOut("slow");
		popup2Status = 0;
	}
}

// Centering popup based on window dimensions
function centerPopup(popupIndex)
{
	if(popupIndex == 1)
	{
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popup").height();
		var popupWidth = $("#popup").width();
		
		// Centering
		$("#popup").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
	}
	if(popupIndex == 2)
	{
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popup2").height();
		var popupWidth = $("#popup2").width();
		
		// Centering
		$("#popup2").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
	}
}

// jQuery event control
$(document).ready(function()
{
	
	// Show popup when "button" clicked
	$("#popup-button").click(function()
	{		
		if(popupStatus == 0)
		{
			centerPopup(1);
			loadPopup(1);
		}
		else
		{
			disablePopups(); // button works as a toggle
		}
	});
	$("#popup2-button").click(function()
	{		
		if(popup2Status == 0)
		{
			centerPopup(2);
			loadPopup(2);
		}
		else
		{
			disablePopups(); // button works as a toggle
		}
	});
				
	// Close popup when close "button" is clicked, or close link at bottom
	$("#popup-close").click(function() { disablePopups(); });
	$("#popup-close2").click(function() { disablePopups(); });
	$("#popup2-close").click(function() { disablePopups(); });
	$("#popup2-close2").click(function() { disablePopups(); });

	// Close popup when ESC button pressed on keyboard
	// Does not work across all browsers, unfortunately 
	$(document).keypress(function(e)
	{
		if(e.keyCode == 27 && popupStatus == 1)
		{
			disablePopups();
		}
	});
	
	// Sliding Elements
	$("#lunchcrunch-button").click(function(event)
	{
		event.preventDefault();
		$("#crushhour-button").removeClass("selected");
		$("#crushhour:visible").slideUp(1000);
		$("#lunchcrunch:hidden").slideDown(1000); 
		$("#lunchcrunch-button").addClass("selected");
	});
	$("#crushhour-button").click(function(event)
	{
		event.preventDefault();
		$("#lunchcrunch-button").removeClass("selected");
		$("#lunchcrunch:visible").slideUp(1000);
		$("#crushhour:hidden").slideDown(1000); 
		$("#crushhour-button").addClass("selected");
	});
});

