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

$(function() {
    //setup - all values in pixels
    var navWidth = 170;
    var ulIndent = 0;
    //end setup

    var pathname = window.location.pathname;

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

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

    //prepend expand/collapse icons
    $('#nav li').each(function() {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                //$(this).append('<img src="./images/common/expanded.gif" />');
            } else {
                $(this).children('a').prepend('<img src="http://bannershop.com.hk/images/common/collapsed.gif"/>&nbsp;&nbsp;&nbsp;&nbsp;');
            }
        }
    });

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

    //prepend expand/collapse icons
    $('#nav li').each(function() {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                $(this).children('img').attr('src', 'http://bannershop.com.hk/images/common/collapsed.gif');
            }
        }
    });
    
    $('#nav a.sub_menu_head').click(function() {
        if ($(this).parent('li').children('ul').html() != null) {
            $(this).parent('li').parent('ul').children('li').children('ul').hide(slideSpeed);
            //$(this).parent('li').parent('ul').children('li').children('img').attr('src', './images/common/expanded.gif');
            $(this).delay(100).is(':hidden');
            if ($(this).parent('li').children('ul').css('display') == "block") {
                $(this).parent('li').children('ul').hide(slideSpeed);
                //$(this).attr('src', './images/common/collapsed.gif');
            } else {
                $(this).parent('li').children('ul').show(slideSpeed);
                //$(this).attr('src', './images/common/expanded.gif');
            }
            return false;
        }
    });
    
    $('#nav img').click(function() {
        if ($(this).parent('li').children('ul').html() != null) {
            $(this).parent('li').parent('ul').children('li').children('ul').hide(slideSpeed);
            //$(this).parent('li').parent('ul').children('li').children('img').attr('src', './images/common/expanded.gif');
            $(this).delay(100).is(':hidden');
            if ($(this).parent('li').children('ul').css('display') == "block") {
                $(this).parent('li').children('ul').hide(slideSpeed);
                //$(this).attr('src', './images/common/collapsed.gif');
            } else {
                $(this).parent('li').children('ul').show(slideSpeed);
                //$(this).attr('src', './images/common/expanded.gif');
            }
            return false;
        }
    });
    //End Required Section
    
    //Optional Section - Show Carrots
    var imgOffClosed = "url(http://bannershop.com.hk/images/common/collapsed.gif) no-repeat 5px ";
    var imgOnClosed = "url(http://bannershop.com.hk/images/common/expanded.gif) no-repeat 5px ";
    var imgOnOpen = "url(http://bannershop.com.hk/images/common/expanded.gif) no-repeat 5px ";
    var charBeforeLB = 120; //characters before line break - you must calculate this - based on font-size and LI width;
    var paddingBig = 25; //push carrot arrow down (in pixels) when no there is a line break in the LI
    var paddingSmall = 20;  //push carrot arrow down (in pixels) when no there is no line break in the LI
    var maxLIHeight = 70; // max height of LI when list is closed

    $('#nav > li').each(function() {
        var childText = $(this).children('span').text();
        var topPadding = paddingBig;
        if (childText.length < charBeforeLB) { topPadding = paddingSmall; }
        if ($(this).height() < maxLIHeight) {//list is closed
            if ($(this).children('span').attr('class') == "Current") {
               //$(this).parent('li').children('img').attr('src', './images/common/collapsed.gif');
            } else {
               //$(this).parent('li').children('img').attr('src', './images/common/expanded.gif');
            }
        }
        else {// list is open
            //$(this).children('img').attr('src', './images/common/collapsed.gif');
        }
    });
    //End Optional Section
});
