

function imgGalleryInit(imgGalleryJson){
	var slideshowThumbID=0;
	var slideshowThumbSelected=0;
	
	$(function(){
		var thumbID;
		
		for(var i=0;i<imgGalleryJson.imgs.length;i++){
			thumbID = addSlideshowThumb(i);
			if(i==0){
				setSlideshowImg(i);
			}
		}
		
		function clickGalleryBtnLeft(){
			if(slideshowThumbSelected>0){
				setSlideshowImg(slideshowThumbSelected-1);
			}
		}
		function clickGalleryBtnRight(){
			if(slideshowThumbSelected<imgGalleryJson.imgs.length-1){
				setSlideshowImg(slideshowThumbSelected+1);
			}
		}
		function addSlideshowThumb(num){
			var url = imgGalleryJson.imgs[num].url;
			var description = imgGalleryJson.imgs[num].description;
			
			
			$("#imageGalleryController").append('<li id="galleryThumb'+String(num)+'"><table><tr><td><img id="galleryThumb'+num+'_img" src="'+url+'" /></td></tr></table></li>');
			//$("#galleryThumb"+slideshowThumbID+"_img").css({visibility:"hidden"});
			
			$("#galleryThumb"+num).click(function(){
				setSlideshowImg(num);

			});
			$("#galleryThumb"+num+"_img").load(function(){
				var tw = 64;
				var th = 48;
				var w = $(this).width();
				var h = $(this).height();
				var nw = w;
				var nh = h;
				if(nw>tw){
					nw = tw;
					nh = Math.floor(h*(tw/w));
				}
				if(nh>th){
					nh = th;
					nw = Math.floor(w*(th/h));
				}
				$(this).attr("width",nw);
				$(this).attr("height",nh);
				$(this).css({visibility:"visible"});
			});
			
			
			return "galleryThumb"+num;
			
		}
		function selectThumbSlideshow(id){
			
			$("#imageGalleryController li").each(function(){
				if($(this).attr("id")==id){
					$(this).addClass("selected");
				}else{
					$(this).removeClass("selected");
				}
			});
		}
		function setSlideshowImg(num){
			var url = imgGalleryJson.imgs[num].url;
			var description = imgGalleryJson.imgs[num].description;
			var id = "galleryThumb"+num;
			selectThumbSlideshow(id);
			
			$("#imageGalleryView").html('<img src="'+url+'" id="slideshowImg" />');
			$("#slideshowImg").css({display:"none"});
			$("#slideshowImg").load(function(){
				$(this).fadeIn(500);
				var tw = 360;
				var th = 240;
				var w = $(this).width();
				var h = $(this).height();
				var nw = w;
				var nh = h;
				if(nw>tw){
					nw = tw;
					nh = Math.floor(h*(tw/w));
				}
				if(nh>th){
					nh = th;
					nw = Math.floor(w*(th/h));
				}
				$(this).attr("width",nw);
				$(this).attr("height",nh);
				$("#imageGalleryDescription").html(description);
			});
			
			slideshowThumbSelected=num;
			if(slideshowThumbSelected==0){
				$("#imageGalleryBtnLeft").animate({opacity: 0},{duration: 300, easing: "linear"});
				$("#imageGalleryBtnLeft").unbind("click");
			}else{
				$("#imageGalleryBtnLeft").animate({opacity: 1},{duration: 300, easing: "linear"});
				$("#imageGalleryBtnLeft").unbind("click");
				$("#imageGalleryBtnLeft").click(function(){
					clickGalleryBtnLeft();
				});
			}
			if(slideshowThumbSelected==imgGalleryJson.imgs.length-1){
				$("#imageGalleryBtnRight").animate({opacity: 0},{duration: 300, easing: "linear"});
				$("#imageGalleryBtnRight").unbind("click");
			}else{
				$("#imageGalleryBtnRight").animate({opacity: 1},{duration: 300, easing: "linear"});
				$("#imageGalleryBtnRight").unbind("click");
				$("#imageGalleryBtnRight").click(function(){
					clickGalleryBtnRight();
				});
			}
			
		}
		
	});
}
