/**
 * HoverAccordion - jQuery plugin for intuitively opening accordions and menus
 * http://berndmatzner.de/jquery/hoveraccordion/
 * Copyright (c) 2007 Bernd Matzner
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Version: 0.4
 */
(function($){
    $.fn.hoverAccordion = function(options){
        options = jQuery.extend({
            speed: 'fast',
            activateitem: 'true',
            active: 'active',
            header: 'header',
            hover: 'hover',
            opened: 'opened',
            closed: 'closed',
            keepheight: 'true'
        }, options);
        thislist = $(this);
        thislist.attr('hovering', 'false');
        var thisurl = window.location.href;
        function doHover(obj){
            if ($(obj).html() == undefined) 
                obj = this;
            if (thislist.attr('hovering') == 'false') {
                var newelem = $(obj).parent().children('ul');
                var oldelem = $(obj).parent().parent().children('li').children('ul:visible');
                if (options.keepheight == 'true') {
                    thisheight = maxheight
                }
                else {
                    thisheight = newelem.height()
                }
                if (!newelem.is(':visible')) {
                    thislist.attr('hovering', 'true');
                    if (!oldelem.length > 0) {
                        newelem.animate({
                            height: thisheight
                        }, {
                            duration: options.speed,
                            easing: 'linear'
                        })
                    }
                    else {
                        newelem.show()
                    }
                    oldelem.animate({
                        height: 'hide'
                    }, {
                        step: function(n){
                            newelem.height(Math.ceil(thisheight - n))
                        },
                        duration: options.speed,
                        easing: 'linear'
                    });
                    oldelem.parent().children('a').addClass(options.closed).removeClass(options.opened);
                    newelem.parent().children('a').addClass(options.opened).removeClass(options.closed);
                    resetHover = window.setTimeout("thislist.attr('hovering','false');", 600)
                }
            }
        };
        function doNothing(){
        
        };
        var itemNo = 0;
        var maxheight = 0;
        $(this).children('li').each(function(){
            var thisitem = $(this);
            itemNo++;
            var thislink = thisitem.children('a');
            if (thislink.length > 0) {
                thislink.hover(function(){
                    $(this).addClass(options.hover)
                }, function(){
                    $(this).removeClass(options.hover)
                });
                var thishref = thislink.attr('href');
                if (thishref == '#') {
                    thislink.click(function(){
                        thislist.attr('hovering', 'false');
                        doHover(this);
                        this.blur();
                        return false
                    })
                }
                else 
                    if (options.activateitem == 'true' && thisurl.indexOf(thishref) > 0 && thisurl.length - thisurl.lastIndexOf(thishref) == thishref.length) {
                        thislink.parent().addClass(options.active)
                    }
            }
            var thischild = thisitem.children('ul');
            if (thischild.length > 0) {
                if (maxheight < thischild.height()) 
                    maxheight = thischild.height();
                thischild.children('li.' + options.active).parent().parent().children('a').addClass(options.header);
                try {
                    thislink.hoverIntent({
                        sensitivity: 1,
                        interval: 80,
                        over: doHover,
                        timeout: 600,
                        out: doNothing
                    })
                } 
                catch (err) {
                    thislink.hover(function(){
                        doHover(this)
                    }, function(){
                        doNothing()
                    })
                }
                if (options.activateitem == 'true') {
                    thischild.children('li').each(function(){
                        var m = $(this).children('a').attr('href');
                        if (m) {
                            if (thisurl.indexOf(m) > 0 && thisurl.length - thisurl.lastIndexOf(m) == m.length) {
                                $(this).addClass(options.active).parent().parent().children('a').addClass(options.opened)
                            }
                        }
                    })
                }
                else 
                    if (parseInt(options.activateitem) == itemNo) {
                        thisitem.addClass(options.active).children('a').addClass(options.opened)
                    }
            }
            thischild.not($(this).parent().children('li.' + options.active).children('ul')).not(thischild.children('li.' + options.active).parent()).hide().parent().children('a').addClass(options.closed)
        })
    }
})(jQuery);
