$(document).ready(function() {
	var siteurl = 'http://konstans.com.pl/_new/';
	var obj = $(this);
	
	$(".foto-gal",obj).click(function(){ //powiększenie foty
	
			$("body",obj).append('<div id="bkd"></div><div id="foto-big"><a href="#" id="foto-close"></a></div>');
			
			$('html, body').animate({scrollTop:0}, 'slow')

			Fidx = $(".foto-gal").index(this);
			
			if(Fidx>1 || (/*!$.support.noCloneEvent && */Fidx>0)){
				$("#foto-big",obj).append('<div class="span"><a href="#" id="foto-prev"></a></div>');
			}else{
				$("#foto-big",obj).append('<div class="span"></div>');
			}
			
			$("#foto-big",obj).append('<div id="foto-mid" style="width:120px; height:120px;"></div>');
			
					
			if(Fidx<$(".foto-gal").size(this)-1){
				$("#foto-big",obj).append('<div class="span"><a href="#" id="foto-next"></a></span>');
			}else{
				$("#foto-big",obj).append('<div class="span"></div>');
			}
			
			$("#foto-big",obj).append('<p id="foto-opis"></p>');
			
			$("#foto-mid").css('background','url(\''+siteurl+'gfx/preload.gif\') no-repeat center center');
			$("#foto-opis").append($(this).attr('alt'));
			
			
			$("#bkd").animate({opacity:0.0}, 10, function(){
				$(this).show().animate({opacity:0.7}, 500, function(){
					$("#foto-big").show('scale', {percent: 100},500,function(){
					var totalH = $(document).height()+50;
					$("#bkd").css('height',totalH+"px");
				});
				});				
			});
			
			
			$("#bkd").click(function(){
				closeFoto();
			});
			
			var imgTesting = new Image();

			function CreateDelegate(contextObject, delegateMethod)
			{
				return function()
				{
					return delegateMethod.apply(contextObject, arguments);
				}
			}

			function imgTesting_onload()
			{
				//alert(this.width + " by " + this.height);
				$("#foto-mid").css('width', imgTesting.width+'px');
				$("#foto-mid").css('height', imgTesting.height+'px');
				$("#foto-big").css('width', imgTesting.width+60+'px');
				$("#foto-big").css('height', imgTesting.height+75+'px');
				
				$("#foto-big").css('margin','-30px -'+$("#foto-big",obj).width()/2+'px auto');
				
				var dlugosc = $(this).attr('src').length;
				$("#foto-mid").css('background','url(\''+$(this).attr('src').substring(0,dlugosc-4)+$(this).attr('src').substring(dlugosc-4,dlugosc)+'\') no-repeat center center');
				
				
				
				$("#foto-close").click(function(){
					closeFoto();
				}); 
				
				$("#foto-next").click(function(){
					Fidx = Fidx+1;
					NextOrPrev();
				});	
				
				$("#foto-prev").click(function(){
					Fidx = Fidx-1;
					NextOrPrev();
				});	
			}

			imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
				var dlugosc = $(this).attr('src').length;
				var source = $(this).attr('src').substring(0,22);
				
				//imgTesting.src = 'http://konstans.com.pl/_new/foto/ec1cb4.jpg';
				
				if(source=='http://a18.asariwww.pl')
				{
					imgTesting.src = $(this).attr('src');
				}
				else{
					imgTesting.src = $(this).attr('src').substring(0,dlugosc-6)+$(this).attr('src').substring(dlugosc-4,dlugosc);
					//imgTesting.src = 'http://konstans.com.pl/_new/foto/ec1cb4.jpg';
				}
			
			

	});
	
	
	
	
	
	function NextOrPrev(){//przewijanie fotek <prev next>
				
				
				
				
				var strTmp = $(".foto-gal").get(Fidx);
				
				
				$("#foto-big").empty();
				$("#foto-big").append('<a href="#" id="foto-close"></a>');
				
			if(Fidx>1|| (/*!$.support.noCloneEvent && */Fidx>0)){
				$("#foto-big",obj).append('<a href="#" id="foto-prev"></a>');
			}else{
				$("#foto-big",obj).append('<div class="span"></div>');
			}
			
			$("#foto-big",obj).append('<div id="foto-mid" style="width:inherit; height:inherit;"></div>');
			
			if(Fidx<$(".foto-gal").size(this)-1){
				$("#foto-big",obj).append('<div class="span"><a href="#" id="foto-next"></a><div class="span">');
			}else{
				$("#foto-big",obj).append('<div class="span"></div>');
			}
			
			$("#foto-big",obj).append('<p id="foto-opis">'+$(strTmp).attr("alt")+'</p>');
			$("#foto-mid").css('background','url(\''+siteurl+'gfx/preload.gif\') no-repeat center center');
			
			

			$("#bkd").click(function(){
				closeFoto();
			});
			
		var imgTesting = new Image();

			function CreateDelegate(contextObject, delegateMethod)
			{
				return function()
				{
					return delegateMethod.apply(contextObject, arguments);
				}
			}

			function imgTesting_onload()
			{
				//alert(this.width + " by " + this.height);
				$("#foto-mid").css('width', imgTesting.width+'px');
				$("#foto-mid").css('height', imgTesting.height+'px');
				$("#foto-big").css('width', imgTesting.width+60+'px');
				$("#foto-big").css('height', imgTesting.height+75+'px');
				
				$("#foto-big").css('margin','-30px -'+$("#foto-big",obj).width()/2+'px auto');
					var dlugosc = $(strTmp).attr('src').length;
					var source = $(strTmp).attr('src').substring(0,22);
					
					if(source=='http://a18.asariwww.pl')
					{
						$("#foto-mid").css('background','url(\''+$(strTmp).attr('src')+'\') no-repeat center center');
					}
					else{
						$("#foto-mid").css('background','url(\''+$(strTmp).attr('src').substring(0,dlugosc-6)+$(strTmp).attr('src').substring(dlugosc-4,dlugosc)+'\') no-repeat center center');
					}			

				
				$("#foto-opis").append($(this).attr('alt'));
					
				$("#foto-big").show('scale', {percent: 100},500,function(){
					var totalH = $(document).height()+50;
					$("#bkd").css('height',totalH+"px");
				});
				$("#foto-close").click(function(){
					closeFoto();
				}); 
				
				$("#foto-next").click(function(){
					Fidx = Fidx+1;
					NextOrPrev();
				});	
				
				$("#foto-prev").click(function(){
					Fidx = Fidx-1;
					NextOrPrev();
				});	
			}

			imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
				var dlugosc = $(strTmp).attr('src').length;
				var source = $(strTmp).attr('src').substring(0,22);
				
				if(source=='http://a18.asariwww.pl')
				{
					imgTesting.src = $(strTmp).attr('src');
				}
				else{
					imgTesting.src = $(strTmp).attr('src').substring(0,dlugosc-6)+$(strTmp).attr('src').substring(dlugosc-4,dlugosc);
				}			
		}
	  
	   function closeFoto(){
			$("#foto-big").hide('scale',{percent: 0},500,function(){
				$("#foto-big").detach();
				$("#bkd").animate({opacity:0.0}, 500, function(){
					$(this).detach();
				});
			});
	   }
	  
	  /*
	   * powiększanie fot - koniec
	   */
});
