/*
 * Font Resizer - jQuery anchor navigation
 * Function: create an accessible navigation of <a> tags within a specific area.
 * Copyright (c) 2010 Media Dog Productions - www.mediadog.ca
 *
 * V1.0
 */

(function($) {

	// jQuery plugin definition
	
	$.fn.anchorNavigation = function(settings) {
		var hasAppliedCss;
		var config = {
			titleAttribute: "rel",
			yMaxOffset: settings.onNavHiddenByScroll.yMaxOffset || 0
		};
		var buffer = '';
		var yPosition;
		var container;
		var containerHeight;
		var wrapper;
		var wrapperHeight;
		var scrolledFromTop;
		var reachedMaxHeight;
		var maxHeightDiff;
		var isIE6;
		var doScroll = true;
		
		if( (jQuery.browser.version <= 7) && $.browser.msie ) {
			isIE6 = true;
		}
		
		
		if (settings) $.extend(config, settings);
		if($(config.outputTarget).length) {
			buffer += '<ul>';
		
			this.each(function() {
				var matchedAnchors = $(this).find("a").filter(function(){
					return $(this).attr("name").length;
				});
				if(matchedAnchors.length) {
					matchedAnchors.each(function(){
						var ttl = $(this).attr(config.titleAttribute);
						if(ttl.length) {
							//buffer += '<li><a href="javascript:void(0)" onclick="jQuery.fn.anchorNavigation.linkSelected(\''+ $(this).attr("name") + '\')">';
							buffer += '<li><a href="#' + $(this).attr("name") + '">';
							buffer += ttl;
							buffer += '</a></li>';
						}
					});
				} else {
					$(config.wrapper).hide();
				}
			});
			
			buffer += "</ul>";
			$(config.outputTarget).html(buffer);
			
			if(config.onNavHiddenByScroll && !isIE6) {
				
				if($(config.onNavHiddenByScroll.wrapper).length) {
					wrapper = $(config.onNavHiddenByScroll.wrapper);
					wrapperHeight = wrapper.outerHeight();
				}
				
				if($(config.onNavHiddenByScroll.container).length) {
					container = $(config.onNavHiddenByScroll.container);
					containerHeight = container.outerHeight();
										
					container.before('<div style="line-height: 0;" id="anchorNavYPositionMarker">&nbsp;</div>');	//set a div which indicates the div's original position.
					
					yPosition = $("#anchorNavYPositionMarker").offset().top;
					
					if((yPosition + containerHeight) > (wrapperHeight - containerHeight - config.yMaxOffset)) {
						//if the anchornav is the bottom-most element on the page then scrolling is not required.
						//without this, funky things happen... lots of blinking of the anchornav.
						doScroll = false;
					}
					
					if(doScroll) {
						$(window).scroll(function() {
							scrolledFromTop = $(window).scrollTop();
							yPosition = $("#anchorNavYPositionMarker").offset().top;
							if( (scrolledFromTop > yPosition) && !hasAppliedCss ) {
								//let's not do this recursively
								hasAppliedCss = true;
								container.addClass(config.onNavHiddenByScroll.cssClass);
							} else if( (scrolledFromTop < yPosition) && hasAppliedCss ) {
								hasAppliedCss = false;
								container.removeClass(config.onNavHiddenByScroll.cssClass);
							}
							//if wrapper exists, limit its y-travel
							if(wrapper) {
								var currYPos = container.offset().top;
								var navBottomYPos = currYPos + containerHeight;
								var wrapperBottomYpos = wrapper.offset().top + wrapperHeight;
								var diff = (wrapperBottomYpos - navBottomYPos) - config.yMaxOffset;
								var maxY = wrapperHeight - containerHeight - config.yMaxOffset;
																						
								if(diff <= 0) {
									if(!reachedMaxHeight) {
										reachedMaxHeight = true;
										container.css({"position": "absolute", "top": maxY + "px"});
										//maxHeightDiff is the difference when locked at the bottom
										maxHeightDiff = (wrapper.offset().top + wrapperHeight) - (container.offset().top + containerHeight) - config.yMaxOffset;
									}
								} else if (diff == maxHeightDiff) {
									//if you're locked to the bottom!
									if(scrolledFromTop < container.offset().top) {
										if( container.css("position") == "absolute" ) {
											container.removeAttr("style");
											reachedMaxHeight = false;
										}
									};
									
								}
							}
						});
					}
				}
			}
		}
		
		return this;
	};
	
})(jQuery);


