var image_width = 100;
var margin_width = 12;
var num_images = 0;
var slthumbs_width = 0; 
var current_image = 0;

$('document').ready(function(){

	image_width = 100;
	margin_width = 12;
	num_images = $('#slthumbs').children().length;
	slthumbs_width = (num_images * (image_width + margin_width)) + margin_width;
	//$('#slthumbs').css('width' , slthumbs_width + 'px');
	greyOutButtons('init');

	$('#slthumbs').children().click(function(){
		var large_image = $(this).attr('href');
		setSlideshowImage(large_image);
		current_image = $(this).prevAll().length;
		return false;
	});
	
	// Back and forward buttons on thumbs
	$('#slforward').click(function(){
		var left = $('#slthumbs').css('left');
		var left_int = parseInt(left.replace(/[^0-9\-]/g , ''));
		if(left_int > (((image_width + margin_width) * 5) - slthumbs_width)){
			//var new_pos = left_int - (image_width + margin_width);
			var new_pos = left_int - (((image_width + margin_width) * 4));
			$('#slthumbs').animate( { left: new_pos } , 600 , 'swing' , slCheckPos);
		} else {
			new_pos = left_int;
		}
		greyOutButtons(new_pos);
		return false;
	});
	$('#slback').click(function(){
		var left = $('#slthumbs').css('left');
		var left_int = parseInt(left.replace(/[^0-9\-]/g , ''));
		if(left_int < 0 && left_int > (0 - slthumbs_width)){
			//var new_pos = left_int + (image_width + margin_width);
			new_pos = left_int + ((image_width + margin_width) * 4);
			$('#slthumbs').animate( { left: new_pos }, 600 , 'swing' , slCheckPos);
		} else {
			new_pos = left_int;
		}
		greyOutButtons(new_pos);
		return false;
	});
	
	$('#slnext').click(function(){
		imgindex = current_image + 1;
		if($('#slthumbs').children('a:eq(' + imgindex + ')').attr('href')){
			setSlideshowImage($('#slthumbs').children('a:eq(' + imgindex + ')').attr('href'));
			current_image++;
		};
		return false;
	});
	
	$('#slprevious').click(function(){
		imgindex = current_image - 1;
		if($('#slthumbs').children('a:eq(' + imgindex + ')').attr('href')){
			setSlideshowImage($('#slthumbs').children('a:eq(' + imgindex + ')').attr('href'));
			current_image = current_image - 1;
		};
		return false;
	});
	$('#slthumbs_wrap').hover(
		function() {
			$(this).fadeTo("medium", "1");
			$('#slthumbs').fadeTo("medium", "1");
			//$('#slthumbs').children().css("display", "block");
		},
		function() {
			$(this).fadeTo("medium", "0.4");
			$('#slthumbs').fadeTo("medium", "0.2");
			//$('#slthumbs').children().css("display", "none");
	});
	

});	


//Change slideshow image
function setSlideshowImage(url){
	$('#slideshow').css('background-image' , 'url(\'/layout/slideshow_loading_image.gif\')');
	newimg = new Image();
	//newimg.src = url;
	$(newimg)
	.load(function(){
		$('#slideshow').css('background-image' , 'url(\'' + url + '\')');
	})
	.attr('src' , url);
}

// Next and previous buttons on the current larg image
function greyOutButtons(new_pos){
	if(new_pos == 'init'){
		var left = $('#slthumbs').css('left');
		var left_int = parseInt(left.replace(/[^0-9\-]/g , ''));
	} else {
		left_int =  parseInt(new_pos);
	}
	if(left_int >= 0){
		$('#slback').addClass('greyout');
	} else {
		$('#slback').removeClass('greyout');
	}
	//alert(left_int);
	//alert(slthumbs_width - ((image_width + margin_width) * 4));
	if(left_int < (((image_width + margin_width) * 5) - slthumbs_width)){
		$('#slforward').addClass('greyout');
	} else {
		$('#slforward').removeClass('greyout');
	}

}

//Checks the position of the lider, to make sure it hasn't vanished!
function slCheckPos(){
	var left = $('#slthumbs').css('left');
	if(left > 0 || left < (0 - slthumbs_width)){
		$('#slthumbs').css('left' , '0');
	}

}