/*
 *  jQuery HelyeNav plugin
 *  ======================
 *
 *  @desc HelyeNav is a simple on level Accordion vertical menu. 
 *        It's fully accessible, non obstrusive, and uses a cookie to 
 *        remember the current opened menu through pages.
 *
 *
 *  @require jQuery 1.3.x
 *  @require [jQuery Cookie Plugin](http://dev.jquery.com/browser/trunk/plugins/cookie/)
 *
 *  -- License --
 *  Copyright (c) 2009 Fabrice Luraine  
 *  Published under [MIT license](http://www.opensource.org/licenses/mit-license.php)
 *  --
 *
 */

(function($) {
    // PLUGIN DEFINITION
    $.fn.helyenav = function(options) {
        // init plugin and build options before elemnts iteration
        var opts = $.extend({}, $.fn.helyenav.defaults, options);
		$.helyenav.opts = opts;
        $.helyenav.current = opts.current;      
        
        return this.each(function() {
            // execute plugin on each elt
            $.helyenav.init(this, opts);
            $(this).click(onClick);
        });
        
        // private
        function onClick() {
            return $.cookie(opts.cookieName, $.helyenav.current(), { path: opts.cookiePath});
        }
    };

    $.helyenav = {
        init: function(container, opts) {
            var current = $.helyenav.current();
            var subnavs = $(container).find('> li > ul');
            var subnavsNames = $.map(subnavs, function(el) {
                return $(el).parent().attr('class').split(' ')[1];
            });
            $(subnavs).hide();
            if($.cookie(opts.cookieName) == current){
                subnav(current).show();
            } else {
              $.each(subnavsNames, function(i, name){
                 if($.cookie(opts.cookieName)==name){
                       subnav(name).show();
                       subnav(name).css('display', 'block'); /* for FF2, in case it's inline */
                       subnav(name).slideUp(opts.speed);
                    }
                    if(current==name){
                       subnav(name).slideDown(opts.speed);
                       $.cookie(opts.cookieName, current, { path: opts.cookiePath});
                    }
              });
           }
       
           // private
           function subnav(name){
               return $(container).find('.' + name + ' > ul');
           }
        }
    };
    
    // PLUGIN DEFAULTS
    $.fn.helyenav.defaults = {
        current: function(){
        	var cls = $('body').attr('class');
        	var tmp = cls.split('root-pageid-');
        	var id = (tmp[1]) ? tmp[1].split(' ')[0] : null;
        	if (!id) {
        		tmp = cls.split('pageid-');
        		id = (tmp[1]) ? tmp[1].split(' ')[0] : null;
        	}
        	return (id) ? 'page-item-'+id : false;
        },
        speed: 'slow',
        cookieName: 'last',
        cookiePath: '/'
    };
    
})(jQuery);
