/*
    :::
     |  Author: Jared Harrington <jared (_at_) dailydna.com>
     |  Created: June 15, 2006
     |  Last modified: June 21, 2006
    ::: 
    
    :::
     |  How it works:
     |  only two lines of html needed, it needs an image with an ID called "slideshow"
     |  the hidden input keeps track of the next image to load going off
     |  the array index numbe
    :::


    <img src="images/slideshow/slideshow_loading.gif" id="slideshow"><br>
    <input type="hidden" id="slideshow_id" value="0">
    
    ::: Call it by
    
    <script language="javascript">
    //images in an array, can be fed by a php script or whatever
    images = new Array('image.jpg', 'image02.jpg', image03.jpg', 'etc.jpg');
    swapIt('',1);
    </script>
    
    :::
     |  Then you'll need the controls to start playback, if you want it to
     |  autoplay, just leave both values blank -> swapIt('', '');
     |  Please don't rip this off, but if you do a least give me credit
    ::: 
*/


//------- fade functions: http://clagnut.com/sandbox/imagefades/ --------
function initImage(imageId) {
  image = document.getElementById(imageId);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  fadeIn(imageId,0);
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+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 (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 70);
    }
  }
}

//------- end fade functions --------




//this function does the actual slideshow
function swapIt(i,no_play) {
    swapout = document.getElementById('slideshow');
    swapout_id = document.getElementById('slideshow_id');
    i=parseInt(swapout_id.value); //should the current image, default is 0
    total = images.length;


    swapout.src = images[i];    //changes image
    initImage('slideshow');     //fades in image

    i=i+1;                      //increments the counter
    if(i == total) i=0;         //when it reaches the last image, start over
    swapout_id.value = i;       //updates id

    //if no_play is set, don't start the refresh
    if(!no_play && total != 1) player = setTimeout("swapIt("+i+")",5000); // refresh every x milli secs
    else player = null;


}


//------- control functions --------

function start_swapIt() {
    control = document.getElementById('controls');
    control_image = document.getElementById('controls_img');

    control.href = "javascript:stop_swapIt();";
    //changes play to pause and image properties
    control_image.src = "images/slideshow/slideshow_pause.gif";
    control_image.alt = "Click to Pause";
    control_image.title = "Click to Pause";

    //starts in half a second
    setTimeout("swapIt()",500);

}

function stop_swapIt() {
    control = document.getElementById('controls');
    control_image = document.getElementById('controls_img');

    control.href = "javascript:start_swapIt();";
    control_image.src = "images/slideshow/slideshow_play.gif";
    control_image.alt = "Click to Play";
    control_image.title = "Click to Play";

    clearTimeout(player);
}

//previous and next image
function move_swapIt(dir) {
    swapout_id = document.getElementById('slideshow_id');
    total = images.length;
    new_id = 0;

    stop_swapIt();

    if(total > 1) {
        if(dir == "-") {
            //remember, the form has moved to the next image, so to go back, it must be -2
            new_id = parseInt(swapout_id.value) - 2;

            //last image
            if(new_id == -1) {
                new_id = total-1; //moves to the last image if the backed up off the first
            }
            //on the last image, back it up to one before
            if(new_id == -2) new_id = total-2;
            swapout_id.value = new_id;
        }

        if(dir == "+") {
            new_id = parseInt(swapout_id.value);
            if(new_id >= total) new_id = 0; //moves to the first image if on the last image
            swapout_id.value = new_id;
        }

        //since the form value was updated, call swapIt to show the new image
        swapIt(new_id,'no');
    }
}

//------- end control functions --------
