$(document).ready(function () {
    /** Accordian **/
    $('.accordian').find('h4:first').addClass('first');
    $('div.accordian> div').hide(); //hide all accordians

    /* main functionality */
    $('div.accordian> h4 a').click(function (e) {
        e.preventDefault();
    });
    $('div.accordian> h4').click(function () {
        var item = $(this);
        if (item.hasClass('on')) { item.next('div').slideToggle('slow', function () { item.removeClass('on'); }); }
        else {
            $('div.accordian> h4.on').removeClass('on');
            $('div.accordian> div').slideUp('slow');
            item.next('div').slideToggle('slow');
            item.toggleClass("on");
        }
    });
});

(function ($) {
    $.fn.dropDownNav = function (options) {
        var defaults = {
            speed: 'slow'
        };
        var options = $.extend(defaults, options);

        $(this).children('ul').children('li:last-child').addClass('last');

        $('#nav > ul li:first-child').addClass('firstItem');

        $(this).children('ul').each(function(){
            var hasChildren = false;
            $(this).find('li').each(function(){
                if ($(this).children('ul').length != 0) {
                    hasChildren = true;
                }
            });
            
            if(!$(this).parent('li').hasClass('firstItem')){
                $(this).addClass('noThirdLevel');
            }
            if(!hasChildren){
                $(this).addClass('noThirdLevel');
            }

            if ($(this).children('li').length == 0) {
                $(this).remove();
            }
        });

        $(this).hover(function () {
            var $this = $(this);
            $this.addClass('hovering');
            $this.children('ul').fadeIn(0);
        }, function () {
            var $this = $(this);
            if ($this.children('ul').length == 0) { //if there isn't anything to slide up, we still want to remove the hovering class
                $this.removeClass('hovering');
            }
            else {
                $this.children('ul').fadeOut(0, function () {
                    $this.removeClass('hovering');
                });
            }
        });
    };
})(jQuery);
