
/**
 * jquery.scrollable 0.11. Making HTML elements scroll.
 * 
 * http://flowplayer.org/tools/scrollable.html
 *
 * Copyright (c) 2008 Tero Piirainen (tero@flowplayer.org)
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * >> Basically you can do anything you want but leave this header as is <<
 *
 * Since  : 0.01 - 03/01/2008
 * Version: 0.11 - 05/29/2008
 */
var currentArrowDirection = false;
var intervalAction = null;
var intervalStatus = false;

(function($) {
		
	// plugin initialization
	$.fn.extend({
		scrollable: function(arg1, arg2, arg3) { 
			
			return this.each(function() {
				if (typeof arg1 == "string") {
					var el = $.data(this, "scrollable");
					el[arg1].apply(el, [arg2, arg3]);
					
				} else { 
					new $.scrollable(this, arg1, arg2);
				}
			});
		}		
	});
		
	// constructor
	$.scrollable = function(el, opts) {   
		$.data(el, "scrollable", this);
		this.init(el, opts); 
	};

	// methods
	$.extend($.scrollable.prototype, { 
			
		init: function(el, config)  {
			 
			// current instance
			var self = this;  
			var check = $('div[class="__scrollable"]').length;

			var opts = {								
				size: 5,
				horizontal:false,				
				activeClass:'active',
				speed: 300,
				onSeek: null,
				poss: null,
				
				// jquery selectors
				items: '.items',
				prev:'.prev',
				next:'.next',
				navi:'.navi',
				naviItem:'span'
			}; 
	
			this.opts = $.extend(opts, config); 			
			this.index = 0;
			
			// root / itemRoot
			var root = this.root = $(el);			
			var itemRoot = $(opts.items, root);			
			if (!itemRoot.length) itemRoot = root;	
			
			this.newInterval = function(){
				//console.log(self.index + ' - ' +currentArrowDirection);
				self.seekTo(self.index + currentArrowDirection);
			}
			// wrap itemRoot.children() inside container
			
			if(check  < 1){
				
				itemRoot.css({position:'relative', overflow:'hidden', visibility:'visible'});
				itemRoot.children().wrapAll('<div class="__scrollable" style="position:absolute"/>');
				this.wrap = itemRoot.children(":first");
				this.wrap.css(opts.horizontal ? "width" : "height", "200000em").after('<br clear="all"/>');		
			
				// mousewheel

				if ($.isFunction($.fn.mousewheel)) { 
					root.bind("mousewheel.scrollable", function(event, delta)  { 
						self.move(-delta, 50);		
						return false;
					});
				} 
				
			}else{
				itemRoot.css({position:'relative', overflow:'hidden', visibility:'visible'});
				this.wrap = itemRoot.children(":first");
				this.wrap.css(opts.horizontal ? "width" : "height", "200000em");
			}
			
			this.items = this.wrap.children();
		
			// set height based on size
			if (opts.horizontal) {
				itemRoot.width(opts.size * (this.items.eq(1).offset().left - this.items.eq(0).offset().left) -2);	
			} else {
				itemRoot.height(opts.size * (this.items.eq(1).offset().top - this.items.eq(0).offset().top) -2);	
			} 

			//this.activeIndex = 0;
			
			if(check  < 1){
				// prev
				$(opts.prev, root).click(function() { self.prev() });
				
				$(opts.prev, root).mousedown(function() { 	
					currentArrowDirection = -1;
					self.startInterval();
					self.createInterval();
				});
			   
			   $(opts.prev, root).mouseup(function() { 	
					self.stopInterval();
				});
				
				// next
				$(opts.next, root).click(function() { self.next() });
				$(opts.next, root).mousedown(function() { 	
					currentArrowDirection = 1;
					self.startInterval();
					self.createInterval();
				});
			   
			    $(opts.next, root).mouseup(function() { 	
					self.stopInterval();
				});
			}				
			
			
			if(check > 0){
				self.begin();
			}
			
			if(this.opts.poss != null){
				var calc = this.opts.poss - (Math.round(this.opts.size/2)) ;
				self.seekTo(calc);	
			}
		
		},
		
		getStatus: function() {
			var len =  this.items.size();
			var s = {
				length: len, 
				index: this.index, 
				size: this.opts.size,
				pages: Math.floor(len / this.opts.size),
				page: Math.floor(this.index / this.opts.size)
			};

			return s;
		}, 
		
		// all other seeking functions depend on this generic seeking function		
		seekTo: function(index, time) {
			
			var check = $('div[class="__scrollable"]').length;
			
			if (index < 0) index = 0;	
			index = Math.min(index, this.items.length - this.opts.size);
			this.index = index;

			var item = this.items.eq(index-1);	
			if (item.size() == 0) { this.stopInterval(); return false; } 			

			if (this.opts.horizontal) {
				var left = this.wrap.offset().left - item.offset().left;				
				this.wrap.animate({left: left}, time || this.opts.speed);
			} else {
				var top = this.wrap.offset().top - item.offset().top;
				this.wrap.animate({top: top}, time || this.opts.speed);							
			}
			
			return true; 
		},
		
		startInterval: function(){
			intervalStatus = true;	
		}		
		,
		stopInterval: function(){
			intervalStatus = false;
			clearInterval(intervalAction);
		},
		
		createInterval: function(){
			if(intervalStatus){	
				intervalAction = setInterval(this.newInterval,450);
			}
		},

		move: function(offset, time) {
			this.seekTo(this.index + offset, time);
		},
		
		next: function(time) {
			this.move(1, time);	
		},
		
		prev: function(time) {
			this.move(-1, time);	
		},
		
		movePage: function(offset, time) {
			this.move(this.opts.size * offset, time);		
		},
		
		setPage: function(index, time) {
			this.seekTo(this.opts.size * index, time);
		},
		
		prevPage: function(time) {
			var page = Math.floor(this.index / this.opts.size);
			this.seekTo(this.opts.size * (page-1), time);
		},  

		nextPage: function(time) {
			var page = Math.floor(this.index / this.opts.size);
			this.seekTo(this.opts.size * (page+1), time);
		}, 
		
		begin: function(time) {
			this.seekTo(0, time);	
		},
		
		end: function(time) {
			this.seekTo(this.items.size() - this.opts.size, time);	
		}

		
	});  
	
})(jQuery);




