$(document).ready(
	function () {
		
		$('.zoomImage').click(
			function () {
				
				$('.big_img').remove();
				
				cache_img = new Image();
				cache_img.src = $(this).attr("href");
				
				if(cache_img.complete) {
					showImage($(this));
				} else {
					var self = $(this);
					cache_img.onload = function () {
						showImage(self);
					}
				}
				
				return false; 
			}
		);
		
	}
);

var img_popup_border = 4;


function showImgDesc(alt) {
	var desc_img_node = "<div class=\"desc_image\"><div class=\"in\">"+alt+"</div></div>";
	$("body").append(desc_img_node);
	$(".desc_image").css('position', 'absolute');
	$(".desc_image").css('opacity', '0');
	$(".desc_image").css('z-index', '4');
	var top = $('.big_img').offset().top+$('.big_img').height()+img_popup_border*2;
	var left = $('.big_img').offset().left;
	$(".desc_image").css('top', top+'px');
	$(".desc_image").css('left', left+'px');
	$(".desc_image").css('width', ($('.big_img').width()+img_popup_border*2)+'px');
	
	$(".desc_image").animate( {'opacity': 1}, 400);
	
}

function showImage(but) {
	
	var big_img_node = "<img class=\"big_img\" src=\""+but.attr("href")+"\" alt=\""+but.find('img').attr("alt")+"\" />";
	
	$("body").append(big_img_node);
	
	$(".big_img").css('position', 'absolute');
	$(".big_img").css('opacity', '0');
	$(".big_img").css('z-index', '4');
	
	$(".big_img").css('width', but.find('img').width()+'px');
	$(".big_img").css('height', but.find('img').height()+'px');
	
	$(".big_img").css('left', but.find('img').offset().left+"px");
	$(".big_img").css('top', but.find('img').offset().top+"px");
	
	var offsetTop = $(document).scrollTop() + $(window).height()/2 - cache_img.height/2;
	var offsetLeft = $(window).width()/2 - $(document).scrollLeft() - cache_img.width/2;
	
	$(".big_img").animate( {width:cache_img.width+"px", height:cache_img.height+"px", left:offsetLeft, top:offsetTop, 'opacity': 1}, 400, function () { if(but.find('img').attr("alt")) showImgDesc(but.find('img').attr("alt")) } );
	
	$(".big_img").click(	
		function () {
			var self = this;
			$('.desc_image').remove();
			$('.popup_image_close').remove();
			$(this).animate({width:but.find('img').width()+'px', height:but.find('img').height(), left:but.find('img').offset().left+'px', top: but.find('img').offset().top, 'opacity':0},300, function() { $(self).remove(); });
			
		}
	);

}
