/*
Title: Slideshow
Author: Hans Vedo, hans@cultivate.it
2010-11-23: Updated to perform AJAX requests.
2010-07-29: Created
*/

// Slideshow
$(document).ready(function(){
	// Hide this at first.
	$('#slideshow_select').hide();
	
	// Load the first image if there's only one.
	if(document.getElementById("slideshow_records") && document.getElementById("slideshow_records").value == 1){
		$("#image-placeholder").load(
			'/wp-content/plugins/slideshow/public/api/image.php?image=1'
			, function(){
				$("#image-placeholder").fadeIn(
					"slow"
					,function(){

					}	
				);		
			}
		);	
	}
	
	// Homepage: Only activate the slideshow if there are more than two records.
	if(document.getElementById("slideshow_records") && document.getElementById("slideshow_records").value >= 2){
		// Settings
		images_total = document.getElementById("slideshow_records").value;
		slideshow_counter = 2;
		previous_slideshow_counter = 2;
		
		// Highlight the 1st selector
		$('.slideshow_selector').css("background-color","#000000");
		$("#1").css("background-color","#666666");
		
		// Load the first image.
		$("#image-placeholder").hide();
		$("#image-placeholder").load(
			'/wp-content/plugins/slideshow/public/api/image.php?image=1'
			, function(){
				$("#image-placeholder").fadeIn(
					"slow"
					,function(){
						// Get the dimensions of the navigation.
						selector_height = $('#slideshow_select').height();
						selector_width = $('#slideshow_select').width();
		
						// Position the navigation.
						var image_height = $("#image-placeholder img").attr('height');
						var image_width = $("#image-placeholder img").attr('width');
						$('#slideshow_select').css('left',(image_width-selector_width-10));
						$('#slideshow_select').css('top',(image_height-selector_height-20));
						$('#slideshow_select').show();
					}	
				);
				
				// Set an interval to rotate the images.
				rotate_interval = setInterval("rotate_images()",7000);		
			}
		);
	}
	
	// Product Details Page Thumbnails
	if(document.getElementById("slideshow_select")){
		$('.slideshow_selector').click(function(index){
			// Adjust the background colours of the selectors.
			$('.slideshow_selector').css("background-color","#000000");
			$(this).css("background-color","#666666");
			
			// Get the selected image ID.
			selected_id = $(this).attr('id');

			// Load the selected image.
			$("#image-placeholder").fadeOut("fast");
			$("#image-placeholder").load(
				"/wp-content/plugins/slideshow/public/api/image.php?image="+selected_id
				, function(){
					$("#image-placeholder").fadeIn("slow");		
				}
			);	

			// Stop the slideshow.
			clearInterval(rotate_interval);
		});
	};
});

// Homepage: Rotate the images.
function rotate_images(){
	// Reset the previous slideshow_counter.
	previous_slideshow_counter = (slideshow_counter-1);
	if(slideshow_counter == 1){
		previous_slideshow_counter = images_total;
	}
	
	// Adjust the background colours of the selectors.
	$('.slideshow_selector').css("background-color","#000000");
	$("#"+slideshow_counter).css("background-color","#666666");	
	
	// Load the next image.
	$("#image-placeholder").fadeOut("fast");
	$("#image-placeholder").load(
		"/wp-content/plugins/slideshow/public/api/image.php?image="+slideshow_counter
		, function(){
			$("#image-placeholder").fadeIn("slow");		
		}
	);	
	
	// Reset the slideshow_counter.
	slideshow_counter = (slideshow_counter+1);
	if(slideshow_counter > images_total){
		slideshow_counter = 1;
	}
}
