/*
*   jqContentSlider v1.0  -  jQuery Plugin
*   -------------------------------------------
*   By: Gian Pompilio (gian.pompilio@gmail.com)
*   Released under the GPL license
*   http://www.gnu.org/licenses/gpl.html
*   -------------------------------------------
*/

(function($){
		  
    $.fn.jqContentSlider = function(options){
		
		var defaultOptions = {
			pageControls: true,		// enable pagination? 								(true/false)
			navControls: false,		// enable left/right navigation controls? 			(true/false)
			autoSlide: true,		// automatically scroll slides? 					(true/false)
			autoResume: true,		// resume auto scrolling after manual navigation? 	(true/false)
			pauseOnHover: true,		// pause scrolling on mouse over? 					(true/false)
			showCaptions: true,		// show image title attribute as caption?			(true/false)  // NEW
			direction: 'left',		// direction in which slides will move 				(left/right)
			speed: 0.3,				// slide transition speed (smaller = faster)		(seconds)
			delay: 6,				// time between slide transitions 					(seconds)
			width: 320,				// slide width										(pixels)
			transition: 'swing',	// type of transition								(swing)
			fadeInOut:true			// fade slides in and out?							(true/false)
		};
		
		var options = $.extend(defaultOptions, options);					// extend default options (if applicable)
		
		return this.each(function(){
								  
			var slider			= $(this);									// get <div> container
			var list 			= slider.find('ul');						// get <ul> list
			var slides 			= list.children();							// get <li> slides
			var totalSlides		= slides.length;							// # of slides
			var firstSlide		= list.children(':first').clone();			// clone first slide
			var lastSlide 		= list.children(':last').clone();			// clone last slide
			var activeSlide  	= 1;										// current active slide
			var position		= 0;										// current slider position
			var pagination		= $('<div class="slider_pagination" />');			// pagination container <div>
			var paginationBttn	= '';										// pagination button(s) <a>
			var paginationWidth	= '';										// totalSlides * (width + margins) ... see (#pagination a) in CSS
			var timer			= '';										// interval timer
			var ani				= {};										// animation sequence
			var leftNavArea		= $('<div class="slider_navigation" style="left:0px;"><a class="slider_previous" title="Previous"></a></div>');
			var rightNavArea	= $('<div class="slider_navigation" style="right:0px;"><a class="slider_next" title="Next"></a></div>');
			var anchor			= ''; // NEW
			var imageTitle		= ''; // NEW

			list.append(firstSlide).prepend(lastSlide);						// prepare slides for 'infinite loop', copy first slide to end and last slide to front
			
			if(options.showCaptions){										// SLIDE CAPTIONS
				slides.each(function(index){								// loop through slides
					slide = $(this);										// for each slide
					imageTitle = slide.find("a").find("img").attr("alt");	// identify the title attribute on the image
					slide.find("a").append('<div class="slider_caption">' + imageTitle + '</div>');		// and add it below the image as text
				});
			}
						
			if(options.pageControls) {										// PAGE CONTROLS
				slider.prepend(pagination);									// add pagination container <div>
				slides.each(function(index){								// for each slide
					paginationBttn = $('<a href="#" />');					// create a button
					paginationBttn.click(function(){						// add a click event
						activeSlide = index+1;								// associate slide index with button
						moveSliderTo(activeSlide);							// move slider to that slide on click
						setTimer('stop');									// stop the timer
						if(options.autoResume){setTimer('start');}			// restart the timer (if applicable)
						return false;										// return nothing
					});
					pagination.append(paginationBttn);						// add button to pagination container
				});
				setPagination(1);											// active first pagination button
			}

			if(options.navControls) {										// NAV CONTROLS
				list.after(leftNavArea).after(rightNavArea);				// add left & right controls after <ul> list
				slider.find(".navArea a").hide();							// hide nav control buttons
				leftNavArea.hover(											// add hover state to left nav area
					function(){												// on mouse over
						slider.find(".previous").show();					// show previous button
						setTimer('stop');									// stop the timer
					},
					function(){												// on mouse out
						slider.find(".previous").hide();					// hide previous button
						setTimer('start');									// start the timer
					}
				);
				slider.find(".previous").click(function(){					// add click event to previous button
					moveSliderTo(--activeSlide);							// move to previous slide
				});	
				rightNavArea.hover(											// add hover state to right nav area
					function(){												// on mouse over
						slider.find(".next").show();						// show next button
						setTimer('stop');									// stop the timer
					},
					function(){												// on mouse out
						slider.find(".next").hide();						// hide next button
						setTimer('start');									// start the timer
					}
				);
				slider.find(".next").click(function(){						// add click event to next button
					moveSliderTo(++activeSlide);							// move to next slide
				});
			}
			
			if(options.autoSlide) {											// AUTO SLIDE
				setTimer('start');											// start the timer
				if(options.pauseOnHover) {									// enable pause on hover
					list.hover(												// add hover state to <ul> list
						function(){setTimer('stop');},						// stop the timer on mouse over
						function(){setTimer('start');}						// start the timer on mouse out
					);														// end hover statefor <ul> list
				}															// end pause on hover
			}
																			// ADJUST STYLES
			list.css({'left':-options.width});								// hide first slide (cloned last slide)
			paginationWidth = totalSlides*paginationBttn.outerWidth(true);	// determine pagination width
			pagination.css({'width':(paginationWidth)});					// adjust pagination width 
			
			/******************************  Define functions  ******************************/
			
			function setTimer(action){															// SET TIMER
				if(action=='start'){ 															// start timer
					timer=setInterval(															// initialize interval timer
						function(){																// determine action to be taken
							if(options.direction=='left'){moveSliderTo(++activeSlide);}			// if direction = left, move to next slide
							else if(options.direction=='right'){moveSliderTo(--activeSlide);}	// if direction = right, move to previous slide
							else{alert("Error: slide direction must = 'left' or 'right'");}		// if direction is invalid, display error
						}, 
						options.delay*1000														// set delay between intervals (miliseconds)
					);
				}
				else if(action=='stop'){clearInterval(timer);}									// stop timer
				else{alert("Error: timer action must = 'start' or 'stop'");}					// if action is invalid, display error
			}
			
			function moveSliderTo(slide){													// MOVE SLIDER
				position = slide*options.width;												// determine the position of the slide in the list <ul>
				ani[options.direction] = -position;											// sets how far in a particular direction to move
				list.animate(																// animate the <ul> list
					ani,																	// set direction & value
					options.speed*1000,														// set transition speed (miliseconds)
					options.transition,														// set transition type
					function(){																// run this function on completion
						if(activeSlide > totalSlides){										// if on last slide (and moving right)
							list.css(options.direction, -options.width);					// jump to the first slide (cloned last slide)
							activeSlide=1;													// set active slide to first slide
						}
						else if (activeSlide < 1){											// if on first slide (and moving left)
							list.css(options.direction, -(options.width*totalSlides));		// jump to the last slide (cloned first slide)
							activeSlide = totalSlides;										// set active slide to last slide
						}
						setPagination(activeSlide);											// set appropriate pagination button
					}
				);
			}
			
			function setPagination(index){														// SET PAGINATION
				pagination.find('a').removeClass('active').eq(index-1).addClass('active');		// find appropriate button and set class to active
			}
			
		});
	}; 																							// end mySlider function
})(jQuery);																						// end jquery plugin
