/**	
 * Custom Modal dialog
 * @author Julian Guy
 */
jQuery.modal = function(modalContent,options) {
	
	// Cufon Support
	function doCufon() {
		if(typeof Cufon != "undefined") {
			Cufon.refresh();
		}
	}
	
	function closeModal() {
		$('html').removeClass('jQueryModalOverlayOpen');
		$("#jQueryModalWindow").fadeOut(500,function(){
			$("#jQueryModalOverlay").fadeOut();
		});
	}
	
	// Default settings
	var settings = jQuery.extend({
	     			verticalCentering: false
	  				}, options);
	
		
	// Check for modal HTML construct
	var modalWrapper = $("#jQueryModalWrapper");
	if(modalWrapper.size()==0) {
		var modalHTML = $('<div id="#jQueryModalWrapper"><div id="jQueryModalOverlay"></div><div id="jQueryModalWindow"></div></div>');
		$("body").append(modalHTML);
		modalWrapper = $("#jQueryModalWrapper");
	}
	
	// Hide Modal
	$("#jQueryModalOverlay,#jQueryModalWindow",modalHTML).hide();
		
	// Append HTML to modal Window
	var modalWindow = $("#jQueryModalWindow");
	var modalContent = $(modalContent);
		$(".close",modalContent).css({cursor:"pointer"}).click(function(){
			closeModal();
			return false;
		})
		modalWindow.empty().html(modalContent);
		doCufon();		

	// Screen Dimensions
	 var screenHeight = $(document).height();
	$("#jQueryModalOverlay").css({height:screenHeight+"px"}) 
		

	// IE fix
	if($.browser.msie && $.browser.version<=6) {
		var screenWidth = $(document).width()
		$("#jQueryModalOverlay").css({width:screenWidth+"px"})
	}
		
	// Position window (Fixed, vertically centering style)
	if(settings.verticalCentering) { 
		modalWindow.css({"top":"50%","margin-top":"-"+(windowHeight/2)+"px"});
	} 
	// Position window (Standard absolute style)
	else {
		var windowWidth  = modalWindow.width();
		var windowHeight = modalWindow.height();	
		modalWindow.css({"margin-left":"-"+(windowWidth/2)+"px"});
	}
	
	// Show Modal
	$("#jQueryModalOverlay").fadeIn(200,function(){		
		modalWindow.fadeIn();
		var offset 	= modalWindow.offset()
		if($(document).scrollTop()>offset.top) {		
			$('html,body').animate({scrollTop:offset.top},1000)	
		}		
	});

	$('html').addClass('jQueryModalOverlayOpen');

};
