﻿(function ($) {
    $.fn.dropDownNav = function (options) {
        var defaults = {
            speed: 'slow'
        };
        var options = $.extend(defaults, options);

        $(this).find('ul li:last').addClass("last");
        $(this).find('ul li:first').addClass("first");

        $(this).hoverIntent(function () {
            var $this = $(this);
            $this.addClass('hovering');
            var liWidth = $this.outerWidth();
            var ulWidth = $this.find('ul').outerWidth();
            $this.find('ul li').css('width', (ulWidth - 30));//this # subtracted is right+left padding on the ul
            var leftOffset = -((ulWidth - liWidth) / 2);
            if ($this.hasClass('last')) {
                $this.find('ul').css('right', 0).slideDown(300); //prevent dropdown from extending beyond main bar
            }
            else {
                $this.find('ul').css('left', leftOffset).slideDown(300);
            }
        }, function () {
            var $this = $(this);
            $this.find('ul').slideUp(300, function () {
                $this.removeClass('hovering');
            });
            if ($this.find('ul').length == 0) { //if there isn't anything to slide up, we still want to remove the hovering class
                $this.removeClass('hovering');
            }
        });
    };
})(jQuery);

$(document).ready(function() {
    /** Accordian **/    
    $('div.accordian> div').hide(); //hide all accordians

//    /* open 1st accordian upon page load */
//    $('div.accordian').find('div:first').delay(300).slideToggle('slow');
//    $('div.accordian').find('h3:first').delay(300).toggleClass("active");
    
    /* main functionality */
    $('div.accordian> h3').click(function() {
        var item = $(this);
        if (item.hasClass('active')) { item.next('div').slideToggle('slow', function() { item.removeClass('active'); }); }
        else {
            $('div.accordian> h3').removeClass('active');
            $('div.accordian> div').slideUp('slow');
            item.next('div').slideToggle('slow').siblings('div:visible');
            item.toggleClass("active");
        }
    });
});
