var autoPlaySpeed = 3000;
var currentImage=0;
var images = new Array();
var playing=false;
var timer //For canceling a setTimeout
var controlTimer

ns4 = (document.layers);
ns6 = (!document.all && document.getElementById);
ie4 = (document.all && !document.getElementById && !window.opera);
ie5 = (document.all && !document.fireEvent && !window.opera);
op7 = (window.opera && document.createComment) ;
w3dom = (document.getElementById || op7); 

function updateYpp()
{
    if ((images[currentImage] != null) &&
        (images[currentImage].id != null))
    {
        newImgID = images[currentImage].id;
        newImgURL = images[currentImage].src;
        newImgURL = newImgURL.replace(/&#47;/g, '/');
        if(document.getElementById('pw-viewshared-ypp') != null)
        {
            yppDivOuter = document.getElementById('pw-viewshared-ypp');
            if (yppDivOuter.getElementsByTagName("div") == null || yppDivOuter.getElementsByTagName("div").length == 0)
            	return;
            
            yppDivInner =  yppDivOuter.getElementsByTagName("div")[0];

            //change link on anchor
            yppAnchor = yppDivInner.getElementsByTagName("a")[0];
            idIdx = yppAnchor.href.indexOf('photoId=');
            yppAnchor.href = yppAnchor.href.substr(0,(idIdx+8)) + newImgID;

            //change image url
            yppImages = yppAnchor.getElementsByTagName("img");
            for (i = 0; i < yppImages.length; i++)
                if (yppImages[i].className == 'photo')
                    yppImages[i].src = newImgURL;
        }
    }
}

function trans(outImg,inImg)
{
    if(document.getElementById('img'+outImg.id.substring(8))!=null)
    {
        document.getElementById('img'+outImg.id.substring(8)).style.border="0px solid #000000";
        document.getElementById('img'+inImg.id.substring(8)).style.border=selectedThumbBorder;
        document.getElementById('imageCount').innerHTML = currentImage + 1; 
    }
    document.getElementById('captionsBlurb').innerHTML = images[inImg.id.substring(8)].caption;
    if(document.getElementById("buy")!=null)
    {
        document.getElementById("buy").innerHTML = ((images[inImg.id.substring(8)].bought)?"BOUGHT":"BUY THIS PRINT");
    }
    if(outImg.id != inImg.id)
    {
        fadeOut(outImg.id, 100);
        initImage(inImg.id);
    }
    updateYpp();
} 

function showControls()
{
    var cont = document.getElementById("controls"); 
    if(cont!=null)
    {
        cont.style.visibility="visible";
        pw.dnd.controller.elements.speedSlider.show();
    }
}

function hideControls()
{
    var cont = document.getElementById("controls"); 
    if(cont!=null)
    {
        cont.style.visibility="hidden";
        pw.dnd.controller.elements.speedSlider.hide();
    }
}

function play()
{
    clearTimeout(timer);
    playing=true;
    timer = window.setTimeout("next()", autoPlaySpeed);
}

function pause()
{
    clearTimeout(timer);
    playing=false;
}

function gotoImg(inImg)
{
    clearTimeout(timer);
    thePhoto = document.getElementById("thephoto"+currentImage);
    thePhoto2 = document.getElementById("thephoto"+inImg);

//    if(!(thePhoto.style.visibility=="visible" && thePhoto2.style.visibility=="visible"))
//    {
        currentImage=inImg;
        trans(thePhoto, thePhoto2);
//    }
}


function initImage(imageId) {
//  imageId = 'thephoto';
  image = document.getElementById(imageId);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  fadeIn(imageId,0);

}

function setOpacity(obj, opacity) {
    
    if(obj==null)
        return;
  
  // IE/Win
  if (typeof obj.style.filter !== "undefined")
  {
    // IE 6/7 alpha filter has a bug with JPEGs that contain true black pixels;
    // they are rendered as transparent. We have a parent with a black background
    // to "fixup" the transparent pixels and keep them black.
    // zoom is just one of those properties that gives an element layout which is
    // necessary for the alpha filter
    obj.parentNode.style.zoom = "1";
    obj.parentNode.style.backgroundColor = "#000000";
    obj.parentNode.style.display = opacity > 0 ? "" : "none";
    
    if (opacity == 100)
    {
      obj.style.filter = "alpha(enabled:false)";
      obj.parentNode.style.filter = "alpha(enabled:false)";
    }
    else
    {
      obj.style.filter = "alpha(opacity:"+opacity+")";
      obj.parentNode.style.filter = "alpha(opacity:"+opacity+")";
    }
  }
  else
  {
    opacity = (opacity == 100)?99.999:opacity;
  }
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}



function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if(!doFade && opacity<100)
        opacity=100;
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += fadeIncrease;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", fadeSpeed);
    }
  }
}

function fadeOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if((op7 || !doFade) && opacity>0) //BECAUSE OPERA DOESN'T DO OPACITY CORRECTLY
        opacity=0;
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      opacity -= fadeIncrease;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", fadeSpeed);
    }
    else if(opacity<=0)
    {
//        obj.style.visibility="hidden";
    }
  }
}

function sm_prev()
{
    clearTimeout(timer);

    if(currentImage>0)
    {
        thePhoto = document.getElementById("thephoto"+currentImage);
        thePhoto2 = document.getElementById("thephoto"+(currentImage-1));
        currentImage--;
        trans(thePhoto, thePhoto2);
    }
    else if(currentImage==0)
    {
        thePhoto = document.getElementById("thephoto"+currentImage);
        thePhoto2 = document.getElementById("thephoto"+(images.length-1));
        currentImage = images.length-1;
        trans(thePhoto, thePhoto2);
    }
}

function sm_next()
{

    if(currentImage<images.length-1)
    {
        thePhoto = document.getElementById("thephoto"+currentImage);
        thePhoto2 = document.getElementById("thephoto"+(currentImage+1));
    }
    else if(currentImage==images.length-1)
    {
        thePhoto = document.getElementById("thephoto"+currentImage);
        thePhoto2 = document.getElementById("thephoto"+(0));
        currentImage = -1;
    }

    if(currentImage==images.length-1)
    {
        togglePlay('pause');
//        return;
    }

    currentImage++;
    trans(thePhoto, thePhoto2);
    if(playing)
    {
        if(currentImage+1<images.length)
            timer = window.setTimeout("next()", autoPlaySpeed);
        else
            togglePlay('pause');
    }
}

function prev()
{
    clearTimeout(timer);
    
    if(window.SSL)
    {
        if(!SSL.started) 
        {
            if(playing) playing = !playing;
            togglePlay('pause');
            SSL.started = true;
            SSL.outputSomething += "(inside not started)";
        }
        
        // SlideShowLarge
        var bufferIndex = SSL.bufferIndex;//(currentImage%SSL.bufferLength);
        
        if( SSL.loadImagesDynamically && bufferIndex==SSL.boundPrev )
        {
            SSL.loadNewImages(currentImage-SSL.BOUND_SIZE_PREV, -1);
        }
        
        if(bufferIndex > 0)
        {
            thePhoto  = document.getElementById("thephoto"+bufferIndex);
            thePhoto2 = document.getElementById("thephoto"+(bufferIndex-1));
        }
        else if(bufferIndex == 0)
        {
            thePhoto  = document.getElementById("thephoto0");
            thePhoto2 = document.getElementById("thephoto"+(SSL.bufferLength-1));
        }
        
        currentImage--;
        if(currentImage < 0)
            currentImage = SSL.numImages + currentImage;
        
        SSL.bufferIndex--;
        if(SSL.bufferIndex < 0)
            SSL.bufferIndex = SSL.bufferLength + SSL.bufferIndex;
        
        trans(thePhoto, thePhoto2);
    }
    else
    {
        sm_prev(); // small slide show
    }
}

function next()
{
    // this is true for SlideShowLarge
    if(window.SSL)
    {
        if(!SSL.started) 
        {
            if(playing) playing = !playing;
            togglePlay('pause');
            SSL.started = true;
            SSL.outputSomething += "(inside not started)";
        }
        
        var bufferIndex = SSL.bufferIndex;
        
        if( SSL.loadImagesDynamically && bufferIndex==SSL.boundNext )
        {
            // add to the buffer if we're near the end
            SSL.loadNewImages(currentImage+SSL.BOUND_SIZE_NEXT, 1);
        }
        
        if(bufferIndex < SSL.bufferLength-1)
        {
            thePhoto  = document.getElementById("thephoto"+bufferIndex);
            thePhoto2 = document.getElementById("thephoto"+(bufferIndex+1));
        }
        else if(bufferIndex == SSL.bufferLength-1)
        {
            thePhoto  = document.getElementById("thephoto"+bufferIndex);
            thePhoto2 = document.getElementById("thephoto0");
        }
        
        // increment counters
        currentImage++;
        currentImage %= SSL.numImages;
        SSL.bufferIndex = (SSL.bufferIndex + 1) % SSL.bufferLength;
        
        if(currentImage == SSL.numImages-1)
        {
            togglePlay('pause');
        }
        
        trans(thePhoto, thePhoto2);
        if(playing)
        {
            if(currentImage < (SSL.numImages - 1))
            {
                timer = window.setTimeout("next()", autoPlaySpeed);
            }
            else
            {
                togglePlay('pause');
            }
        }
    }
    else
    {
        sm_next(); // small slide show
    }
}
