$(document).ready(function () {

	if ($.browser.msie) {
		currentWindowHeight = document.documentElement.clientHeight;
	} else {
		currentWindowHeight = window.innerHeight;
		
	}
	
	currentBodyHeight = $(window).height();
	currentBodyWidth = $('body').innerWidth();
	currentModalWindowWidth = $('.modal_window').innerWidth();
	
	//$(".modal").css({height: currentBodyHeight + "px"});
	//$(".modal_window").css({'margin-top': Math.round(currentWindowHeight * 0.25) + "px"});
	$(".modal_window").css({'margin-left': Math.round((currentBodyWidth - currentModalWindowWidth) / 2) + "px"});
	
	$("div.gallery div.gallery_photo a").click(function () {
		$(".modal").css({display: "block"});
		$(".modal").animate({opacity: '0.9'}, 500);
		preloadImage ($(this).attr("href"), $(this).children("img").attr("title"));
		
		return false;
	});
	
	$(".close_button, .modal_photo").click(function () {
		$(".modal, .modal_window").animate({opacity: '0.0'}, 500);
		setTimeout('$(".modal, .modal_window").css({display: "none"})', 1100);
		return false;
	});
	
	$('div.section div.section_text div.text_container, div.gallery div.gallery_photo').jScrollPane({showArrows:false,scrollbarWidth:7,dragMaxHeight:38});
});

function preloadImage(filename, title) {
	Photo = new Image;
	Photo.src = filename;
	
	$(Photo).bind('load', function () {
		$(".modal_window .modal_photo img").attr("src", Photo.src);
		$(".modal_window .modal_text").text(title);
		$(".modal_window").css({'margin-left': Math.round((currentBodyWidth / 2) - (Photo.width / 2) + 10) + "px",
								'margin-top': Math.round((currentBodyHeight / 2) - (Photo.height / 2) - 30) + "px"});
		$(".modal_text").parents("table").css({'width': Photo.width});
		//$(".modal_text").parents("table").find(".modal_text").css({'width': (Photo.width * 0.85)});
		//$(".modal_text").parents("table").find(".close_block").css({'width': (Photo.width * 0.15)});
		setTimeout("showImage()", 250);
	});
}

function showImage() {
	$(".modal_window").css({display: "block"});
	$(".modal_window").animate({opacity: '1.0'}, 500);
	
	$(Photo).unbind('load');
	Photo = '';
}
