$(".zoom").live("click",function(){
	//$("div.shadowwrapper,div.zoomwrapper").remove();
	var iid=this.id;
	$(document).keydown(function(event){if(event.keyCode == 27) close();});
	$("a.close, a.closetext, div.all").live("click",close);
	var win_w = $(window).width();
	var win_h = $(window).height();
	var scrolltop = $(window).scrollTop();
	var scrollleft = $(window).scrollLeft();
	$.ajax({
		type: "GET",
		url: "/ajax.gallery.php",
		data: "iid="+iid,
		success: function(html){
			$("body").prepend($(html));
			loadimage(iid);
			var zw=$("div.zoomwrapper");
			if ($(zw).height()>win_h*0.8) var top=scrolltop;
			else var top = Math.round(scrolltop+((win_h - $(zw).height())/2));
			var left = Math.round(scrollleft+((win_w - $(zw).width())/2));
			$(zw).css({"display":"block","margin-top":top+"px","margin-left":left+"px"});
			$("div.shadowwrapper").css({"height":$(zw).height()+"px","display":"block","margin-top":top-9+"px","margin-left":left-14+"px"});
		}
	});
	
	function close() {
		if(jQuery.browser.msie)$("div.shadowwrapper,div.zoomwrapper").remove();
		else $("div.shadowwrapper,div.zoomwrapper").fadeOut(200);
	}
});

$("div.train img").live("click",function(){
	var id=$(this).parent().attr("id").replace(/t/, "");
	loadimage(id);
});
$("#rarr,td.imagewrapper img").live("click",function(){nextimage();});
$("#larr").live("click",function(){previmage();});
function nextimage() {
	var first=$("div.train td:first").attr("id").replace(/t/, "");
	var active=$("div.train td.active").attr("id").replace(/t/, "");
	var last=$("div.train td:last").attr("id").replace(/t/, "");	
	var n=$("div.train td").length;

	xxx=$("#t"+active).next("td").position().left;
	yyy=$("div.window").width();
	if(xxx>yyy) {
		//alert('Упс!');
		$("div.train td:first").clone(true).insertAfter("div.train td:last");
		$("div.train td:first").remove();
	}
	//alert(active);
	if (active==last) loadimage(first);
	else {
		var next=$("#t"+active).next("td").attr("id").replace(/t/, "");
		loadimage(next);
	}
}
function previmage() {
	var first=$("div.train td:first").attr("id").replace(/t/, "");
	var active=$("div.train td.active").attr("id").replace(/t/, "");
	var last=$("div.train td:last").attr("id").replace(/t/, "");	
	if (active==first) loadimage(last);
	else {
		var prev=$("#t"+active).prev("td").attr("id").replace(/t/, "");
		loadimage(prev);
	}
}
$(document).keydown(function(event){
	if(event.ctrlKey) if(event.keyCode == 39) nextimage();
});
$(document).keydown(function(event){
	if(event.ctrlKey) if(event.keyCode == 37) previmage();
});

function loadimage(id) {
	$("div.train td").removeClass("active");
	$("#t"+id).addClass("active");
	var iw=$("td.imagewrapper");
	$("div.shadowwrapper").css('display','none');
	$(iw).contents().remove();
	$(iw).append("<div class=loading></div>");
	var timeout = setTimeout(function(){$("div.loading").css('display','block');}, 400);
	var img = new Image();
	$(img).load(function () {
		$("div.loading").remove();
		$(iw).append(this);
		var shheight=this.height+$("div.title").height()+$("div.carusel").outerHeight();
		//alert($("div.title").height());
		//alert($("div.cl").height());
		//alert($("div.carusel").height());
		//alert(this.height);
		$("td.imagewrapper").height(this.height);
		//$("div.zoomwrapper").height(shheight+105);
		$("div.shadowwrapper").height(shheight+104);
		$("div.shadowwrapper").css('display','block');
	}).error(function () {
		$("div.loading").remove();
		close();
		alert('Ошибка загрузки изображения');		
	}).attr('src','/photos/xxl/'+id);
	if ($("div.train").length>0) $(img).attr('title','Следующая').css('cursor','pointer');
}
