var $act;
var first,last;
var $first,$last;
var $nextitem;
var $prevtitem;
var sem=1;
function ini(){
	var totalph=565;
	var bigph=0;
	var pd=0;
	$act=$("ul#gallery li.item:first-child img");
	$first=$act;
	first=$act.attr("alt");
	$first=$act;
	$last=$("ul#gallery li.item:last-child img");
	last=$last.attr("alt");
	bigph=$("ul#gallery li.item img").height()*6.612;	
	pd=totalph-bigph;
	if(pd>0 & pd!="undefined" & bigph>0 & bigph!="undefined"){
		pd=pd/2;
	}else{
		pd=0;
	}
	//$("#gal1 .padingimage").css("padding-top", pd+"px");

$("#btn-next").click(function(){
	  
	  if(sem==1){
		  sem=0;
	if(last==$act.attr("alt")){
		$act=$first;
		$nextitem=$first;
	}else{
	$act=$act.parent();
	$nextitem=$act.next().find("img");
	}
	
	
	bigph=$nextitem.height()*6.612;
	
	pd=totalph-bigph;
	if(pd>0){
		pd=pd/2;
	}else{
		pd=0;
	}
	var imageSource=""+$nextitem.attr("alt");	
	$("#gal1").addClass("loading");
	$("#gal1 .padingimage").css("padding-top", pd+"px");
	showImage(imageSource,"#gal1");	
	
	$act=$nextitem;
	sem=1;
	 }
	
});
$("#btn-prev").click(function(){
	  if(sem==1){	
	  sem=0;
	if(first==$act.attr("alt")){
		$act=$last;
		$previtem=$act;
	}else{
		$act=$act.parent();
		$previtem=$act.prev().find("img");
	}
		
	

	bigph=$previtem.height()*6.612;
	pd=totalph-bigph;
	if(pd>0){
		pd=pd/2;
	}else{
		pd=0;
	}
	var imageSource=""+$previtem.attr("alt");	
	$("#gal1").addClass("loading");
	$("#gal1 .padingimage").css("padding-top", pd+"px");
	showImage(imageSource,"#gal1");	
	
	$act=$previtem;
	sem=1;
	  }
	
});
function showImage(src,idgal)
{
$(idgal+" img").fadeOut("normal").remove();
var largeImage = new Image();
$(largeImage).load(function()
                        {
                        $(this).hide();                       
					 
                        $(this).fadeIn("slow");  
						     $(idgal).append(this).removeClass("loading");
      });    
	$(largeImage).attr("src", src);                                                                               
}
$("ul#gallery li.item img").click(function(){
	$act=$(this).parent();
	$nextitem=$act.next().find("img");
	$prevtitem=$act.prev().find("img");
	$act=$act.find("img");
	bigph=$(this).height()*6.612;
	$(this).parent().addClass("selected-item");
	
	//$(this).parent().parent().addClass("selected-item");
	pd=totalph-bigph;
	if(pd>0){
		pd=pd/2;
	}else{
		pd=0;
	}

		
	var imageSource=""+$(this).attr("alt");	
	$("#gal1").addClass("loading");
	$("#gal1 .padingimage").css("padding-top", pd+"px");
	showImage(imageSource,"#gal1");	
	
	
          return true;
});	

}
