
// JavaScript Document
if (!window.jQuery) {
	throw("jQuery must be referenced before using formValidation");
} else {
	(function($){
		$.fn.carousel = function(options) {
			
			var settings = {
				timeout			: 5000,
				animateSpeed	: 500,
				pauseOnMouseOver: false,
				direction		: 'x',
				circular		: true,
				effect			: 'slide',
				onClick			: false,
				clickMenu		: '',
				clickMenuSel	: '',
				galleryCtrl		: ''
			};
			var Container = this;					
			var TInt;
			var currentItem = 1;
			var totalItems = $('#' + this.attr('id') + ' .container > li').length;		
			
			if(options) {
				jQuery.extend(settings, options);
			}	
			
			if(settings['galleryCtrl'] != ""){
				currentItem = 0;
			}
			
			if(settings['circular'] == true){
				totalItems++;
				$('#' + this.attr('id') + ' .container li').eq(0).clone().appendTo($('#' + this.attr('id') + ' .container'));
			}			
			
			if(settings['direction'] == 'x'){
				var totalWidth = parseInt(this.width()) * totalItems;
				$('#' + this.attr('id') + ' .container').css("width",totalWidth);
			} else {
				var totalWidth = parseInt(this.height()) * totalItems;
				$('#' + this.attr('id') + ' .container').css("height",totalWidth);
			}
			
			function startCarousel(){
				TInt = setInterval(goNext,settings['timeout']); //Time Interval
				//alert(settings['animation_length']);
			}//end function
			
			function goNext(){
				if(settings['clickMenu'] != ''){
					select_clickMenu_element();
				}//end if
				if(settings['direction'] == 'x'){
					var newPos = parseInt(Container.width()  *currentItem);
					$('#' + Container.attr('id') + ' .container').animate({					
						left: '-'+newPos	
					},settings['animateSpeed'],function(){
						if(settings['galleryCtrl'] == ""){
							currentItem++;	
						}//end if
						if(settings['circular'] == true){
							if(currentItem == totalItems){
								$('#' + Container.attr('id') + ' .container').css("left","0");								
							}
						}//end if
												
						if(currentItem == totalItems){
							currentItem = 0;	
						}	//end if
						
						if(settings['circular'] == true){
							if(currentItem == 0){								
								goNext();
							}
						} 
					});
				} else {
					var newPos = parseInt(Container.height()  *currentItem);
					$('#' + Container.attr('id') + ' .container').animate({		
						top: '-'+newPos					
					},settings['animateSpeed'],function(){
						if(settings['galleryCtrl'] == ""){
							currentItem++;	
						}//end if		
						if(settings['circular'] == true){
							if(currentItem == totalItems){
								$('#' + Container.attr('id') + ' .container').css("top","0");								
							}
						}
									
						if(currentItem == totalItems){
							currentItem = 0;	
						}	
						
						if(settings['circular'] == true){
							if(currentItem == 0){								
								goNext();
							}
						}
					});	
				}
				//$('#' + Container.attr('id') + ' .container').css("left","-"+(parseInt(Container.width()*currentItem))+"px");	
								
			}//end function			
			
			function goTo(itemNumber){
				currentItem = itemNumber;
				if(settings['direction'] == 'x'){
					var newPos = parseInt(Container.width()  *currentItem);
					$('#' + Container.attr('id') + ' .container').animate({					
						left: '-'+newPos	
					},settings['animateSpeed']);
				} else {
					var newPos = parseInt(Container.height()  *currentItem);
					$('#' + Container.attr('id') + ' .container').animate({		
						top: '-'+newPos					
					},settings['animateSpeed']);	
				}
			}
			
			function stopCarousel(){
				clearInterval(TInt);  
			}//end function
			
			if(settings['pauseOnMouseOver'] == true){			
				Container.mouseover(function(){
					stopCarousel();
				});				
				Container.mouseout(function(){
					startCarousel();
				});
			}//end if			
			/*
			if($('.carouselControl').length > 0){
				$('.carouselControl a').click(function(e){
					e.preventDefault();
					var clickPos = $(this).attr("href");
					clickPos = clickPos.replace("#","");
					clickPos = parseInt(clickPos);
					clickPos--;
					currentItem = clickPos;
					goNext();
					return false;	
				});
			}
			*/
			
			//check if we have the gallery control thingy
			if(settings['galleryCtrl'] != ""){
				update_scrollers();
				$(settings['galleryCtrl'] + ' a').click(function(e){
					e.preventDefault();		
					
					//alert("now " + currentItem + " > " + totalItems);
								
					if($(this).attr("rel") == "next"){
						if(currentItem < (totalItems)){
							currentItem++;							
							goTo(currentItem);							
						}						
					
					} else {
						if(currentItem > 0){
							currentItem--;
							goTo(currentItem);					
						}						
					}
					update_scrollers();
				});
			}//end if
			
			function update_scrollers(){
				$(settings['galleryCtrl'] + " a[rel='next']").css("visibility","visible");
				$(settings['galleryCtrl'] + " a[rel='prev']").css("visibility","visible");
				
				if(currentItem == (totalItems-1)){
					$(settings['galleryCtrl'] + " a[rel='next']").css("visibility","hidden");							
				}
				if(currentItem == 0){
					$(settings['galleryCtrl'] + " a[rel='prev']").css("visibility","hidden");							
				}	
			}
			
			if(settings['onClick'] == false){
				currentItem = 1;	
				startCarousel();				
				if(settings['clickMenu'] != ''){
					$(settings['clickMenu']).find("a").click(function(e){
						e.preventDefault();
						//if(currentItem != $(this).parent().index()){
							goTo($(this).parent().index());
							select_clickMenu_element();
						//}//end if
					});
				}//end if
			} else {	
				currentItem = 0;			
				$(settings['clickMenu']).find("a").click(function(e){
					e.preventDefault();
					//if(currentItem != $(this).parent().index()){
						goTo($(this).parent().index());
						select_clickMenu_element();
					//}//end if
				});
			}//end if
			
			function select_clickMenu_element(){
				$(settings['clickMenu']).find("a").each(function(){
					if($(this).parent().index() == currentItem){
						$(this).addClass(settings['clickMenuSel']);
					} else {
						$(this).removeClass(settings['clickMenuSel']);
					}//end if
				});
			}
		};
	})(jQuery);
}
