/* 
	Progress Bar Image Slider - for jQuery 1.3.2+
	Written by: Aaron West of Propaganda3
	Version: 1.0
*/

(function($){  
	$.fn.progressSlider = function(options) {  
		//DEFAULT VARS
		var defaults = {  
			total_items: 0,  
			slide_width: 455,  
			animation_speed: 500,  
			curr_pos: 1,  
			left_nav_disable: true,
			right_nav_disable: false
		};
		//EXTEND DEFAULT VARS WITH OPTIONS IF PROVIDED
		var options = $.extend(defaults, options);
		//SET ELEMENT TO THE ELEMENT THIS FUNCTION WAS CALLED ON
		var element = this;		

		//COUNT NUMBER OF SLIDES
		$("li",element).each(function(){
			options.total_items++;
			//SET BACKGROUND IMAGES TO LIST ITEMS. CHANGE FILE STRUCTURE AS NECESSARY
			$(this).css("backgroundImage","url(images/slideshows/"+$(this).parent().attr("class")+"/"+$(this).attr("class")+"."+$(this).attr("rel")+")")
		});
		//SET WIDTH OF THE CONTAINER FOR THE SLIDES
		$("ul",element).width(options.total_items*options.slide_width);

		//ANIMATE IN THE SLIDER
		animateSlider();
		
		/************************************
		On left arrow click, determine if the click should
		perform an action. If so, se the icon images,
		change the current position variable, and animate.
		Also, if currently on the second slide, a click of
		the left arrow will result in disabling the arrow
		since that click would place the slideshow at the
		position of the first slide.
		************************************/
		$(".left_arrow",element).click(function(e){
			e.preventDefault();
			if (!options.left_nav_disable)
			{
				if(options.curr_pos > 2)
				{
					//SET NAVIGATION ICON STATES
					$(".left_arrow",element).css({backgroundImage: "url(images/left_slide_nav_on.png)"});
					$(".right_arrow",element).css({backgroundImage: "url(images/right_slide_nav_on.png)"});
					//DECREMENT THEN ANIMATE
					options.curr_pos--;
					animateSlider();
				}
				else if(options.curr_pos == 2)
				{
					//SET NAVIGATION ICON STATES
					$(".left_arrow",element).css({backgroundImage: "url(images/left_slide_nav_off.png)"});
					$(".right_arrow",element).css({backgroundImage: "url(images/right_slide_nav_on.png)"});
					//DECREMENT THEN ANIMATE
					options.curr_pos--;
					animateSlider();
					//PREVENT THE LEFT ARROW FROM MOVING THE SLIDESHOW ANY FARTHER
					options.left_nav_disable = true;
				}
				//IF THE LEFT ARROW WAS CLICKED THE RIGHT ARROW SHOULD BE ACTIVE
				options.right_nav_disable = false;
			}
			//DISABLE DEFULT ACTION OF A TAG
			return false;
		});
		
		/************************************
		On right arrow click, determine if the click should
		perform an action. If so, se the icon images,
		change the current position variable, and animate.
		Also, if currently on the second to last slide, a
		click of the right arrow will result in disabling
		the arrow since that click would place the
		slideshow at the position of the last slide.
		************************************/
		$(".right_arrow",element).click(function(e){
			e.preventDefault();
			if (!options.right_nav_disable)
			{
				if(options.curr_pos != options.total_items && options.curr_pos != (options.total_items-1))
				{
					//SET NAVIGATION ICON STATES
					$(".left_arrow",element).css({backgroundImage: "url(images/left_slide_nav_on.png)"});
					$(".right_arrow",element).css({backgroundImage: "url(images/right_slide_nav_on.png)"});
					//INCREMENT THEN ANIMATE
					options.curr_pos++;
					animateSlider();
				}
				else if(options.curr_pos == (options.total_items-1))
				{
					//SET NAVIGATION ICON STATES
					$(".left_arrow",element).css({backgroundImage: "url(images/left_slide_nav_on.png)"});
					$(".right_arrow",element).css({backgroundImage: "url(images/right_slide_nav_off.png)"});
					//INCREMENT THEN ANIMATE
					options.curr_pos++;
					animateSlider();
					//PREVENT THE RIGHT ARROW FROM MOVING THE SLIDESHOW ANY FARTHER
					options.right_nav_disable = true;
				}
				//IF THE RIGHT ARROW WAS CLICKED THE LEFT ARROW SHOULD BE ACTIVE
				options.left_nav_disable = false;
			}
			//DISABLE DEFULT ACTION OF A TAG
			return false;
			
		});
		
		/************************************
		All Animations For the slideshow Are contained within
		this function. Animation speed can be changed in the
		global variables. DO NOT EDIT UNLESS YOU ARE SURE OF
		YOUR MATH SKILLZ!
		************************************/
		function animateSlider(){
			$("ul",element).stop(false, false).animate({left:(-options.slide_width*(options.curr_pos-1))},options.animation_speed);
			$(".slider_left",element).stop(false, false).animate({width:((options.slide_width/options.total_items)*(options.curr_pos))},options.animation_speed).css('overflow', 'visible');
			$(".slide_marker",element).html((Math.floor((100/options.total_items)*(options.curr_pos)))+"%");
		}

	};  
})(jQuery); 
