$(document).ready(loadData);

var url = "http://www.chouette-agricole.com/";

function loadData(){
    
    
		
 jQuery('#mycarousel').jcarousel({
        auto: 4,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
	
    

//chargerAnnonceRecherche();

//chargerImgBandeau();

$('#newsletterMail').click(function(){
    $('#newsletterMail').val('');                
});


$('#button_newsletter').click(addUtilisateurNewsletter);

$('iframe .ywimg').css('display','none');


/*$("#europeA").qtip({
        
        
        content:{
         text: "<h3>Organise tes soirées grâce à la boîte à outils</h3><h3>Profile et sélectionne tes invités</h3><h3>Ouvrant un pot commun pour récupérer les participations</h3>",
         title: {
          text: "<h1>Organiser</h1>"
         }
         },
         hide: { when: { event: 'mouseout' } },
         show: { when: { event: 'mouseover' } },
       style: { 
          width: 400,
          padding: 5,
            margin: 10,
          color: 'black',
          textAlign: 'center',
           name: 'green', 
          border: {
             width: 3,
             color: '#000000'
          },
          tip: {
            corner: 'leftMiddle'
         },
       },
        position: {
             corner: {
                target: 'leftMiddle',
                tooltip: 'leftMiddle'
            }
        }
    });

*/




}


function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}


//Formulaire d'enregistrement Newsletter
function addUtilisateurNewsletter(){
    //alert('coucou');
    $('#pResult').empty();
    
    $('<img width="20" height="20" src="http://www.chouette-agricole.com/img/ajax-loader.gif" />').appendTo('#pResult');
    
    var email = $('#newsletterMail').val();
    
    
    if(email!=""){
        $.getJSON(url+'ajax/newsletter/'+email,traiterReponseNewsletter);
    }else{
        alert('Entrez votre email');
    }
    
}


function traiterReponseNewsletter(reponse, textStatus){
    $.each(reponse.resultats, traiterNewsLetter);
}

function traiterNewsLetter(i,resultats){
    $('#pResult').empty();
    
    $('<i>'+resultats.result+'</i>').appendTo('#pResult');
}


//chargement du tableau des dernières recherches
function chargerAnnonceRecherche(){
    $.getJSON(url+'ajax/listeRech/10',traiterReponseRech);
    
}
function traiterReponseRech(reponse,textStatus){

	$.each(reponse.recherches,traiterRecherche);
}
function traiterRecherche(i,uneRech){
    
	$("<tr><td><a href='http://www.chouette-agricole.com/log/materiel/fiche_recherche/"+uneRech.idRech+"' title='"+uneRech.mat+"-"+uneRech.marque+"'>"+uneRech.mat+"</a></td><td><a href='http://www.chouette-agricole.com/log/materiel/fiche_recherche/"+uneRech.idRech+"' title='"+uneRech.mat+"-"+uneRech.marque+"'>"+uneRech.marque+"</a></td><td><a href='http://www.chouette-agricole.com/log/materiel/fiche_recherche/"+uneRech.idRech+"' title='"+uneRech.mat+"-"+uneRech.marque+"'>"+uneRech.desc+"</a></td><td><a href='http://www.chouette-agricole.com/log/materiel/fiche_recherche/"+uneRech.idRech+"' title='"+uneRech.mat+"-"+uneRech.marque+"'><img width='12' height='12' src='"+url+"img/rpd.png' alt='repondre à cette annonce de recherche'/></a></td></tr>").appendTo("#tabilRech");
}


//
function chargerImgBandeau(){

$.getJSON(url+'ajax/bandeau',traiterReponseBandeau);
}
function traiterReponseBandeau(reponse,textStatus){

$.each(reponse.materiels,traiterBandeau);

   jQuery('#mycarousel').jcarousel({
        auto: 4,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });

}
function traiterBandeau(i,uneAnnonce){


$('<li><a href="http://www.chouette-agricole.com/log/materiel/fiche_materiel/'+uneAnnonce.idMat+'"><img class="imgBandeau" src="http://www.chouette-agricole.com/upload/materiel/'+uneAnnonce.mail+'/petit/'+uneAnnonce.img+'" width="100" height="100"/></a>'+uneAnnonce.mat+'<br>'+uneAnnonce.prix+' €</li>').appendTo("#mycarousel");
}
//mycarousel
