/**********************************************
* Carrousel v1.0							  *
* By Intermix Management					  *
* 01-09-2009 								  *
*  								  			  *
* Jordy Houtman								  *
**********************************************/

	// initiatlize the slideshow
	var current_slide = 0;
	var max_slides = '';
	var c=0
	var delay_time = 5 // in seconds
	var t = '';
	
	// timefunction for the script 
	function timedCount()
	{
		c=c+1;
		if( c > delay_time)
		{
			nextSlide();
			c=0;
		}
		t=setTimeout("timedCount()",1000);
	}	
	
	// clear the style to standard CSS values
	function clearStyle(id)
	{
		$(id).removeClassName('selected')	
	}
	

	// clear all styles function
	function clearStyles()
	{
	
		var children = $$('#Carrousel a');
		for (var i =0; i < children.length; i++ )
		{
			clearStyle(children[i].id)
		}
	}
	

	function initialize()
	{
		/* Hook functions to the carrousel*/
		var children = $$('#Carrousel a');	
		for (var i =0; i < children.length; i++ )
		{
			
			
			// clear all the links which may otherwise redirect them
			// when javascript is not active
			
			// ************** hide links **************** //
			// $(children[i].id).href = '#';
			
			// hook the function ('click'  or 'mouseover' ) .. if it's on click,...remove the link (by uncommenting the "hide links"
			$(children[i].id).observe('mouseover', function()
			{
				// selectSlide(this.id)	
			});	
			
		}
	}
	
	

	
	// initialize and start slideshow
	function startSlideshow()
	{
		var children = $$('#Carrousel a');

		current_slide = 0;
		max_slides = children.length;
		$(children[0]).toggleClassName('selected', '');
		// start counting
		timedCount();
	}

	
	function selectSlide(id, number)
	{
		var image_array = new Array();
		var link_array = new Array();
		var children = $$('#Carrousel a');
		for (var i =0; i < children.length; i++ )
		{
			image_array[i] = 'img_'+children[i].id
			if(image_array[i]=='img_'+id)
			{
				current_slide = i
				
			}
			$(image_array[i]).setStyle('display: none;');
		}
		
		var image = 'img_'+id;

		// apply the transition effect with a que
		clearStyles();
		$(id).addClassName('selected');
		
		 
		 $(image).setStyle('display: block;');
		
			// Restart counting, making sure the slideshow slide as expected.

			// new Effect.toggle(image_array[current_slide], 'appear', {duration: 0.5});		
			// new Effect.toggle(image, 'appear', {duration: 0.5, delay: 0.5});	
			
			current_slide = id.substring(4,5);
			
			clearTimeout(t);
			t = setTimeout("timedCount()",3000);
			timedCount();	
		
	}
	
	
	// function to go to the next slyde
	function nextSlide()
	{
		var image_array = new Array();
		var link_array = new Array();
		var children = $$('#Carrousel a');
		for (var i =0; i < children.length; i++ )
		{
			image_array[i] = 'img_'+children[i].id
			link_array[i] = children[i].id
		}
		
			
		if(current_slide+1 <= max_slides-1)
		{
			next_image_number = current_slide+1	
		}
		else
		{
			next_image_number = 0;	
		}	

		// apply the transition effect with a que
		new Effect.toggle(image_array[current_slide], 'appear', {duration: 0.5});
		new Effect.toggle(image_array[next_image_number], 'appear', {duration: 0.5, delay: 0.5});
		current_slide = next_image_number;
		
		// clear all styles to make sure even when pressed fast there will always be no button activated
		clearStyles();
		
		$(link_array[next_image_number]).toggleClassName('selected', '');		
	}
	
	Event.observe(window, 'load', function () 
	{
		if ($('Carrousel') != undefined){
			initialize();
			startSlideshow();	
		}
	})

	