﻿$j(function() {
    var navWidth = 205;
    var ulIndent = 10;

    var pathname = window.location.pathname;
	var imgOpen = 'view/images/minus.png';
	var imgClosed = 'view/images/plus.png';

	$j('#nav ul').css('display','none');
	
    $j('#nav').css('width', navWidth + 'px');
    $j('#nav ul').css('width', navWidth + 'px');
    $j('#nav ul').css('margin-left', ulIndent + 'px');

    $j('#nav a').each(function() {
        var level = $j(this).parents('ul').length;
        var liWidth = navWidth - (ulIndent * level) + 30;
        $j(this).parent('li').css('width', liWidth + 'px');
    });

    $j('#nav li').each(function() {
        if ($j(this).children('ul').length > 0) {
            if ($j(this).children('ul').is(":visible")) {
                $j(this).prepend('<img src="'+imgOpen+'" />'); //<div class="visible_img"></div>
            }
            else {
                $j(this).prepend('<img src="'+imgClosed+'" />'); //<div class="visible_img"></div>
            }
        } else {
				$j(this).prepend('<div class="none_img"></div>');
		}
    });


    var slideSpeed = 'slow'; // 'slow', 'normal', 'fast', or miliseconds 
   // var slideSpeed = ''; // 'slow', 'normal', 'fast', or miliseconds 
    $j('#nav a').each(function() {
        var thisHref = $j(this).attr('href')
        if ((window.location.pathname.indexOf(thisHref) == 0) || (window.location.pathname.indexOf('/' + thisHref) == 0)) {
            $j(this).addClass('Current');
        }
    });
    
    $j('.Current').parent('li').children('ul').show();
    $j('.Current').parents('ul').show();

    $j('#nav li').each(function() {
        if ($j(this).children('ul').length > 0) {
            if ($j(this).children('ul').is(":visible")) {
                $j(this).children('img').attr('src', imgOpen);
            } else {
				$j(this).children('img').attr('src', imgClosed);
			}
        }
    });
    
    $j('#nav img').click(function() {
        if ($j(this).parent('li').children('ul').html() != null) {
      //      $j(this).parent('li').parent('ul').children('li').children('ul').hide();
      //      $j(this).parent('li').parent('ul').children('li').children('img').attr('src', imgClosed);
     //       $j(this).delay(100).is(':hidden');
		//	alert($j(this).parent('li').children('ul').css('display'));
            if ($j(this).parent('li').children('ul').css('display') == "table") {
                $j(this).parent('li').children('ul').hide(slideSpeed);
                $j(this).attr('src', imgClosed);
		//		alert('table');
            } else {
			$j(this).parent('li').parent('ul').children('li').children('ul').hide(slideSpeed);
		//	$j(this).delay(100).is(':hidden');
           // $j(this).parent('li').parent('ul').children('li').children('img').attr('src', imgClosed);
                $j(this).parent('li').children('ul').show(slideSpeed);
                $j(this).attr('src', imgOpen);
			//	alert('notable');
            }
            return false;
        }

    });

    $j('#nav li').click(function() {
        if ($j(this).children('a').length == 0) {
            if ($j(this).children('ul').html() != null) {
                $j(this).parent('ul').children('li').children('ul').hide(slideSpeed);
                if ($j(this).children('ul').css('display') == "table") {
           //         $j(this).children('ul').hide(slideSpeed);
			//		alert('table');
                } else {
             //       $j(this).children('ul').show(slideSpeed);
			//		alert('notable');
                }
            }
        }
    });

    var imgOffClosed = "url(view/images/imgOffClosed.gif) no-repeat 5px ";
    var imgOnClosed = "url(view/images/imgOnClosed.gif) no-repeat 5px ";
    var imgOnOpen = "url(view/images/imgOnOpen.gif) 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

    $j('#nav > li').each(function() {

        var childText = $j(this).children('a').text();
        var topPadding = paddingBig;
        if (childText.length < charBeforeLB) { topPadding = paddingSmall; }
        if ($j(this).height() < maxLIHeight) {//list is closed
            if (($j(this).children('a').attr('class') == "Current") || ($j(this).attr('class') == "Current")) {
                $j(this).parent('li').children('img').attr('src', imgOpen);
            } else {
                $j(this).parent('li').children('img').attr('src', imgClosed);
            }

            
        }
        else {// list is open
            $j(this).children('img').attr('src', imgOpen);
        }
    });
});
