//*
(function($)
{
    var findChildren = function(ul){
        $(ul).find('li').each(function(i, li){
            if($(li).children('ul').length>0)
            {
                $(li).addClass('hasChildren');
            }
        });
    }
    
    var findSelected = function(ul)
    {
        var a = ul.find('a').filter(function(){return this.href.toLowerCase() == location.href.toLowerCase();});
        
        if(a.length > 0)
            a.addClass('selected');
    }
    
    var openBranch = function(jQueryElement, noAnimation)
    {
        jQueryElement.addClass('OPEN').removeClass('CLOSE');
        if(noAnimation)
            jQueryElement.parent().find('ul:first').show();
        else
            jQueryElement.parent().find('ul:first').slideDown();
    }
    
    var closeBranch = function(jQueryElement, noAnimation)
    {
        jQueryElement.addClass('CLOSE').removeClass('OPEN');
        if(noAnimation)
            jQueryElement.parent().find('ul:first').hide();
        else
            jQueryElement.parent().find('ul:first').slideUp();
    }
    
    var toggleBranch = function(jQueryElement, noAnimation)
    {
        if(jQueryElement.hasClass('OPEN'))
            closeBranch(jQueryElement, noAnimation);
        else
            openBranch(jQueryElement, noAnimation);
    }
    
    var openSelected = function(ul)
    {
        ul.find('.selected').parents().each( function(i, el) {
            if ($(el).is('ul'))
            {
                openBranch($(el), true);
                return;
            }
        });
        
        if(ul.find('.selected').next('ul').length)
        {
            openBranch(ul.find('.selected').next('ul'), true);
        }
    }
    
    var functionEvent = function(current, ul)
    {
        var href = current.attr('href')
                
        ul.find('li.hasChildren > a').each(function(i, a){
            if($(a).attr('href') != href)
                closeBranch($(a));
        });
        toggleBranch(current);
        
    }
    
    $.fn.treeview = function(options){
        
        var defaultOptions = {
            eventListener : 'click'
        };
        
        var ul = $(this);
        
        if(!ul.hasClass('dynamized'))
	{
            //dynamically add the '.last' class on each last item of a branch
            ul.find('li:last-child').addClass('last');
            
            //collapse every expanded branch
            ul.find('li').addClass('CLOSE').find('ul:first').hide();
            ul.show();
            
            findChildren(ul);
            findSelected(ul);
            openSelected(ul);
            
            
            switch(options.eventListener.toLowerCase())
            {
                case 'click':
                    ul.find('li.hasChildren > a').click(function(){
                        functionEvent($(this), ul);
                        
                        //return false;
                    });
                    break;
                
                default:
                case 'mouseover':
                    ul.find('li.hasChildren > a').mouseover(function(){
                        functionEvent($(this), ul);
                    });
                    break;
            }
            
            //mark this 'ul.tree' elements as already 'dynamized'
            ul.addClass('dynamized');
        }
    }
    
})(jQuery);
//*/
