/* ---------------------------------------------------------------------
*   Le Phare JavaScript library - (c) 2010
--------------------------------------------------------------------- */
(function($) {
   // Options utilisées pour les effets par défaut
   var options = {
         'delay'    : 8   * 1000,
         'duration' : 1.5 * 1000,
         'debug'    : false
       },
       tiptip_options = {
          defaultPosition: 'bottom',
          maxWidth: '210px',
          activation: 'focus'
       };

   // Intégration de firebuglite
   if (options.debug && typeof console == 'undefined') {
      var fb = $('<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>');
      $('head').append(fb);
   }

   // Nouveau Loader
   $(document).ready(function() {
      var id = $('body').attr('id');
      if (!id)
         alert("ATTENTION\n la balise body n'a pas d'id !");

      // Register specifics
      switch (id) {
         case 'pageContact' :
            $('form').validate({
            events: {
               success : function() {},
               failed  : function(msg) {
                  var input           = $(this),
                      target          = input,
                      position_tip    = 'leftMiddle',
                      position_corner = {
                        target: 'rightMiddle',
                        tooltip: 'leftMiddle'
                      };

                  if(input.closest('div.colFormA').length) {
                     target       = input.parent();
                     position_tip = 'rightMiddle';
                     position_corner = {
                        target: 'leftMiddle',
                        tooltip: 'rightMiddle'
                     };
                  }

                  target.qtip({
                     content: {
                        text : msg
                     },
                     show: false,
                     style: {
                        tip:        position_tip,
                        background: '#F2F3F3',
                        color:      '#333',
                        border: {
                           width: 2,
                           radius: 3,
                           color: '#F26F24'
                        }
                     },
                     position: {
                        corner: position_corner,
                        adjust: {
                           x: 15,
                           y: 0
                        }
                     }
                  });
                  target.qtip('show');
                  return false;
               }
            }
         });
            break;
         default:
      }


      // Register generics
      $('.fade').delay(options.delay).fadeOut(options.duration);
      $('.placeholder').placeholder();


      var email = $('#chpEmail');
      $('#abonnement-newsletter')
         .validate({
            events: {
               success : function() {},
               failed  : function(msg) {
                  email.qtip({
                     content: {
                        text : msg
                     },
                     show: false,
                     style: {
                        tip:        'topMiddle',
                        background: '#F2F3F3',
                        color:      '#333',
                        border: {
                           width: 2,
                           radius: 3,
                           color: '#F26F24'
                        }
                     },
                     position: {
                        corner: {
                           target:  'bottomMiddle',
                           tooltip: 'topMiddle'
                        },
                        adjust: {
                           x: 15,
                           y: 15
                        }
                     }
                  });
                  email.qtip('show');
                  return false;
               }
            }
         })
         .bind('submit', function() {
            var form  = $(this);
            $.post(form.attr('action'), form.serialize(), function(response) {
               var msg = $('#mainContent p', response).html();


          // Tool tip abn newsletter
               email.qtip({
                  content: {
                     text : msg
                  },
                  show: false,
                  style: {
                     tip:        'topMiddle',
                     background: '#F2F3F3',
                     color:      '#333',
                     border: {
                        width: 2,
                        radius: 3,
                        color: '#F26F24'
                     }
                  },
                  position: {
                     corner: {
                        target:  'bottomMiddle',
                        tooltip: 'topMiddle'
                     },
                     adjust: {
                        x: 15,
                        y: 15
                     }
                  }
               });
               email.qtip('show');
            });

            return false;
         });
   });

})(jQuery);
