/** 
 *  jquery.columnview-1.1.1.js
 *  
 *  Created by Chris Yates on 2009-02-26.
 *  http://christianyates.com
 *  Copyright 2009 Christian Yates and ASU Mars Space Flight Facility. All rights reserved.
 *  
 *  Requires jQuery 1.3.x
 *  Also available with jQuery 1.2.6 support (with Live Query plugin) - see
 *  http://christianyates.com/blog/jquery/finder-column-view-hierarchical-lists-jquery
 *  
 *  Tested with Firefox 3.x, Safari 3.x,4.x, Internet Explorer 6.x,7.x
 *  Dual licensed under MIT and GPL.
 *
 */
(function($) {
    $.fn.columnview = function() {
        // Add stylesheet, but only once
        if (!$('.containerobj').get(0)) {
            //$('head').prepend('');
        }
        // Hide original list
        $(this).hide();
        // Create new top container from top-level LI tags
        var top = $(this).children('li');
        var container = $('<div/>').addClass('containerobj').attr('id', 'cv' + Math.floor(Math.random() * 10e10)).insertAfter(this);
        var topdiv = $('<div class="top scroll-pane"></div>').appendTo(container);
        $.each(top, function(i, item) {
            var topitem = $(':eq(0)', item).clone().data('sub', $(item).children('ul')).appendTo(topdiv);
            if ($(topitem).data('sub').length) {
                $(topitem).addClass('hasChildMenu');
                if ($.browser.safari) {
                    $(topitem).css({ 'margin-right': '15px' });
                }
            }
        });
        // Event handling functions
        $('.containerobj a', container).live("mouseover", function() {
            var container = $(this).parents('.containerobj');
            // Handle clicks
            var level = $('div', container).index($(this).parents('div'));
            // Remove blocks to the right in the tree, and 'deactivate' other links within the same level
            $('div:gt(' + level + ')', container).filter(".jScrollPaneContainer").remove();
            $('div:eq(' + level + ') a', container).removeClass('active').removeClass('inpath');
            $('.active', container).addClass('inpath');
            $(this).addClass('active');
            if ($(this).hasClass('hasChildMenu')) {
                submenu(container, this);
            }
            return false;
        });
        // Keyboard navigation
        $('a', container).live('keydown', function(key) {
            switch (key.which) {
                case (37): //left
                    $(this).parent().prev().children('.active').focus().click();
                    break;
                case (38): //up
                    $(this).prev().focus().click();
                    break;
                case (39): //right
                    if ($(this).hasClass('hasChildMenu')) {
                        $(this).parent().next().children('a:first').focus().click();
                    }
                    break;
                case (40): //down        
                    $(this).next().focus().click();
                    break;
                case (13): //enter
                    $(this).dblclick();
                    break;
            }
        });
    };
    // Generate deeper level menus
    function submenu(container, item) {
        var leftPos = 0;
        $.each($(container).children('div'), function(i, mydiv) {
            leftPos += $(mydiv).width();
        });
        var submenu = $('<div/>').css({ 'top': 0, 'left': leftPos }).appendTo(container);
        $(submenu).addClass('scroll-pane');
        $(submenu).addClass('childcol');
        $(submenu).hide();
        if ($(item).data('sub').children('li')) {
            var subitems = $(item).data('sub').children('li');
            $.each(subitems, function(i, subitem) {
                var subsubitem = $(':eq(0)', subitem).clone().data('sub', $(subitem).children('ul')).appendTo(submenu);

                if ($(subsubitem).data('sub').length) {
                    $(subsubitem).addClass('hasChildMenu');
                    if ($.browser.safari) {
                        $(subsubitem).css({ 'margin-right': '15px' });
                    }
                }
            });
        }
        $(submenu).fadeIn(100);
        $(container).children('.scroll-pane').jScrollPane({
            scrollbarWidth: 8,
            showArrows: true,
            arrowSize: 8
        });
    }
})(jQuery);
