
/**
 * cookieMenu plugin 0.9
 *
 * Copyright (c) 2008 Karl Swedberg (learningjquery.com) and
 *                    Charles Clarkson (cclarkson@htcomp.net)
 *
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * This plugin came about during a conversation on the jQuery mailing list.
 * Karl Swedberg of www.learningjquery.com coded much of the plugin.
 * His demo page is http://test.learningjquery.com/cookie-menu.html.
 * My demo page is http://www.clarksonenergyhomes.com/demos/jq/cookie-menu.html.
 *
 * Currently, this plugin assumes the menus are unordered lists of unordered lists.
 *
 *      <ul id="menu-1">
 *          <li><a href="#">one</a>
 *              <ul>
 *                  <li><a href="#">menu item 1a</a></li>
 *                  <li><a href="#">menu item 1b</a></li>
 *                  <li><a href="#">menu item 1c</a></li>
 *                  <li><a href="#">menu item 1d</a></li>
 *              </ul>
 *          </li>
 *          <li><a href="#">two</a>
 *              <ul>
 *                  <li><a href="#">menu item 2a</a></li>
 *                  <li><a href="#">menu item 2b</a></li>
 *                  <li><a href="#">menu item 2c</a></li>
 *                  <li><a href="#">menu item 2d</a></li>
 *              </ul>
 *          </li>
 *
 *			.
 *			.
 *			.
 *
 *      </ul>
 *
 *      $(document).ready(function(){
 *          $('#menu-1').cookieMenu('cookie-1');
 *      });
 *
 *
 *
 */

    (function($) {

            $.cookie.menu = function(cookieName){

            // Private
            var cookieValue = $.cookie(cookieName) || '';

            var bigIndex = function (inival) {
                return inival < 10 ? '0' + inival + '-' : '' + inival + '-';
            };

            // For the delete/show Cookie demo. We don't need this in production.
            // Add cookie info

            if ( ! $('#jar ul').size() ) {
                $('<ul></ul>').prependTo( $('#jar') );
            }

            if ( !$('#jar #' + cookieName).size() ) {
                $('<li id="' + cookieName + '"><button>Delete Cookie</button> <span></span></li>').appendTo($('#jar ul'));
            }

            // For the delete/show Cookie demo. We don't need this in production.
            this.showCookie = function(){
                $('#' + cookieName + ' span').html( cookieName + ' = ' + cookieValue );
            };

            // Public
            this.isOpen = function(index){
                return cookieValue.indexOf(bigIndex(index)) > -1;
            };

            this.open = function(index){
                cookieValue = cookieValue + bigIndex(index);
                $.cookie(cookieName, cookieValue);
            };

            this.close = function(index){
                cookieValue = cookieValue.replace(bigIndex(index), '');
                $.cookie(cookieName, cookieValue);
            };

            this.deleteCookie = function(){
                cookieValue = '';
                $.cookie(cookieName, cookieValue, { expires: -1 });
            };

        };

        $.fn.cookieMenu = function(cookieName){

            var $topLevel = this.find('li ul');
            $topLevel.hide();

            var menuCookie = new $.cookie.menu(cookieName);
            menuCookie.showCookie();


            // For the delete/show Cookie demo. We don't need this in production.
            $('#' + cookieName + ' button').click(function() {
                menuCookie.deleteCookie();
                $topLevel.hide();
                menuCookie.showCookie();
            });

            this.find('> li > a').each(function(index) {

                var $this = $(this), $checkElement = $this.next('ul');

                if ( menuCookie.isOpen(index) ) {
                    $checkElement.show();
                }
                $this.click(function() {
                    if ($checkElement.is(':hidden')) {
                        $checkElement.slideDown();
                        menuCookie.open(index);

                    } else {
                        $checkElement.slideUp();
                        menuCookie.close(index);
                    }
                    menuCookie.showCookie();
                    return false;
                });
            });
        };

    })(jQuery);