
/*
 * Carousal
 * varsion 1.1
 * 06.04.2009.pm.
 * function to create Carousal Effect
 * requires: 
 * 	   1. Id containing the carousal eg. '#carousal', '#carousal1'
 *     2. Width of the slide in the carousal eg. 332
 */


var Carousal = Carousal ||  {};

Carousal = {
	//global variables
	totalBoxes:0, 	// total boxes
	slideUnit:0,	// width of each box (they have to be same)
	currentBox:1,	// current box being displayed, 1 default
	boxesToShow:1, 	// number of boxes to be handled in one go, 1 default
	 
	init: function(carousalID, slideWidth){	//initialiser function
		
		//if the carousal structure exists on the page
		if ($(carousalID).length > 0  && slideWidth)  {
			
				//setups
				Carousal.slideUnit = slideWidth;
				Carousal.totalBoxes = $('#sliderBelt li').length;
			 	// adjusts the width of the slider-belt, or else all the boxes will not show... only those fitting in the width showup 
	 		    var nwidth = (Carousal.slideUnit * Carousal.totalBoxes) + 'px'; 
			    $('#sliderBelt').css('width', nwidth ); //resize the slider belt width as per the number of boxes
				
				
				if (Carousal.totalBoxes >= Carousal.boxesToShow) {
		
					//activate NEXT button
					$('#next').click(function(){
						if (Carousal.currentBox < Carousal.totalBoxes) {
								Carousal.bringUp(Carousal.currentBox + 1);
						}
						else {
								//alert('you are at last');
						}
					});

					//activate PREV button
					$('#prev').click(function(){
						if (Carousal.currentBox > 1) {
								Carousal.bringUp(Carousal.currentBox - 1);
						}
						else {
								//alert('you are at first');
						}
					});

				}//if

		}//if
	},

	bringUp:function(gotoBox){

			//calculate the units to scroll-animate
			var animateValue = (Carousal.currentBox - gotoBox); 
			animateValue = ((gotoBox - 1) * Carousal.slideUnit)* -1 ;
			var t = animateValue+'px';

			//scroll-animate the sliderBelt
			$("#sliderBelt").animate({
						"left":animateValue+"px"
			}, "slow");

			//reset the currentBox value
			
			Carousal.currentBox = gotoBox;
			
			//reset the first/last navigator buttons

				//if on the last element, disable the next button, else enable it
				if(Carousal.currentBox == Carousal.totalBoxes ){
						$('#next').addClass('atLast');
				}else{
						$('#next').removeClass('atLast');
				}

				//if on the first element, disable the prev button, else enable it 
			
				if(Carousal.currentBox == 1 ){
						$('#prev').addClass('atFirst');
				}else{
						$('#prev').removeClass('atFirst');
				}

		
	}//bringUp()

};
