 $(document).ready(function()
 {
	
    /*--------------------------------*/
    function suggest_updater(updateStat)
    {
        //si la boite à suggestion n'existe pas on la créé ===========
        if ( $('#suggest').length < 1)
        {
            $("#selection_cadd").before("<div id='suggest'></div>");
        }

        var wHref = window.location.href;
        var flagPos = wHref.indexOf("flag");
        if(flagPos >= 0){
        $("#suggest").html("<img src='http://"+window.location.hostname+"/images/interface/loadingAnimation.gif' />").children("img").css({margin:"25px 0 25px 234px"});
        var accroche = "<p>Les internautes qui ont sélectionné ce(s) programme(s) ont également choisi les programmes suivants :</p>";
        
        var getFlag = wHref.substring(flagPos+5,flagPos+6);
        
        $.ajax({
        type:"POST",
        url:"http://"+window.location.hostname+"/recherche/selection_updater.php",
        data:{act:"update",flag:getFlag,updatestat:updateStat},
        
        success: function(reponse)
                 {  //mise à jour de la liste suggestion ===
                    if(reponse){
                      $("#suggest").html(accroche+reponse); 
                    }else{
                      $("#suggest").html("");
                    }
                 }
        });
        }
    }
    
    function stats_updater()
    {
        var wHref = window.location.href;
        var flagPos = wHref.indexOf("flag");
        var getFlag = wHref.substring(flagPos+5,flagPos+6);
        
        $.ajax({
        type:"POST",
        url:"http://"+window.location.hostname+"/recherche/selection_updater.php",
        data:{flag:getFlag,updatestat:"updateStat"}
        });
    }
    
    $("#suggest a:not(.mybox)" ).click(function(){
          stats_updater();
    });
    
   
   /* BLOCK SELECTION */
   $(".mybox").livequery('click', function(event) 
   {
      //recherche si le click vient de la list suggestion
      if($(this).parents().is("#suggest")){ var updateStat="updateStat";}else{  var updateStat=0;} 
       
      var idproduit = $(this).attr("href");
      idproduit = idproduit.slice(1);
      if(idproduit !="selection_cadd") // programme pas encore selectionné
      {

          $(".new_tools a[href$='"+idproduit+"']").parents(".new_tools").parents(".programme_neuf").removeClass().addClass("programme_neuf2"); 
          //
          $(".new_tools a[href$='"+idproduit+"']").parents(".new_tools").find("a:contains('Détail')").replaceWith("<span class='ital' style=\"width:140px;\" >Programme sélectionné</span>");
          $(this).replaceWith( "<a class='red' style=\"width:114px;\" href='#selection_cadd' title='voir votre sélection'> &gt; Voir la sélection</a>" );

          $.ajax({
           type: "POST",
           url: "http://"+window.location.hostname+"/recherche/selection_block.php",
           data: {selectid:idproduit,ajaxcall:"true",pageEnvoi:'R'},
           success: function(msg){
             if(msg == false)
             { 
                alert("Des informations semblent manquer pour prendre en compte votre demande.\nVeuillez revalider un formulaire en passant par une page de détail du programme\nque vous voulez sélectionner.");
             }
                
             else
             {
                //affichage du caddy si pas visible ===  solution temporaire
                if($(".cadd_content").length < 1){ window.location.reload(); }
                 $('#ajaxGGConvert').remove();
                 $('body').append("<iframe id='ajaxGGConvert' src =\"http://"+window.location.hostname+"/conversion-resultat.html\" width=\"1\" height=\"1\" style='display:none;'></iframe>");
                 //
                 $(".cadd_content").html(msg);     
                 //$(".cadd_content").html(msg);          
                 //affichage du message ---
                 $("#selection_cadd").show();
                 $(".mess_add_prod").show();
                 var t = setTimeout('$(".mess_add_prod").slideUp()',1000);
                 var nb_ann = $(".cadd_content .programme_neuf").length; 
                 $("#selection_cadd #nbSelect, #selection_cadd #send_nb").text(nb_ann);
                 suggest_updater(updateStat);
             }
              
           }
           
           
         });
          return false;
      }
   }); 
   

   
   /* MAIL AMI */

       $("a[href$='mailami']").click(function(){
           var addmail = prompt('Saisissez une adresse email','');
           if(addmail != null && addmail != "")
           {
                $.ajax({
                type:"POST",
                url:"http://"+window.location.hostname+"/recherche/selection_printinterne.php",
                data:{flgemail:addmail},
                success: function(reponse)
                         {
                            alert(reponse);
                         }
                });
           }

              return false;  
       });

   
   /* RECEPTION MAIL */
   $("a[href$='flgemail=1']").click(function(){

        $.ajax({
        type:"POST",
        url:"http://"+window.location.hostname+"/recherche/selection_printinterne.php",   
        data:{flgemail:"1"},
        success: function(reponse)
                 {
                    alert(reponse);
                    if(reponse.indexOf('envoyé') > 0){ $('#suggest').remove(); $('#selection_cadd').fadeOut('slow'); }
                 }
        });
          return false;  
   });
   
   /* VIDER LA SELECTION */
   $("#ClearSelect").click(function(){ 
        
        $.ajax({

          type: "POST",

          url: "http://"+window.location.hostname+"/recherche/selection_block.php",

          data: {selectid:'drop',ajaxcall:"true"},

          success: function(msg)
                    {
                        if(msg){ alert(msg); }
                        //var urlpage = document.location.href;
                        
                        //window.location.href = urlpage;
                        $("#selection_cadd").hide();
                        $("#suggest").remove();
                        
                        $(".programme_neuf2").removeClass('programme_neuf2').addClass('programme_neuf');
                        alert("Votre sélection est vide");  
                        //document.location.href=document.location.href;   
                    }
          });

        return false;      

      });

	
   /*--------------------------------*/
});
