//Total slides
var total = 3;
//Left position
var l_pos = "-155px";

//Set up variables
var slides = new Array();
var current_slide;
var count = 0;
var interval = "";
var toggle_extend = false;
var loading = false;
var loading_thumb = false;

//Once the window is loaded
$(window).load(function() {
	//Disable highlighting
	$("#slides_wrap").css({
		"-webkit-user-select" : "none",
		"-khtml-user-select" : "none",
		"-moz-user-select" : "none",
		"-o-user-select": "none",
		"user-select" : "none"
	});
	//Set opacity for thumbnails and rollovers
	$("#slide_thumbs_wrap a.slide_thumb").fadeTo(100, 0.6);
	$("#slide_thumbs_wrap a.slide_thumb").hover(
		function() {
			$(this).fadeTo(150, 1);
		},
		function() {
			$(this).fadeTo(150, 0.6);
		}
	);
	//For each slide push into array and push z-index values
	for (var a = 1; a < total+1; a++) {
		slides.push("#slide_"+a);
	}
	//Set the z-index for each slide
	for (var b = 0; b < slides.length; b++) {
		if (b != 0) {
			$(slides[b]).hide();
		}
	}
	//Set current slide
	current_slide = slides[0];
	interval = window.setInterval("change_slide()", 4000);
	//$("#slide_thumbs_wrap").hide();
});

//Change the slide
function change_slide() {
	//If it's not already loading
	if (loading == false) {
		//It's loading now
		loading = true;
		//Fade out current slide
		$(current_slide).fadeOut(400, function() {
			//Once complete:
			//If it's the last slide - reset counter, if not plus the counter
			if (count == total-1) {
				count = 0;
			} else {
				count++;
			}
			//Fade the new slide in
			$(slides[count]).fadeIn(400, function() {
				loading = false;
			});
			//Reset the current slide
			current_slide = slides[count];
		});
	}
}

//When a thumb is selected
function select_thumb(thumb) {
	//If it's not already loading
	if (loading == false) {
		//It's loading now
		loading = true;
		//Clear the interval so there's no cross fade
		window.clearInterval(interval);
		//Get values for the new slide
		for (var c = 0; c < slides.length; c++) {
			//Match the selected slide with the array and reset count
			if (thumb == slides[c]) {
				count = c; 
			}
		}
		//Fade out current slide
		$(current_slide).fadeOut(400, function() {
			//Fade the new slide in
			$(slides[count]).fadeIn(400, function() {
				loading = false;
			});
			//Reset the current slide
			current_slide = slides[count];
			//Set the interval again
			interval = window.setInterval("change_slide()", 4000);
		});
	}
}

//Extend
function extend(path) {
	if (toggle_extend == false) {
		toggle_extend = true;
		$("#slide_controls_wrap").animate({left: '0'}, 200);
		$("#slide_extend span").css({"background" : "url("+path+"/imgs/home_slideshow/expanded_arrow.png) no-repeat" });
	} else {
		toggle_extend = false;
		$("#slide_controls_wrap").animate({left: l_pos}, 200);
		$("#slide_extend span").css({"background" : "url("+path+"/imgs/home_slideshow/expand_arrow.png) no-repeat" });
	}
}
