$(function() {
	
	$("div.prod_box").each(function() {
		$(this).data('promar_loaded',false);
		var id=$(this).attr("id");
		id=id.substr(6);
		var src=$(this).find("div.ramka_foto img");
		var alt = $(src).attr("alt");
		src=$(src).attr("src");
		
		src=src.replace("min/","srednie2/");

		var im = new Image();
		$(im).attr("alt",alt).attr("id","dzial_big_"+id).addClass("tooltip_img").css("opacity",0.0);
		var $self = $(this);
		$(im).load(function() {
			wymiary[id] = new Array( $(this).width(), $(this).height() );
			$self.data('promar_loaded',true);
		});
		
		$(im).attr("src",src);
		$(this).append(im);
		
		var tooltip_nazwa = $('<div class="tooltip_nazwa" id="dzial_nazwa_'+id+'"><span>'+alt+'</span></div>');
		$(this).append(tooltip_nazwa);
		
	});
	$(".tooltip_img").mouseenter(function() {
		var id=$(this).attr("id");
		id=id.substr(10);	
		clearTimeout(tooltip_timeout[id]);
	})
	
	
	
	
	$("div.prod_box").hover(
		function() {
			if( $(this).data('promar_loaded') ) {
				var id=$(this).attr("id");
				id=id.substr(6);
				if(tooltip_cur!=id) {
					SchowajTooltip(tooltip_cur);
				}
				
				var im1 = $(this).find("img");
				var im2 = $("#dzial_big_"+id);
				var tn = $("#dzial_nazwa_"+id);
				im2.css("left",im1.offset().left).css("top",im1.offset().top).css("width",im1.width()).css("height",im1.height());
				tn.css("left",im1.offset().left).css("top",im1.offset().top-36).css("width",im1.width()).css("opacity",0.0).show();
				
				
				var l = $(this).offset().left + $(this).outerWidth() + 10; //($(window).width() - wymiary[id][0]) /2;
				var t = $(this).offset().top - 10;//($(window).height() - wymiary[id][1]) /2 + $(window).scrollTop();
				if(l + wymiary[id][0] > $(window).width()) {
					l = $(this).offset().left - 10 - wymiary[id][0];
				}
				if(l<0) l =0;
				if(t+wymiary[id][1] > $(window).height() + $(window).scrollTop()) {
					t = $(window).height() + $(window).scrollTop() - wymiary[id][1];
				}
				if(t < $(window).scrollTop()) {
					t = $(window).scrollTop() + 36;
				}
				im2.animate({"left":l,"top":t,"width":wymiary[id][0],"height":wymiary[id][1],"opacity":1.0});
				tn.animate({"left":l,"top":t-36,"opacity":1.0,"width":wymiary[id][0]});
				
				
				tooltip_cur = id;
			}
		
		},function() {
			if( $(this).data('promar_loaded') ) {
				var id=$(this).attr("id");
				id=id.substr(6);
				SchowajTooltip(id);
			}
			
		});
		
});
function SchowajTooltip(id) {
	var im1 = $("#dzial_"+id).find("img");
		
	var im2 = $("#dzial_big_"+id);
	var tn = $("#dzial_nazwa_"+id);
	im2.stop().hide();
	tn.stop().hide();
	//im2.css("left",im1.offset().left).css("top",im1.offset().top).css("width",im1.width()).css("height",im1.height());

	//im2.animate({"left":im1.offset().left,"top":im1.offset().top,"width":im1.width(),"height":im1.height(),"opacity":0.0},"fast");
}


var wymiary = new Array();		
var tooltip_timeout = new Array();
var tooltip_cur = null;

