$(function(){
	$("body").append("<div id='modalBox' style='display: none;'><div id='modalBox_close'><a href='javascript:Modalbox.close();' id='modalBox_close_link'>X</a></div><div id='modalBox_content'></div></div><div id='darken' style='display: none;'></div>");

	$("#modalBox").css({"position" : "absolute",
		"top" : "50%",
		"left" : "50%",
		"margin-top" : "-200px",
		"border" : '#ccc 10px solid',
		"padding" : "20px",
		"background" : "white",
		"font-size" : "12px",
		"color" : "black",
		"z-index" : "200",
		"overflow" : "hidden"});

	$("#modalBox_close").css({"position" : "absolute",
								"top" : "5px",
								"right" : "5px"});

	$("#modalBox_close_link").css({"color" : "silver",
									"text-decoration" : "none"});
	$("#modalBox_close_link").hover(function(){ $(this).css({"color" : "black"});},function(){$(this).css({"color" : "silver"});});
	
	$("#darken").css({"position" : "absolute",
		"top" : "0px",
		"left" : "0px",
		"width" : "100%",
		"height" : "100%",
		"z-index" : "199",
		"background" : "black",
		"opacity" : ".7",
		"filter" : "Alpha(opacity=70)"});
});

function Fitcom_Modalbox(){
	this.close = function(){
		$('#modalBox').fadeOut('500');
		$('#darken').fadeOut('500');
		$('body').css({"overflow" : "auto"});
		$("#modalBox_content").css({"overflow-y" : "auto", "overflow-x" : "visible", "height" : "auto"});
	}
	this.update = function(text){
		$('#modalBox_content').html(text);
		$('#modalBox').css({"margin-left" : "-" + $("#modalBox").get(0).offsetWidth	/ 2 + "px"});
	}
	this.submit = function(form){
		if($(form).attr("method").toLowerCase() == "post"){
			$.post($(form).attr("action"), $(form).serializeArray(), function(data, textStatus){Modalbox.update(data)});
		}
		else{
			$.get($(form).attr("action"), $(form).serializeArray(), function(data, textStatus){Modalbox.update(data)});
		}
		return false;
	}
	this.show = function(content, width){
		if(!content){
			this.close();
		}
		else{
			if(window.innerWidth){
				_w = window.innerWidth;
				_h = window.innerHeight;
				_scrollX = window.pageXOffset;
				_scrollY = window.pageYOffset;
				isIE = false;
			}
			else{
				_w = document.documentElement.offsetWidth;
				_h =  document.documentElement.offsetHeight;
				_scrollX = document.documentElement.scrollLeft;
				_scrollY = document.documentElement.scrollTop;
				isIE = true;
			}
			$('#modalBox_content').html(content);

			if(width > 0){
				$('#modalBox').css({"width" : width});
			}
			$("#modalBox").css({"left" : "-1000px"});
			$("#modalBox").show();
			$('#modalBox').css({"margin-left" : "-" + $("#modalBox").get(0).offsetWidth / 2 + "px"});
			if($('#modalBox').get(0).offsetHeight > (_h - 50)){
				if(isIE){
					$("#modalBox_content").css({"overflow-y" : "auto", "overflow-x" : "visible", "height" : ($('#modalBox').get(0).offsetHeight > (_h - 50)?(_h - 100):"auto")});
				}
				else{
					$("#modalBox_content").css({"overflow-y" : "auto", "overflow-x" : "visible", "max-height" : (_h - 100)});
				}
			}
			$('#modalBox').css({"margin-top" : "-" + $("#modalBox").get(0).offsetHeight / 2 + "px"});
//			alert($("#modalBox").get(0).offsetWidth);
			$("#modalBox").hide();
			$("#modalBox").css({"left" : _w/2 + _scrollX, "top" : _h/2 + _scrollY});
			$('#darken').css({"left" : _scrollX, "top" : _scrollY});
			$('body').css({"overflow" : "hidden"});
			
			$('#darken').fadeIn('500');
			$('#darken').click(this.close);
			$('#modalBox').fadeIn('500');
		}
		return false;
	}
	
	this.resize = function(reposition){
		if(window.innerWidth){
			_w = window.innerWidth;
			_h = window.innerHeight;
			_scrollX = window.pageXOffset;
			_scrollY = window.pageYOffset;
			isIE = false;
		}
		else{
			_w = document.documentElement.offsetWidth;
			_h =  document.documentElement.offsetHeight;
			_scrollX = document.documentElement.scrollLeft;
			_scrollY = document.documentElement.scrollTop;
			isIE = true;
		}
		if($('#modalBox').get(0).offsetHeight > (_h - 50)){
			if(isIE){
				$("#modalBox_content").css({"overflow-y" : "auto", "overflow-x" : "visible", "height" : ($('#modalBox').get(0).offsetHeight > (_h - 50)?(_h - 100):"auto")});
			}
			else{
				$("#modalBox_content").css({"overflow-y" : "auto", "overflow-x" : "visible", "max-height" : (_h - 50)});
			}
		}
		if(reposition){
			$('#modalBox').css({"margin-left" : "-" + $("#modalBox").get(0).offsetWidth / 2+ "px"});
			$('#modalBox').css({"margin-top" : "-" + $("#modalBox").get(0).offsetHeight / 2+ "px"});
			$("#modalBox").css({"left" : _w/2 + _scrollX, "top" : _h/2 + _scrollY});
			$('#darken').css({"left" : _scrollX, "top" : _scrollY});
			$('body').css({"overflow" : "hidden"});
		}
		$("#modalBox").html($("#modalBox").html());

	}
	
	this.load = function(uri, width){
		var text;
		$.ajax({
			  type: "GET",
			  async: false,
			  url: uri,
			  success: function(data){
			    text = data;
			  }
			});
		this.show(text);
		return false;
	}
}

var Modalbox = new Fitcom_Modalbox();

