
/*
 * 
 * ppFeatureSlider Plugin 1.0
 * Version 1.0
 * @requires jQuery v1.4.2
 * 
 * Copyright (c) 2010 Gareth Preston
 * Licensed under the GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 * 
 */
 
(function($) {
	$.fn.ppFeatureSlider = function (options) {
		
		var defaults = {
			controller : ".ppSliderController",
			slidesWrapper : ".ppSliderSlides",
			slideWrapper : ".ppSliderSlide",
			selectedWrapper: ".ppSliderSelected",
			paginationWrapper: ".ppSliderPagination",
			paginationSelected: ".ppSliderSelected",
			infoPane: '.ppDetails',
			duration: 'slow',
			auto: true,
			intervalSpeed: 5000, // integer value, 'slow' or 'fast'
			infoMode: 'bottom', // 'bottom', 'top', 'left', 'right'or 'static'
			autoCentre: true,
			drag: false
		};
		
		var animating = false;
		var interval = null;
		var opts = $.extend({}, defaults, options);
		if(opts.drag) {
			opts.infoMode = 'static';
		}
		
		if(!$(this).hasClass(opts.controller.substring(1))) {
			$(this).addClass(opts.controller.substring(1));
		}
		
		var links = new Array();
		for(var i = 1; i <= $(this).find(opts.slideWrapper).length; i++) {
			links[links.length] = '<a href="javascript:;"><span>' + i + '</span></a>';
		}
		$(opts.controller).find(opts.paginationWrapper).html(links.join(''));
		$(opts.controller).find(opts.paginationWrapper + ' a:first').addClass(opts.paginationSelected.substring(1));
		
		$(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ':first').addClass(opts.selectedWrapper.substring(1));
		
		var modeOut = {};
		var modeStart = {};
		
		if(opts.infoMode == 'bottom') {
			modeOut = {left: 0, bottom: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerHeight() + 'px'};
			modeStart = {left: 0, bottom: 0};
		}
		else if(opts.infoMode == 'left') {
			modeOut = {top: 0, left: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerWidth() + 'px'};
			modeStart = {top: 0, left: 0};
		}
		else if(opts.infoMode == 'right') {
			modeOut = {top: 0, right: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerWidth() + 'px'};
			modeStart = {top: 0, right: 0};
		}
		else if(opts.infoMode == 'top') {
			modeOut = {left: 0, top: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerHeight() + 'px'};
			modeStart = {left: 0, top: 0};
		}
		$(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ' ' + opts.infoPane).css(modeOut);
		$(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).css(modeStart);
		
		if(opts.autoCentre) {
			
			$(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ' img').each(function() {
				
				var height = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper).outerHeight() - $(this).attr('height');
				
				var width = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper).outerWidth() - $(this).attr('width');
				
				var top, left = 0;
				if(height > 0)
					top = height / 2;
				if(width > 0)
					left = width / 2;
				
				$(this).css({position: 'relative', display: 'block', top: top, left: left});
			});
		}
		
		
		var getWidth = function(length) {
			var totalWidth = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ':first').width() * length;
			return totalWidth;
		}
		
		var slide = function(num) {
			
			if(!animating) {
				
				animating = true;
				
				var modeOut = {};
				var modeIn = {};
				if(opts.infoMode == 'bottom') {
					modeOut = {left: 0, bottom: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerHeight() + 'px'};
					modeIn = {left: 0, bottom: 0};
				}
				else if(opts.infoMode == 'left') {
					modeOut = {top: 0, left: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerWidth() + 'px'};
					modeIn = {top: 0, left: 0};
				}
				else if(opts.infoMode == 'right') {
					modeOut = {top: 0, right: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerWidth() + 'px'};
					modeIn = {top: 0, right: 0};
				}
				else if(opts.infoMode == 'top') {
					modeOut = {left: 0, top: '-' + $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).outerHeight() + 'px'};
					modeIn = {left: 0, top: 0};
				}
				
				$(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).animate(
					modeOut,
					opts.duration,
					function() {
						
						$(opts.controller).find(opts.slidesWrapper).animate(
							{left: "-" + getWidth(num) + "px"},
							opts.duration
							, function() {
								
								$(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper).removeClass(opts.selectedWrapper.substring(1));
								$(opts.controller).find(opts.paginationWrapper + ' a').removeClass(opts.paginationSelected.substring(1));	
								
								var current = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ':nth-child(' + (num+1) + ')');
								if(current.length > 0) {
									current.addClass(opts.selectedWrapper.substring(1));
									$(opts.controller).find(opts.paginationWrapper + ' a:nth-child(' + (num+1) + ')').addClass(opts.paginationSelected.substring(1));
								}
								else {
									$(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ':first').addClass(opts.selectedWrapper.substring(1));
									$(opts.controller).find(opts.paginationWrapper + ' a:first').addClass(opts.paginationSelected.substring(1));
								}
								
								$(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper + ' ' + opts.infoPane).animate(
									modeIn,
									opts.duration,
									function() {
										
										animating = false;
										
								});
								
						});	
						
					
				});
				
			}
			
		}
		
		var timerSlide = function() {
			var num = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.selectedWrapper).prevAll().length + 1;
			if(num >= $(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper).length) {
				num = 0;
			}
			slide(num);
		}
		
		var stop = function() {
			window.clearInterval(interval);
			interval = null;
		}
		
		var startTicker = function() {
			if(opts.auto) {			
				stop();
				interval = window.setInterval(function(){
					if(!animating) {
						timerSlide();
					}
				}, opts.intervalSpeed);
			}
		}
		
		$(this).find(opts.paginationWrapper + ' a').each(function(cntrl) {
			$(this).click(function(e) {
				if(!animating) {
					stop();
					slide(cntrl);
					startTicker();
				}
				e.preventDefault();
			});
		});
		
		$(this).mouseover(function() {
			stop();
		});
		
		$(this).mouseout(function() {
			startTicker();
		});
		
		if(opts.drag) {
			
			$(opts.controller).find(opts.slidesWrapper).draggable({
				axis: 'x', 
				stop: function() {
					
					var width = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper + ':first').width();
					var newPos = 0;
					var currentPos = parseInt($(opts.controller).find(opts.slidesWrapper).css("left").replace("px",""));	
					if(currentPos >= 0) {
						newPos = 0;
					}
					else {
						newPos = Math.round(Math.abs(currentPos / width));
					}
					
					if(newPos >= $(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper).length) {
						newPos = $(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper).length - 1;
					}
					
					slide(newPos);
					
				}
			});
		}
		
		var totalWidth = getWidth($(opts.controller).find(opts.slidesWrapper + ' ' + opts.slideWrapper).length);
		$(opts.controller).find(opts.slidesWrapper).width(totalWidth);
		
		if(opts.auto) {
			startTicker();
			
			$(window).unload(function() {
				stop();
			});
		}
		
		return this;
		
	}
})(jQuery);

