/*
 * CU Navi jQuery Plugin Version 1.0
 * (c) 2011 comunique GmbH & Co. KG: http://www.comunique.com
 *
 * $('div').cu_navi({'slidetime' : 200});
 *
 */
 
(function( $ ){
    var methods = {
        init : function( opt ) { 
            return this.each(function(){
                var $this = $(this);
                opt = $.extend({}, $.fn.cu_navi.defaults, opt || {});
                var $thisli=$(this).find("LI");
                $thisli.data('cu_navi', opt);
                $(this).find("ul ul ").css({display: "none"}); // Opera Fix
                $thisli.hover( function(){$(this).cu_navi('hover',$(this));}, function(){$(this).cu_navi('unhover',$(this));});
            });
        },
        hover : function( cont ) { 
            var opt=cont.data('cu_navi');
            
            if(cont.parents().eq(1).attr('class')!='MenuBarArea'){
                cont.parents().eq(1).find('A:first').addClass('sitemapactive');
            }
            if (cont.find('ul:first').hasClass("sitemappages")){
                return;
            }
            /* 2. Ebene */
            if(cont.parents().get(1).tagName=='DIV' && (cont.hasClass("sitemapreverse") || cont.hasClass("sitemaplast"))){
                newval=cont.outerWidth()-cont.find('ul:first').outerWidth()+1;
                cont.find('ul:first').css({left:newval});
            }
            /* 3. Ebene */
            if(cont.parents().get(3).tagName=='DIV' && cont.find('UL:first').length > 0){
                thistop=cont.position().top;
                parentheight=cont.parent().outerHeight(true);
                mychild=cont.find('UL:first');
                //childtop=mychild.position().top;
                childheight=cont.find('UL:first').outerHeight();
                if (childheight+thistop > parentheight){
                    newval2=-(thistop+childheight-parentheight-16);
                    cont.find(':nth-child(2):first').css({top:newval2});
                }
            }
            
            cont.find('ul:first').css({visibility: "visible",display: "none"}).slideDown(opt.slidetime);
        },
        unhover : function( cont ) { 
            var opt=cont.data('cu_navi');
            cont.find('ul:first').css({visibility: "hidden"});
            cont.parents().eq(1).find('A:first').removeClass('sitemapactive');
        }
    };
    
    $.fn.cu_navi = function( method ) {
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.cu_navi' );
        }    
    };
    $.fn.cu_navi.defaults = {
        'slidetime' : 200
    }
    
})( jQuery );


