// config variables for slideshow
var easeFunc = "easeInOutQuad";
var easeTime = 900;
var pictureHeight = 160;
var myTimer = {};
var counter = -1;
var autoRun = false;
var controls = true;

// function to initialize
function slideshowInit(controls, autoRun, pictureHeight) {
	controls = controls;
	autoRun = autoRun;

	if (controls)
	{
		$(".work").each(function(i, work) {
			var images = $(work).find("ul.images");
			var container = $(work).find(".work_images");

			// build the controller
			$(work).find("li").each(function(i, element) {
				// for each li in the original UL, build a button
				$(work).find(".selector ul").append("<li><a class='image_button' href='#' id='nav" + i + "'><span>" + i + "</span><\/a><\/li>");

				// Make each button (each <a> tag) do something.
				$(work).find(".selector li a").each(function(z, a_element) {
					// on click, do..
					$(a_element).click(function(){
						navSelect(z, work, images);
						return false; // this disables the <a> tag
					});
				});
			});

			// set the first button to "on"
			navSelect(0, work, images);
		});
	}
};

function timerHandler() {
	counter++;

	var length = $(".images li").size();
	if (counter == length)
	{
		counter = 0;
	}

	navSelect(counter, null, $(".images"));
}

// generic function for selecting a gallery item and animating to it
function navSelect(selected, container, images) {

	if (controls)
	{
		// turn off the current item and turn on the new item
		$(container).find(".current").removeClass("current");

		// Turn the current selected on and turn off the previously selected
		var selectedItem = $(container).find("#nav" + selected);
		$(selectedItem).addClass("current");
	}

	/*if (selected == 0) {
		$("#previousslide img").fadeOut("slow");
	};

	if (selected == pictEls - 1) {
		$("#nextslide img").fadeOut("slow");
	}*/

	// animate to the new item
	images.animate({top: -(selected * pictureHeight)}, {duration: easeTime, easing: easeFunc});
}
