var image_path = "images/gallery/";
var file_ext = ".jpg";
thumbClick = false;
slideUp = true;
			
function initGallery(mainImageDir) {
	
	totalImages = $("#thumbs ul li").not(".empty").size();
	imageStartNum = 1;
	
	main_image_directory = mainImageDir;
	
	$("#total_images").replaceWith(totalImages);
	$("#image_num").text(imageStartNum);
	
	thumb_panel = $("#thumb-wrapper");
	infoBox = $(".infoBox");
	
	// on load, thumbs stay at full height on screen for 3.5 secs, then slide down 
	timer = $.timer(3500, slideDownThumbs);
	
	thumb_panel.hoverIntent(animateThumbPanelUp, animateThumbPanelDown);
	
	if (thumbClick == false) {		
		controls(imageStartNum);
	}
		
	
	$("#slideUp").click(function() {
		toggleInfoBox();		
	});
}

function toggleInfoBox() {

	if (slideUp) {
		
		$("#infoBoxBg").animate({
			height: "24px",
			top: "93px"
		}, "normal");
		
		$(".infoBox").children("h3, p").hide();
		
		$(".infoBox").animate({
			height: "24px",
			top: "-67px"
		}, "normal");
		
		$("#slideUp").css("background", "url(images/gallery/gallery_arrow_down.gif) no-repeat");		
		slideUp = false;
		
	} else {
		
		$("#infoBoxBg").animate({
			height: "187px",
			top: "93px"
		}, "normal");
			
		$(".infoBox").animate({
			height: "187px",
			top: "95px"
		}, "normal", "", function() {$(".infoBox").children("h3, p").show();});
		
		$("#slideUp").css("background", "url(images/gallery/gallery_arrow_up.gif) no-repeat");		
		slideUp = true;
	}
}


function slideDownThumbs() {
	animateThumbPanelDown();
	timer.stop();
}


function animateThumbPanelUp() {
	timer.stop();
			
	if (BrowserDetect.OS == "Windows" && BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6) {
		thumb_panel.animate({
			height: "94px",
			top: "370px"
		}, "normal", "", thumbs());
	} else {
		thumb_panel.animate({
			height: "94px",
			top: "367px"
		}, "normal", "", thumbs());
	}

}
	
function animateThumbPanelDown(){		
	thumb_panel.animate({
		height: "17px",
		top: "448px"
	}, "normal");
}

function thumbs() {
	
	$("#thumbs ul li").not(".empty").each(function(i) {
		var count = i + 1;
						
		//$(this).append("<div class='imgBg'></div>");
											
		$(this).click(function() {
									
			thumbClick = true;			
			updateMainImage(count);							
			updateText(count);
			
			if (thumbClick == true) {
				controls(count);
			}
			
		});
		
		$(this).hoverIntent(rollOver, rollOut);
		
		function rollOver() {
			$(this).children(".imgBg").children("img").fadeTo("fast", 1);
			$(this).siblings().children(".imgBg").children("img").fadeTo("fast", 0.4);
		}
		
		function rollOut() {
			$(this).siblings().children(".imgBg").children("img").fadeTo("fast", 1);
		}			
	});
}

function updateMainImage(imageNum) {
		
	var mainImgDir = main_image_directory + "/main/";
	var main_img_name = "main" + imageNum;
	var main_image = image_path + mainImgDir +  main_img_name + file_ext;
	
	//sets the gallery image on screen to be the img inside #gallery-image div
	var main_img = $(".widget #gallery-image img");	
	
	$(main_img).css('visibility','hidden');		
	
	// hide the < buttons > when image is first loaded to prevent click spam and multiple images loading
	// once all the images are loaded, you can click through them as fast as you like
	$("#scrollPrev, #scrollNext").css('visibility','hidden');	
	
	//show image and set the source
	$(main_img).load(function(){		
		$(main_img).css('visibility','visible');
		$("#scrollPrev, #scrollNext").css('visibility','visible');
	}).attr('src', main_image);	

}

function updateText(imageNum) {
	
	$("#image_num").text(imageNum);
	
	var infoCopy = $("#thumbs ul li").eq(imageNum - 1).children("p");
	
	if (slideUp == true) {
		infoBox.children("p").replaceWith(infoCopy.clone());
		infoBox.children("p").show();

	} else {
		infoBox.children("p").replaceWith(infoCopy.clone());
		infoBox.children("p").hide();
	}
}

function controls(imageNum) {
	
	var prev = $("#scrollPrev");
	var next = $("#scrollNext");
	var pos = imageNum;
	
	next.click(function() {
						
		if (pos >= totalImages) {
			pos = imageStartNum;
		} else {
			pos += 1;
		}
		
		updateMainImage(pos);
		updateText(pos);
	});
	
	prev.click(function() {
		if (pos <= imageStartNum) {
			pos = totalImages;
		} else {
			pos -= 1;
		}		
		updateMainImage(pos);
		updateText(pos);
	});	
}	

function startWidget() {
	$(".widget #thumbs").jCarouselLite({
		btnNext: ".widget #next",
		btnPrev: ".widget #prev",
		speed: 800,
		circular: false,
		visible: 7,
		scroll: 7
	});
}
