// JavaScript Document
/*$(function() {
 $('#last_insc *').tooltip({
    track: true,
   delay: 0,
   showURL: false,
});
 
  $('#last_annonce *').tooltip({
    track: true,
   delay: 0,
   showURL: false,
});
 
});*/

$(document).ready(function(){
						   
   $('img[@src$=.png],#top,#toplog,#perso,#menu li,#polaroid,#membre,#annonce,.visuel').ifixpng();

  var type =''

   $(".local_ville[value='']").attr("disabled","disabled")
   $(".local_pays").change(function() {

               typerech = $(this).attr('id').substr(4)

               ville="#ville"+typerech;
      $(ville).flushCache()


         completeVille($(this),typerech)
   })

   $('.divchoix').hide()
   $(".encartphoto").hide()

   $("a.close").click(function(){ $("#messages").hide();});

   $("a.toggle").click(function(event){
         $("#rech_crit").slideToggle("normal");
         return false;
   });

   $("a.rech").click(function(event){
         $("#recherche").slideToggle("normal");
         return false;
   });

 $.datepicker.setDefaults({showOn: 'both', buttonImage: 'admin/imgs2ktp/calendar.png', buttonImageOnly: true,dateFormat: 'dd-mm-yy'});
   $('.date').datepicker();


   $('#p_ou_r').each(function(){
      v=$(this).val()
      if(v=='P')$(".encartphoto").show()
      $('#div_'+v).show()
   })
   $('#p_ou_r').change(function(){
      $('.divchoix').hide()
      v=$(this).val()

      if(v=='P')$(".encartphoto").show()
      else $(".encartphoto").hide()

      $('#div_'+v).show()

  })

   $(".miniature a").click(function(){
      var largePath = $(this).attr("href");
     var largeAlt = $(this).attr("title");
      $("#largeImg").attr({ src: largePath, alt: largeAlt });
      $("h2 em").html(" (" + largeAlt + ")"); return false;
  });
   


 
if($("#messages li").length > 0){
  tb_show('', '#TB_inline?&width=450&inlineId=messages', 'false');
 	} 
  	$(".allezsimple").each(function(){getprix($(this))
	});
  
  
});


function ScanCookie(variable)
   {
   cook = document.cookie;
   variable += "=";
   place = cook.indexOf(variable,0);
   if (place <= -1)
      return("0");
   else
      {
      end = cook.indexOf(";",place)
      if (end <= -1)
         return(unescape(cook.substring(place+variable.length,cook.length)));
      else
         return(unescape(cook.substring(place+variable.length,end)));
      }
   }
function CreationCookie(nom,valeur,permanent)
   {
   if(permanent)
      {
      dateExp = new Date(2020,11,11);
      dateExp = dateExp.toGMTString();
      ifpermanent = '; expires=' + dateExp + ';';
      }
   else
      ifpermanent = '';
   document.cookie = nom + '=' + escape(valeur) + ifpermanent;
   }
if(ScanCookie("dejapopup")==0)
   {
$(document).ready(function() {
tb_show('', 'message.html?height=200&width=500&inlineId=myOnPageContent', 'false');
});
   CreationCookie("dejapopup","oui",false)
}




function completeVille(cible,typerech){

               ville="#ville"+typerech;
               idville="#id_ville"+typerech

             $(ville).removeAttr("disabled")
               $(ville).autocomplete("ajax.php?action=ville",{

                              cacheLength:1,
                               minChars:3,
                                 extraParams:{pays:$("#pays"+typerech).val()},

                                 formatItem: function(data, i, n, value)
                                 {
                                    return value.split(">")[0];
                                 },
                                 formatResult: function(data, value) {


                                    return value.split(">")[0].split("-")[1];
                                 }
               });


              $(ville).result(function(event, data, formatted) {
                                       $("#id_ville"+typerech).val(formatted.split(">")[1])
                   });

}


function CreatePhoto(){

  $("#liste_photos").append("<div class='photo'><a href='#' class='thickbox'><img src='design/fond/loading.gif' /></a><input type='hidden' name='saisie[liste_photos][]' value=''/></div>")
   tb_init('a.thickbox');

}

function AssignPhoto(name){
   if($('#liste_photos .photo').size()==6) {$(".uploader").hide()}

   unique=name.split("_")
   unique.shift()
   unique=unique.toString()
      if($('#liste_photos .photo:has(input[value$='+unique+'])').size()>0){
      $('#liste_photos .photo:last').remove();
      }else{
            $('#liste_photos .photo:last img').attr('src','upload/vignettes/tb'+name)
            $('#liste_photos .photo:last input').val(name)
            $('#liste_photos .photo:last a').attr('href','upload/'+name)
            $('#liste_photos .photo:last').append("<a href='#' onclick=\"supprimePhoto('"+name+"')\"><img src='design/element/delete.gif' alt='Supprimer' title='Supprimer' class='suppr_photo' /></a>")
      }
}

function HideContent(d) {
        if(d.length < 1) { return; }
        document.getElementById(d).style.display = "none";
        }
        function ShowContent(d) {
        if(d.length < 1) { return; }
        document.getElementById(d).style.display = "block";
        }
        function ChangeClassOn(d, e, f) {
        if(d.length <1) {return; }
        document.getElementById(d).className = "on";
        document.getElementById(e).className = "none";
        document.getElementById(f).className = "none";
}

function ajoutePhoto()
{
   $.ajax({type: "GET",url: "ajax.php?action=ajoutephoto",success: function(xml)
   {
   }
   }
  )
}

function supprimePhoto(fich)
{
   $('#liste_photos .photo:has(input[value='+fich+'])').remove()
   $.ajax({type: "GET",url: "ajax.php?action=supprimephoto&id_objet="+fich,success: function(xml)
   {
   }
   }
   )
}

function ajouteFavori(id)
{
    $.ajax({type: "GET",url: "ajax.php?action=ajoutefavori&id_annonce="+id,success: function(xml)
   {
      var reponse=$('action',xml);
      if(reponse.attr('reponse')== "-1")alert("Cette annonce est déjà dans vos favoris");
      else alert("Cette annonce a été ajoutée à vos favoris");
   }
   }
   )
}

function getprix(cible)
{
	
   var lid=cible.attr('id').substr(7)   
	$("#asprix_"+lid).css({"background":"url('http://www.co-clic-co.com/design/fond/loading_trans.gif') no-repeat"});

   var param   =  cible.val().split("|");
   //alert(param);
   var coded   =  param[0]
   var codea   =  param[1]
   var catvehi =  param[2];
   var dated   =  param[3]
   

   //prompt("url","ajax.php?action=gettarif&catVehi="+catvehi+"&codeDepart="+coded+"&codeArrivee="+codea+"&dateDepart="+dated);
   if(coded != '' && codea != ''){
   var urlajax="ajax.php?action=gettarif&catVehi="+catvehi+"&codeDepart="+coded+"&codeArrivee="+codea+"&dateDepart="+dated
   $.ajax({type: "GET",url:urlajax ,success: function(xml)
   {
      //info dispo
      //<TARIF TTC="840" RemisePromo="0" TTCNet="840" LeJourSup="130" LeKmSup="0,24" Jour="3" Km="800" Distance="797" arJour="2,5" arTTCLoc="510" arCarburant="478" arPeage="256" Consommation="20,00"/>
      var reponse=$('TARIF',xml);
      //alert(lid+" "+reponse.attr('TTC'));
      var typecamion;
      if(catvehi==44)typecamion=12;else typecamion=20;
      var chaine="<a target='_blank' href='http://www.allez-simple.com/simulation-de-location-pour-un-"+typecamion+"m-cube-le-"+dated+"-pour-les-villes-"+coded+"-et-"+codea+".html?utm_medium=coclicco&utm_source=resultats_codem'>"+reponse.attr('TTC')+"&nbsp;&euro;"+"</a>"
      $("#asprix_"+lid).html(chaine);
      $("#asprix_"+lid).css({"background":"none"});
   }});
   }else{
	   
      $("#asprix_"+lid).css({"background":"none"});
   }
}
