/**
 * Javascript for Enbee_Slider
 * Author - Nick Burchall
 * Year - 2011
 */
 
 // Wait for page to be ready
 $(document).ready(function(){
 	 
 	// If slideshow exists	
 	if($('#banner_slide')){
 		
 		// ----- SET UP
 		
 		// set vars
 		var curSlide = 0;
 		var slideWidth = 880;
 		var slides = $('.slide');
 		var numSlides = slides.length;
 		
 		// hide scrollbar
 		$('#slides').css('overflow', 'hidden');
 		
 		// add an innerwrapper to slideshow and add width
 		slides.wrapAll('<div id="slidesInner"></div>').css({'float' : 'left'});
 		
 		// Set the width of inner slide relative to num of slides
 		$('#slidesInner').css('width', slideWidth * numSlides);
 		
 		
 		// ----- AUTOMATIC ROTATION
 		
 		rotationSwitch = function(){
 			
 			play = setInterval(function(){
 				// Manage rotation
 				if(curSlide != numSlides - 1){
 					rotateRight();
 				}else{
 					backToStart();
 				}
 				
 				updateBtn();
 				
 			}, 4000);
 		
 		}
 		
 		// Normal rotation
 		function rotateRight(){
 			curSlide = curSlide + 1;
 			$('#slidesInner').animate({
 				'marginLeft' : slideWidth * (-curSlide)
 			});
 			updateBtn();
 		}
 		
 		// Rotate left
 		function rotateLeft(){
 			curSlide = curSlide - 1;
 			$('#slidesInner').animate({
 				'marginLeft' : slideWidth * (-curSlide)
 			});
 			updateBtn();
 		}
 		
 		// Go back to start
 		function backToStart(){
 			curSlide = 0;
 			$('#slidesInner').animate({
 				'marginLeft' : 0
 			});
 			updateBtn();
 		}
 		
 		function updateBtn(){
 			$('.active_btn').removeClass('active_btn');
 			$('#slide_btn_'+curSlide).addClass('active_btn');
 		}
 		
 		// Start rotation
 		rotationSwitch();
 		
 		
 		// ----- CONTROLS FOR SLIDESHOW
 		
 		$('#left_arrow').click(function(){ 
 			clearInterval(play);
 			if(curSlide != 0){
 				rotateLeft();
 			}
 			rotationSwitch();
 		});
 		
 		$('#right_arrow').click(function(){
 			clearInterval(play); 
 			if(curSlide != numSlides - 1){
 				rotateRight();
 			}else{
 				backToStart();
 			}
 			rotationSwitch();
 		});
 		
 		// ----- Buttons
 		
 		// Add btns
 		if($('#slide_btns')){
 			
 			for(i=0; i < numSlides; i++){
 				// Add Btns
 				$('#slide_btns').append('<div id="slide_btn_'+i+'" class="slide_btn"></div>');
 				
 				// Make first btn active
 				if(i==0){
 					$('#slide_btn_0').addClass('active_btn');
 				}
 			}
 		}
 		
 		// Btn Controls
 		$('.slide_btn').click(function(){
 			
 			var clicked_slide_id = this.id.split('_');
 			// Make sure value is coverted to an Int
 			var clicked_slide = parseInt(clicked_slide_id[2]);
 			
 			goToSlide(clicked_slide);
 		});
 		
 		function goToSlide(clicked_slide){
 			
 			clearInterval(play);
 			curSlide = clicked_slide;
 			$('#slidesInner').animate({
 				'marginLeft' : slideWidth * (-curSlide)
 			});
 			updateBtn();
 			rotationSwitch();
 		}
 		
 		// ----- PAUSING
 		
 		// Hover to pause and restart rotation
 		$('.slide').hover(function(){
 			clearInterval(play);
 		}, function(){
 			rotationSwitch();
 		});
 	}
 
 }); // END : ready()
