$(document).ready(function () {
	//$("div.tram2-wrapper").css({width: ($("div.tram2 div.wagon2").length * 763)+ 2000 + "px"});
	//$("div.tram2").animate({scrollLeft: $(".wagon2.selected").prevAll().length * 763}, 1);

	if ($.browser.msie) {
		currentWindowHeight = document.documentElement.clientHeight;
	} else {
		currentWindowHeight = window.innerHeight;
	}
	
	currentBodyHeight = $(window).height();
	currentBodyWidth = $('body').innerWidth();
	currentModalWindowWidth = $('.popup').innerWidth();
	
	$(".modal").css({height: currentBodyHeight + "px"});
	//$(".modal_window").css({'margin-top': Math.round(currentWindowHeight * 0.25) + "px"});
	$(".popup").css({'margin-left': Math.round((currentBodyWidth - currentModalWindowWidth) / 2) + "px"});

	$(".our_work_block a").click(function () {
		if (currentWindowHeight > 660) {
			currentBodyHeight = $(window).height();
			$(".modal").css({height: currentBodyHeight + "px"});
			$(".modal").css({display: "block"});
			$(".modal").animate({opacity: '0.9'}, 500);
			//$(this).attr("target", "_blank");
			preloadImage ($(this).attr("href"), $(this).children("img").attr("title"));
			return false;
		} else {
			return true;
		}
	});
	
	$(".b_close").click(function () {
		$(".modal, .popup").animate({opacity: '0.0'}, 500);
		setTimeout('$(".modal, .popup").css({display: "none"})', 1100);
		return false;
	});
	
	/*$("a#point_right").click(function () {
		moveGoodsByArrow ('right');
		return false;
	});
	
	$("a#point_left").click(function () {
		moveGoodsByArrow ('left');
		return false;
	});
	*/
});

function moveGoodsByArrow (direction) {
	var step = 763;
	var divGoods = $("div.tram2");
	var scrollOffset = $("div.tram2").scrollLeft();

	if(direction == 'right') {
		//alert(scrollOffset + ' ' + (($("div.tram2 div.wagon2").length * 763) + 2000));
		if(scrollOffset >= ($("div.tram2 div.wagon2").length * 763)) {
			return false;
		} else {
			divGoods.stop();
			divGoods.animate({scrollLeft: scrollOffset + step}, 1000);
		}
	} else if (direction == 'left') {
		divGoods.stop();
		divGoods.animate({scrollLeft: scrollOffset - step}, 1000);
	}
}

function preloadImage(filename, title) {
	Photo = new Image;
	Photo.src = filename;
	
	$(Photo).bind('load', function () {

		$(".popup img").not('.b_close img').attr("src", Photo.src);
		$(".popup .modal_text").text(title);
		$(".popup").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").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() {
	$(".popup").css({display: "block"});
	$(".popup").animate({opacity: '1.0'}, 500);
	
	$(Photo).unbind('load');
	Photo = '';
}
