/*******

	***	Ruler Navigation ***
	*** http://manic.com.sg @wtfroland***
	
*****/
(function($){
	var settings = {
		sections: [],
		rulerClasses: [],
		marginTop: 28,
		lst: 0,
		clicked: false,
		homeFunc : function(){},
		footV:0
	};
	var expanded,actclass,drag,footclick,clickeind,int;
	var methods = {
		init : function(options) {
			return this.each(function(){
				if (options) { 
					$.extend(settings, options);
			  	}
				
				drag = false;
				footclick = false;
				
				$('.marker').css({'top':-7});
				
				$(window).bind('scroll.ruler',function() {
					if(settings.clicked == false){
						methods.scrollCheckSideNav();
					}
					
					methods.checkList();
					methods.posLinks();
					methods.sticky($('.marker'));
				});
				
				$(window).bind('resize.ruler',function() {
					methods.scrollCheckSideNav();
					methods.checkList();
					methods.posLinks();
					methods.sticky($('.marker'));
				});
				
				$('.marker').css({
					'cursor':'pointer'
				}).mousedown(function(){
					drag = true;
					methods.bindBody();
				});
				
				$('#sideNav').click(function(event){
					var elname = event.target.nodeName;
					if(elname == 'ASIDE'){
						var pos,perpos,scrollto;
						pos = event.pageY - $(this).offset().top;
						perpos = methods.getPosRatio($(this).height(),pos);
						scrollto = perpos * $(document).height();
						
						$('body,html').animate({ scrollTop: scrollto }, 500);
					}
				});
				
				methods.initNav();
				methods.makeActive(0);
				
				methods.scrollCheckSideNav();
				
				methods.checkList();
				methods.posLinks();
				
				// show the ruler
				$(this).fadeIn(500);
			});
		},
		bindBody : function () {
			$(document).bind('mousemove',function(e){
				var pos,perpos,scrollto;
				if(drag == true){
					
					pos = e.pageY - $('#sideNav').offset().top;
					
					if(pos > ($('#sideNav').height()-($('.marker').height()*.5))){
						pos = $('#sideNav').height()-($('.marker').height()*.5);
					}
					if(pos < 0){
						pos = 2;
					}
					
					perpos = methods.getPosRatio($('#sideNav').height(),pos);
					
					scrollto = perpos * $(document).height();
					
					$('#debug').text(pos + ' : ' + settings.footV);
					
					$('body,html').scrollTop(scrollto);
					
					if(pos > $('.foot').offset().top){
						pos = $('.foot').offset().top;
					}
					
					$('.marker').css({'top':pos-10});
					
				}
			}).mouseup(function(){
				drag = false;
				
				methods.unbindBody();
				
				methods.scrollCheckSideNav();
				methods.checkList();
				methods.posLinks();
				methods.sticky($(this));
				
				$('#sideNav').css({'cursor':'auto'});
			});
		},
		unbindBody : function () {
			$(document).unbind('mousemove');
			$(document).unbind('mouseup');
		},
		scrollCheckSideNav : function() {
			var pos, st, bot;
		
			st = $(window).scrollTop();
			
			bot = $(document).height()-$(window).height();
			
			// adding 80 will activate it 80 px before the top
			if(st > settings.lst){
				pos = st + 80;
			}else{
				pos = st + 100;
			}
			
			var ind = 0;
			
			for(i=0;i<settings.sections.length;i++){
				if(pos > $(settings.sections[i]).offset().top){
					ind = i;
				}
			}
			
			if(pos > bot){
				ind = settings.sections.length-1;
			}
			
			if(ind == 3 && settings.homeFunc()!== 'undefined'){
				settings.homeFunc();
			}
			
			methods.makeActive(ind);
			
			settings.lst = st;
		},
		checkList : function(h,p) {
			methods.updateFootV();
			methods.updateNPos();
			methods.checkWindowWidth();
		},
		sticky : function($actNav){
			var docH = $(document).height();
			if(settings.footV > 0){
				docH = $(document).height() - settings.footV;
			}
			var topoffset = methods.getPosRatio(docH,$(window).scrollTop());
			var ntop = $('#sideNav').height() * topoffset;
			
			if(ntop < 1.5){
				ntop = -7;
			}
			
			if(drag == false){
				// check if footer
				if(clickeind == settings.sections.length-1){
					$actNav.stop(true).animate({ top: ($('#sideNav').height() - $('.foot').height() - 10) + "px" }, 300);
				}else{
					$actNav.stop(true).animate({ top: ntop + "px" }, 300);
				}				
			}
		},
		updateFootV : function(){
			var loc = $(window).scrollTop() + $(window).height();
		 	
			settings.footV = loc - $('footer').offset().top;
		},
		checkWindowWidth : function(h,p) {
			var mw = 1130 + $('#ruler').width() + $('#sideNav').width();
			if($(window).width() >= mw){
				$('ul#guide').stop(true, true).animate({ opacity: 1 }, 'fast');
				expanded = true;
			}else{
				$('ul#guide').stop(true, true).animate({ opacity: 0 }, 'fast');
				expanded = false;
			}
		},
		updateNPos : function() {
			if(settings.footV > 0){
				h = $(window).height() - settings.footV - settings.marginTop + "px";
			}else{
				h = $(window).height() - settings.marginTop + "px";
			}
			
			$('#sideNav').css({height: h});
			$('#marker').css({height: h});
			
			ho  = $('header').offset();
			
			if($(window).width() >= 1080){
				$('#sideNav').css({left: ho.left-70});
			}else{
				$('#sideNav').css({left: 0});
			}
		},
		posLinks : function() {
			var y,c,r;
			
			$('.home').css({top: -7 + "px"});
			
			var docH = $(document).height();
			
			$('ul#guide li').each(function(index) {
				if(index > 0){
					c = $(settings.sections[index]).offset();
					r = methods.getPosRatio(docH,c.top);
					y = $('#sideNav').height() * r;
					
					$nav = $(this);
					if(index ==settings.sections.length-1){
						$nav.css({top: ($('#sideNav').height()-$(this).height()-10) + "px"});
					}else{
						$nav.css({top: y + "px"});
					}
				}
			});
			
			$('ul#ruler li').each(function(index) {
				if(index > 0){
					c = $(settings.sections[index]).offset();
					r = methods.getPosRatio(docH,c.top);
					y = $('#sideNav').height() * r;
					
					$nav = $(this);
					if(index ==settings.sections.length-1){
						$nav.css({top: ($('#sideNav').height()-$(this).height()-10) + "px"});
					}else{
						$nav.css({top: y + "px"});
					}
					
				}
			});
		},
		initNav : function() { 
			$('ul#ruler li').each(function(index) {
				$(this).css( 'cursor', 'pointer' ).click(function(){
					
					// make active then reset the nav status
					methods.makeActive(index);
					
					settings.clicked = true;
					v = methods.getSection(index);
					
					$('html, body').animate({ scrollTop: v }, 500, function() {
						settings.clicked = false;
					});
					
				});
			});
			
			$('ul#guide li').each(function(index) {
				$(this).css( 'cursor', 'pointer' ).click(function(){
					
					methods.makeActive(index);
					
					settings.clicked = true;					
					v = methods.getSection(index);
					
					$('html, body').animate({ scrollTop: v }, 500, function() {
						settings.clicked = false;
					});
					
				});
			});
		},
		makeActive : function(l) {
			// make guide active 
			$('ul#guide li').each(function(index) {
				if(l==index){
					$(this).addClass("activeGuide");
					$(this).attr("mnc","");
				}else{
					$(this).removeClass("activeGuide");
					$(this).removeAttr("mnc");
				}
			});
			
			clickeind = l;
			
			// add/bind hover again
			methods.resetNav();
			
			methods.posLinks();
		},
		resetNav : function() {
			// reapply events
			$('ul#ruler li').each(function(index) {
				var attrib = $(this).attr('mnc');
				if(typeof attrib !== 'undefined' && attrib !== false){
					$(this).unbind('mouseenter mouseleave');
					$(this).css({'background-position':'-168px 50%','width':'14px'});
				}else{
					$(this).css({'background-position':'0 50%','width':'14px'});
					
					$(this).hover(
						function () {
							actclass = $(this).attr('class');
							if(expanded==false){
								var hw = methods.getGuideWidth($(this).text());
								$(this).stop(true).animate({ 
									backgroundPosition: '-20px',
									width: hw
								}, 300, 'easeOutBack')
								.css({"text-indent":'7px'});
							}else{
								$(this).css({'background-position':'-168px 50%','width':'14px'});
								$('ul#guide li.'+actclass).addClass("activeGuide");
							}
						}, 
						function () {
							if(expanded==false){
								$(this).stop(true).animate({ 
									backgroundPosition: '0',
									width: '14px'
								}, 300, 'easeOutBack')
								.css({"text-indent":'-9999px'});
							}else{
								$(this).css({'background-position':'0 50%','width':'14px'});
								$('ul#guide li.'+actclass).removeClass("activeGuide");
							}
						}
					);
				}
			});
			
			$('ul#guide li').each(function(index) {
				var attrib = $(this).attr('mnc');
				if(typeof attrib !== 'undefined' && attrib !== false){					
					$(this).unbind('mouseenter mouseleave');					
				}else{
					$(this).hover(
						function () {
							if(expanded==true){
								$(this).addClass("activeGuide");
							}
						},
						function () {
							if(expanded==true){
								$(this).removeClass("activeGuide");
							}
						}
					);
				}
			});
		},
		getGuideWidth : function(name) {
			var w;
			$('ul#guide li').each(function(index) {
				if($(this).text() == name){
					w = $(this).width() + 20 + "px"
				}
			});
			return w; 
		},
		getPosRatio : function(h,p) {
			var r = p/h;
			return r;
		},
		getSection : function(s) {
			var v;
			if(s == settings.sections.length-1){
				v = $(document).height()-$(window).height();
			}else{
				v = $(settings.sections[s]).offset().top;
			}
			return v;
		},
		destroy : function() {
			$(window).unbind('scroll.ruler');
			$(window).unbind('resize.ruler');
			return this;
		}
	};

	$.fn.rulerscroll = function(method) {
		if (methods[method]) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if (typeof method === 'object' || ! method) {
			return methods.init.apply( this, arguments );
		} else {
			$.error('Method ' +  method + ' does not exist on this plugin.');
		}    
	
	};

})(jQuery);
