/** 
 *  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.
 *
 *  Anpassungen durch Felipe Wieman
 *
 *  ToDo: 
 *  Bug 1: 
 *  -> Meldung: $(this).data("sub") is undefined
 *  -> Fehlerzeile: 60 
 */
(function($){
    $.fn.columnview = function(){
             
        // Hide original list
        $(this).hide();
        
        // Create new top container from top-level LI tags
        var top = $(this).children('li');
        var container = $('<div/>').addClass('containerobj').addClass('navColumn').attr('id', 'nav-container').insertAfter(this);
        var topdiv = $('<div class="nav1"></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'
                    });
                }*/
            }
            if ($(item).hasClass('active')) {
                $(topitem).addClass('active');
                if ($(topitem).data('sub').length) {
                    var container = $(topitem).parents('.containerobj');
                    var level = $('div', container).index($(topitem).parents('div'));
                    submenu2(container,topitem,level);
                    
                }
            }
        });
        
        // Event handling functions
        //$('a', container).live('mouseover', function(){
        $('a', '#nav').live('mouseover', function(){
            var container = $(this).parents('.containerobj');
            // Handle mouseover
            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).remove();
            $('div:eq(' + level + ') a', container).removeClass('active').removeClass('inpath');
            $('.active', container).addClass('inpath');
            $(this).addClass('active');
            if ($(this).data('sub').children('li').length) {
                // Menu has children, so add another submenu
                submenu(container, this, level);
            }
            else {
                // No children, show title instead (if it exists, or a link)
                var title = $('<a/>').attr({
                    href: $(this).attr('href')
                }).text($(this).attr('title') ? $(this).attr('title') : $(this).text());
                // Set the width
                var remainingspace = 0;
                $.each($(container).children('div').slice(0, -1), function(i, item){
                    remainingspace += $(item).width();
                });
            }
            return false;
        });
    };
    var nav = 1;
    // Generate deeper level menus
    function submenu(container, item, nav){
        var leftPos = 0;
        $.each($(container).children('div'), function(i, mydiv){
            leftPos += $(mydiv).width();
        });
        nav += 2;
        var submenu = $('<div/>').addClass('nav' + nav).appendTo(container);
        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'
                    });
                }*/
            }
        });
    }
	
	function submenu2(container, item, nav){
        var leftPos = 0;
        $.each($(container).children('div'), function(i, mydiv){
            leftPos += $(mydiv).width();
        });
        nav += 2;
        var submenu = $('<div/>').addClass('nav' + nav).appendTo(container);
        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 ($(subitem).hasClass('active')) {
				$(subsubitem).addClass('active');
			}
            if ($(subsubitem).data('sub').length) {
                $(subsubitem).addClass('hasChildMenu');
				if ($(subitem).hasClass('active')) {
					var container = $(subsubitem).parents('.containerobj');
                    var level = $('div', container).index($(subsubitem).parents('div'));
                    submenu2(container,subsubitem,level);
				}	
                /*if ($.browser.safari) {
                    $(subsubitem).css({
                        'margin-right': '15px'
                    });
                }*/
            }
        });
    }
	    
})(jQuery);
