
$.modal = {}
$.modal.modalStatus = 0;
$.modal.modalWhich; 

function setWhich(thisOne){
	$.modal.modalWhich = thisOne;
}

function loadmodal(){
	if($.modal.modalStatus==0){
		$("#modal_bg").css({
			"opacity": "0.7"
		});
		$("#modal_bg").fadeIn("slow");
		$("#modal_" + $.modal.modalWhich).fadeIn("slow");
		$.modal.modalStatus = 1;
	}
}

function disablemodal(){
	if($.modal.modalStatus==1){
		$("#modal_bg").fadeOut("slow");
		$("#modal_" + $.modal.modalWhich).fadeOut("slow");
		$.modal.modalStatus = 0;
	}
}

function centermodal(){
	var scrolled_y = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrolled_y = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrolled_y = document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrolled_y = document.documentElement.scrollTop;
	}
	

	
	var windowWidth = 0;
	var windowHeight = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	
	
	var modalHeight = $("#modal_" + $.modal.modalWhich).height();
	var modalWidth = $("#modal_" + $.modal.modalWhich).width();
	//centering
	$("#modal_" + $.modal.modalWhich).css({
		"position": "absolute",
		"top": windowHeight/2-modalHeight/2 + scrolled_y,
		"left": windowWidth/2-modalWidth/2
	});
	//only need force for IE6
	
	$("#modal_bg").css({
		"height": windowHeight
	});
	
}


$(document).ready(function(){

	$(".modal_link").click(function(){
		setWhich(this.id);
		//centering with css
		centermodal();
		//load modal
		loadmodal();
		
	});

	$(".modal_close").click(function(){
		disablemodal();
	});

	$("#modal_bg").click(function(){
		disablemodal();
	});

	$(document).keypress(function(e){
		if(e.keyCode==27 && $.modal.modalStatus==1){
			disablemodal();
		}
	});

});

$(window).resize(function() {
	centermodal();
});

//$(window).scroll(function() {
//	centermodal();
//});