

var normalWidth=140;
var normalHeight=90;

var bigWidth=180;
var bigHeight=116;

var opacHover=1.0;
var opacNoHover=0.95;

var arrOfPos=new Array();

var preloadingNow=false;
var loadWatch=null;
var imgPreloading=null;

function galleryScriptStart(){
	
	$(window).resize(function(){ 
		  $('#gallery_mini img').each(function(index){
			var id=$(this).attr('id');
			var normalTop=$(this).position().top;
			var normalLeft=$(this).position().left;
			arrOfPos[id]=[normalTop,normalLeft];
			$(this).css({left:normalLeft,top:normalTop});
		});
	});


	$(document).ready(function(){ 
	   $('#gallery_mini img').each(function(index){
			var id=$(this).attr('id');
			var normalTop=$(this).position().top;
			var normalLeft=$(this).position().left;
			arrOfPos[id]=[normalTop,normalLeft];
			$(this).css({left:normalLeft,top:normalTop});
		});
	   
	   
	   $('#gallery_mini img').css({opacity:opacNoHover});
	   $('#gallery_mini img').hover(
			function() {
				var topChange=arrOfPos[$(this).attr('id')][0]-(bigHeight-normalHeight)/2;
				var leftChange=arrOfPos[$(this).attr('id')][1]-(bigWidth-normalWidth)/2;	
				
				$(this).css({'z-index':101,'position':'absolute'});
				$(this).stop(false,false).animate({width:bigWidth,height:bigHeight,top:topChange,left:leftChange,opacity:opacHover},300);
			},
			function() {
				var topChange=arrOfPos[$(this).attr('id')][0];
				var leftChange=arrOfPos[$(this).attr('id')][1];	
				$(this).css({'z-index':100});
				$(this).stop(true,true).animate({width:normalWidth,height:normalHeight,top:topChange,left:leftChange,opacity:opacNoHover},300,
										'',function(){$(this).css({'position':'static'});});	
		}); 
	});
	
	startLoading();
}


function startLoading(){
		if(preloadingNow==true)return;
		
		var obj=$("a[topreload]:first");
		if(obj==undefined)return;
		var source=$(obj).attr('topreload');
			
		if(source!=undefined){
			preloadingNow=true;
			imgPreloading=new Image();	
			$(imgPreloading).attr('src', source);
			loadWatch=setInterval(watchPreload,100);
			$(obj).removeAttr('topreload');
			//alert('pobieram '+source);
		}
	
}

function watchPreload(){
		if ($(imgPreloading).attr("complete")==true) {
			clearTimeout(loadWatch);
			preloadingNow=false;
            setTimeout(startLoading,100);
        }
 }




