// jQuery edPopup Plugin
jQuery.edPopup = {
	'open' : new Array()
};
(function($) {
	// Function to close all open Popup Boxes
	$.edPopupClose = function(aReturn) {
		for ( var i = 0; i < $.edPopup.open.length; i++) {
			if ($.edPopup.open[i] == true) {
				$.edPopup.open[i] = false;
				$('._edPopup' + (i + 1)).fadeOut(1000,function() { $('._edPopup' + (i + 1)).hide() });
			}
		}
		$('select').each(function() { 
			$(this).css('visibility',$(this).data('edPopupSelectVisibility'));
		});
		$('#edPopupBackground').fadeTo(1000, 0, function() {
			$('#edPopupBackground').hide();
		});
		return aReturn;
	}

	$.fn.edPopup = function(aObject, settings) {
		var config = {
			'autoOpen' : false
		};

		if (settings)
			$.extend(config, settings);

		var contentObj = $(aObject).css( {
			'position' : 'absolute',
			'z-index' : '99999'
		});
		$(contentObj).find('.edPopupClose-true').click(function() {
			$.edPopupClose(true);
		});
		$(contentObj).find('.edPopupClose-false').click(function() {
			$.edPopupClose(false);
		});

		if ($('#edPopupBackground').length == 0) {
			
			$('body').prepend(
					$('<div/>').hide().attr('id', 'edPopupBackground').css( {
						'position' : 'absolute',
						'z-index' : 99998,
						'top' : 0,
						'bottom' : 0,
						'left' : 0,
						'right' : 0,
						'width': $(document).width(),
						'height': $(document).height(),
						'border':0,
						'margin':0,
						'padding':0,
						'background-color' : 'black',
						'opacity' : 0
					}).click(function() { 
						$.edPopupClose(false);
					}));
		}

		$.edPopup.open.push(false);
		var edCount = $.edPopup.open.length;

		$('body').append(
				$(contentObj).addClass('_edPopup' + edCount).data('edPopup',
						edCount).hide());
		
		var openFunction = function(aNum) {
			// Hide select fields and store old visibility-property
			$('select').each(function() { 
				$(this).data('edPopupSelectVisibility',$(this).css('visibility')).css('visibility','hidden');
			});
			// Calculate bounds and position
			$('#edPopupBackground').show().fadeTo(1000, 0.7);
			$('._edPopup' + aNum)
			.css({'opacity': 0})
			.show()
			.css({
				'top': $(window).scrollTop() + (document.documentElement.clientHeight / 2 - $('._edPopup' + aNum).height() / 2),
				'left': (document.documentElement.clientWidth / 2) - ($('._edPopup' + aNum).width() / 2)
			})
			.fadeTo(1000,1);
			$.edPopup.open[aNum - 1] = true;
		};

		this.each(function() {
			$(this).data('edPopup', edCount);
			$(this).click(function(e) {
				if ($.edPopup.open[$(this).data('edPopup') - 1] == false) {
					openFunction($(this).data('edPopup'));
				}
				e.preventDefault();
			});
		});

		if (config.autoOpen == true) {
			openFunction(edCount);
		}

		return this;

	};

})(jQuery);


$(function() {
	if (typeof edPopupImportOnly == "undefined" || !edPopupImportOnly) {
		$("a.popup").edPopup($('#popupBox'));
		$(".popupContactClose").click(function(e){
			e.preventDefault();
			$.edPopupClose(false);
		});
	}
	$(document).keypress(function(e){
		if(e.keyCode==27){
			$.edPopupClose(false);
		}
	});
});