$(function() {
   var loginBoxDelay = 500;
   $('#tool_form_search_input')
      .focus(function() {
         if ($(this).val() == $('.sns-header .tool_form_search label').text()) {
            $(this).val('');
         }
      })
      .blur(function() {
         if ($(this).val() == '') {
            $(this).val($('.sns-header .tool_form_search label').text());
         }
      })
      .val($('.sns-header .tool_form_search label').text());
   $('.tool_layered_form').hover(
      function() {
         if (this.timeoutID == undefined) {
            // Ouverture initiale du menu
            this.timeoutID = null;
         }
         else if (this.timeoutID != null) {
            // Une demande de fermeture de la boite de login est en cours
            // On annule les demandes
            window.clearTimeout(this.timeoutID);
            this.timeoutID = null;
            return;
         }
         var $this=$(this);
         this.timeoutID = window.setTimeout(function() {
            $this.get(0).timeoutID = null;
            $this
            .addClass('hovered')
            .children('.button').addClass('hovered');
            $this.find('.formline.user .input_text').focus();
         }, loginBoxDelay);

      },
      function() {
         // Une demande d'ouverture de la boite de login est en cours
         // On n'en tient plus compte
         if (this.timeoutID != null) {
            window.clearTimeout(this.timeoutID);
            this.timeoutID = null;
            return;
         }
         // On diffère la fermeture
         var $this=$(this);
         this.timeoutID = window.setTimeout(function() {
            $this.get(0).timeoutID = null;
            $this
               .removeClass('hovered')
               .children('.button').removeClass('hovered');
            $this.find('.formline .input_text').blur();
         }, loginBoxDelay );
      });
});

function imgResultPopup(url, locale)
{
   newWindow = window.open('','imgResult','toolbar=0, location=0, directories=0, status=0, scrollbars=0, resizable=0, menubar=0, top=180, left=180, height=200, width=200');
   var tmp = newWindow.document;
   tmp.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>");
   tmp.write("<html><head><title>Previmer.org</title></head>");
   tmp.write("<body><img id=\"resultImg\" src=\"" + url + "\" />");
   tmp.write("<center><p><a href='javascript:self.close()'>" +
         ( locale == 'en' ? 'Close' : 'Fermer' ) +
         "</a></p></center>");
   tmp.write("</body>");
   tmp.close();
   var img = tmp.getElementById( 'resultImg' ) ;
   img.onload = function()
   {
      newWindow.resizeTo( img.width + 30, img.height + 130 );
   };
   img.src = url;
   if ( img.complete ) 
   {
      newWindow.resizeTo( img.width + 30, img.height + 130 );
   }
   newWindow.focus();
   return false;
}

