﻿// Plugin created by Brock Nusser - http://mlamenu.blogspot.com/

jq132(function() {
    //setup - all values in pixels
    var navWidth = 160;
    var ulIndent = 10;
    //end setup

    var pathname = window.location.pathname;

    jq132('#nav').css('width', navWidth + 'px');
    jq132('#nav ul').css('width', navWidth + 'px');
    jq132('#nav ul').css('margin-left', ulIndent + 'px');

    jq132('#nav a').each(function() {
        var level = jq132(this).parents('ul').length;
        var liWidth = navWidth - (ulIndent * level) + 30;
        jq132(this).parent('li').css('width', liWidth + 'px');
    });

    //prepend expand/collapse icons
    jq132('#nav li').each(function() {
        if (jq132(this).children('ul').length > 0) {
            if (jq132(this).children('ul').is(":visible")) {
//                jq132(this).prepend('<img src="images/imgOnOpen.gif" />');
              //  jq132(this).prepend('<img src="./images/1rightarrow.png" />');
            }
            else {
//                jq132(this).prepend('<img src="images/imgOffClosed.gif" />');
                //jq132(this).prepend('<img src="./images/1rightarrow.png" />');
            }
        }
    });

    //Required Section
    var slideSpeed = 'slow'; // 'slow', 'normal', 'fast', or miliseconds 
    jq132('#nav a').each(function() {
        var thisHref = jq132(this).attr('href')
        if ((window.location.pathname.indexOf(thisHref) == 0) || (window.location.pathname.indexOf('/' + thisHref) == 0)) {
            jq132(this).addClass('Current');
        }
    });
    
    jq132('.Current').parent('li').children('ul').show();
    jq132('.Current').parents('ul').show();

    //prepend expand/collapse icons
    jq132('#nav li').each(function() {
        if (jq132(this).children('ul').length > 0) {
            if (jq132(this).children('ul').is(":visible")) {
             //   jq132(this).children('img').attr('src', 'images/imgOnOpen.gif');
			  //  jq132(this).children('img').attr('src', './images/1rightarrow.png');
            }
        }
    });
    
    jq132('#nav a').click(function() {
        if (jq132(this).parent('li').children('ul').html() != null) {
            jq132(this).parent('li').parent('ul').children('li').children('ul').hide(slideSpeed);
        //    jq132(this).parent('li').parent('ul').children('li').children('img').attr('src', 'images/imgOffClosed.gif');
            jq132(this).delay(100).is(':hidden');
            if (jq132(this).parent('li').children('ul').css('display') == "block") {
                jq132(this).parent('li').children('ul').hide(slideSpeed);
              //  jq132(this).attr('src', 'images/imgOffClosed.gif');
            } else {
                jq132(this).parent('li').children('ul').show(slideSpeed);
               // jq132(this).attr('src', 'images/imgOnOpen.gif');
			    jq132(this).attr('src', './images/1rightarrow.png');
            }
            return false;
        }

    });

    jq132('#nav li').click(function() {
        if (jq132(this).children('a').length == 0) {
            if (jq132(this).children('ul').html() != null) {
                jq132(this).parent('ul').children('li').children('ul').hide(slideSpeed);
                if (jq132(this).children('ul').css('display') == "block") {
                    jq132(this).children('ul').hide(slideSpeed);
                } else {
                    jq132(this).children('ul').show(slideSpeed);
                }
            }
        }
    });
    //End Required Section
    
    //Optional Section - Show Carrots
    var imgOffClosed = "url(images/imgOffClosed.gif) no-repeat 5px ";
    var imgOnClosed = "url(images/imgOnClosed.gif) no-repeat 5px ";
//    var imgOnOpen = "url(images/imgOnOpen.gif) no-repeat 5px ";
    var imgOnOpen = "url(images/1rightarrow.png) no-repeat 5px ";

    var imgOnOpen = "url() no-repeat 5px ";
    var charBeforeLB = 23; //characters before line break - you must calculate this - based on font-size and LI width;
    var paddingBig = 12; //push carrot arrow down (in pixels) when no there is a line break in the LI
    var paddingSmall = 8;  //push carrot arrow down (in pixels) when no there is no line break in the LI
    var maxLIHeight = 50; // max height of LI when list is closed
	
	

    jq132('#nav > li').each(function() {

        var childText = jq132(this).children('a').text();
        var topPadding = paddingBig;
        if (childText.length < charBeforeLB) { topPadding = paddingSmall; }
        if (jq132(this).height() < maxLIHeight) {//list is closed

            if (jq132(this).children('a').attr('class') == "Current") {
              //  jq132(this).parent('li').children('img').attr('src', 'images/imgOnOpen.gif');
			   jq132(this).parent('li').children('img').attr('src', 'images/1rightarrow.png');
			  
            } else {
                jq132(this).parent('li').children('img').attr('src', 'images/1rightarrow.png');
            }

            
        }
        else {// list is open
            //jq132(this).children('img').attr('src', 'images/imgOnOpen.gif');
			jq132(this).children('img').attr('src', 'images/1rightarrow.png');
        }




    });
    
    //End Optional Section
});
