var SlideShow = {
    arrayFoto : [],
    testo : "",
    startFoto : 1,
    timerID : false,
    timer2ID : false,
    slideSize : [695, 565],
	
    prefetchImages : function ()
    {
        imageObj = [];
        //for(var i=0;i<SlideShow.arrayFoto.length;i++)
        var cont = 0;
        for(var i in SlideShow.arrayFoto)
        {
            imageObj[i] = new Image();
            if(cont == 0)
            {
                imageObj[i].onload = function(e)
                                     {
                                         //SlideShow.startSlide();
                                     }
            }
            imageObj[i].src=SlideShow.arrayFoto[i].urlFoto;
            cont++;
        }
    },
    
	printSlideShow : function(urlDati, startFoto)
    {
        SlideShow.startFoto = startFoto ? startFoto : 0;
        SlideShow.timer2ID = false;
        
        var url = urlDati;
        var http_request = "";
        if (window.XMLHttpRequest) 
            http_request = new XMLHttpRequest();
        else if (window.ActiveXObject) 
            http_request = new ActiveXObject("Microsoft.XMLHTTP");

        http_request.open("GET", url, true);
        http_request.onreadystatechange = function () 
        {
            if (http_request.readyState == 4) 
            {
            if (http_request.status == 200) 
            {
                var data = eval("(" + http_request.responseText + ")");
                var altezza = document.documentElement.clientHeight;
                SlideShow.arrayFoto = data.foto;
                SlideShow.testo = data.testo ? data.testo : "";
                SlideShow.prefetchImages();
                var container = document.createElement('DIV');
                container.style.width = "100%";
                container.style.height = altezza+"px";
                container.style.position = "absolute";
                container.style.top = "0px";
                container.style.left = "0px";
                var viewportSize = getViewportSize();
                var xPos = Math.round((viewportSize[0] - SlideShow.slideSize[0])/2);
                var yPos = Math.round((viewportSize[1] - SlideShow.slideSize[1])/2);
                container.id = "slideShow";
                var html = '<div style="position:relative;filter:alpha(opacity=80);opacity:0.8;width:100%;height:'+altezza+'px;background:#000;z-index:100000;"></div>';
                html += '<div style="z-index:100000;position:absolute;top:0px;width:100%;left:0px;border:0px solid #f00;height:'+altezza+'px">';
                html += '<div id="popup" style="z-index:10000;position:absolute;left:'+xPos+'px;top:'+yPos+'px"><div class="scheda" id="slide" style="opacity:1.0">';
                html += '<a href="#" onclick="SlideShow.closeSlideShow();return false;" title="close"><img src="images/close.gif" alt="close" class="close" /></a>';
                html += '<img id="fotoSlide" src="'+SlideShow.arrayFoto[SlideShow.startFoto].urlFoto+'" alt="" />';
                //html += '<div id="bottom">';
                html += '<div id="bottom">';
                html += '<div id="intestazione">';
                if(data.nomeProdotto)
                    html += '<h1>'+data.nomeProdotto+'</h1>';
                if(data.citta)
                    html += '<span>'+data.citta+'</span>';
                if(data.nomeDesigner)
                    html += '<h2>'+data.nomeDesigner+'</h2>';
                if(data.photo)
                    html += '<div id="photoCredits">Photo: '+data.photo+'</div>';
                html += '</div>';
                //if(data.testo)
                {
                    html += '<div id="descrizione" '+((data.testo)?"":"style='display:none' background-color:transparent;")+' >';
                    html += '<p ><div id="pDescrizione"></div></p>';
                    html += '</div>';
                }
				if(SlideShow.arrayFoto.length>1){
					html += '<div id="player">';
						html += '<a href="#" onclick="SlideShow.showPrev();return false;"><img style="width:14px;height:15px" src="images/rew.png" alt="" /></a>';
						//html += '<a href="#" onclick="SlideShow.stopSlide();return false;"><img style="width:15px;height:15px" src="images/stop.png" alt="" /></a>';
						html += '<span id="datiSlide">'+(SlideShow.startFoto+1)+'/'+SlideShow.arrayFoto.length+'</span>';
						//html += '<a href="#" onclick="SlideShow.startSlide();return false;"><img  style="width:15px;height:15px" src="images/play.png" alt="" /></a>';
						html += '<a href="#" onclick="SlideShow.showNext();return false;"><img style="width:14px;height:15px" src="images/ffw.png" alt="" /></a>';
					html += '</div>';
				}
                html += '</div>';
                html += '</div>';
                html += '</div>';
                html += '</div>';
                container.innerHTML = html;
                document.getElementsByTagName("body")[0].appendChild(container);
                SlideShow.printTestoFoto(SlideShow.startFoto);
                
                if(ie6)
                    correctPNG();
           }
           else
            alert("not found");
           }
        }
        http_request.send(null);
    },
    
    printTestoFoto : function(indice)
    {
        var testo = SlideShow.arrayFoto[indice].testo ? SlideShow.arrayFoto[indice].testo : this.testo;
        document.getElementById("descrizione").style.display = "";
        document.getElementById("pDescrizione").innerHTML = testo;
    },
    
    closeSlideShow : function()
    {
        SlideShow.stopSlide();
        if(SlideShow.timer2ID)
            clearInterval(SlideShow.timer2ID); 
        var container = document.getElementById("slideShow");
        container.parentNode.removeChild(container);
    },
    
    showFoto : function(indice)
    {
        /*var s = SlideShow.arrayFoto[indice];
        swap(s);
        /*$("#fotoSlide").fadeOut("fast",function(){
                                    $("#fotoSlide").attr("src",s);
                                    //$("#titolo").html(titolo);
                                    $("#fotoSlide").fadeIn("slow");  
                                 });*/
        var statusSlide = SlideShow.stopSlide();
        //filter:alpha(opacity=80);opacity:0.8
        var value = 9;
        var incremento = -1;
        SlideShow.timer2ID = setInterval(function() {
                SlideShow.printTestoFoto(indice);
                
                document.getElementById("fotoSlide").style.opacity = "0."+value;
                document.getElementById("fotoSlide").style.filter = 'alpha(opacity = '+(value*10)+')';
                value+=incremento;
                if(value==0)
                {
                    incremento = 1;
                    document.getElementById("fotoSlide").display = "none";
                    document.getElementById("fotoSlide").src = SlideShow.arrayFoto[indice].urlFoto;
                    document.getElementById("datiSlide").innerHTML = (SlideShow.startFoto+1)+'/'+SlideShow.arrayFoto.length;
                    document.getElementById("fotoSlide").display = "";
                    
                }
                if(value==9)
                {
                    document.getElementById("fotoSlide").style.opacity = "1.0";
                    document.getElementById("fotoSlide").style.filter = 'alpha(opacity = 100)'; 
                    clearInterval(SlideShow.timer2ID); 
                    SlideShow.timer2ID = false; 
                    if(statusSlide)
                        SlideShow.startSlide();
                }
        },100);
        
    },
    
    showPrev : function()
    {
        if(SlideShow.timer2ID)
            return;
        SlideShow.startFoto--;
        if(SlideShow.startFoto<0)
            SlideShow.startFoto = SlideShow.arrayFoto.length -1;
        SlideShow.showFoto(SlideShow.startFoto);
    },
    
    showNext : function()
    {
        if(SlideShow.timer2ID)
            return;
        SlideShow.startFoto++;

        if(SlideShow.startFoto >= SlideShow.arrayFoto.length)
            SlideShow.startFoto = 0;
        SlideShow.showFoto(SlideShow.startFoto);
    },
    
    startSlide : function()
    {
        SlideShow.timerID = setInterval(SlideShow.showNext,3000);
    },
    
    stopSlide : function()
    {
        if(!SlideShow.timerID)
            return false;
        clearInterval(SlideShow.timerID); 
        SlideShow.timerID = false;
        return true;
    }
}