/**
 * @author jeremy DNA
 */
//declare javascript global variables
var image_arr = new Array();
var image_names = new Array();
var image_tag;
var num_img; //number of total images
var wait_time; //amount of time between the slideshow
var interrupt = 0;
var cur_index;
var selected_index;
var names_tag;
var last_index;
var active_color; //color of active link
var default_color; //color of default link

//toggles the rowName provided
//removes information in the aName and replaces with the current operation
function toggle(aName, rowName)
{
   $('#'+rowName).slideToggle("slow", function(){
      switch($('#'+aName).attr("name")) //simply swaps values - this take care of making a pretty link name change
      {
         case "add":    
            var newInfo="Remove Name";
            var newName="remove";
            break;
         case "remove":
            var newInfo="Add Name";
            var newName="add";
            remove(aName);
            break;
      }
      //change the name attribute
      $('#'+aName).attr("name", newName);
      
      //fade out the old link - fade in the new
      $('#'+aName).fadeOut("slow", function(){
         $('#'+aName).empty();
         $('#'+aName).append(newInfo);
         $('#'+aName).fadeIn("fast");
      });
   });
}

function remove(aName) //empties the contents of the name select box and the specified-name text field
{
   aName = aName.split("-"); //second value of aName is the id of the image
   var id= aName[1];
   $('#specified_name-'+id).val("");
   $('#name-'+id).val("");
}

function enableSlideShow(image_str,img_names,nme_tag,img_tag,act_color,def_color,wait)
{
   if(image_str!=0)
   {
      //set the image array and image_tag variables
      image_arr = image_str.split("||");
      image_names = img_names.split("||");
      num_img = Number(image_arr.shift()); //first element is the number of images
      image_tag = img_tag;
      names_tag = nme_tag;
      wait_time = wait;
      active_color = act_color;
      default_color = def_color;
      
      //load all of the images
      for(i in image_arr)
      {
         $(new Image()).load().attr('src',image_arr[i]);      
      }
      
      //start the slideshow
      cur_index = 0;
      slideShow(0);
   }
   else
   {
      $("#slideshow_container").hide();
   }
}

//this takes care of replacing the img tag specified
function replaceImg(cur_img, image_index)
{
   $('#'+image_tag).fadeOut("slow", function(){
      $('#'+image_tag).attr("src", cur_img);
      $('#' + image_tag).fadeIn("slow");
   });
}

//this takes care of replacing the name tag specified
function replaceName(image_index)
{
      //alert(image_names[image_index]);
   $('#'+names_tag).fadeOut("fast", function(){
      $('#'+names_tag).empty();
      $('#'+names_tag).append(image_names[image_index]);
      $('#'+names_tag).fadeIn("fast");
   });
}

function link_background(new_index) //swaps the background color and link color
{
   $('#slideshow-'+new_index).css("background-color", active_color);
   $('#slideshow-'+new_index+" a").css("color", default_color);
   $('#slideshow-'+last_index).css("background-color", default_color);  
   $('#slideshow-'+last_index+" a").css("color", active_color);
   
   last_index = new_index;
}

function interruptSlideshow(image_index)
{
      interrupt = 1;
      //replace the current image
      replaceName(image_index);
      replaceImg(image_arr[image_index], image_index);
      //after wait_time restart the slideshow at the next index
      selected_index = image_index;
      link_background(image_index+1);
      cur_index = selected_index;
      setTimeout('slideShow(' + selected_index + ',' + 1 + ')', wait_time);
}

function slideShow(next_img, reset) //provided an array of images - the slideshow will cycle through them
{  
   if (num_img > 1) {
      if (reset === 1 && selected_index === next_img) {
         interrupt = 0;
         next_img = (next_img + 1 >= num_img) ? 0 : next_img + 1;
      } //continue the slideshow after they select a new image
      if (interrupt === 0) {
         cur_index = next_img;
         var cur_img = image_arr[next_img];
         next_img++;
         
         if (next_img >= num_img) {
            link_background(next_img);
            next_img = 0;
         }
         else {
            link_background(next_img);
         }
         
         replaceName(cur_index);
         replaceImg(cur_img, next_img);
         setTimeout('slideShow(' + next_img + ')', wait_time);
      }
   }
   else
   {
      cur_index = next_img;
      var cur_img = image_arr[next_img];
      replaceName(cur_index);
      replaceImg(cur_img, next_img);
      link_background(next_img+1);
   }
}
