/***********************************
 *                                 *
 *  Navigation jQuery JS           *
 *                                 *
 *  © 2008 Timo Besenreuther       *
 *         EZdesign (ezdesign.de)  *
 *                                 *
 *  Developed for DEBEOS GmbH      *
 *                                 *
 ***********************************

 
*/



(function($) {

    $(document).ready(function() {
        // shadow
        $('#mainnav ul li:first-child').addClass('first');
        $('#mainnav ul').wrapInner('<div class="navshadow1"><div class="navshadow2"></div></div>');
        
        // mouse over for first level
        $('#mainnav > li > a').mouseover(function() {
            // opera compatibility
            $(this).css('width', $(this).width());
            $('#mainnav .hover').removeClass('hover');
            $(this).parent().addClass('hover');
        });
        
        // mouse over common
        $('#mainnav a').hover(function() {
            // timeout
            if (debeosMainnavTimeout != false) {
                window.clearTimeout(debeosMainnavTimeout);
                debeosMainnavTimeout = false;
            }
            
            var hoverLi = $(this).parent();
            // close sublevels, that are not parent of current item
            $('#mainnav ul:visible').each(function() {
                if (!hoverLi.isChildOf($(this), 'li')) {
                    $(this).hide();
                }
            });
            $('#mainnav li.opened').each(function() {
                if (!hoverLi.isChildOf($(this), 'li')) {
                    var el = $(this);
                    el.removeClass('opened').addClass('off');
                }
            });
            // open sublevel
            hoverLi.addClass('opened').removeClass('off');
            hoverLi.children('ul').show();
            
        }, function() {
            // timeout
            window.clearTimeout(debeosMainnavTimeout);
            debeosMainnavTimeout = window.setTimeout(function() {
                debeosMainnavTimeout = false;
                $('#mainnav ul').hide();
                $('#mainnav .hover').removeClass('hover').addClass('off');
                $('#mainnav .opened').removeClass('opened').addClass('off');
            }, 500);
        });
        
        // arrows
        $('#mainnav ul li').each(function() {
            var el = $(this);
            if (el.children('ul').length > 0) {
                el.prepend('<div class="mainnav_arrow">&raquo;</div>');
            }
        });
        
    });

    var debeosMainnavTimeout = false;


    /**
     * jQuery plugin
     * checks, whether a node is a child of another node
     */

    jQuery.compareId  = 1;
    jQuery.fn.compare = function(compareWith) {
        var el = $(this);
        if (!el.attr('id')) {
            el.attr('id', 'jQuery_compare_'+jQuery.compareId);
            jQuery.compareId++;     
        }
        if (el.attr('id') == compareWith.attr('id')) {
            return true;
        }
        return false;
    }

    jQuery.fn.isChildOf = function(parentNode, selector) {
        var selector = selector ? selector : '*';
        if (parentNode) {
            var children = $(selector, parentNode);
            var len      = children.length;
            for (var i = 0; i < len; i++) {
                if ($(children[i]).compare($(this))) {
                    return true;
                }
            }
        }
        return false;
    };

})(jQuery);

jQuery.noConflict();
