(function ($) {
  resize = function() {
	  
				var borderVt=100; //value based on css style. top bar + bottom bar + padding of photoContain
				var borderHz=0; //value based on css style photoContain padding
				
				$("#photo").css("width", "auto").css("height", "auto"); // Remove existing CSS
				$("#photo").removeAttr("width").removeAttr("height"); // Remove HTML attributes			
				
				var origSizeW = $("#photo").width();
				var origSizeH = $("#photo").height();
				var ratioVt=(origSizeW/origSizeH);
				var ratioHz=(origSizeH/origSizeW);
				var winW = 1024; //value based on css style width of page and photo contain
				var winH = $(window).height();
				var screenSizeW=Math.round(winW-borderHz);
				var screenSizeH=Math.round(winH-borderVt);
				
				if (origSizeW>=origSizeH){
						var newHeight = Math.round(screenSizeW*ratioHz);
						if (newHeight <= screenSizeH){
							$("#photo").css("width", screenSizeW); // Set new width
							$("#photo").css("height", newHeight);
							}
						else{
							$("#photo").css("height", screenSizeH);
							}
				}
				else{
				$("#photo").css("height", screenSizeH); // Set new height
				}
  		};
		
	cursor = function(){
		var firstone = $(".thumbs li:first")
		firstone.addClass("cur");
		};
			
		
	prevImage = function(){
				var current = $(".thumbs li.cur");
				var previous  = (current.prev(".thumbs li"));
				if (previous.length <1){
						previous =$(".thumbs li:last");	
				}	
				var imgPath = (previous.find("a").attr("href"));
				var imgAlt = (previous.find("img").attr("alt"));
				$("#photo").attr({ src: imgPath });
				$("#photo").attr({ alt: imgAlt });
				resize();
				previous.addClass("cur");
				current.removeClass();
				$("#caption").text($("#photo").attr("alt"));
	};
		
		
	nextImage = function(){
				var current = $(".thumbs li.cur");
				var nextone  = (current.next(".thumbs li"));
				if (nextone.length <1){
						nextone =$(".thumbs li:first");	
				}	
				var imgPath = (nextone.find("a").attr("href"));
				var imgAlt = (nextone.find("img").attr("alt"));
				$("#photo").attr({ src: imgPath });
				$("#photo").attr({ alt: imgAlt });
				resize();
				nextone.addClass("cur");
				current.removeClass();
				$("#caption").text($("#photo").attr("alt"));
	};
		
		
		
})(jQuery);
