﻿// Plugin created by Brock Nusser - http://mlamenu.blogspot.com/

$(function () {
    //setup - all values in pixels
    var menuWidth = 200;
    var ulIndent = 10;
    //end setup

    var pathname = window.location.pathname;

    $('#menu').css('width', menuWidth + 'px');
    $('#menu ul').css('width', menuWidth + 'px');
    $('#menu ul').css('margin-left', ulIndent + 'px');

    $('#menu a').each(function () {
        var level = $(this).parents('ul').length;
        var liWidth = menuWidth - (ulIndent * level) + 30;
        $(this).parent('li').css('width', liWidth + 'px');
    });

    //prepend expand/collapse icons
    $('#menu li').each(function () {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                $(this).prepend('<img src="images/arrowdown.png" />');
            }
            else {
                $(this).prepend('<img src="images/arrow.png" />');
            }
        }
    });

    //Required Section
    var slideSpeed = 'slow'; // 'slow', 'normal', 'fast', or miliseconds 
    /* $('#menu 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();*/
   /* $('#menu a').each(function () {

        function getQuerystring(key, default_) {
            if (default_ == null) default_ = "";
            key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
            var qs = regex.exec(window.location.href);
            if (qs == null)
                return default_;
            else
                return qs[1];
        }
        var catID = getQuerystring('pid');

        var currentHref = window.location.pathname;
        currentHref = currentHref.replace("/topflyte.co.za/", "");
        var fullHref = (currentHref + "?pid=" + catID);
        var thisHref = $(this).attr('href');
        var thisCatID = $(this).attr('id');
        if (fullHref == thisHref || thisCatID == catID) {
            $(this).addClass('Current');
        }
    });*/

    /*$('.Current').parent('li').children('ul').show();
    $('.Current').parents('ul').show();*/

    //prepend expand/collapse icons
    $('#menu li').each(function () {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                $(this).children('img').attr('src', 'images/arrowdown.png');
            }
        }
    });

    $('#menu 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/arrow.png');
            $(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/arrow.png');
            } else {
                $(this).parent('li').children('ul').show(slideSpeed);
                $(this).attr('src', 'images/arrowdown.png');
            }
            return false;
        }

    });

    $('#menu li').click(function () {
        if ($(this).children('a').length == 0) {
            if ($(this).children('ul').html() != null) {
                $(this).parent('ul').children('li').children('ul').hide(slideSpeed);
                if ($(this).children('ul').css('display') == "block") {
                    $(this).children('ul').hide(slideSpeed);
                } else {
                    $(this).children('ul').show(slideSpeed);
                }
            }
        }
    });
    //End Required Section

    //Optional Section - Show Carrots
    var imgOffClosed = "url(images/arrow.png) no-repeat 5px ";
    var imgOnClosed = "url(images/imgOnClosed.gif) no-repeat 5px ";
    var imgOnOpen = "url(images/arrowdown.png) 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

    $('#menu > li').each(function () {

        var childText = $(this).children('a').text();
        var topPadding = paddingBig;
        if (childText.length < charBeforeLB) { topPadding = paddingSmall; }
        if ($(this).height() < maxLIHeight) {//list is closed

            if ($(this).children('a').attr('class') == "Current") {
                $(this).parent('li').children('img').attr('src', 'images/arrowdown.png');
            } else {
                $(this).parent('li').children('img').attr('src', 'images/arrow.png');
            }


        }
        else {// list is open
            $(this).children('img').attr('src', 'images/arrowdown.png');
        }




    });

    //End Optional Section
});
