/*
	Slider plugin
*/

Array.prototype.sum = function() {
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
};
(function( $ ){
	
	var methods = {
		init : function( options ) {
			var settings = {
				mode: 'pages',
				transition: 'slide',
				length: 1,
				speed: 500,
				easing: 'linear',
				resize: false,
				resume: true,
				height: 0,
				interval: 5000
			}
			return this.each(function(){
				if ( options ) { 
					$.extend( settings, options );
				}
				var self = $(this);
				self.data('settings', settings);
				var slider;
				if (self.find('ul').length) {
					slider = self.find('ul').addClass('jq-slider');
				} else {
					var inner = self.html();
					self.empty();
					slider = $('<div class="jq-slider" />').html(inner).appendTo(self);
				}
				var total = 0;
				var pics = [];
				var height = settings.height;
				var imgs = self.find('img');
				var zIndex = imgs.length;
				imgs.each(function(i){
					var img = $(this);
					if (img.closest('li').length) {
						img.closest('li').addClass('jq-picture');
					} else if (img.closest('div').is('.jq-slider')) {
						if (img.parent().is('a')) {
							img.parent().wrap('<div class="jq-picture" />');
						} else {
							img.wrap('<div class="jq-picture" />');
						}
					} else {
						img.closest('div').addClass('jq-picture');
					}
					var width = img.attr('width');
					height = img.attr('height') > parseInt(height) ? img.attr('height') + 'px' : height;
					pics[i] = width;
					var css = {
						'width': width + 'px',
						'margin': 0,
						'padding': 0
					};
					if (settings.transition == 'fade') {
						$.extend(css, {
							'position': 'absolute',
							'z-index': zIndex
						});
						zIndex--;
						total = width > total ? width : total;
					} else {
						$.extend(css, {
							'float': 'left'
						})
						total += width;
					}
					img.closest('.jq-picture').css(css);
				});
				var width = settings.width ? settings.width : pics.slice(0, settings.length).sum() + 'px';
				self.css({
					'width': width, 
					'height': height, 
					'overflow': 'hidden',
					'position': 'relative'
					})
					.data('pictures', pics)
					.data('current', 0)
					.data('first', true);
				slider.css({
					'width': total,
					'height': height,
					'top': 0,
					'left': 0,
					'position': 'absolute'
					})
					.data('moving', false);

				if (settings.mode == 'slider') {
					slider.apply( self );
				}
				
				if (settings.mode == 'pages' && !settings.auto) {
					pages.apply( self );
				}
				
				if (settings.auto) {
					setInterval(function(){
						methods.next.apply( self, [1]);
					}, settings.interval + settings.speed);
				}
			});
		},
		prev: function( step ) {
			var settings = $(this).data('settings');
			if (typeof transition[settings.transition].prev == 'function') {
				transition[settings.transition].prev.apply( this, [step] );
			}
		},
		next: function( step ) {
			var settings = $(this).data('settings');
			if (typeof transition[settings.transition].next == 'function') {
				transition[settings.transition].next.apply( this, [step] );
			}
		},
		show: function( step ) {
			var settings = $(this).data('settings')
			if (typeof transition[settings.transition].show == 'function') {
				transition[settings.transition].show.apply( this, [step] );
			}
		}
	};
	
	var transition = {
		slide: {
			prev : function( step ) {
				return this.each(function(){
					if (!$(this).data('first')) {
						var settings = $(this).data('settings');
						var self = $(this);
						var slider = self.find('.jq-slider');
						var dest;
						if (self.data('current') - step >= 0) {
							dest = self.data('current') - step;
						} else if (settings.resume) {
							dest = self.data('pictures').length - 1;
						}
						methods.show.apply( self, [dest] );
					}
				});
			},
			next : function( step ) {
				return this.each(function(){
					$(this).data('first', false)
					var settings = $(this).data('settings');
					var self = $(this);
					var slider = self.find('.jq-slider');
					var dest;
					if (self.data('current') + step <= self.data('pictures').length - settings.length) {
						dest = self.data('current') + step;
					} else if (settings.resume) {
						dest = 0;
					}
					methods.show.apply( self, [dest] );
				});
			},
			show : function( dest ) {
				return this.each(function(){
					var self = $(this);
					var slider = self.find('.jq-slider');
					if (dest != self.data('current') && !slider.data('moving')) {

						var settings = self.data('settings');
						slider.data('moving', true);
						var left = parseInt(slider.css('left'));
						if (dest > self.data('current')) {
							for (var i = self.data('current'); i < dest; i++) {
								left -= self.data('pictures')[i];
							}
						} else {
							for (var i = self.data('current') - 1; i >= dest; i--) {
								left += self.data('pictures')[i];
							}
						}
						var current = self.data('pictures')[self.data('current')];
						self.data('current', dest);
						var width = self.data('pictures')[dest];
						if (settings.resize && current > width) {
							self.animate({'width': width}, 'fast', function(){
								slider.animate({'left': left}, settings.speed, settings.easing, function(){
									slider.data('moving', false);
									if (typeof settings.each == 'function') {
										settings.each.apply( self, [dest] )
									}
								});
							});
						} else {
							slider.animate({'left': left}, settings.speed, settings.easing, function(){
								if (settings.resize && current < width) {
									$(this).parent().animate({'width': width}, 'fast', function(){
										slider.data('moving', false);
									});
								} else {
									slider.data('moving', false);
								}
								if (typeof settings.each == 'function') {
									settings.each.apply( self, [dest] )
								}
							});
						}

					}
				});
			}
		},
		fade: {
			next: function( step ) {
				var self = $(this);
				var settings = self.data('settings');
				var slider = self.find('.jq-slider');
				self.data('current', (self.data('current') < self.data('pictures').length - 1 ? self.data('current') + step : 0));
				if (typeof settings.each == 'function') {
					settings.each.apply( self );
				}
				self.find('.jq-picture:first').animate({'opacity': 0}, settings.speed, settings.easing, function(){
					$(this).next().animate({opacity: 1}, settings.speed, settings.easing);
					$(this).clone().appendTo(slider).css({
						'z-index': 0,
						'opacity': 1
					});
					$(this).remove();
					slider.find('.jq-picture').each(function(){
						$(this).css('z-index', parseInt($(this).css('z-index')) + 1);
					});
				});
			}
		}
	};

	var pages = function() {
		$('<a href="#" />').css({
			'position': 'absolute',
			'right': 0,
			'top': 0,
			'z-index': '999',
			'width': '50%',
			'height': '100%'
		}).click(function(){
			methods.next.apply( $(this).parent(), [1] );
			return false;
		}).appendTo(this);
		$('<a href="#" />').css({
			'position': 'absolute',
			'left': 0,
			'top': 0,
			'z-index': '999',
			'width': '50%',
			'height': '100%'
		}).click(function(){
			methods.prev.apply( $(this).parent(), [1] );
			return false;
		}).appendTo(this);
	}

	var slider = function() {
		var timer;
		var settings = this.data('settings');
		var slider = this.find('.jq-slider');
		slider.data('length', slider.find('.jq-picture').length);
		var imgs = slider.clone().html();
		var width = parseInt(slider.css('width'));
		slider.append(imgs + imgs);
		slider.css({'left': '-' + width + 'px', 'width': (width * 3) + 'px'});
		slider.data('pivot', width).data('width', width);
		self = this;
		if (self.find('a').length && typeof settings.click == 'function') {
			self.find('a').each(function(){
				$(this).click(function(){
					settings.click.apply( this );
					return false;
				});
			});
		}
		self.mouseover(function(e){
			var self = $(this);
			var left = self.offset().left;
			var width = self.width();
			var over = Math.round(width * (settings.over ? parseInt(settings.over) : 33) / 100);
			if (e.clientX < left + over) {
				self.data('dir', 10);
			} else if (e.clientX > left + width - over) {
				self.data('dir', -10);
			} else {
				self.mouseout();
			}
			if (typeof timer == 'undefined' || timer == 0) {
				var speed = settings.speed != 500 ? settings.speed : 100;
				timer = setInterval(function(){
					if (typeof self.data('dir') != 'undefined' && self.data('dir') != 0) {
						var left = parseInt(slider.css('left'));
						var dir = self.data('dir');
						var limit = width - slider.width();
						if (!settings.infinite) {
							if ((dir > 0 && Math.floor((left + dir) / 10) <= 0) || (dir < 0 && Math.floor((left + dir) / 10) >= Math.floor(limit / 10))) {
								var tmp = left + dir;
								left = tmp > limit ? tmp : limit;
								left = left <= 0 ? left : 0;
								slider.css('left', left);
								
							}
						} else {
							slider.css('left', left + dir);
							var pivot = slider.data('pivot');
							slider.data('pivot', pivot + dir);
							if (dir > 0 && Math.floor((pivot + dir) / 10) >= Math.floor(slider.data('width') * 2 / 10)) {
								slider.data('pivot', slider.data('width'));
								var imgs = slider.find('.jq-picture').slice((0 - slider.data('length')));
								imgs = $.makeArray(imgs).reverse();
								$.each(imgs, function(i, elem){
									slider.prepend(elem);
								});
								slider.css('left', '-' + slider.data('width') + 'px');
							} else if (dir < 0 && Math.floor((pivot + dir) / 10) <= 0) {
								slider.data('pivot', slider.data('width'));
								slider.find('.jq-picture').slice(0, slider.data('length')).appendTo(slider);
								slider.css('left', '-' + slider.data('width') + 'px');
							}
						}

					}
				}, speed);
			}
		}).mouseout(function() {
			if (typeof timer != 'undefined' && typeof $(this).data('dir') != 'undefined') {
				clearInterval(timer);
				timer = 0;
				$(this).data('dir', 0);
			}
		});
	};
	
	$.fn.slider = function( method ) {
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +	method + ' does not exist on jQuery.tooltip' );
		}
	};

})( jQuery );

/*
	end plugin
 */
