var gXhr            = Array();
var gtItems            = Array();
var gtoCarrousel    = Array();
var gDirectoryPath    = "";
var gNumImage        = Array();
var gFading            = Array();
// Valeur configurable !
var gTimeout        = Array();

function carrousel_getXmlNodeValue(reponse, sName) {
    var node = reponse.getElementsByTagName(sName)[0];
    
    if ((node === null) || (node.childNodes.length === 0)){
        return "";
    }
    
    return node.firstChild.nodeValue;
}

function carrousel_getXhr(){
    var xhr = null;
    
    if(window.XMLHttpRequest){        // Firefox et autres
        xhr = new XMLHttpRequest(); 
    } else if(window.ActiveXObject){    // Internet Explorer 
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else { // XMLHttpRequest non support? par le navigateur 
        // alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
        xhr = false;
    }
    
    return xhr;
}

/* Creation du tableau des items
    tPlaylist[i][j] : 
    i de 0 ? n
    j : 0 pour l'image , 1 pour l'url
*/
function carrousel_getItems(xml,iNum) {
    var tItems =[];
    if (!xml){
        return null;
    }
    var node = xml.getElementsByTagName("item");
    
    for (i = 0; i < node.length; i++) {
        tItems[i] = [];
        if (tItems[i]) {
            tItems[i].push(carrousel_getXmlNodeValue(node[i],"image"));
            tItems[i].push(carrousel_getXmlNodeValue(node[i],"url"));
            tItems[i].push(carrousel_getXmlNodeValue(node[i],"theme"));
            tItems[i].push(carrousel_getXmlNodeValue(node[i],"title"));
            tItems[i].push(carrousel_getXmlNodeValue(node[i],"description"));
        }
    }
    
    if ( node.length <= 1 ){
        document.getElementById('hpo_carrousel_control_'+iNum).style.visibility = 'hidden';
    } else {
        document.getElementById('hpo_carrousel_control_'+iNum).style.visibility = 'visible';
    }
    
    return tItems;
}

function opacity(id, opacStart, opacEnd, millisec, iNum) {
    //speed for each frame
    var speed = Math.round(millisec / 50);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "', " +iNum+ ")",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "', " +iNum+ ")",(timer * speed));
            timer++;
        }
    }
}

function carrousel_showItem(num, bFade, iNum) {
    if (!gtItems[iNum]){
        return null;
    }
    gNumImage[iNum] = (num == gtItems[iNum].length ? 0: num);
    
    if (bFade) {
        gFading[iNum] = true;
        opacity("hpo_carrousel_image_"+iNum, 100, 0, 200, iNum);
    }
    else {
        gFading[iNum] = true;
        //opacity("CARROUSEL_IMG", 0, 100, 0);
        
        var href = gtItems[iNum][gNumImage[iNum]][1];
        var theme = gtItems[iNum][gNumImage[iNum]][2];
        var title = gtItems[iNum][gNumImage[iNum]][3];
        var description = gtItems[iNum][gNumImage[iNum]][4];
        var src = gDirectoryPath;
        
        if (src != ""){
            src += '/' + gtItems[iNum][gNumImage[iNum]][0];
        } else {
            src = gtItems[iNum][gNumImage[iNum]][0];
        }
        
        document.getElementById("hpo_carrousel_image_"+iNum).innerHTML = src;
        if (document.getElementById("hpo_carrousel_theme_"+iNum)){
            document.getElementById("hpo_carrousel_theme_"+iNum).innerHTML = theme;
        }
        if (document.getElementById("hpo_carrousel_title_"+iNum)) {
            document.getElementById("hpo_carrousel_title_"+iNum).innerHTML = title;
        }
        if (document.getElementById("hpo_carrousel_header_"+iNum)) {
            document.getElementById("hpo_carrousel_header_"+iNum).innerHTML = description;
        }
        if (document.getElementById("hpocarrousel_indice_"+iNum)) {
            document.getElementById("hpocarrousel_indice_"+iNum).innerHTML = (gNumImage[iNum]+1)+"/"+(gtItems[iNum].length);
        }
        
        for (i = 0; i < gtItems[iNum].length; i++) {
            if (document.getElementById("hpo_carrousel_item_"+iNum+"_"+i)){
                document.getElementById("hpo_carrousel_item_"+iNum+"_"+i).className = "hpo_carrousel_item";
            }
            if (document.getElementById("hpo_carrousel_fd_"+iNum+"_"+i)){
                document.getElementById("hpo_carrousel_fd_"+iNum+"_"+i).className = "hpo_carrousel_item";
            }
        }
        if (document.getElementById("hpo_carrousel_item_"+iNum+"_"+gNumImage[iNum])){
            document.getElementById("hpo_carrousel_item_"+iNum+"_"+gNumImage[iNum]).className = "hpo_carrousel_item over";
        }
        if (document.getElementById("hpo_carrousel_fd_"+iNum+"_"+gNumImage[iNum])){
            document.getElementById("hpo_carrousel_fd_"+iNum+"_"+gNumImage[iNum]).className = "hpo_carrousel_item over";
        }
        
    }
}

function carrousel_onChangeXhr(iNum) {
    if(gXhr[iNum] && gXhr[iNum].readyState == 4 && gXhr[iNum].status == 200 && gXhr[iNum].responseXML !== null) {
        var xml = gXhr[iNum].responseXML.documentElement;
        
        //gtItems[iNum] = new Array();
        
        gNumImage[iNum] = 0;
        gtItems[iNum] = carrousel_getItems(xml,iNum);
        
        if (gtItems[iNum] && gtItems[iNum].length >= 0) {
            carrousel_showItem(0, true, iNum);
            if ( gtItems[iNum] && gtItems[iNum].length > 1 && gTimeout[iNum]>0){
                gtoCarrousel[iNum] = setTimeout("carrousel_timeout("+iNum+");",gTimeout[iNum]);
            }
        }
        
        gXhr[iNum] = null;
    }
}

function carrousel_onLoadXhr(xmlPath, directoryPath, iNum) {
    var param = "";
    gDirectoryPath = directoryPath;
    
//        opacity("CARROUSEL_IMG", 100, 0, 200);
    
    if ( gtoCarrousel[iNum] )
    {
        clearTimeout(gtoCarrousel[iNum]);
        gtoCarrousel[iNum] = null;
    }
    
    if (gXhr[iNum] == null) {
        gXhr[iNum] = carrousel_getXhr();
        
        if (gXhr[iNum] != null) {
            try {
                d = new Date();
                xmlPath = xmlPath +'?'+ d.getTime();
                
                gXhr[iNum].open("GET", xmlPath, true);
                gXhr[iNum].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                gXhr[iNum].onreadystatechange = function (){
                    carrousel_onChangeXhr(iNum);
                };
                gXhr[iNum].send(param);
            }
            catch (e) {
                alert("erreur : " + e);
            }
        }
        else {
            setTimeout("carrousel_onLoadXhr('" + xmlPath + "', '"+ directoryPath + "',"+iNum+");",1000);
        }
    }
}

function carrousel_timeout(iNum) {
    //if (document.getElementById("hpo_carrousel_item_"+iNum+"_"+gNumImage[iNum]))
    //    document.getElementById("hpo_carrousel_item_"+iNum+"_"+gNumImage[iNum]).className = "hpo_carrousel_item";
    carrousel_showItem(gNumImage[iNum] + 1, true, iNum);
    if ( gtItems[iNum] && gtItems[iNum].length > 1  && gTimeout[iNum]>0){
        gtoCarrousel[iNum] = setTimeout("carrousel_timeout("+iNum+");",gTimeout[iNum]);
    }
}


function carrousel_pause(iNum) {
   if (gtoCarrousel[iNum]) {
        clearTimeout(gtoCarrousel[iNum]);
        gtoCarrousel[iNum] = null;
   }
}

function carrousel_play(iNum) {
    clearTimeout(gtoCarrousel[iNum]);
    gtoCarrousel[iNum] = null;
    if (gTimeout[iNum]>0) {
        gtoCarrousel[iNum] = setTimeout("carrousel_timeout("+iNum+");",gTimeout[iNum]);
    }
}

function carrousel_nextItem(iNum) {
    if (gtoCarrousel[iNum]) {
        clearTimeout(gtoCarrousel[iNum]);
        gtoCarrousel[iNum] = null;
    }
    
    carrousel_showItem(gNumImage[iNum] + 1, true, iNum);
    //if ( gtItems[iNum] && gtItems[iNum].length > 1 )
    //    gtoCarrousel[iNum] = setTimeout("carrousel_timeout("+iNum+");",gTimeout[iNum]);
}

function carrousel_prevItem(iNum) {
    if (gtoCarrousel[iNum]) {
        clearTimeout(gtoCarrousel[iNum]);
        gtoCarrousel[iNum] = null;
    }
    
    if (gNumImage[iNum] == 0) {
        gNumImage[iNum] = gtItems[iNum].length;
    }
    
    carrousel_showItem(gNumImage[iNum] - 1, true, iNum);
    //if ( gtItems[iNum] && gtItems[iNum].length > 1 )
    //    gtoCarrousel[iNum] = setTimeout("carrousel_timeout("+iNum+");",gTimeout[iNum]);
}

function carrousel_opac(opacity_, id)
{
    var object = document.getElementById(id).style;
    
    object.opacity = (opacity_ / 100);
    object.MozOpacity = (opacity_ / 100);
    object.KhtmlOpacity = (opacity_ / 100);
    object.filter = "alpha(opacity=" + opacity_ + ")";
    
}

function changeOpac(opacity_, id, iNum) {
    
    carrousel_opac(opacity_, id);
    
    if (gFading[iNum] && (opacity_ == 0)) {
        var href = gtItems[iNum][gNumImage[iNum]][1];
        var theme = gtItems[iNum][gNumImage[iNum]][2];
        var title = gtItems[iNum][gNumImage[iNum]][3];
        var description = gtItems[iNum][gNumImage[iNum]][4];
        var src = gDirectoryPath;
        if (src != ""){
            src += '/' + gtItems[iNum][gNumImage[iNum]][0];
        } else {
            src = gtItems[iNum][gNumImage[iNum]][0];
        }
            
        document.getElementById("hpo_carrousel_image_"+iNum).innerHTML = src;
        if (document.getElementById("hpo_carrousel_theme_"+iNum)){
            document.getElementById("hpo_carrousel_theme_"+iNum).innerHTML = theme;
        }
        if (document.getElementById("hpo_carrousel_title_"+iNum)){
            document.getElementById("hpo_carrousel_title_"+iNum).innerHTML = title;
        }
        if (document.getElementById("hpo_carrousel_header_"+iNum)){
            document.getElementById("hpo_carrousel_header_"+iNum).innerHTML = description;
        }
        if (document.getElementById("hpocarrousel_indice_"+iNum)){
            document.getElementById("hpocarrousel_indice_"+iNum).innerHTML = (gNumImage[iNum]+1)+"/"+(gtItems[iNum].length);
        }
        
        for (i = 0; i < gtItems[iNum].length; i++) {
            if (document.getElementById("hpo_carrousel_item_"+iNum+"_"+i)){
                document.getElementById("hpo_carrousel_item_"+iNum+"_"+i).className = "hpo_carrousel_item";
            }
            if (document.getElementById("hpo_carrousel_fd_"+iNum+"_"+i)){
                document.getElementById("hpo_carrousel_fd_"+iNum+"_"+i).className = "hpo_carrousel_item";
            }
        }
        
        if (document.getElementById("hpo_carrousel_item_"+iNum+"_"+gNumImage[iNum])){
            document.getElementById("hpo_carrousel_item_"+iNum+"_"+gNumImage[iNum]).className = "hpo_carrousel_item over";
        }
        if (document.getElementById("hpo_carrousel_fd_"+iNum+"_"+gNumImage[iNum])){
            document.getElementById("hpo_carrousel_fd_"+iNum+"_"+gNumImage[iNum]).className = "hpo_carrousel_item over";
        }
        gFading[iNum] = false;
        
        opacity("hpo_carrousel_image_"+iNum, 0, 100, 200, iNum);
    }
}

function carrousel_link(link)
{
    for ( i=0; true; ++i )
    {
        var l = document.getElementById('hpo-carrousel-link-'+ i);
        
        if ( !l ){
            break;
        }
            
        l.className = 'Nselected';
    }
            
    link.className = 'selected';
}
