(function( $) {
	$.fn.slider = function(numberOfSlides, slideWidth,totalAnimationTime, animInterval, autoslide) {
	
	/* SLIDER PLUGIN ACCEPTS 4 PARAMETERS 
	
	numberOfSlides = total number of slides
	slideWidth = the pixel width of each slide
	totalAnimationTime = the higher the number the higher the faster speed of the slide transition.
	animInterval = the time in milleseconds between each slide on the timer. */
	
	/*begin auto center of banner controls*/
	
	var baseWidth = 86;
	var marginFix = -(((baseWidth + (numberOfSlides*18)) /2) - 12);
	
	$('#banner_controls').css({marginLeft: marginFix});

	function isIE6(){
		IE6 = (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
		return IE6;
	}
	if(isIE6()){
		var ieSixFix = ((baseWidth + (numberOfSlides * 18)) - 72);
		$('#banner_controls #relative-wrapper').css({width: ieSixFix});
		$('#banner_controls').css({width: ieSixFix});
	}
	
	var currentSlide = 0;
	var indicator = 0;
	var animating = false;
	var slideAnim = false;
	var n = $($("ul.slideshow > li:eq("+currentSlide+")")).queue("fx");
	
	$.fn.slider.myTimer = function() {
		if( autoslide ){
			setTimeout("$.fn.slider.nextSlide()",animInterval);
		}
	}
	$.fn.slider.myTimer();
	
	var obj = this;
	
	$.fn.slider.nextSlide = function(index, distance, button)
	{
		
		
		/* set default values if they are not set explicitly. */
		if(index == null) index = 0;
		if(distance == null) distance = 1;
		
		if(index == distance) {
		slideAnim = false;
			if( autoslide ){
				//$("ul.slideshow > li:eq("+currentSlide+")").stop();
				//myTimer = setTimeout("nextSlide()",3000);
				clearTimeout($.fn.slider.myTimer);
				$.fn.slider.myTimer = setTimeout("$.fn.slider.nextSlide()",animInterval);
			}
		}
		
		
		/* base case for recursive calls. */
		if(index == distance) return ;
		
		
		/*check if slideshow is animating*/
		if ((button==true) && (animating==true))  {
			return;
		}
		
		if ((button==true) && (slideAnim==true))  {
			return;
		}
		
		if ((button==true) && (animating==false))  {
			animating=true;
		}
		
		
		/* compute the speed to move by, if we want the whole animation to */
		/* play in totalAnimationTime, then we animate with speed (totalAnimationTime) / distance */
		/* to get as close as possible to that speed. */
		var delay = totalAnimationTime / distance;
		
		/* slide away the current slide */
		obj.find("ul.slideshow > li:eq("+currentSlide+")").animate({"left": -slideWidth}, delay);
		/* get the next slide */
		currentSlide++;
		/* if the currentSlide number goes above the number of slides, we have to return to the first slide. */
		if(currentSlide >= numberOfSlides) currentSlide = 0;
		
		
		
		/* force the next slide to start at the opposite end of the view pane.*/
		obj.find("ul.slideshow > li:eq("+currentSlide+")").stop(true, true).css({"left": slideWidth});
		/* animate the next slide into the view pane */
		obj.find("ul.slideshow > li:eq("+currentSlide+")").stop(true, true).animate({"left": 0}, delay, function(){ 
			$.fn.slider.nextSlide(index+1, distance); 
			animating=false;
			});
		
		
		
		if(indicator >= (numberOfSlides - 1)){
			indicator=0;	
		}
		else{
		indicator = indicator +1;
		}
		
		/* adds removes class from all of the slide indicator*/
		obj.find("ul.each-slide li").removeClass('active');
		obj.find("ul.each-slide li:eq("+indicator+")").addClass('active');
		
	}
	
	$.fn.slider.prevSlide = function (index, distance, button)
	{
		/* set default values if they are not set explicitly. */
		if(index == null) index = 0;
		if(distance == null) distance = 1;
		
		
		if(index == distance) {
		slideAnim = false;
			if( autoslide ){
				//$("ul.slideshow > li:eq("+currentSlide+")").stop();
				//myTimer = setTimeout("nextSlide()",3000);
				clearTimeout($.fn.slider.myTimer);
				$.fn.slider.myTimer = setTimeout("$.fn.slider.nextSlide()",animInterval);
			}
		}
		
		/* base case for recursive calls. */
		if(index == distance) return ;
		
		/*check if slideshow is animating*/
		if ((button==true) && (animating==true))  {
			return;
		}
		
		if ((button==true) && (slideAnim==true))  {
			return;
		}
		
		if ((button==true) && (animating==false))  {
			animating=true;
		}
		
		/* compute the speed to move by, if we want the whole animation to */
		/* play in totalAnimationTime, then we animate with speed (totalAnimationTime) / distance */
		/* to get as close as possible to that speed. */
		var delay = totalAnimationTime / distance;
		
		/* slide away the current slide */
		$("ul.slideshow > li:eq("+currentSlide+")").animate({"left": slideWidth}, delay);
		/* get the next slide */
		currentSlide--;
		/* if the currentSlide number goes below, we have to move to the last slide. */
		/* the number of slides is a count starting from one, so the last index we can */
		/* access will be numberOfSlides - 1. */
		if(currentSlide < 0) currentSlide = numberOfSlides - 1;
		
		/* force the next slide to start at the opposite end of the view pane.*/
		obj.find("ul.slideshow > li:eq("+currentSlide+")").css({"left": -slideWidth});
		/* animate the next slide into the view pane */
		obj.find("ul.slideshow > li:eq("+currentSlide+")").animate({"left": 0}, delay, function(){ 
			$.fn.slider.prevSlide(index+1, distance); 
			animating=false;
			});
		
		
		if (indicator <= 0) {
			indicator = numberOfSlides -1;
		}
		
		else {
		indicator = indicator -1;
		}
		
		/* adds removes class from all of the slide indicator*/
		obj.find("ul.each-slide li").removeClass('active');
		obj.find("ul.each-slide li:eq("+indicator+")").addClass('active');
		
	}
	
	$.fn.slider.slideToSlide = function (index)
	{
		if((slideAnim == false) && (animating == false)) {
			slideAnim = true;
			/* the first thing we have to do is decide which direction to go */
			/* ...and how far */
			var direction = index - currentSlide;
			var distance = Math.abs(direction); 	// this is just for convience, how many slides to go regardless of direction.
			
			if(direction == 0) return ;	// already there, there is no distance to travel.
			
			if(direction < 0) // we need to move backwards to the correct slide
			{
				this.prevSlide(0, distance);
			}
			else		// we need to move forwards to the correct slide
			{
				this.nextSlide(0, distance);
			}
			
			/* align the indicator to the correct slide */
			//$("#indicator").animate({"margin-left":slideNavPositions[index]});
		}
	}
}
	
})(jQuery);

