var baseheight;
var basewidth;
var rstolerance;

$(document).ready( function () {
	baseheight = $("#page").height() + $("#bottom-margin").height() + $("#top-margin").height();
	basewidth = $("#page").width() - $("#left-panel").width() - $("#right-panel").width();
	rstolerance = 10;
	$(window).resize(function() {
		pageresize();
	});
	//pageresize();
	if (Math.abs($(window).width() - basewidth) > rstolerance|| Math.abs($(window).height() - (baseheight)) > rstolerance) pageresize(); 
	if ($('#dialog_link1').length) 
	{
		$("#dialog-holder").width($("#mm-middle").width());
		$("#dialog-holder").height($("#mm-middle").height());
		$("#dialog-holder").css("overflow","auto");
		$('#dialog_link1').click(
			function() { 
				dialogshide();
				$(this).addClass('dialog_linkselected');
				$("#dialog-holder").html($("#dialog1").html());
		});
		$('#dialog_link2').click(
			function() { 
				dialogshide();
				$('#dialog_link2').addClass('dialog_linkselected');
				$("#dialog-holder").html($("#dialog2").html());
		});
		$('#dialog_link3').click(
			function() { 
				dialogshide();
				$('#dialog_link3').addClass('dialog_linkselected');
				$("#dialog-holder").html($("#dialog3").html());
		});
		$('#dialog_link4').click(
			function() { 
				dialogshide();
				$(this).addClass('dialog_linkselected');
				$("#dialog-holder").html($("#dialog4").html());
		});
		$('#dialog_link4').click(
			function() { 
				dialogshide();
				$(this).addClass('dialog_linkselected');
				$("#dialog-holder").html($("#dialog5").html());
		});
		$('#dialog_link5').click(
			function() { 
				dialogshide();
				$(this).addClass('dialog_linkselected');
				$("#dialog-holder").html($("#dialog6").html());
		});
		$("#dialog_link1").addClass('dialog_linkselected');
		$("#dialog-holder").html($("#dialog1").html());
	}

});

function dialogshide () {
	for (var i=1;i<7 ;i++ )
	{
		$("#dialog_link" + i).removeClass('dialog_linkselected'); 
		$("#dialog" + i).css("display","none");
	}
}

function pageresize() {
	var nh = Math.floor($(window).height() - $("#bottom-margin").height() - $("#top-margin").height() - $("#mm-top").height()- $("#mm-contact").height()- $("#mm-bottom").height()) ;
	var wp = Math.floor(($(window).width() - $("#middle-panel").width()) / 2) - rstolerance;
	if ($(window).width() <= $("#middle-panel").width()) wp = 0;
	$("#right-panel").css("width",wp + "px");
	$("#left-panel").css("width",wp + "px");
	$("#bottom-margin").css("margin-right",wp + "px");
	$("#bottom-margin").css("margin-left",wp + "px");
	if (nh < 200) nh = 200;
	$("#mid-mid").height(nh);
	wp = (wp * 2) + $("#middle-panel").width();
	nh = nh + $("#top-margin").height() + $("#bottom-margin").height() + $("#mm-top").height() + $("#mm-contact").height() + $("#mm-bottom").height(); 
	//$("#screensize").html("wwid=[" + $(window).width() + "] pwid=[" + wp + "] whgt=[" + $(window).height() + "] phgt=[" + nh + "]");
}


