var totalprojects = 0
var active = new Array();

$(function()
{
	
	// redefine Cycle's updateActivePagerLink function
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex){$(pager).find('li').removeClass('activeLI').filter('li:eq('+currSlideIndex+')').addClass('activeLI');};
	
	// Temporary value for numer of projects fetched from db
	// moved to individual pages
	//var totalprojects = 4;
	var slideshowcontainer = "";
	var slideshowbuttons = "";
	// moved to individual pages
	//var active = new Array(true, false, false, false);
	
	// Test function to trace active array
	testArray = function()
	{
		allarray = "";
		
		for(var a=0;a<=(totalprojects-1);a++)
		{
			
			allarray += active[a] + " ";
			
		}
		
		//alert(allarray);
		
	}
	
	pauseSlide = function(which)
	{
		
		$('#s' + which).cycle('pause');
		
	}

	startSlide = function(which)
	{
		
		// Check to see if this slide can be resumed
		// Only the slides which are actually 'playing' can be resumed
		// This is to stop any old slides being able to start after mouse-out
		if(active[which-1]==true)
		{
			
			$('#s' + which).cycle('resume');
			
			//alert("This is the active slideshow so we can resume");
			
		}
		
	}

	stopSlides = function(which)
	{
		
		// Populate the array which records which slideshow is active
		for(var a=1;a<=totalprojects;a++)
		{
		
			$('#s' + a).cycle('pause');
			//*
			
			if(a-1 == which-1)
			{
				
				active[a-1] = true;
				
			}
			else
			{
				
				active[a-1] = false;
				
			}
			//*/
		}
		
		//testArray();
		
		// After stopping all slides, start the correct one
		startSlide(which);
		
	}

	for(var a=1;a<=totalprojects;a++)
	{
		
		slideshowcontainer = "#s"+a;
		
		slideshowbuttons = "#sb"+a;
			
		// Setup slideshow
		$(function()
		{
			$(slideshowcontainer).cycle(
			{
				fx:'fade',
				pause:0,
				timeout:3000,
				speed:500,
				pager:slideshowbuttons,
				pagerAnchorBuilder:function(idx, slide)
				{
					return '<li><a href="#"></a></li>';
				}
			});
		});
		
	}
	
	// Stop all slides and then start the first one
	stopSlides(1);

});