AutoEmptyInputText = {
    
    autoEmptyFieldVal : [],
    
    init: function(){
        var closure = this;
        $('.autoEmpty').each(function(i, item){
            closure.autoEmptyFieldVal.push($(item).val());
            $(item).bind('focus', function(){
                var bindedElem = this;
                $(closure.autoEmptyFieldVal).each(function(i, item){
                    if($(bindedElem).val() == item)
                    {
                        $(bindedElem).val('');
                        $(bindedElem).removeClass('autoEmpty');
                        $(bindedElem).unbind('click');
                    }
                });
            });            
            
            $(item).closest('form').submit(function(){
                var current_item = item;
                $(closure.autoEmptyFieldVal).each(function(i, item){
                    if($(current_item).val() == item)
                        $(current_item).val('');
                });
                return true;
            });
        });
    }
}

WindowBox = {
    open: function(title, message, button){
        $('body').prepend('<div id="overlay" style="position: absolute; width: ' + 
            $(document).width() + 
            'px; height: ' + 
            $(document).height() + 
            'px; opacity: 0; background-color: black; z-index: 1000"></div>');
        $('body').prepend('<div id="windowBox" class="main_bloc" \n\
            style="position: absolute; opacity: 0; background-color: white; z-index: 1001">\n\
            <div class="main_bloc_header"><!-- --></div>\n\
            <div class="main_bloc_content"></div></div>');
        $('body #windowBox div.main_bloc_content').append('<h1>' + title + '</h1>');
        $('body #windowBox div.main_bloc_content').append('<div id="windowBoxContent">' + message + '</div>');
        if(typeof button !== 'undefined') 
            $('body #windowBox div.main_bloc_content').append('<button class="green_bg">' + button + '</button><div style="clear:both;"><!-- --></div>');
        $('body #windowBox').center();             


        if(typeof button !== 'undefined')
        {
            var closure = this;
            $('body #windowBox button').bind('click', function(){
                closure.close();
            });
        }
        $('body div#overlay').animate({opacity: '0.8'}, this.overlaySpeed, function(){
            $('body #windowBox').animate({opacity: 1}, this.overlaySpeed);
        });            
    },
    
    close: function()
    {
        $('body div#overlay, body #windowBox').animate({opacity: '0'}, this.overlaySpeed, function(){
            $('body div#overlay, body #windowBox').remove();
        });
    },
    
    captureFlashMessages: function(){
        if($('div#flashMessage').length == 0) return false;
        this.open('réseau sipa', '<p>' + $('div#flashMessage').text() + '</p>', 'ok');
    }
} 

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


MenuHover = {
    
    init : function(){        
        $('#menu_recrutement').mouseenter(function(){
            $('#menu_recrutement').unbind('mouseenter');
            $('#menu_recrutement_content').fadeIn();
        });
        
        $('#menu_recrutement').mouseleave(function(){
            $('#menu_recrutement_content').fadeOut(200, function(){
                $('#menu_recrutement').mouseenter(function(){
                    $('#menu_recrutement').unbind('mouseenter');
                    $('#menu_recrutement_content').fadeIn();                    
                });
            });
        });
    }
}

$(document).ready(function(){
    AutoEmptyInputText.init();
    MenuHover.init();   
    WindowBox.captureFlashMessages();
    
    /* suppression du focus sur les bouttons */
    $("input[type=submit]").focus(function(){  $(this).blur();  });
    $("button").focus(function(){  $(this).blur();  });
});


