(function($) {
	var settings = {navigation: '.improscroller-navigation', pages : '#pages',page : '.page', next: '#button_next a', prev : '#button_prev a'
		, start : function() {}, end : function() {}}
	, position
	, scrolling = false
	, base
	, fn = {
			init : function(options) {
				if (options) $.extend( true, settings, options);
				if (typeof window.console === "undefined"){ 
					window.console = {};
					window.console.log = function() {return false;}; 
				}
				/**
				 * set initial position as the first page of the set
				 */
				position = $(settings.page).eq(0);
				
				/**
				 * Setup forwards & backwards scroll buttons
				 */
				$(settings.next).bind('click.improscroller', function() {
					var idx = position.parent().children().index(position) + 1;
					if (idx >= $(settings.page).length ) 
						idx = 0;
					fn._scroll(idx);
					return false;
				});
				$(settings.prev).bind('click.improscroller', function() {
					var idx = position.parent().children().index(position) - 1;
					if (idx < 0) 
						idx = $(settings.page).length -1;
					fn._scroll(idx);
					return false;
				});
				
				/**
				 * Setup navigation links
				 */
				$('a', $('body')).each(function(i, linkObj) {
					var url = $(linkObj).attr('href'); 
					
					if($(this).hasClass('noscroll'))
						return;
	
					if (url && (url.substring(0, 1) == '/' || (url.substring(0, 4) != 'http' && url.substring(0, 1) != '#' && url.substring(0, 3) != 'www'))) {
						$(linkObj).unbind('click.improscroller').bind('click.improscroller', function(e) {
							var parts = $(this).attr('href').split('/')
							, link    = (parts.length > 1)? parts.pop() : $(this).attr('href') 
							, target  = $('#goto-' + link)
							, idx     = target.parent().children().index(target);
							fn._scroll(idx);
							return false;
						});
					}
				});
			}
		   , _scroll : function(toIndex) {
			  	var left, from = position, to = $(settings.page).eq(toIndex);
			  	
				if (scrolling) 
					return;
				
				scrolling = true;
				
				left = -1 * (parseInt(toIndex) * 990);
				settings.start.call(position, from, to);
				$(settings.pages).animate({left: left + 'px'}, 800, function() {
					settings.stop.call(position, from, to);
					position = $(settings.page).eq(toIndex);
					scrolling = false;
				});
		    }
	}
	$.fn.improscroller = function(a) {if ( fn[a] ) return fn[a].apply( this, Array.prototype.slice.call( arguments, 1 )); else if ( typeof a === 'object' || ! a ) return fn.init.apply( this, arguments );}
})(jQuery);

