var num_images; // will be set via the below function which the page that contains this script calls via window.onload
var page;
var slide_speed = 10; // In mse

//The variable page comes from the calling PHP file - only way to get it to work was to set it there and not via the function below
function setup_images(num_images_in, page_index_in)
{
	num_images = num_images_in;
	page = page_index_in;
}


//the below 2 variables are to 'lock' the image_num and image_desc spans in the main file while the carousel is rotating, prevents action if the user clicks multiple times
var slide_finished_1 = true;
var slide_finished_2 = true;

$(".image_num").hide(); // hide all spans
$(".image_desc").hide();
$("#image_num_"+page).show(); // now display the first 2 requested
$("#image_desc_"+page).show();

$("#browser").jCarouselLite({
	btnPrev: "#previous",
	btnNext: "#next",
	scroll: 1,
	speed: slide_speed,
	visible: 1,
	start: page - 1,
	btnGo: [".p1",
		".p2",
		".p3",
		".p4",
		".p5"]
});

$("#previous").click(
	function () {
		if ((slide_finished_1 == true)&&(slide_finished_2 == true)) // only process a click if nothing's happening
		{
			slide_finished_1 = false;
			slide_finished_2 = false;
			var prev_page = page; // store previous image number
			page = (page - 1); // get the next one
			if (page == 0) page = num_images; // for when you reach the last image
			switch_text(page, prev_page);
		}
	}
);
$("#next").click(
	function () {
		if ((slide_finished_1 == true)&&(slide_finished_2 == true))
		{
			slide_finished_1 = false;
			slide_finished_2 = false;
			var prev_page = page;
			page = (page % num_images) + 1;
			switch_text(page, prev_page);
		}
	}
);

function switch_text(page, prev_page) {
	$("#image_num_"+prev_page).fadeTo(slide_speed, 1.0); //this is the best way to get a time delay of 1 second, to tie-in with the one second it takes to load the next image
	$("#image_desc_"+prev_page).fadeTo(slide_speed, 1.0);
	$("#image_num_"+prev_page).hide(1, function() {$("#image_num_"+page).show(); slide_finished_1 = true;});
	$("#image_desc_"+prev_page).hide(1, function() {$("#image_desc_"+page).show(); slide_finished_2 = true;});
	
}

$('#previous').hover(
	function () {
		$(this).attr('src', '/images/browser_arrow_left_on.gif');
		$(this).css('cursor', 'pointer');
	},
	function () {
		$(this).attr('src', '/images/browser_arrow_left_off.gif');
	}
);
$('#next').hover(
	function () {
		$(this).attr('src', '/images/browser_arrow_right_on.gif');
		$(this).css('cursor', 'pointer');
	},
	function () {
		$(this).attr('src', '/images/browser_arrow_right_off.gif');
	}
);


