/**
 * @plugin: thi.jquery.scroll
 * @author: THI Dev team
 * @version: 1.1
 * */
(function($) {
	
	$.fn.thiScroll = function(options) {
		
		var settings = {
			target: '.boxes',
			up: '#scrolling-nav #prev',
			down: '#scrolling-nav #next',
			duration: 1000,
			//Callbacks
			onLast: function() {			
			},
			onFirst: function() {				
			},
			resetOnUp: function() {				
			},
			resetOnDown: function() {
			}
		};
		
		//extend
		$.extend(settings, options);
		
		return this.each(function() {
			
			var base 			= this,
				container_off	= $(base).offset(),				
				anchors			= [],
				$up_link		= $(settings.up),
				$down_link		= $(settings.down);
			
			//Set current
			$(base).data('pointer',0);
			
			/**
			 * @method: init
			 */
			this.init = function() {
				
				//Load anchors
				$(base).find(settings.target).each(function() {
					anchors.push(
					 {panel: $(this), offset: $(this).offset()}
					);
				});
				
				if(anchors.length <= 0) return false;
				
				//Down click event
				$down_link.click(function() {
					base.goDown();
				});
				
				//Up click event
				$up_link.click(function() {
					base.goUp();
				});
				
				//Handler Events
				/*$(document).keypress(function(e) {
					switch(e.keyCode) {
						case 38: //Up
						break;
					}
				})*/
				
			};
			
			/**
			 * @method: goUp
			 */
			this.goUp = function() {
				if(!base.isFirst()) {
					var pointer = $(base).data('pointer')-1;
					
					//Move to next anchor
					base.moveTo(anchors[pointer]);
					
					//Refresh pointer
					$(base).data('pointer',pointer);
					
					if(base.isFirst()) {
						settings.onFirst(); //Callback
					}
					else {
						settings.resetOnUp();
					}
				}
				else {
					//$up_link.css({visibility:'hidden'});					
				}
			};
			
			/**
			 * @method: goDown
			 */
			this.goDown = function() {
				if(!base.isLast()) {
					var pointer = $(base).data('pointer')+1;
					
					//Move to next anchor
					base.moveTo(anchors[pointer]);
					
					//Refresh pointer
					$(base).data('pointer',pointer);
					
					if(base.isLast()) {
						settings.onLast(); //Callback
					}
					else {
						settings.resetOnDown(); //Callback
					}
				}
				else {
					//$down_link.css({visibility:'hidden'});					
				}
			};
			
			/**
			 * @method: isLast
			 */
			this.isLast = function() {
				if((anchors.length-1) == $(base).data('pointer')) {
					return true;
				}				
				return false;
			};
			
			/**
			 * @method: isLast
			 */
			this.isFirst = function() {
				if($(base).data('pointer') == 0) {
					return true;
				}
				
				return false;
			};
			
			/**
			 * @method: moveTo
			 */
			this.moveTo = function(p) {
				$(base).stop().animate({
					marginTop: parseInt((p.offset.top - container_off.top)*-1) + "px"
				},
				settings.duration,
				function() {
					//console.info('Animation completed');
				});
			};			
			
			/**
			 * @callback: onFirst
			 */
			settings.onFirst = function() {
				$up_link.css('visibility','hidden');
			};
			
			/**
			 * @callback: onLast
			 */
			settings.onLast = function() {
				$down_link.css('visibility','hidden');
			};
			
			/**
			 * @callback: resetOnDown
			 */
			settings.resetOnDown = function() {
				if($up_link.css('visibility') === 'hidden') {
					$up_link.css('visibility','visible');
				}
			};
			
			/**
			 * @callback: resetOnUp
			 */
			settings.resetOnUp = function() {
				if($down_link.css('visibility') === 'hidden') {
					$down_link.css('visibility','visible');
				}
			};
			
			/**
			 * @handler: 
			 */
			this.actions = function() {
				
			}
			
			this.init();
		});
		
	}
	
})(jQuery);
