/**********************************************/
/*  KaCarousel.js - Copyright Kaweb Ltd. 	  */
/*  Use only with permission. www.kaweb.co.uk */
/*  Author: Peter Pearson.  				  */
/**********************************************/


function kacarousel_simple(parent_ul, 
					left_link, 
					right_link, 
					page_indicators, 
					indicator_type,
					isRandom)
{
	kacarousel_full(parent_ul, 
				left_link, 
				right_link, 
				page_indicators, 
				indicator_type,
				isRandom,
				true,
				850,
				'easeInOutQuint',
				'yes',
				7500);
				
}

function kacarousel_full(parent_ul, //carousel ul obj
					left_link, //left arrow obj
					right_link, //right arrow obj
					page_indicators, //indicator panel obj
					indicator_type, //bullet or numeric
					isRandom,
					scrolling, //true/false
					animation_speed, //ms
					animation_effect, //easing?
					autoscroll, //true/false
					autoscroll_wait //wait between autoscrolls, in ms
					)
{
	var carousel_width = parseInt($(parent_ul).children('li').css('width'));
	var number_elements = $(parent_ul).children('li').size();
	var currentItem = 0;
	var currentlyMoving = '';
	var intervalID = 0;
	
	moveLastToFirst = function ()
	{
		var last = $(parent_ul).children('li').last();
		$(parent_ul).children('li').last().remove();
		$(parent_ul).prepend(last);
		current_position = parseInt($(parent_ul).css('marginLeft'));
		current_position = Math.round(current_position);
		newPos = current_position-carousel_width;
		$(parent_ul).css( 'marginLeft', newPos+'px');
	}
	
	moveFirstToLast = function()
	{
		var first = $(parent_ul).children('li').eq(0);
		$(parent_ul).children('li').eq(0).remove();
		$(parent_ul).append(first);
		current_position = parseInt($(parent_ul).css('marginLeft'));
		current_position = Math.round(current_position);
		newPos = current_position+carousel_width;
		$(parent_ul).css( 'marginLeft', newPos+'px');
	}
	
	finishSlide = function()
	{
		//align it up
		current_position = parseInt($(parent_ul).css('marginLeft'));
		current_position = Math.round(current_position);
		$(parent_ul).css('marginLeft', current_position);
		
		//are we at a final spot?
		if (current_position%carousel_width == 0)
		{
			currentlyMoving = '';
		}
	}
	
	if (indicator_type == 'bullet')
	{
		populate_indicator = function()
		{
			for (i = 0; i < number_elements; i++)
			{
				style = '';
				if (i==0)
				{
					style = ' style="color: rgb(49, 173, 223);"';
				}
				thelink = $('<li><a href="#" onclick="slideTo('+i+'); return false;"'+style+'>&bull;</a></li>');
				page_indicators.append(thelink);
				thelink = null;
			}
		}
		
	}
	else if (indicator_type == 'numeric')
	{
		populate_indicator = function()
		{
			for (i = 0; i < number_elements; i++)
			{
				page_indicators.append('<li>'+i+'</li>');
			}
		}
	}
			
	if (scrolling == true)
	{
		slide = function(distance)
		{
			$(parent_ul).animate({marginLeft: '+='+distance}, animation_speed, animation_effect, finishSlide);
		}
	}
	else
	{
		slide = function(distance)
		{
			$(parent_ul).animate({marginLeft: '+='+distance}, 0);
		}
	}
	
	fadeBullet = function (elementID, itemsGlowing)
	{
		fadeSpeed = Math.round(animation_speed/itemsGlowing);
		$(page_indicators).children('li').eq(elementID).children('a').animate({color: "#000000"}, fadeSpeed);
	}
	
	shortGlow = function ( elementID, itemNum, itemsGlowing)
	{
		//if there are 5 items glowing and this is the 3rd, we want it to glow up between 0.4 and 0.5 and down between 0.5 and 0.6
		glowSpeed = Math.round(animation_speed/itemsGlowing);
		queueToStart = glowSpeed*itemNum;
		glowSpeed = Math.round(glowSpeed / 2);
		setTimeout(
						function() {$(page_indicators).children('li').eq(elementID).children('a').animate( {color: "#31ADDF"}, glowSpeed);},
							queueToStart
						  )
		setTimeout(
						function() {$(page_indicators).children('li').eq(elementID).children('a').animate({color: "#000000"}, glowSpeed);},
						   (queueToStart + glowSpeed+1)
				  );
	
	}
	
	glowBullet = function (elementID, itemsGlowing)
	{
		glowSpeed =  Math.round(animation_speed/itemsGlowing);
		queue = animation_speed - glowSpeed;
		if (queue > 0)
		{
			setTimeout(function() {$(page_indicators).children('li').eq(elementID).children('a').animate( {color: "#31ADDF"}, glowSpeed);}, queue)
		}
		else
		{
			$(page_indicators).children('li').eq(elementID).children('a').animate( {color: "#31ADDF"}, glowSpeed);
		}
	}
	
	resetIntervalSlide = function() {
		if (intervalID > 0)
		{
			clearInterval(intervalID);
			intervalID = setInterval(function () {$(right_link).click();}, autoscroll_wait);
		}
	}
	
	slideTo = function(element)
	{
		if (currentlyMoving == '')
		{
			resetIntervalSlide();
			element = element+1;
			positionInArray = currentItem+1; 
			elementsMoved = Math.abs((currentItem+1)-element)+1;
			fadeBullet(currentItem, elementsMoved);
			differenceFromTarget = (positionInArray - element)*carousel_width;
			halfWayPoint = (number_elements*carousel_width)/2;
			absDifference = Math.abs(differenceFromTarget);
			if (differenceFromTarget == 0)
			{
				//nathin
			}
			else if (absDifference == halfWayPoint)
			{
				differenceFromTarget = -absDifference;
			}
			else if (differenceFromTarget >= halfWayPoint)
			{
				differenceFromTarget = differenceFromTarget - (number_elements*carousel_width); 
			}
			else if (differenceFromTarget <= -halfWayPoint)
			{
				differenceFromTarget = (number_elements*carousel_width) + differenceFromTarget;
			}
			
			if (differenceFromTarget < 0)
			{
				currentlyMoving = 'right';
				slides = Math.abs(Math.floor(differenceFromTarget/carousel_width));
				for(i=0; i<slides; i++)
				{
					moveFirstToLast();
					currentItem++;
					if (currentItem >= number_elements)
						currentItem = 0;
					if (i+1 < slides)
					{
						shortGlow(currentItem, i+1, slides+1);
					}
				}
				
			}
			else
			{
				currentlyMoving = 'left';
				slides = Math.abs(Math.floor(differenceFromTarget/carousel_width));
				for(i=0; i<slides; i++)
				{
					moveLastToFirst();
					currentItem--;
					if (currentItem < 0) 
						currentItem = number_elements-1;
					if (i+1 < slides)
					{
						shortGlow(currentItem, i+1, slides+1);
					}
				}
				
			}
			slide(differenceFromTarget);
			glowBullet(currentItem, elementsMoved);
		}
		
	}
	
	$(left_link).click(function() {
		current_position = parseInt($(parent_ul).css('marginLeft'));
		next_available_position = ( Math.ceil(current_position/carousel_width) * carousel_width);
		distance = next_available_position - current_position;
		
		
		//are we at the start and already moving?
		if (currentlyMoving == '' && distance == 0)
		{
			resetIntervalSlide();
			fadeBullet(currentItem, 2);
			distance = carousel_width;
			currentItem--;
			if (currentItem < 0) 
				currentItem = number_elements-1;
			currentlyMoving = 'left';
			$(parent_ul).stop(true, false);
			moveLastToFirst();
			slide(distance);
			glowBullet(currentItem, 2);
		}
		
		return false;
	});
	
	$(right_link).click(function() {
		
		current_position = parseInt($(parent_ul).css('marginLeft'));
		next_available_position = ( Math.floor(current_position/carousel_width) * carousel_width);
		distance = next_available_position - current_position;
		//are we at the start and already moving?
		if (currentlyMoving == '' && distance == 0)
		{
			resetIntervalSlide();
			fadeBullet(currentItem, 2);
			distance = -carousel_width;
			currentItem++;
			if (currentItem >= number_elements)
				currentItem = 0;
			currentlyMoving = 'right';
			$(parent_ul).stop(true, false);
			moveFirstToLast();
			slide(distance);
			glowBullet(currentItem, 2);
		}

		return false;
	});
	
	//first thing to do is grab last element, shove it in front of the first to give a 'left buffer'
	
	populate_indicator();
	
	itemsToCenter = Math.floor(number_elements/2);
	for ( i=0; i< itemsToCenter; i++)
	{
		moveLastToFirst(parent_ul);
	}
	
	if (isRandom)
	{
		tempSpeed =animation_speed;
		animation_speed = 0;
		sliding = Math.floor(Math.random()*number_elements);
		slideTo(sliding);
		animation_speed = tempSpeed;
	}
	intervalID = setInterval(function () {$(right_link).click();}, autoscroll_wait);
	
}

