var timer = null;

jQuery(function()
{
 // Menu
 $( "#menuNavigation li.FirstLi" )
  .data({ hover: false, timer: null, height: 0 })
  .hover( function()
          {
           $(document).click();
           
           window.clearTimeout( $(this).data( "timer" ) );
           
           $(this)
            .data( "hover", true )
            .children( "ul" )
            .stop()
            .css({ display: "block", height: $(this).children( "ul" ).height() })
            .animate({ height: $(this).data( "height" ) }, "fast");
          },
          function()
          {
           var ulChildren = $(this).children( "ul" );
           
           $(this).data( "timer",
                         window.setTimeout( function()
                                            {
                                             $(ulChildren)
                                              .stop()
                                              .slideUp( function()
                                                        {
                                                         $(ulChildren).css( "height", 0 );
                                                        } );
                                             $(ulChildren)
                                              .parent()
                                              .data( "hover", false );
                                            },
                                            100 ) );
          } )
  .each( function()
         {
          $(this).data( "height", $(this).children( "ul" ).innerHeight() );
          $(this).children( "ul" ).css( "height", 0 ).hide();
         } );
 
 // "Nos services" menu
 $( "#services" )
  .data( "hover", false )
  .hover( function(){ $(this).data( "hover", true ); },
          function(){ $(this).data( "hover", false ); } );
 $( "#services .aMenu" )
  .click( function()
          {
           $( "#service" ).toggle();
          } );
 
 $(document)
  .click( function()
          {
           if( $( "#services" ).data( "hover" ) === false )
            $( "#service" ).hide();
          } );
 /*$('li#services').hover( function() 
                        {
                         var li = $(this);
                         if( $(li).data( "animated" ) !== true )
                         {
                          $(li).data( "animated", true );
                          $('div', li).slideDown('slow', function() {
                          });
                          $('span', li).slideDown('slow', function() {
                          });
                         }
                        },
                        function() {
                         var li = $(this);
                          $('div', li).slideUp('slow', function() {
                           $(li).data( "animated", false ); 
                          });
                          $('span', li).slideUp('slow', function() {
                          });
                        } );*/
                        
   $("form[name=form_contact]")
   .submit( function()
         {
          if( $(this).validForm() )
          {
             $.post( "/system/private/contact_validation.html",
                     $.extend( { action_form: "sendMail" }, ( $(this).find(":input").serializeJSON() || {} ) ),
                     function( data )
                     {
                      if( data.status == true )
                      {
                       ViGiSITE.core.prompt( "Votre email a bien &eacute;t&eacute; envoy&eacute;", { "OK": { 
                                                                                                        callback :  function()
                                                                                                           {
                                                                                                            window.location = "/";
                                                                                                           } 
                                                                                                          } 
                                                                                                  } );
                      }else
                      {
                       $(this).find("p#error").empty();
                       $(this).find("p#error").append("Une erreur est survenue lors de l'envoie du mail.");
                      }
                    }
                  );
          }else
          {
            $(this).find("p#error").empty();
												$(this).find("p#error").append("Un ou plusieurs champs ne sont pas remplis correctement.");
          }
         });
                        
   $("form[name=form_devis]")
   .submit( function()
         {
          if( $(this).validForm() )
          {
            $.post( "/system/private/devis_validation.html",
                    $.extend( { action_form: "sendMail" }, ( $(this).find(":input").serializeJSON() || {} ) ),
                    function( data )
                    {
                     if( data.status == true )
                     {
                      ViGiSITE.core.prompt( "Votre devis a bien &eacute;t&eacute; envoy&eacute;", { "OK": { 
                                                                                                           callback :  function()
                                                                                                           {
                                                                                                            window.location = "/";
                                                                                                           } 
                                                                                                          } 
                                                                                                  } );
                     }else
                     {
                      $(this).find("p#error").empty();
                      $(this).find("p#error").append("Une erreur est survenue lors de l'envoie du mail.");
                     }
                    }
                  );
          }else
          {
            $(this).find("p#error").empty();
												$(this).find("p#error").append("Un ou plusieurs champs ne sont pas remplis correctement.");
          }   
         });
  });       
        
  //Switch between different types of devis
  function switchTypeRegistration( _this , context )
  {
   $(_this).parent().find("li.selected").removeClass("selected");
   $(_this).addClass("selected");
   $("div[id^='title_']").css("display","none");
   $("div#title_" + context ).css("display","block");
   $("form#form_devis table tr").css("display","none");
   $("form#form_devis table tr." + context ).css("display","table-row");
   $("form#form_devis div.third").css("display","block");
    
   if( context != "website" )
   {
    $("form#form_devis div.third").css("display","none");
   }  
   
   if( $(_this).parent().find("li.selected") != $(_this))
   {
    $("input.invalidInput").removeClass("invalidInput");
    $("label.invalidInputLabel").removeClass("invalidInputLabel");
   }
   
   $("input[name=nameDevis]").val( contextNames[context] );
   
  }
         
 function showImage()
 {
  if( $("div.divImagesMini").length > 1 )
  {
      $( "a.miniImages" )
       .click( function()
         {
          $(this).show("img");
         } )
  }
 }




  function getSlide( id_slide )
  {
   $.post( "/system/private/pagesReference.html?action_form=slide",
           { id_slide: id_slide },
           function( data )
           {
            $( ".references .slides .container" )
             .append( data.html );
            // initSlideShow( ".references" );
            // console.log( data );
           } );
  }

  function getNextSlide()
  {
   var next_id = 0;
   var compteur = 0;
   $.each( tableau, function( i, current)
   {
    if( current == current_slide )
    {
     compteur = i;
     next_id = ( typeof( tableau[i+1] ) != "undefined" ? tableau[i+1] : tableau[0] );
    }
   });

   current_slide = next_id;
   if ( compteur == taille - 1 )
   compteur = 1;
   else
   compteur = compteur + 2;
   
   $.post( "/system/private/pagesReference.html",
           { action_form:"slide" , id_slide: current_slide, lenght: taille, position: compteur },
           function( data )
           {
            $( ".references .slides .slide" )
             .html( data.html );
            $( ".references #MenuReferences .compteur" )
             .html( data.numero )
            init_click_photoGallery();
            // initSlideShow( ".references" );
            // console.log( data );
           } );
  } 

  function getPreviousSlide()
  {
   var prev_id = 0;
   var compteur = 0;
   $.each( tableau, function( i, current)
   {
    if( current == current_slide )
    {
     compteur = i;
     prev_id = ( typeof( tableau[i-1] ) != "undefined" ? tableau[i-1] : tableau[taille - 1] );
    }
   });
   
   if ( compteur == 0 )
   compteur = taille;
   else
   compteur = compteur;
   
   current_slide = prev_id;
   
   $.post( "/system/private/pagesReference.html",
           { action_form:"slide" , id_slide: current_slide, lenght: taille, position: compteur},
           function( data )
           {
            $( ".references .slides .slide" )
             .html( data.html );
            $( ".references #MenuReferences .compteur" )
             .html( data.numero )
              init_click_photoGallery();
            // initSlideShow( ".references" );
            // console.log( data );
           } );
  }

