/**
 * jquery.scrollable 1.0.2. Put your HTML scroll.
 *
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/scrollable.html
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : March 2008
 * Version : 1.0.2 - Tue Feb 24 2009 10:52:08 GMT-0000 (GMT+00:00)
 */
(function($) {
	function fireEvent(opts, name, self, arg) {
		var fn = opts[name];
		
		if ($.isFunction(fn)) {
			try {
				return fn.call(self, arg);
			} catch (error) {
				if (opts.alert) {
					alert("Error calling scrollable." + name + ": " + error);
				} else {
					throw error;
				}
				return false;
			}
		}
		return true;
	}
	
	var current = null;
	
	
	// constructor
	function Scrollable(root, conf) {
		// current instance
		var self = this;
		if (!current) { current = self; }
		
		// horizontal flag
		var horizontal = !conf.vertical;
		
		
		// wrap (root elements for items)
		var wrap = $(conf.items, root);
		
		// current index
//		var index = 0;
		var index = 5;
		
		
		// get handle to navigational elements
		var prev = root.siblings(conf.prev).eq(0);
		var next = root.siblings(conf.next).eq(0);
		
		
		// methods
		$.extend(self, {
			getVersion: function() {
				return [1, 0, 1];
			},
			
			getIndex: function() {
				return index;
			},
			
			getConf: function() {
				return conf;
			},
			
			getSize: function() {
				return self.getItems().size();
			},
			
			getPageAmount: function() {
				return Math.ceil(this.getSize() / conf.size);
			},
			
			getPageIndex: function() {
				return Math.ceil(index / conf.size);
			},
			
			getRoot: function() {
				return root;
			},
			
			getItemWrap: function() {
				return wrap;
			},
			
			getItems: function() {
				return wrap.children();
			},
			
			/* all seeking functions depend on this */
			seekTo: function(i, time, fn) {
				// default speed
				time = time || conf.speed;
				
				// function given as second argument
				if ($.isFunction(time)) {
					fn = time;
					time = conf.speed;
				}
				
				if (i < 0) { i = 0; }
				if (i > self.getSize() - conf.size) { return self; }
				
				var item = self.getItems().eq(i);
				if (!item.length) { return self; }
				
				// onBeforeSeek
				if (fireEvent(conf, "onBeforeSeek", self, i) === false) {
					return self;
				}
				
				if (horizontal) {
					var left = -(item.outerWidth(true) * i);
					wrap.animate({left: left}, time, conf.easing, fn ? function() { fn.call(self); } : null);
				} else {
					var top = -(item.outerHeight(true) * i); // wrap.offset().top - item.offset().top;
					wrap.animate({top: top}, time, conf.easing, fn ? function() { fn.call(self); } : null);
				}
				
				current = self;
				index = i;
				
				// onSeek after index being updated
				fireEvent(conf, "onSeek", self, i);
				
				return self;
			},
			
			move: function(offset, time, fn) {
				var to = index + offset;
				if (conf.loop && to > (self.getSize() - conf.size)) {
					to = 0;
				} else if (conf.loop && to < 0) {
					to = self.getSize() - conf.size;
				}
				return this.seekTo(to, time, fn);
			},
			
			next: function(time, fn) {
				return this.move(1, time, fn);
			},
			
			prev: function(time, fn) {
				return this.move(-1, time, fn);
			},
			
			movePage: function(offset, time, fn) {
				return this.move(conf.size * offset, time, fn);
			},
			
			setPage: function(page, time, fn) {
				var size = conf.size;
				var index = size * page;
				var lastPage = index + size >= this.getSize();
				if (lastPage) {
					index = this.getSize() - conf.size;
				}
				return this.seekTo(index, time, fn);
			},
			
			begin: function(time, fn) {
				return this.seekTo(0, time, fn);
			},
			
			end: function(time, fn) {
				return this.seekTo(this.getSize() - conf.size, time, fn);
			},
			
			reload: function() {
				return load();
			},
			
			click: function(index, time, fn) {
				var item = self.getItems().eq(index);
				var klass = conf.activeClass;
				
				if (!item.hasClass(klass) && (index >= 0 || index < this.getSize())) {
					self.getItems().removeClass(klass);
					item.addClass(klass);
					var delta = Math.floor(conf.size / 2);
					var to = index - delta;
					
					// next to last item must work
					if (to > self.getSize() - conf.size) { to --; }
					
					if (to !== index) {
						return this.seekTo(to, time, fn);
					}
				}
				
				return self;
			}
		});
		
		// mousewheel
		if ($.isFunction($.fn.mousewheel)) {
			root.bind("mousewheel.scrollable", function(e, delta) {
				// opera goes to opposite direction
				var step = $.browser.opera ? 1 : -1;
				
				self.move(delta > 0 ? step : -step, 50);
				return false;
			});
		}
		
		// prev button
		prev.addClass(conf.disabledClass).click(function() {
			self.prev();
		});
		
		// next button
		next.click(function() {
			self.next();
		});
		
		// navi
		function load() {
			// item.click()
			if (conf.clickable) {
				self.getItems().each(function(index, arg) {
					var el = $(this);
					if (!el.data("set")) {
						el.bind("click.scrollable", function() {
							self.click(index);
						});
						el.data("set", true);
					}
				});
			}
			
			
			// hover
			if (conf.hoverClass) {
				self.getItems().hover(function() {
					$(this).addClass(conf.hoverClass);
				}, function() {
					$(this).removeClass(conf.hoverClass);
				});
			}
			
			return self;
		}
		
		load();
		
		
		// interval stuff
		var timer = null;
		
		function setTimer() {
			timer = setInterval(function() {
				self.next();
			}, conf.interval);
		}
		
		if (conf.interval > 0) {
			root.hover(function() {
				clearInterval(timer);
			}, function() {
				setTimer();
			});
			
			setTimer();
		}
	}
	
	
	// jQuery plugin implementation
	jQuery.prototype.scrollable = function(conf) {
		// already constructed --> return API
		var api = this.eq(typeof conf == 'number' ? conf : 0).data("scrollable");
		if (api) { return api; }
		
		var opts = {
			// basics
			size: 3,
			vertical:false,
			clickable: false,
			loop: true,
			interval: 0,
			speed: 400,
			
			// other
			activeClass: null,
			disabledClass: null,
			hoverClass: null,
			easing: 'swing',
			
			// navigational elements
			items: '#scrItem',
			prev: '#scrBtnL',
			next: '#scrBtnR',
			
			// callbacks
			onBeforeSeek: null,
			onSeek: null,
			alert: true
		};
		
		
		$.extend(opts, conf);
		
		this.each(function() {
			var el = new Scrollable($(this), opts);
			$(this).data("scrollable", el);
		});
		
		return this;
	};
})(jQuery);
