

var priorCenterItem = 1;
// pour recuperer la Categorie ou l'ID_Site
//var SiteID;
var mail_ID;
var MotCle="";

var Affiche_date=0;

mail_ID=url('mail_ID');

var imageList = new Array();
var urlList = new Array();
var IconList = new Array();
var DateList = new Array();
var SiteList = new Array();
var TabIDMail = new Array();
var DateListSansJour = new Array();

//importeXML(imageList,urlList,IconList,"xml/data_mail.xml.php?Cat="+Categorie);
//makeRequest("http://www.verifspam.com/xml/data_mail.xml.php?Cat="+Categorie,'',imageList,urlList,IconList);

var lastRan = -1;

/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/

var fmtItem = function(imgUrl, url, title, i,alto) {
	  
	  var innerHTML = '<a target="_blank" id="carousel-anchor-' + i + '" href="http://www.verifnews.com/' + 
          url + 
          '">' +
          title + '<\/a>'+
          '<img id="carousel-image-' + i + '" src="/' + 
          imgUrl +
        '" width="' +
        75 +
        '" height="' +
        75+
        '" alt="'+alto+'" title="'+alto+'"/>';
  
    return innerHTML;
     
};

var fmtItem2 = function(imgUrl, url, title, i) {
      var innerHTML = 
          '<img id="carousel-image-' + i + '" src="/' + 
          imgUrl +
        '" width="' +
        75 +
        '" height="' +
        75+
        '"/><a id="carousel-anchor-' + i + '" href="http://www.verifnews.com/' + 
          url + 
          '">' + 
          title + '<\/a>';
  
    return innerHTML;
     
};

/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
  
    
    load(this, start, last);
    
    spotlight(this);    
    preview(this);
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {
    // get the last middle item and turn off spotlight
    var li = this.getItem(priorCenterItem);
  
   //alert(zozo.getAttribute("id"));
   // var tata=zozo.getAttribute("id");
    //alert(zozo);
    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
   
    if(!alreadyCached) {
        load(this, start, last);
    }
    spotlight(this);
    preview(this);
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/

var loadPrevItems = function(type, args) {
    // get the last middle item and turn off spotlight
    var li = this.getItem(priorCenterItem);

    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    
    if(!alreadyCached) {
        load(this, start, last);
    }
    spotlight(this);
    preview(this);
};

var load = function(carousel, start, last) {
    

    makeRequest("/xml/data_data.xml.php?Cat="+Categorie+"&SiteID="+SiteID+"&Tag_ID="+Tag_ID+"&MotCle="+MotCle,'',imageList,urlList,IconList,DateList,carousel);

};

var getRandom = function(max, last) {
    var randomIndex;
    do {
        randomIndex = Math.floor(Math.random()*max);
    } while(randomIndex == last);
    
    return randomIndex;
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {

    var enabling = args[0];
    var leftImage = args[1];
    if(enabling) {
        leftImage.src = "/images/left-enabled.gif";    
    } else {
        leftImage.src = "/images/left-disabled.gif";
    }
    
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the next button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: nextButtonStateHandler
 **/
var handleNextButtonState = function(type, args) {

    var enabling = args[0];
    var rightImage = args[1];
    if(enabling) {
        rightImage.src = "/images/right-enabled.gif";    
    } else {
        rightImage.src = "/images/right-disabled.gif";
    }
    
};

function completeHandler(type, args) {
    // instead of doing the preview in the loadNext and loadPrev you can
    // wait for the animation scroll to stop before showing the preview
    //preview(this);
}
function preview(carousel) {
    var firstVisible = carousel.getProperty("firstVisible");
    var middle = firstVisible + 2;
    var anchor = YAHOO.util.Dom.get('carousel-anchor-' + middle);
   // YAHOO.util.Dom.get('preview').innerHTML = '<img src="' + anchor.href + '"/>';
    rempli_iframe('toto',anchor.href);
    
   Rempli_Mot_Cle(middle);
    
}
function Rempli_Mot_Cle(indice){
  var my_txt='-';
  var i;
 //Rempli les mot clés avec les valeur des objets du tableau
    for(i=0;i<Tag_tab[indice-1].length;i++){
     // my_txt=my_txt+" "+"<a class='Tag' href='Consult.php?Tag_ID="+Tag_tab[indice-1][i].ident+"'><font color='"+Tag_tab[indice-1][i].couleur+"'>"+Tag_tab[indice-1][i].nom+"</font></a>";
     my_txt=my_txt+" "+"<a class='Tag'  onclick=\"RafraichiTag(\'"+Tag_tab[indice-1][i].ident+"\');\"><font color='"+Tag_tab[indice-1][i].couleur+"'>"+Tag_tab[indice-1][i].nom+"</font></a> -";
    
    }
    document.getElementById('MailDesc').innerHTML = my_txt;
}


function spotlight(carousel) {
    var firstVisible = carousel.getProperty("firstVisible");
    var start = firstVisible;
    var revealAmount = carousel.getProperty("revealAmount");
    var size = carousel.getProperty("size");
    
    if(revealAmount && firstVisible > 1) {
        start = firstVisible - 1;
    }
    var lastVisible = firstVisible + carousel.getProperty("numVisible") - 1;
    var end = lastVisible;
    if(revealAmount && lastVisible < size) {
        end = lastVisible + 1;
    }
    
    var middle = firstVisible + 2;
    
    for(var i=start; i<=end; i++) {
        var li = carousel.getItem(i);
        
        if(i == middle) {
            YAHOO.util.Dom.replaceClass(li, 'non-spotlight', 'spotlight');
            priorCenterItem = i;
        } else {
            YAHOO.util.Dom.replaceClass(li, 'spotlight', 'non-spotlight');
        }
    }
}

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 **/

var carousel; // for ease of debugging; globals generally not a good idea
var pageLoad = function() 
{
  
    carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
        {
            numVisible:         5,
            animationSpeed:     0.6,
            animationMethod:    YAHOO.util.Easing.backBoth,
            scrollInc:          1,
            navMargin:          40,
            scrollBeforeAmount: 2,
            firstVisible:       -1,
            size:               20,
            scrollAfterAmount:  2,
            prevElement:        "prev-arrow",
            nextElement:        "next-arrow",
            loadInitHandler:    loadInitialItems,
            loadNextHandler:    loadNextItems,
            loadPrevHandler:    loadPrevItems,
            prevButtonStateHandler:   handlePrevButtonState,
            nextButtonStateHandler:   handleNextButtonState,
            animationCompleteHandler: completeHandler
        }
        
    );
    
    //carousel.loadNextHandlerEvt.subscribe(beforeNext, carousel);
};

// rajout d'un fonction pour recharger le carousel par de l'ajax
function recharge_site(id_site) {
  imageList=new Array();
  urlList= new Array();    
  IconList= new Array();              
  DateList = new Array();
  DateListSansJour = new Array();
  SiteList = new Array();
  TabIDMail=new Array(); 
  Affiche_date=0;
  MotCle="";
  //importeXML(imageList,urlList,IconList,"xml/data_data.xml.php?SiteID="+id_site);
  SiteID=id_site;
  carousel.clear();
  carousel.reload();
}
function RafraichiSite(NumSite){
  SiteID=NumSite;
  Categorie=0;
  MotCle="";
  imageList=new Array();
  urlList= new Array();    
  IconList= new Array();              
  DateList = new Array();
  TabIDMail=new Array(); 
  DateListSansJour=new Array();
 //makeRequest("http://www.verifspam.com/xml/data_data.xml.php?Cat="+NumCat+"&SiteID="+SiteID,'',imageList,urlList,IconList,DateList,carousel);
 carousel.clear();
 carousel.reload();
}
function RafraichiTag(NumTag){
  Tag_ID=NumTag;
  SiteID=0;
  Categorie=0;
  MotCle="";
  imageList=new Array();
  urlList= new Array();    
  IconList= new Array();              
  DateList = new Array();
  TabIDMail=new Array(); 
  DateListSansJour=new Array();
 //makeRequest("http://www.verifspam.com/xml/data_data.xml.php?Cat="+NumCat+"&SiteID="+SiteID,'',imageList,urlList,IconList,DateList,carousel);
 carousel.clear();
 carousel.reload();
 Tag_ID=0;
}

function RafraichiMotCle(sMotCle){
  MotCle=sMotCle;
  SiteID=0;
  Categorie=0;
  imageList=new Array();
  urlList= new Array();    
  IconList= new Array();              
  DateList = new Array();
  TabIDMail=new Array(); 
  DateListSansJour=new Array();
  //alert(MotCle);
 //makeRequest("http://www.verifspam.com/xml/data_data.xml.php?Cat="+NumCat+"&SiteID="+SiteID,'',imageList,urlList,IconList,DateList,carousel);
 carousel.clear();
 carousel.reload();
 Tag_ID=0;
 MotCle="";
 //document.getElementById('textRechercheResultat').style.display = 'none';
}
//var tot=pageLoad();
YAHOO.util.Event.addListener(window, 'load', pageLoad);



