/*

Interactive plugin
by Jon Krauss, vinagency.com

*/

(function($) {	

	$.fn.scroller = function(options) {
			
		var defaults = {
			scroll_nav_id:'scroll-nav',
			nav_links_id:'scroll-nav-links',
			section_class:'nav-section',
			menu_id:'scroll-nav-links',
			button_main_id:'button-main',
			button_top_id:'button-top',
			extra_spacing:-200
			//hotspot_class: 'hotspot'//html element comprising each individual page
		};
		
		var settings = $.extend(defaults, options);

		var 	el = this,
			$scroller,// = $('#'+settings.scroll_nav_id),
			$menu = $('#'+settings.nav_links_id),
			$menu_items = $menu.find('a'),
			$sections = $('.'+settings.section_class),
			total_sections = $sections.length + 1,
			id_array = [],
			current_index = 0;

		init = function(){
			
			if (total_sections < 2){
				return false;
			}
			
//log('scroller '+total_sections+' '+$menu_items.length);
			
			$('body').append( $('<div id="'+ settings.scroll_nav_id +'"><div><a href="#" id="'+ settings.button_main_id +'"><span></span></a></div></div>') );
			//<a href="#" id="'+ settings.button_top_id +'">Top</a>
			$scroller = $('#'+settings.scroll_nav_id);

			//stash ids in array
			id_array[0] = 'top';
			$.each($menu_items,function(i){
				//$(this).attr('rel',i);
				id_array[i+1] = $(this).attr('href').slice(1);
//log('id_array  '+id_array[i+1]);

			});

//log('scroller  '+total_sections+' '+id_array.length);


			attachEvents();
		};

		log = function(s){
			if (typeof console != "undefined" && typeof console.debug != "undefined") {
				console.log(s);
			}
		};

		attachEvents = function(){


			$('#'+settings.button_main_id).bind("click", scrollClick);
			$menu_items.bind("click", menuItemClick);
			
			
			$('#'+settings.button_top_id).bind('click',function(){
			
				//$('html, body').animate({scrollTop:0}, 'slow');
				//return false;
				select(0);
				
			});
		};
		
		scrollClick = function(e){

			var 	id,
				index = current_index;
			
			e.preventDefault();
			e.currentTarget.blur();
			
			index++;
			
			index = index >= total_sections ? 0 : index;

			
			//id = $menu_items.eq(index).attr('href').slice(1);
log('scrollClick '+index+' '+id_array[index]);
			
			
//log('scrollClick  '+id+' '+current_index+'/'+index +' '+ total_sections);
			
			select(index);
			
			e.currentTarget.blur();
			
			//return false;
		};
		
		menuItemClick = function(e){
			
			var 	o = $(e.currentTarget),
				index = o.index() + 1,
				//target = $sections.eq(index),
				id = e.currentTarget.hash.slice(1);
				
			e.preventDefault();
			o.blur();
log('menuItemClick '+ index +' '+ id +' '+id_array[index]);
			
			select(index);

			

		};
		
		getIndexAtID = function(id){
			
			
			//var  item = $(id_array).find(id),
			//index = item.index();
				
//alert('getIndexAtID '+id);
//var index = 1;
			var index = id_array.indexOf(id);
				
//log('getIndexAtID '+index+' '+id);
	
			return index;
		};
		
		select = function(index){
			
			//var index;
			//index = getIndexAtID(id);
			
			var id = id_array[index];

			current_index = index;	
			
			scrollToID( id );
			
			setHighlight(index);
//log('select '+id+' '+index+' '+current_index);
		};
		
		
		setHighlight = function(index){
			
			//$menu_items.removeClass('active');
			
			/*$menu_items.each(function(i){
				if (i == index){
					
					$(this).addClass('active');
				}
			});*/
			
			if (index == total_sections - 1){
				$('#'+settings.scroll_nav_id).find('span').addClass('invert');
			} else {
				$('#'+settings.scroll_nav_id).find('span').removeClass('invert');
			}
		};
		
		scrollToID = function(id){
		
			//animate to inner link
			var 	target = $('#'+id),
				new_top = target.offset().top + settings.extra_spacing;
				
log('scrollToID  '+new_top+' '+id);
				
			$('html, body').animate({scrollTop:  new_top}, 'slow');
		};

		
		init();
	};
})(jQuery);
