jQuery(document).ready(function() {

	// uncomment the following two lines to enable auto rotate
	// var speed = 5000;
	// var run = setInterval('rotate()', speed);	
	
	var current_group = 0;
	var item_width;
	var loaded_groups;
	var domain = 'http://'+window.location.hostname;
	
	// find the random array in the page and store in an array
	var carousel_IDs = jQuery('#carousel_IDs').val().split(",");
	// number of items
	var item_numbr = carousel_IDs.length;
	// number of groups - will become <li>
	if (item_numbr == 1) {
		jQuery.ajax({
		url: domain,  
		type: 'GET',  
		data: "carousel_IDs="+carousel_IDs[0],
		success: function(result) {  
			// avoid duplicate thickbox if already attached
			jQuery('a.thickbox').removeClass('thickbox');
			// append new <li>
			jQuery('#singe_carousel_'+carousel_groups[0]).append(result);
			// attach thickbox to new items
			tb_init('a.thickbox, area.thickbox, input.thickbox');
		} 
	});

	}
	var item_groups = Math.ceil(item_numbr/3);
	// new array with grouped IDs
	var carousel_groups = new Array();
	var i=0;
	var n='';
	for (i=0; i<item_groups; i++) {
		n=carousel_IDs[i*3];
		if (carousel_IDs[i*3+1]) {
			n = n+','+carousel_IDs[i*3+1];
		}
		if (carousel_IDs[i*3+2]) {
			n = n+','+carousel_IDs[i*3+2];
		}
		carousel_groups.push(n);
	}

	// fill the li elements with the first three sets of the array
	jQuery.ajax({
		url: domain,  
		type: 'GET',  
		data: "carousel_IDs="+carousel_groups[0],
		success: function(result) {  
			// avoid duplicate thickbox if already attached
			jQuery('a.thickbox').removeClass('thickbox');
			// append new <li>
			jQuery('#slides ul').append(result);
			// attach thickbox to new items
			tb_init('a.thickbox, area.thickbox, input.thickbox');
			// hide spinning wheel
			jQuery("#carousel_loading").hide();
		}  
	});
	jQuery.ajax({
		url: domain,  
		type: 'GET',  
		data: "carousel_IDs="+carousel_groups[1],
		success: function(result) {  
			// avoid duplicate thickbox if already attached
			jQuery('a.thickbox').removeClass('thickbox');
			// append new <li>
			jQuery('#slides ul').append(result);
			// attach thickbox to new items
			tb_init('a.thickbox, area.thickbox, input.thickbox');
		}
	});
	jQuery.ajax({
		url: domain,  
		type: 'GET',  
		data: "carousel_IDs="+carousel_groups[2],
		success: function(result) {
			// avoid duplicate thickbox if already attached
			jQuery('a.thickbox').removeClass('thickbox');
			// append new <li>
			jQuery('#slides ul').append(result);
			// attach thickbox to new items
			tb_init('a.thickbox, area.thickbox, input.thickbox');
		} 
	});	
	
	// move the last item before first item, just in case user click prev button
	// jQuery('#slides li:first').before(jQuery('#slides li:last'));

	// set the default item to the correct position 
	// jQuery('#slides ul').css({'left' : left_value});

	// if user clicked on prev button
	jQuery('#carousel_prev').click(function() {
		// update resulting carousel position
		current_group--;
		//temporarily disable link
		jQuery("#carousel_prev").hide();
		// show spinning wheel
		item_width = jQuery('#slides li').innerWidth();
		jQuery('#slides ul').animate({left : '+='+item_width}, 200, function(){	
			// on the 2nd to last group show the right arrow
			if (current_group < item_groups) {
				jQuery("#carousel_next").show();
			}
			// on the 1st group hide the left arrow
			if (current_group != 0) {
				jQuery("#carousel_prev").show();
			}
			// move the last item and put it as first item				
			// jQuery('#slides li:first').before(jQuery('#slides li:last'));

			// set the default item to correct position
			// jQuery('#slides ul').css({'left' : left_value});
		
		});

		// cancel the link behavior			
		return false;
			
	});
 
	// if user clicked on next button
	jQuery('#carousel_next').click(function() {
		// update resulting carousel position
		current_group++;
		//temporarily disable link
		jQuery("#carousel_next").hide();
		// get the right position
		item_width = jQuery('#slides li').innerWidth();
		var left_indent = parseInt(jQuery('#slides ul').css('left')) - item_width;
		// var left_value = item_width * (-1);

		// slide the item
		jQuery('#slides ul').animate({'left' : left_indent}, 200, function () {
			// if th carousel is near to the end of loaded elements
			loaded_groups = jQuery("#slides ul li").length;
			if (current_group == loaded_groups && loaded_groups < item_groups) {
				// show spinning wheel
				jQuery("#carousel_loading").show();
				jQuery.ajax({
					url: domain,
					type: 'GET',  
					data: "carousel_IDs="+carousel_groups[loaded_groups],
					success: function(result) {
						// hide spinning wheel
						jQuery("#carousel_loading").hide();
						// avoid duplicate thickbox if already attached
						jQuery('a.thickbox').removeClass('thickbox');
						// append new <li>
						jQuery('#slides ul').append(result);
						// update width of element
						jQuery('#slides ul').width(600*(loaded_groups+1));
						// attach thickbox to new items
						tb_init('a.thickbox, area.thickbox, input.thickbox');
					}
				});
			}
			// move the first item and put it as last item
			// jQuery('#slides li:last').after(jQuery('#slides li:first'));				 	
			
			// set the default item to correct position
			// jQuery('#slides ul').css({'left' : left_value});
			
			// on the last group hide the right arrow
			if (current_group < item_groups-1) {
				jQuery("#carousel_next").show();
			}
			// on the 2nd group show the left arrow
			if (current_group > 0) {
				jQuery("#carousel_prev").show();
			}

		});

		// cancel the link behavior
		return false;

	});
	// if mouse hover, pause the auto rotation, otherwise rotate it
/*	jQuery('#slides').hover(
		
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('rotate()', speed);	
		}
	);
*/

});


// a simple function to click next link
// a timer will call this function, and the rotation will begin :)
function rotate() {
	jQuery('#carousel_next').click();
}