/*
	Created by Simon Keefe for Cornerstone Technologies
	3/11/10
	skeefe@cstech.net.au
	cstech.net.au
*/

$(function(){
	$(function(){



		var BannerQuantity = $('#Rotator ul:first li').length;

		var RunRotate = true;

		var SelectionQueue = false;

		

		/* Prevents the last image displaying on load. */

		$('#Rotator ul:first > li').each(function(Index){

			$(this).css('zIndex', '' + ((BannerQuantity - 1) - Index) + '');

		});

		$('#Rotator ul li').show();

		

		setTimeout(function() {Rotate(0);}, 5000);

		

		/* Setup banner navigation. */

		var Counter = 0;

		$('#Rotator ul:first > li').each(function(Index){

			Counter = 0;

			while(Counter < BannerQuantity){

				if(Index == Counter){

					$(this).children('ul.Index').append('<li><a rel="' + (Counter + 1) + '" class="Current">&nbsp;</a></li>');

					Counter++;

				}

				else{

					$(this).children('ul.Index').append('<li><a rel="' + (Counter + 1) + '"></a></li>');

					Counter++;

				}

			}

		});

		

		function Rotate(Current){

			if(RunRotate == true){

				Current = Current % BannerQuantity;				

				//Change the number to change the amount of time as image is fading (unit is milliseconds).

				$('#Rotator ul:first > li').eq(Current).fadeOut(1500, function(){

					//Reorder the z-index

					$('#Rotator ul:first > li').each(function(Index){

						//Calculate z-index

						var ZIndexLevel = ((BannerQuantity - Index) + Current) % BannerQuantity				

						$(this).css({

							'zIndex' : ZIndexLevel,

							'display' : 'block'

						});

					});	

					//Change the number to change the amount of time an image is fully visible (unit is milliseconds).

					setTimeout(function() {Rotate(++Current);}, 5000);

				});

			}

			else{

				RestartRotate(RunRotate);

				return false; //Probably this one or the next one is not necessary.

			}

			return false; //Probably this one or the first one is not necessary.

		}

	

		/* Calls "BannerSelected" to display the selected banner. */

		$('#Rotator ul ul.Index li a').click(function(){		

			if(SelectionQueue == false){

				var SelectedBanner = $(this).attr('rel');

				BannerSelected(SelectedBanner);

			}

			else{

				SelectionQueue = $(this).attr('rel');


			}

		});

		

		function BannerSelected(SelectedBanner){

			//Ensure only 1 click is being processed at a time.

			SelectionQueue = true;



			//This stop is used to stop all movement caused by previous clicks of the nav buttons. Possibly no longer required.

			$('#Rotator ul:first > li').each(function(){

				$(this).stop(true, true);

			});

			

			//Place selected banner on top and fade in. Possilbly not required.

			$('#Rotator ul:first > li:eq(' +  (SelectedBanner - 1) + ')').css({

				'zIndex' : BannerQuantity,

				'display' : 'block',

				'opacity' : '1'

			});

			

			//Switch the Rotator class off and store selected banner index.

			RunRotate = SelectedBanner;



			//Re-allocate z-indexes.

			var ZIndexLevel = 0;

			SelectedBanner = (SelectedBanner - 2) % BannerQuantity;

			$('#Rotator ul:first > li').each(function(Index){

				//Calculate z-index

				 ZIndexLevel = ((BannerQuantity - Index) + SelectedBanner) % BannerQuantity;	

				$(this).css({

					'zIndex' : ZIndexLevel,

					'display' : 'block',

					'opacity' : '1'

				});

			});

			

			if(SelectionQueue == true){

				SelectionQueue = false;

			}

			else{

				BannerSelect(SelectionQueue);


			}

		}

		

		function RestartRotate(SelectedBanner){

			RunRotate = true;

			//SelectionQueue = false;

			setTimeout(function() {Rotate(SelectedBanner - 1);}, 4000);

		}


	});

});

