// Last updated: August 21, 2009
// IE6 compliant

var dropsheet;
var isOpen = false;
var animSpeed = 500;

$(document).ready(function () {
	
	$("body").append("<div id=\"dropsheet\"></div>");
	dropsheet = $("#dropsheet");
	dropsheet.hide().fadeTo(1, 0);
	if (ie6)
		$("body").append("<iframe id=\"popup-iframe\" style=\"display: none;\"></iframe>");
	
	window.onresize = function () {
		for (var i=0; i<popupStack.length; i++)
			positionPopup(popupStack[i]);
	}
	
	if (ie6)
		window.onscroll = function () { positionPopup(); window.status = "Scrolling..."; }
	
	$(".popup-closer").clickToClosePopup();
	
	$("[popup]").click(function () {
		openPopup($(this).attr("popup"));
	});
	
	
						   		   
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.popup-cont').click(function() {
									 
		var popID = $(this).attr('rel'); 		
		var popWidth = $(this).attr('pWidth');
		
		showLoginPopUp(popID, popWidth);		
		return false;
	});
	
	
	
});

(function($) {
$.fn.clickToOpenPopup = function (popup_id) {
	return $(this).click(function () {
		openPopup(popup_id);
	});
}

$.fn.clickToClosePopup = function () {
	return $(this).click(function () {
		closePopup($(this).attr("popup"));
	});
}

})(jQuery);

popupStack = [];

function openPopup (id) {
	if (ie6)  {
		$("select").css("visibility", "hidden");
		$(".popup select").css("visibility", "visible");
	}
	
	var popup = $("#"+id);
	popupStack.push(popup);
	
	if (popup.length == 0)
		return false;
	
	positionPopup(popup);
	
	var currentDropSheetZIndex = dropsheet.css("z-index") - 0;
	dropsheet.css("z-index",currentDropSheetZIndex + 2);
	popup.css("z-index",currentDropSheetZIndex + 3);
		
	popup.show();
	if (ie6)
		$("#popup-iframe").show();
	dropsheet.show().fadeTo(animSpeed, .75);
}

function closePopup (reopen) {
	var closedPopup = popupStack.pop();
	isOpen = popupStack.length > 0;
	
	var currentDropSheetZIndex = parseInt(dropsheet.css("z-index"));
	dropsheet.css("z-index",currentDropSheetZIndex-2);
	
	reopen = reopen || false;
	if (!reopen && !isOpen) {
		dropsheet.fadeTo(animSpeed, 0, function () { $(this).hide(); });
		if (ie6)
			$("select").css("visibility", "visible");
	}
	
	if (ie6)
		$("#popup-iframe").hide();
	closedPopup.hide();
}

function positionPopup (popup) {	
	if (popup == null || popup == undefined)
	{
		if (popupStack.length > 0)
			popup = popupStack[popupStack.length - 1];
		else
			return false;
	}
	var width = popup.width();
	width += parseInt(popup.css("padding-left"));
	width += parseInt(popup.css("padding-right"));
	var height = popup.height();
	height += parseInt(popup.css("padding-top"));
	height += parseInt(popup.css("padding-bottom"));
	
	var vpWidth = $(window).width();
	var vpHeight = $(window).height();
	
	var left = Math.floor((vpWidth - width) / 2);
	if (left<0) { left = 0; }
	
	var top = Math.floor((vpHeight - height) / 2);
//	if (top<0) { top = 0; }
	top = Math.max(top,35);
	top *= .9; // Brings is up a bit.. looks better to the eye.
	
	if (ie6) {
		var ieWidth = document.documentElement.clientWidth + "px";
		var ieHeight = document.documentElement.clientHeight + "px";
		var ieTop = document.documentElement.scrollTop + "px";
		var ieLeft = document.documentElement.scrollLeft + "px";
		dropsheet.css({"left":ieLeft,"width":ieWidth,"height":ieHeight,"top":ieTop});
		top += document.documentElement.scrollTop;
		left += document.documentElement.scrollLeft;
		$("#popup-iframe").css({"width":width,"height":height,"left":left,"top":top});
	}
	
	left+="px";
	top+="px";
	popup.css({"top":top,"left":left});
}

function showLoginPopUp(popID, popWidth)
{
			
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });
		
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		$('body').append('<div id="fade"></div>'); 
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); 
		
		$('a.close, #fade').live('click', function() { 
			$('#fade , .popup_block').fadeOut(function() {
				$('#fade').remove();  
			});
		});
		
		return false;
}
