(function($){
  $(document).ready(function(){

    /* ajoute un target _blank au lien avec une classe external */
    $("a[rel='external'],a[rel='document'], a[href$='.pdf'], a[href$='.doc'], a[href$='.xls'], a[href$='.txt']").each(function(){
            /*$(this).attr({ title: ($(this).attr("title"))?$(this).attr("title"):$(this).attr("href")+" (nouvelle fenêtre)" });*/
            $(this).click(function() {window.open($(this).attr('href'));return false;});
    });
    /*--------------------*/

    $("a.delete_account").click(function(){
      $("#infos_delete").dialog("open");
    }); 
    
    $("#infos_delete").dialog({
        autoOpen: false,
        width: 450,
        title: 'Suppression de votre compte'
    });
    
    $("h2.helper").after('<div id="help" class="slider"></div>');
    $("h2.helper a").removeAttr('href');
    
    $("h2.helper a").click(function(){
      $("#help").dialog("open");
      $('.slider').anythingSlider({
          easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
          autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
          delay: 3000,                    // How long between slide transitions in AutoPlay mode
          startStopped: false,            // If autoPlay is on, this can force it to start stopped
          animationTime: 600,             // How long the slide transition takes
          hashTags: true,                 // Should links change the hashtag in the URL?
          buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
          pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
          startText: "Start",             // Start text
          stopText: "Stop"
      });          
    });  

    $("#help").dialog({
        autoOpen: false,
        width: 800,
        height: 460,
        modal: true,
        dialogClass: 'help_bloc', 
        title: 'Comment utiliser alerte-auto ?'
    });  

    $("#help").load("/site/helpContent");    
		
    /*tooltip detail annonce*/
    if(document.getElementById('qualite'))
    {
            /* CONFIG */
            xOffset = -10;
            yOffset = 20;
            /* END CONFIG */
            $(".tooltip").mouseover(function(e){
                    this.t = this.title;
                    this.title = "";
                    $("body").append("<p id='tooltip'>"+ this.t +"</p>");
                    $("#tooltip")
                            .css("top",(e.pageY - xOffset) + "px")
                            .css("left",(e.pageX + yOffset) + "px")
                            .fadeIn("fast");
                    })
            $(".tooltip").mouseout(function(e){
                    this.title = this.t;
                    $("#tooltip").remove();
                    });
            $(".tooltip").mousemove(function(e){
                    $("#tooltip")
                            .css("top",(e.pageY - xOffset) + "px")
                            .css("left",(e.pageX + yOffset) + "px");
            });
    }
    /*----------------------------*/

    /* Recherche  par mot cles*/
    $("#connexion p:not(.submit) input").each(function(){
      if(!$(this).val())
            {
              $(this).attr('value',$(this).prev().html());
            }
            $(this).prev().css('display','none');
            $(this).click(function() {
          if ($(this).val() == $(this).prev().html()){
            $(this).val('');
          }
        });
        $(this).blur(function() {
          if ($(this).val() == '')
          {
            $(this).val($(this).prev().html());
          }
        });
    });
    /*--------------------*/

    /*------------bloc extensible----------------------*/
    $(".expanded").prev().each(function(){
      $(".expanded").css("display","none");
      $(".selected").css("display","block");
      $(".expanded").prev().css('background','url(/css/img/desc-tri-rouge-droit.gif) right 4px no-repeat');
      $(".selected").prev().css('background','url(/css/img/desc-tri-rouge-bas.gif) right 4px no-repeat');
      $(".expanded").prev().css('cursor','pointer');
    });

    jQuery(".expanded").prev().toggle(
       function(){ jQuery(this).next().fadeIn();jQuery(this).css('background','url(/css/img/desc-tri-rouge-bas.gif) right 4px no-repeat');},
       function(){ jQuery(this).next().fadeOut();jQuery(this).css('background','url(/css/img/desc-tri-rouge-droit.gif) right 2px no-repeat');}
    );
    /*-------------end bloc extensible-----------------*/

    /*-------------Confirmation suppression annonce---------------*/
    $('a.delete').click(function(){
            var answer = confirm('Voulez-vous vraiment supprimer cette annonce ?');
            return answer;
    });
    /*------------------------End confirmation------------------------*/

    /*-------------depot annonce---------------*/
    $('input#valid_depot').click(function(){
            $('img#wait').css('display', 'block');
    });
    /*-------- End depot annonce--------------*/

    /*liste annonce*/
    $('.inside table tbody tr').each(function() {
            $(this).mouseover(function() {
                    $('td.res-photo, td.res-annonce, td.res-kilometrage, td.res-departement, td.res-actions, td.res-annee, td.action, td#action', this).css('background', '#dbf296');
                    $('td.res-prix', this).css('background', '#e9f9bb');
            });
            $(this).mouseout(function() {
                    $('td.res-photo, td.res-annonce, td.res-kilometrage, td.res-departement, td.res-actions, td.res-annee, td.action, td#action', this).css('background', '#F1F1F1');
                    $('td.res-prix', this).css('background', '#E2E2E2');
            });
    });

    $('.inside table tbody tr.top-clic').each(function() {
            $(this).mouseover(function() {
                    $('td.res-photo, td.res-annonce, td.res-kilometrage, td.res-departement, td.res-actions, td.res-annee, td.action, td#action', this).css('background', '#fdc2c2');
                    $('td.res-prix', this).css('background', '#ffcccc');
            });
            $(this).mouseout(function() {
                    $('td.res-photo, td.res-annonce, td.res-kilometrage, td.res-departement, td.res-actions, td.res-annee, td.action, td#action', this).css('background', '#fce0e0');
                    $('td.res-prix', this).css('background', '#ffcccc');
            });
    });
    /*---------end liste annonce-------------*/

    /*Formulaire*/
    $('.inside form input[type="text"], .inside form input[type="password"], .inside form textarea, .inside form select').each( function() {
      $(this).focus( function() {
         jQuery(this).prev('label').css( 'background', 'url(/css/img/bullet.gif) 4px 6px no-repeat' );
         jQuery(this).css( 'border', '1px solid #dc3131' );
      })
      .blur( function() {
         jQuery(this).prev('label').css( 'background', "none" );
         jQuery(this).css( 'border', "1px solid #B4A86A" );
      });
    });
    /*-----------End Formulaire------------------------*/

    /* chargement des bannieres pubs */
    if($('#banniere-droite-home').length > 0)
    {
      $.ajax({
        type: 'GET',
        url: '/publicite/bannieredroiteHome',
        error: function(XMLHttpRequest, textStatus, errorThrown) {},
        success: function(data, textStatus) {
          $('#banniere-droite-home').html(data);
        },
        timeout: 5000
      });
    }

    if($('#banniere-1').length > 0)
    {
      $.ajax({
        type: 'GET',
        url: '/publicite/bannierehaute',
        error: function(XMLHttpRequest, textStatus, errorThrown) {},
        success: function(data, textStatus) {
          if(data != "")
          {
            $('#banniere-1').html(data);
          }
        },
        timeout: 5000
      });
    }

    if($('#banniere-2').length > 0)
    {
      $.ajax({
        type: 'GET',
        url: '/publicite/bannierebas',
        error: function(XMLHttpRequest, textStatus, errorThrown) {},
        success: function(data, textStatus) {
          if(data != "")
          {
            $('#banniere-2').html(data);
          }
        },
        timeout: 5000
      });
    }

    if($('#banniere-3').length > 0)
    {
      $.ajax({
        type: 'GET',
        url: '/publicite/bannieredroite',
        error: function(XMLHttpRequest, textStatus, errorThrown) {},
        success: function(data, textStatus) {
          if(data != "")
          {
            $('#banniere-2').html(data);
          }
        },
        timeout: 5000
      });
    }
    /*--------------------*/

    /*footer nav*/
    $('#footer #footer_nav ul li span').each(function() {
            $(this, 'span').html('|');
    });
    /*---------end footer nav-------------*/

    /*-------------Flash recherche par région--------------*/
    if(document.getElementById('search_region'))
    {
      var so = new SWFObject("/flash/region.swf", "region", "565", "470", "9", "#fff", "high" );
      so.write("search_region");
    }
    /*-------------------------------------------------------------*/

  });
})(jQuery);	