
jQuery(document).ready(function(){
    
    // search default value
    jQuery.fn.search = function()
    {
        // nothing is entered by default
        if (!this.hasClass('entered'))
        {
            return this.focus(function()
            {
                if( this.value == this.defaultValue )
                {
                    this.value = "";
                }
            }).blur(function() {
                if( !this.value.length )
                {
                    this.value = this.defaultValue;
                }
            });

        }
    };
    
    jQuery("#searchFormQuery").search();
    
    
    
    
    // center submenu
    if (jQuery('.subMenu span').length)
    {
        var parent_pos = jQuery('ul.mainMenu li.active').position();
        var parent_width = jQuery('ul.mainMenu li.active').outerWidth();
        var child_width = jQuery('.subMenu').outerWidth();
        var offset = (child_width - parent_width) / 2;

        var child_count = jQuery(".subMenu > span").size();
        
        // if children count even - center by gap between center elements
        if (child_count % 2 == 0)
        {
            // child next to center
            var middle_child_nr = child_count / 2 + 1;
            var total_left_child_width = 0;

            for (i = 1; i < middle_child_nr; i++)
            {
                total_left_child_width += jQuery('.subMenu > span:nth-child(' + i + ')').outerWidth();
            }
            
            // calculate offset without padding
            var offset = (((total_left_child_width - 10) * 2) - parent_width) / 2;

        }

        var newLeft = parent_pos.left - offset + 11;
        
        // align to right margin
        if (newLeft + child_width > 960)
        {
            jQuery(this).find('.subMenu').css({
                left:       (960 - child_width) + "px"
            });
        }
        // align center
        else if (newLeft > 0)
        {
            jQuery(this).find('.subMenu').css({
                left:       newLeft + "px"
            });
        }
        // else no change to default position which is 0
    }
                
});
