var second_depth_els;
var first_depth_els;
var timeout;

//function($)  {
$.fn.extend({
                show_for:   function(time)  {
                    return this.each(function()  {
                        this.hide_timeout = null;
                        function hideEl(el)  {
                            return function()  {   
                                $(el).hide(); 
                            }
                        }
                        function ClearTimeout(el)  {
                            //console.info('clear');
                            return function()  {  if (el.hide_timeout != null)  {console.info('clear');clearTimeout(el.hide_timeout); el.hide_timeout = null; }}
                        }
                        function SetTimeout(el)  {
                            return function()  {   {clearTimeout(timeout); ; timeout = setTimeout(hideEl(el), time); }}
                        }
                        SetTimeout(this)();
                        if (!this.initialized)  {
                            $(this).bind('mouseover', SetTimeout(this));
                            $(this).bind('mousemove', SetTimeout(this));
                            $(this).bind('mouseout', SetTimeout(this));
                        }
                        this.initialized = true;
                        $(this).show();
                    });
                }
           });

$(function()  {
    $('ul.tdnavbar').each(function()  {
        var P = $(this);
        second_depth_els = $('li ul li', this);
        first_depth_els = $('li', this).not(second_depth_els);

        var fdp = first_depth_els.parent();

        /* dynamic positioning of 2nd depth */
        var first_offset = fdp.offset()
        var sdp = second_depth_els.parent();
        var sdp_offset_top = $(this).offset().top + (fdp.height() );
        /* some tricks to third depth elements */
        sdp.css('display', 'none').css('visibility', 'visible').css('left', 0);

        //if ($.browser.msie)
         //   sdp_offset_top += 10;

        sdp.css('top', sdp_offset_top).css('left', first_offset.left);

        /* dynamic positioning of 2nd depth */
        sdp.each(function()  {
            var po = $(this).parent().offset();
            $(this).css('position', 'absolute').css('left', po.left).css('padding-right', '0'); //our left position is our parent's left - the left of its parent (level 1 categories)
            $(this).css('top', sdp_offset_top + P.parent().height());
            if ($.browser.msie)
                $(this).css('top', sdp.height() + 2);
        });

        first_depth_els.bind('mouseover', function()  {
            var me = this;
            first_depth_els.each(function()  {
                if (this == me)  {
                    $(this).addClass('active');
                    $('ul', this).show_for(1500);
                    this.expanded = true;
                }
                else if (this.expanded) {
                    $(this).removeClass('active');
                    $('ul', this).hide();
                    this.expanded = false;
                }

            });

        });

        /*
        second_depth_els.bind('mouseover', function()  {
            var me = this;
            second_depth_els.each(function()  {
                if (this == me)  {
                    $(this).addClass('active');
                    $('ul', this).show_for(1500);
                    this.expanded = true;
                }
                else if (this.expanded)  {
                    $(this).removeClass('active');
                    $('ul', this).hide();
                    this.expanded = false;
                }

            });

        });
        */
    });
});
//}(jQuery);
