jQuery(document).ready(function($){
	//initialize the slideshow if the slides div is present
	if ($("#slideshow").length) {
		//clear out all the a's in the slideshow list and make it into an array.
		images = new Array();
		curimg = 0;
		slidetime = "";
		sliding = true;
		showWidth = $("#slideshow").width(); //showwidth will set the left position for sliding images
		$("#slideshow li").each(function(i){
			images[i] = $(this).find("a").attr("href");
			$(this).remove();
		});
		//then start us off by just appending the first slide (no animation) or hiding the slides div if no images were found
		if (images.length >= 1) {
			//slideshow and controls hidden by default in case JS is disabled
			$("#slideshow").show();
			$("#slidenav_r").show();
			$("#slidenav_l").show();
			var newimg = "<img src='" + images[curimg] + "' />";
			$(newimg).load(function(){
				var slide = "<li>" + newimg + "</li>";
				$(slide).appendTo("#slideshow").css({left:0});
				sliding = false;
				//if there is more than one picture, start the slideshow and append the slider controls
				if (images.length > 1) {
					slidetime = setTimeout("slideIt()",4500);
					$("#slide_prev,#slide_next").show();
				}
			});
		} else {
			$("#slideshow").hide();
		}
	}
	
	$("#slidenav_r").click(function(e){
		e.preventDefault();
		if (sliding == false) {
			clearTimeout(slidetime);
			slideIt();
		}
	});
	
	$("#slidenav_l").click(function(e){
		e.preventDefault();
		if (sliding == false) {
			clearTimeout(slidetime);
			slideIt("prev");
		}
	});
	
	//the menu functions - show large image on thumbnail click.
	$(".menu_images a").click(function(e){
		e.preventDefault();
		var url = $(this).attr("href");
		var newimg = "<img src='" + url + "' />";
		$("<div class='cover'></div>").appendTo("body").hide().fadeTo("fast",.5);
		$("<img class='ajax-load' src='/images/ajax-load.gif' />").appendTo("body");
		$(newimg).load(function(){
			$(".ajax-load").remove();
			$("<div class='pop_img'><a href='#'>Close</a>" + newimg + "</div>").appendTo("body").hide();
			var ypos = $(window).scrollTop() + ($(window).height() / 2) - ($(".pop_img").height()/2);
			$(".pop_img").css({"top":ypos}).fadeIn();
		});
	});
	
	$(".pop_img,.cover,.pop_img a,.pop_img img").live("click",function(e){
		e.preventDefault();
		$(".pop_img").fadeOut();
		$(".cover").fadeOut(function(){
			$(".pop_img,.cover,.ajax-load").remove();
		});
	});
});

function slideIt(d) {
	//sliding var prevents mutliple clicks screwing things up.
	sliding = true;
	
	//set up some vars depending on the direction
	if (d == "prev") {
		curimg--;
		startL = -1 * showWidth;
		endL = showWidth;
	} else {
		curimg++;
		startL = showWidth;
		endL = -1 * showWidth;
	}
	
	//make sure we don't go outside our range of images.
	if (curimg >= images.length) {
		curimg = 0;
	} else if (curimg < 0) {
		curimg = images.length - 1;
	}
	
	var newimg = "<img src='" + images[curimg] + "' />";
	jQuery(newimg).load(function(){
		var slide = "<li>" + newimg + "</li>";
		//slide the images
		jQuery("#slideshow li").addClass("lastimg").animate({left:endL},1000);
		jQuery(slide).appendTo("#slideshow").css({left:startL}).animate({left:'0px'},1000,function(){
			jQuery(".lastimg").remove();
			slidetime = setTimeout("slideIt()",4500);
			sliding = false;
		});
	});
}
