$(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"});
	
	$(".envelopes 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").click(function () {
		$(".modal, .modal_window").animate({opacity: '0.0'}, 500);
		setTimeout('$(".modal, .modal_window").css({display: "none"})', 1100);
		return false;
	});
});

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 .frame iframe").attr("src", filename);
		$(".modal_window .modal_text").text(title);
		currentModalWindowWidth = $('.modal_window').innerWidth();
		currentModalWindowHeight = $('.modal_window').innerHeight();
		$(".modal_window").css({'margin-left': Math.round((currentBodyWidth / 2) - (currentModalWindowWidth / 2) + 10) + "px",
								'margin-top': Math.round((currentBodyHeight / 2) - (currentModalWindowHeight / 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 = '';
}
