//Author Brannon Beames Aug 2007

//Declare and set some variables
var type;
var interval = 5000;
var random_display = 0;
var imageArray;
var imageNum = 0;
var totalImages = 0;
var image_dir;
var timerID;
var paused = false;

//function called on page load
//sets the divs off, looks for a cookie 
//turns divs on depending on the cookie
function noDiv() {
   //sets both divs display to off
   $('weather_pic').style.display = 'none';
   $('optical_pic').style.display = 'none';

   //gets the cookie 
   var ImgCookie = getCookie("pic_type");
   
   //if cookie is set calls the switchDiv to turn the div display on
   //else to defaults to the weather_pic div
   if (ImgCookie) {
      switchDiv(ImgCookie);
   } else {
      switchDiv('weather_pic');
   }
}

//turn the divs on or off, clears the timer, and sets the cookie accordingly
function switchDiv(picType) {

   //resets the timer 
   clearTimeout(timerID);

   if (picType == 'weather_pic') {
      $('weather_pic').style.display = 'inline';
      $('optical_pic').style.display = 'none';
   }
   if (picType == 'optical_pic') {
      $('optical_pic').style.display = 'inline';
      $('weather_pic').style.display = 'none';
   }

   //set the cookies and images to rotate
   cookies(picType);
   rotate(picType);
}

//set cookie function, deletes the old and sets a new for 30 days 
//get, set, and deleteCookie is found in /support/cookie.js
function cookies(picType) {
   deleteCookie("pic_type");
   setCookie("pic_type", picType, 30);
}

//function is called by switchDiv 
//array is set and the img set to be used is determined
function rotate(picType) {
   //set the value for the loop each time the function is called
   var i = 0;


   //if the weather_pic has been selected or the cookie is set to it
   if (picType == 'weather_pic') {
      image_dir = "img/Weather_index/";//set the path
      type = "id_weather";//id that will be used
      imageNum = 0;//image to start with
      imageArray = 0;//array's starting index
      imageArray = new Array();//initialize array

      //fills the array
      for (i=1; i<=26; i++) {
         imageArray[imageNum++] = new imageItem(image_dir + i + ".jpg");
      }
   }
   //if the optical_pic has been selected or the cookie is set to it
   if (picType == 'optical_pic') {
      image_dir = "img/Optics/";//set the path
      type = "id_optical";//id that will be used
      imageNum = 0;//image to start with
      imageArray = 0;//array's starting index
      imageArray = new Array();//initialize array

      //fills the array
      for (i=1; i<=56; i++) {
         imageArray[imageNum++] = new imageItem(image_dir + i + ".jpg");
      }
   }

   //sets that array lenth and starts the rotation through the images
   totalImages = imageArray.length;
   switchImage(type);
}

//called by rotate()
//sets a image for each element in the array in rotate()
function imageItem(image_location) {
   this.image_item = new Image();//new image
   this.image_item.src = image_location;//location
}

//called by index.php
//sets the paused boolean var
//var paused_or_play = true if paused, false if play.
function pausePushed(pause_or_play) {
   var playimg = document.getElementById("play");
   var pauseimg = document.getElementById("pause");
   if (pause_or_play == "pause") {
      paused = true;
      pauseimg.src = "./img/pause_clicked.png";
      playimg.src = "./img/play.png";
   } else {
      paused = false;
      pauseimg.src = "./img/pause.png";
      playimg.src = "./img/play_clicked.png";
   }
}

//called by rotate
//This is the function that controls the rotation of the images
function switchImage(place) {
   if (paused == false) {
   var new_image = getNextImage();//determines starting place
   var img = document.getElementById(place);//get the id that will be used i.e. id_weather or id_optical see the html
   
   //set the id to the img or show a error
   if (img) {
      img.src = new_image;//div to this image
   } else {
      //debug
      alert(place + " does not exist can't rotate images");
   }
   }
   //the above if repaced this due to IE errors
   //document[place].src = new_image;
   var recur_call = "switchImage('"+place+"')";//recur_call will evalute to something like switch("id_weather")
   
   //starts the time are recalls switchImage() at the set interval
   //interval is set at the top of the page
   timerID = setTimeout(recur_call, interval);
}

//sets the starting place for the image rotation in the array
//also returns the current position next position ?
function getNextImage() {
   if (random_display) {
      imageNum = randNum(0, totalImages-1);
   } else {
      imageNum = (imageNum+1) % totalImages;
   }
   
   var new_image = get_ImageItemLocation(imageArray[imageNum]);
   return(new_image);
}

//called by getNextImage if a random number is desired
//this currently not being used
function randNum(x, y) {
   var range = y - x + 1;
   return Math.floor(Math.random() * range) + x;
}

//returns the item location ?
function get_ImageItemLocation(imageObj) {
   return(imageObj.image_item.src)
}

