/**
 * tools.scrollable 1.1.2 - Scroll your HTML with eye candy.
 * 
 * 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
 * Date: ${date}
 * Revision: ${revision} 
 */
(function($) { 
		
	// static constructs
	$.tools = $.tools || {};
	
	$.tools.scrollable = {
		version: '1.1.2',
		
		conf: {
			
			// basics
			size: 5,
			vertical: false,
			speed: 400,
			keyboard: true,		
			
			// by default this is the same as size
			keyboardSteps: null, 
			
			// other
			disabledClass: 'disabled',
			hoverClass: null,		
			clickable: true,
			activeClass: 'scroll-navi-active', 
			easing: 'swing',
			loop: false,
			
			items: '.items',
			item: null,
			
			// navigational elements			
			prev: '.prev',
			next: '.next',
			prevPage: '.prevPage',
			nextPage: '.nextPage', 
			api: false
			
			// CALLBACKS: onBeforeSeek, onSeek, onReload
		} 
	};
				
	var current;		
	
	// constructor
	function Scrollable(root, conf) {   
		
		// current instance
		var self = this, $self = $(this),
			 horizontal = !conf.vertical,
			 wrap = root.children(),
			 index = 0,
			 forward;  
		
		
		if (!current) { current = self; }
		
		// bind all callbacks from configuration
		$.each(conf, function(name, fn) {
			if ($.isFunction(fn)) { $self.bind(name, fn); }
		});
		
		if (wrap.length > 1) { wrap = $(conf.items, root); }
		
		// navigational items can be anywhere when globalNav = true
		function find(query) {
			var els = $(query);
			return conf.globalNav ? els : root.parent().find(query);	
		}
		
		// to be used by plugins
		root.data("finder", find);
		
		// get handle to navigational elements
		var prev = find(conf.prev),
			 next = find(conf.next),
			 prevPage = find(conf.prevPage),
			 nextPage = find(conf.nextPage);

		
		// methods
		$.extend(self, {
			
			getIndex: function() {
				return index;	
			},
			
			getClickIndex: function() {
				var items = self.getItems(); 
				return items.index(items.filter("." + conf.activeClass));	
			},
	
			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);	
			},

			getNaviButtons: function() {
				return prev.add(next).add(prevPage).add(nextPage);	
			},
			
			getRoot: function() {
				return root;	
			},
			
			getItemWrap: function() {
				return wrap;	
			},
			
			getItems: function() {
				return wrap.children(conf.item);	
			},
			
			getVisibleItems: function() {
				return self.getItems().slice(index, index + conf.size);	
			},
			
			/* all seeking functions depend on this */		
			seekTo: function(i, time, fn) {

				if (i < 0) { i = 0; }				
				
				// nothing happens
				if (index === i) { return self; }				
				
				// function given as second argument
				if ($.isFunction(time)) {
					fn = time;
				}

				// seeking exceeds the end				 
				if (i > self.getSize() - conf.size) { 
					return conf.loop ? self.begin() : this.end(); 
				} 				

				var item = self.getItems().eq(i);					
				if (!item.length) { return self; }				
				
				// onBeforeSeek
				var e = $.Event("onBeforeSeek");

				$self.trigger(e, [i]);				
				if (e.isDefaultPrevented()) { return self; }				
				
				// get the (possibly altered) speed
				if (time === undefined || $.isFunction(time)) { time = conf.speed; }
				
				function callback() {
					if (fn) { fn.call(self, i); }
					$self.trigger("onSeek", [i]);
				}
				
				if (horizontal) {
					wrap.animate({left: -item.position().left}, time, conf.easing, callback);					
				} else {
					wrap.animate({top: -item.position().top}, time, conf.easing, callback);							
				}
				
				
				current = self;
				index = i;				
				
				// onStart
				e = $.Event("onStart");
				$self.trigger(e, [i]);				
				if (e.isDefaultPrevented()) { return self; }				
	
				
				/* default behaviour */
				
				// prev/next buttons disabled flags
				prev.add(prevPage).toggleClass(conf.disabledClass, i === 0);
				next.add(nextPage).toggleClass(conf.disabledClass, i >= self.getSize() - conf.size);
				
				return self; 
			},			
			
				
			move: function(offset, time, fn) {
				forward = offset > 0;
				return this.seekTo(index + offset, 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) {
				forward = offset > 0;
				var steps = conf.size * offset;
				
				var i = index % conf.size;
				if (i > 0) {
				 	steps += (offset > 0 ? -i : conf.size - i);
				}
				
				return this.move(steps, time, fn);		
			},
			
			prevPage: function(time, fn) {
				return this.movePage(-1, time, fn);
			},  
	
			nextPage: function(time, fn) {
				return this.movePage(1, time, fn);
			},			
			
			setPage: function(page, time, fn) {
				return this.seekTo(page * conf.size, time, fn);
			},			
			
			begin: function(time, fn) {
				forward = false;
				return this.seekTo(0, time, fn);	
			},
			
			end: function(time, fn) {
				forward = true;
				var to = this.getSize() - conf.size;
				return to > 0 ? this.seekTo(to, time, fn) : self;	
			},
			
			reload: function() {				
				$self.trigger("onReload");
				return self;
			},			
			
			focus: function() {
				current = self;
				return self;
			},
			
			click: function(i) {
				
				var item = self.getItems().eq(i), 
					 klass = conf.activeClass,
					 size = conf.size;			
				
				// check that i is sane
				if (i < 0 || i >= self.getSize()) { return self; }
				
				// size == 1							
				if (size == 1) {
					if (conf.loop) { return self.next(); }
					
					if (i === 0 || i == self.getSize() -1)  { 
						forward = (forward === undefined) ? true : !forward;	 
					}
					return forward === false  ? self.prev() : self.next(); 
				} 
				
				// size == 2
				if (size == 2) {
					if (i == index) { i--; }
					self.getItems().removeClass(klass);
					item.addClass(klass);					
					return self.seekTo(i, time, fn);
				}				
		
				if (!item.hasClass(klass)) {				
					self.getItems().removeClass(klass);
					item.addClass(klass);
					var delta = Math.floor(size / 2);
					var to = i - delta;
		
					// next to last item must work
					if (to > self.getSize() - size) { 
						to = self.getSize() - size; 
					}
		
					if (to !== i) {
						return self.seekTo(to);		
					}
				}
				
				return self;
			},
			
			// bind / unbind
			bind: function(name, fn) {
				$self.bind(name, fn);
				return self;	
			},	
			
			unbind: function(name) {
				$self.unbind(name);
				return self;	
			}			
			
		});
		
		// callbacks	
		$.each("onBeforeSeek,onStart,onSeek,onReload".split(","), function(i, ev) {
			self[ev] = function(fn) {
				return self.bind(ev, fn);	
			};
		});  
			
			
		// prev button		
		prev.addClass(conf.disabledClass).click(function() {
			self.prev(); 
		});
		

		// next button
		next.click(function() { 
			self.next(); 
		});
		
		// prev page button
		nextPage.click(function() { 
			self.nextPage(); 
		});
		
		if (self.getSize() < conf.size) {
			next.add(nextPage).addClass(conf.disabledClass);	
		}
		

		// next page button
		prevPage.addClass(conf.disabledClass).click(function() { 
			self.prevPage(); 
		});		
		
		
		// hover
		var hc = conf.hoverClass, keyId = "keydown." + Math.random().toString().substring(10); 
			
		self.onReload(function() { 

			// hovering
			if (hc) {
				self.getItems().hover(function()  {
					$(this).addClass(hc);		
				}, function() {
					$(this).removeClass(hc);	
				});						
			}
			
			// clickable
			if (conf.clickable) {
				self.getItems().each(function(i) {
					$(this).unbind("click.scrollable").bind("click.scrollable", function(e) {
						if ($(e.target).is("a")) { return; }	
						return self.click(i);
					});
				});
			}				
			
			// keyboard			
			if (conf.keyboard) {				
				
				// keyboard works on one instance at the time. thus we need to unbind first
				$(document).unbind(keyId).bind(keyId, function(evt) {

					// do nothing with CTRL / ALT buttons
					if (evt.altKey || evt.ctrlKey) { return; }
					
					// do nothing for unstatic and unfocused instances
					if (conf.keyboard != 'static' && current != self) { return; }
					
					var s = conf.keyboardSteps;				
										
					if (horizontal && (evt.keyCode == 37 || evt.keyCode == 39)) {					
						self.move(evt.keyCode == 37 ? -s : s);
						return evt.preventDefault();
					}	
					
					if (!horizontal && (evt.keyCode == 38 || evt.keyCode == 40)) {
						self.move(evt.keyCode == 38 ? -s : s);
						return evt.preventDefault();
					}
					
					return true;
					
				});
				
			} else  {
				$(document).unbind(keyId);	
			}				

		});
		
		self.reload(); 
		
	} 

		
	// jQuery plugin implementation
	$.fn.scrollable = function(conf) { 
			
		// already constructed --> return API
		var el = this.eq(typeof conf == 'number' ? conf : 0).data("scrollable");
		if (el) { return el; }		 
 
		var globals = $.extend({}, $.tools.scrollable.conf);
		conf = $.extend(globals, conf);
		
		conf.keyboardSteps = conf.keyboardSteps || conf.size;
		
		this.each(function() {			
			el = new Scrollable($(this), conf);
			$(this).data("scrollable", el);	
		});
		
		return conf.api ? el: this; 
		
	};
			
	
})(jQuery);


/**
 * jQuery TOOLS plugin :: scrollable.circular 0.5.1
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/scrollable.html#circular
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : September 2009
 * Date: ${date}
 * Revision: ${revision} 
 */
(function($) {

	// version number
	var t = $.tools.scrollable; 
	t.plugins = t.plugins || {};
	
	t.plugins.circular = {
		version: '0.5.1', 
		conf: { 
			api: false,
			clonedClass: 'cloned'
		} 		
	};

	
	$.fn.circular = function(opts)  {
	
		var config = $.extend({}, t.plugins.circular.conf), ret;
		$.extend(config, opts);
		
		this.each(function() {			
 
			var api = $(this).scrollable(),
				 items = api.getItems(), 
				 conf = api.getConf(), 
				 wrap = api.getItemWrap(), 
				 index = 0;
				 
			if (api) { ret = api; }
				
			// too few items. no need for this plugin.
			if (items.length < conf.size) { return false; }
			

			// clone first visible elements and append them to the end			
			items.slice(0, conf.size).each(function(i) {
  				$(this).clone().appendTo(wrap).click(function()  {
					api.click(items.length + i);
					
				}).addClass(config.clonedClass);			
			});			
			
			// clone last set of elements to the beginning in reversed order
			var tail = $.makeArray(items.slice(-conf.size)).reverse();
			
  			$(tail).each(function(i) {
				$(this).clone().prependTo(wrap).click(function()  {
					api.click(-i -1);			
					
				}).addClass(config.clonedClass);				
			});
			
			var allItems = wrap.children(conf.item);
			
			
			// reset hovering for cloned items too
			var hc = conf.hoverClass;
			
			if (hc) {
				allItems.hover(function()  {
					$(this).addClass(hc);		
				}, function() {
					$(this).removeClass(hc);	
				});						
			}
		 
			// custom seeking function that does not trigger callbacks
			function seek(i) {
				
				var item = allItems.eq(i);
				
				if (conf.vertical) {						
					wrap.css({top: -item.position().top});
				} else {
					wrap.css({left: -item.position().left});							
				}					
			}
			
			// skip the clones at the beginning
			seek(conf.size);			

			// overridden scrollable API methods
			$.extend(api, {

				move: function(offset, time, fn, click) {  
					
					var to = index + offset + conf.size;				
					var exceed = to > api.getSize() - conf.size; 
					
					if (to <= 0 || exceed) {
						var fix = index + conf.size + (exceed ? -items.length : items.length);
						seek(fix);
						to = fix + offset;
					} 
					
					if (click) {
						allItems.removeClass(conf.activeClass)
							.eq(to + Math.floor(conf.size / 2)).addClass(conf.activeClass);
					}
					
					// nothing happens
					if (to === index + conf.size) { return self; }					

					return api.seekTo(to, time, fn);
				},			
				
				begin: function(time, fn) {
					return this.seekTo(conf.size, time, fn);	
				},
				
				end: function(time, fn) {				
					return this.seekTo(items.length, time, fn);	
				},
				
				click: function(i, time, fn) {		
					
					if (!conf.clickable) { return self; }
					if (conf.size == 1) { return this.next(); }
					
					var to = i - index, klass = conf.activeClass;				
					to -= Math.floor(conf.size / 2);				
					
					return this.move(to, time, fn, true);
				},
				
				getIndex: function() {
					return index;  
				},
				
				setPage: function(page, time, fn) {
					return this.seekTo(page * conf.size + conf.size, time, fn);	
				},
				
				getPageAmount: function()  {
					return Math.ceil(items.length / conf.size);		
				},
				
				getPageIndex: function()  {            	
					if (index < 0) { return this.getPageAmount() -1; }
					if (index >= items.length) { return 0; }
					var i = (index + conf.size) / conf.size -1;
					return i;
				},

				getVisibleItems: function() {
					var i = index + conf.size;
					return allItems.slice(i, i + conf.size);	
				} 
				
			});  
			
			// update index 
			api.onStart(function(e, i) {		
				index = i - conf.size;
				
				// navi buttons are never disabled
				return false;
			});				
			
			api.getNaviButtons().removeClass(conf.disabledClass);
			
				
		});
		
		return config.api ? ret : this;
		
	};

		
})(jQuery);
		
/**
 * jQuery TOOLS plugin :: scrollable.autoscroll 1.0.1
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/scrollable.html#autoscroll
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : September 2009
 * Date: ${date}
 * Revision: ${revision} 
 */
(function($) {		

	var t = $.tools.scrollable; 
	t.plugins = t.plugins || {};
	
	t.plugins.autoscroll = {
		version: '1.0.1',
		
		conf: {
			autoplay: true,
			interval: 3000,
			autopause: true,
			steps: 1,
			api: false
		}
	};	
	
	// jQuery plugin implementation
	$.fn.autoscroll = function(conf) { 

		if (typeof conf == 'number') {
			conf = {interval: conf};	
		}
		
		var opts = $.extend({}, t.plugins.autoscroll.conf), ret;
		$.extend(opts, conf);   	
		
		this.each(function() {		
				
			var api = $(this).scrollable();			
			if (api) { ret = api; }
			
			// interval stuff
			var timer, hoverTimer, stopped = true;
	
			api.play = function() {
	
				// do not start additional timer if already exists
				if (timer) { return; }
				
				stopped = false;
				
				// construct new timer
				timer = setInterval(function() { 
					api.move(opts.steps);				
				}, opts.interval);
				
				api.move(opts.steps);
			};	

			api.pause = function() {
				timer = clearInterval(timer);	
			};
			
			// when stopped - mouseover won't restart 
			api.stop = function() {
				api.pause();
				stopped = true;	
			};
		
			/* when mouse enters, autoscroll stops */
			if (opts.autopause) {
				api.getRoot().add(api.getNaviButtons()).hover(function() {			
					api.pause();
					clearInterval(hoverTimer);
					
				}, function() {
					if (!stopped) {						
						hoverTimer = setTimeout(api.play, opts.interval);						
					}
				});
			}			
			
			if (opts.autoplay) {
				setTimeout(api.play, opts.interval);				
			}

		});
		
		return opts.api ? ret : this;
		
	}; 
	
})(jQuery);		


/**
 * jQuery TOOLS plugin :: scrollable.navigator 1.0.2
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/scrollable.html#navigator
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : September 2009
 * Date: ${date}
 * Revision: ${revision} 
 */
(function($) {
		
	var t = $.tools.scrollable; 
	t.plugins = t.plugins || {};
	
	t.plugins.navigator = {
		version: '1.0.2',
		
		conf: {
			navi: '.navi',
			naviItem: null,		
			activeClass: 'scroll-navi-active',
			indexed: false,
			api: false,
			idPrefix: null
		}
	};		
		
	// jQuery plugin implementation
	$.fn.navigator = function(conf) {

		var globals = $.extend({}, t.plugins.navigator.conf), ret;
		if (typeof conf == 'string') { conf = {navi: conf}; }
		
		conf = $.extend(globals, conf);
		
		this.each(function() {
			
			var api = $(this).scrollable(),
				 root = api.getRoot(), 
				 navi = root.data("finder").call(null, conf.navi), 
				 els = null, 
				 buttons = api.getNaviButtons();
			
			if (api) { ret = api; }
			
			api.getNaviButtons = function() {
				return buttons.add(navi);	
			}; 
				
			// generate new entries
			function reload() {
				
				if (!navi.children().length || navi.data("navi") == api) {
					
					navi.empty();
					navi.data("navi", api);
					
					for (var i = 0; i < api.getPageAmount(); i++) {		
						navi.append($("<" + (conf.naviItem || 'a') + "/>"));
					}
					
					els = navi.children().each(function(i) {
						var el = $(this);
						el.click(function(e) {
							api.setPage(i);							
							return e.preventDefault();
						});
						
						// possible index number
						if (conf.indexed)  { el.text(i); }
						if (conf.idPrefix) { el.attr("id", conf.idPrefix + i); }
					});
					
					
				// assign onClick events to existing entries
				} else {
					
					// find a entries first -> syntaxically correct
					els = conf.naviItem ? navi.find(conf.naviItem) : navi.children();
					
					els.each(function(i)  {
						var el = $(this);
						
						el.click(function(evt) {
							api.setPage(i);
							return evt.preventDefault();						
						});
						
					});
				}
				
				// activate first entry
				els.eq(0).addClass(conf.activeClass); 
				
			}
			
			// activate correct entry
			api.onStart(function(e, index) {
				var cls = conf.activeClass;				
				els.removeClass(cls).eq(api.getPageIndex()).addClass(cls);
			});
			
			api.onReload(function() {
				reload();		
			});
			
			reload();			
			
			// look for correct navi item from location.hash
			var el = els.filter("[href=" + location.hash + "]");	
			if (el.length) { api.move(els.index(el)); }			
			
			
		});		
		
		return conf.api ? ret : this;
		
	};
	
})(jQuery);			


/**
 * jQuery TOOLS plugin :: scrollable.mousewheel 1.0.1
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/scrollable.html#mousewheel
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : September 2009
 * Date: ${date}
 * Revision: ${revision} 
 *
 * 
 * jquery.event.wheel.js - rev 1 
 * Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
 * Liscensed under the MIT License (MIT-LICENSE.txt)
 * http://www.opensource.org/licenses/mit-license.php
 * Created: 2008-07-01 | Updated: 2008-07-14
 */
(function($) {
		
	$.fn.wheel = function( fn ){
		return this[ fn ? "bind" : "trigger" ]( "wheel", fn );
	};

	// special event config
	$.event.special.wheel = {
		setup: function(){
			$.event.add( this, wheelEvents, wheelHandler, {} );
		},
		teardown: function(){
			$.event.remove( this, wheelEvents, wheelHandler );
		}
	};

	// events to bind ( browser sniffed... )
	var wheelEvents = !$.browser.mozilla ? "mousewheel" : // IE, opera, safari
		"DOMMouseScroll"+( $.browser.version<"1.9" ? " mousemove" : "" ); // firefox

	// shared event handler
	function wheelHandler( event ) {
		
		switch ( event.type ){
			
			// FF2 has incorrect event positions
			case "mousemove": 
				return $.extend( event.data, { // store the correct properties
					clientX: event.clientX, clientY: event.clientY,
					pageX: event.pageX, pageY: event.pageY
				});
				
			// firefox	
			case "DOMMouseScroll": 
				$.extend( event, event.data ); // fix event properties in FF2
				event.delta = -event.detail / 3; // normalize delta
				break;
				
			// IE, opera, safari	
			case "mousewheel":				
				event.delta = event.wheelDelta / 120;
				break;
		}
		
		event.type = "wheel"; // hijack the event	
		return $.event.handle.call( this, event, event.delta );
	}
	
	
	// version number
	var t = $.tools.scrollable; 
	t.plugins = t.plugins || {};
	t.plugins.mousewheel = {	
		version: '1.0.1',
		conf: { 
			api: false,
			speed: 50
		} 
	}; 
	
	// scrollable mousewheel implementation
	$.fn.mousewheel = function(conf) {

		var globals = $.extend({}, t.plugins.mousewheel.conf), ret;
		if (typeof conf == 'number') { conf = {speed: conf}; }
		conf = $.extend(globals, conf);
		
		this.each(function() {		

			var api = $(this).scrollable();
			if (api) { ret = api; }
			
			api.getRoot().wheel(function(e, delta)  { 
				api.move(delta < 0 ? 1 : -1, conf.speed || 50);
				return false;
			});
		});
		
		return conf.api ? ret : this;
	};
	
})(jQuery); 


