var Navigation = new function()
{
 
	this.SPEED = 400;
    this.arr = [];
    this.minHeight = 0;
    this.offset = 10;
    this.height = 0;

    this.init = function()
    {
    
        $('header').css('display', 'block');
        Navigation.minHeight = $('header').height() + Navigation.offset;
        
        $('.children').each(function()
        {
            Navigation.arr.push($(this));
			

			$(this).parent().attr("_w",$(this).parent().width() );
            $(this).parent().mouseover(function()
            {
                Navigation.openMenu();
            });
            if (Navigation.height < $(this).height()) {
                Navigation.height = $(this).height();
            }
            
        });
        
        $('header').animate({
            top: '0px'
        }, Navigation.SPEED, function()
        {
            Navigation.addListeners();
        });
    };
    
    this.addListeners = function()
    {
        $('.children').each(function()
        {
            Navigation.highlight($(this));
        });
    };    
    
    this.highlight = function(obj)
    {
        $(obj).mouseover(function()
        {
            $(obj).parent().find('.head').css("color", "#fec92e");
        }).mouseout(function()
        {
            $(obj).parent().find('.head').css("color", "");
        });
    };
    
    this.openMenu = function()
    {
        Navigation.checkMenu();
        jQuery.each(Navigation.arr, function(i, val)
        {
            var _width = $(val).width();
            var _parent = $(val).parent().get(0);
			
			
            
            $(_parent).animate({
                width: _width + 'px'
            }, Navigation.SPEED, function()
            {
                $(val).fadeIn('fast');
            });
            
        });
        
        Navigation.expandHeader();
    };
    
    this.checkMenu = function()
    {
        $('header').bind('mouseleave', function()
        {
            Navigation.closeMenu();
        });
    };  
    
    this.expandHeader = function()
    {
        $('header').animate({
            height: Navigation.minHeight + Navigation.height + 'px'
        }, Navigation.SPEED);
    };   
    
    this.closeMenu = function()
    {
        $("header").unbind('mouseleave');
        
        $('header').stop(true, false).delay(100).animate({
            height: Navigation.minHeight - Navigation.offset + 'px'
        }, 100);
        
        jQuery.each(Navigation.arr, function(i, val)
        {
            var _width = $(val).width();
            var _parent = $(val).parent().get(0);
            $(val).css({
                'display': 'none'
            });
            
            $(_parent).stop(true, false).animate({
                width: $(_parent).attr('_w')+"px"
            }, Navigation.SPEED);
        });
        
    };    
}();

$(document).ready(function() {
 Navigation.init();
});
