/*
 *  jquery-frba-navigation
 *  Howard Fore, f1thf01
 *  Created : May 18, 2010
 *  
 *  Creates a vertical navigation menu from an existing HTML structure of DIV and A tags. Use leftNavigation.cfm pod from FRBA to create structure.
 *  
 */
//
// create closure
//
(function ($) {
    //
    // plugin definition
    //
    $.fn.createNavigation = function (options) {
        // build main options before element iteration
        var main_opts = $.extend({}, $.fn.createNavigation.defaults, options);
		if (this.attr('id')) {
			var parentDivId = this.attr('id').toString();
		} else {
			var parentDivId = opts.parentDivId;
		}
		
        this.each(function () {
            $t = $(this);
            // build element specific options
            var opts = $.meta ? $.extend({}, main_opts, $t.data()) : main_opts; // update element styles

			// hide all divs from 2nd level down
			$("#" + parentDivId + " > div div").hide();

			// set class on stand-alone items in nav
			$("#" + parentDivId).find("div:not(.singleline)").each(function(){
				$(this).children("a").addClass(opts.menuCollapsedClass);
			});
			$("#" + parentDivId).find("div.singleline").each(function(){
				$(this).children("a").addClass(opts.configmenuItemClass);
			});
			
			var navOpts = $.extend({},opts);
			navOpts.pageHref = opts.pageHref;
			navOpts.parentItemDivId = opts.parentItemDivId
			$.fn.createNavigation.setCurrentItem(opts);

			
        });
		
		return this;
    };

	
	$.fn.createNavigation.setCurrentItem = function(options) {
		var foundDirectMatch = false;
		var foundPathMatch = false;
		var directMatchItem = null;
		var pathMatchItem = null;
		var matchedItem = null;
		
		if (leftNavParentItemHref !== null) {
			options.pageHref = leftNavParentItemHref;
			if (options.navDebug) {
				console.log("pageHref override");
				console.log("pageHref = " + options.pageHref);
			}
		}
				
		var pageHref = options.pageHref;
		

		// Now go through the parent div, looking at each anchor tag to see if it matches the direct URL or the level path
		$("#" + options.parentDivId).find("a").each(function(){
			if ( foundDirectMatch ) {
				// previous loop found match, exiting
				return true;
			}

			var navItemHref = $(this).attr("href").toString();
if (options.navDebug) { console.log("navItemHref = " + navItemHref)};
			
			if (navItemHref.charAt(navItemHref.length - 1) == "/") {
				// this is an index page
				navItemHref = $(this).attr("href") + "index.cfm";
			}
if (options.navDebug) { console.log("navItemHref = " + navItemHref)};
			var navItemPath = navItemHref.slice(0,navItemHref.lastIndexOf('/')+1);
if (options.navDebug) { console.log("navItemPath = " + navItemPath)};
			
			// look for direct match
			if ($.trim(pageHref) == $.trim(navItemHref)) {
				// found direct match
				var oThis = $(this);
				oThis.toggleClass(options.menuItemCurrentClass);
				oThis.toggleClass(options.menuItemClass);
				oThis.siblings().show();
				oThis.parentsUntil("#" + options.parentDivId).each(function(){
					$(this).show();								
					$(this).siblings().show();
				});

				foundDirectMatch = true;
if (options.navDebug) { console.log("Direct Match!")};
			} 
			
			// look for URL path matches
			if (foundDirectMatch == false) {
				if (pageLevelPath == navItemPath) {
					if(navItemHref.indexOf('index.cfm') > 0){
						// found match for level
						var oThis = $(this);
						oThis.siblings().show();
						oThis.parentsUntil("#" + options.parentDivId).each(function(){
							$(this).show();								
							$(this).siblings().show();
						});
						
						foundPathMatch = true;
						if (options.navDebug) { console.log("Path Match!")};
					}
				}
			}
			
			
		});		
		
		return;
		
	}
    //
    // plugin defaults
    //
    $.fn.createNavigation.defaults = {
		pageHref : '',
		pageLevelPath : '',
		menuCollapsedClass : 'p7plusmark',
		menuExpandedClass : 'p7minusmark',
		menuItemClass : 'p7defmark',
		menuItemCurrentClass : 'p7currentmark',
		parentDivId : 'p7TMnav',
		parentItemHref : ''
    };
    //
    // end of closure
    //
})(jQuery);
