/***************************
 * ImgLoad.js
 * 2008-01-15
 ***************************/

// How many cameras?
var cameraCount = 2;
 
// Starting Path to find the images
var startPath = "http://imcproductions.csuchico.edu/animation/StudentHousingCAMs/";

// Array to contain the name of the directories containing the camera images
var cameraDir = new Array();
cameraDir[0] = "TehamaCAM";
cameraDir[1] = "ButteCAM";




// Type of images used
var imgType = ".jpg";

////////////////////////////////////////////////////////////////////////////////////////
var startYear = 2008;

//Array to hold the names of the months of the  year
var monthDir = new Array();
monthDir[1] = "january";
monthDir[2] = "february";
monthDir[3] = "march";
monthDir[4] = "april";
monthDir[5] = "may";
monthDir[6] = "june";
monthDir[7] = "july";
monthDir[8] = "august";
monthDir[9] = "september";
monthDir[10] = "october";
monthDir[11] = "november";
monthDir[12] = "december";

//Array to store what kind of image the day on the calendar displays (blank, red or black)
var dayColorDir = new Array();

var img = new Image();

var imgSrc;
var imgID = "picOfTheDay";
var monthID = "calendarMonth";
var yearID = "calendarYear";

var cameraNum = 0;

var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var monthStr = (month < 10) ? "0" + month : month;
var day = today.getDate();
var dayStr = (day < 10) ? "0" + day : day;
var theFirst;

// ==> TMS 20090821 ==> //
var selectedDay = day;
var selectedMonth = month;
var selectedYear = year;
// <== TMS 20090821 <== //
theFirst = new Date();
theFirst.setFullYear(year);
theFirst.setMonth(month-1);
theFirst.setDate(1);


//Moved from showDays function by ALAN
var offset;

//resetDate();


//Gets todays date and sets all time variables accordingly
function resetDate() {
  today = new Date();
  year = today.getFullYear();
  month = today.getMonth() +1;
  if (month < 10) { monthStr = "0" + month; } else { monthStr = month; }
  day = today.getDate();
  if (day < 10) { dayStr = "0" + day; } else { dayStr = day; }

  theFirst = new Date();
  theFirst.setFullYear(year);
  theFirst.setMonth(month-1);
  theFirst.setDate(1);
  
  loadCameraImage(imgID);
  loadMonthImage(monthID);
  loadYearImage(yearID);
 
  //Code Added By Alan 3/24/08
  showDays();
  //End Code
}


function setCamera(newCamera, relative) {
  if(relative == null) {
    cameraNum = newCamera;
  } else {
    // relative == true
    cameraNum += newCamera;
  }
  cameraNum = enforceBounds(cameraNum, 0, cameraCount-1);
  loadCameraImage(imgID);
}

function setDay(newDay, relative) {
  if(relative == null) {
    day = newDay;
  }else {
    // relative == true
    day += newDay;
  }
  
  //Code added By Alan 2/13/2008
  if (month == 1 && year == startYear && newDay==-1 && day == 0) {
       day = 1;
  }
  else if (day == 0 && newDay == -1) {
    setMonth(-1, true);
	day = daysInMonth(month,year);
  }
  if (day == daysInMonth(month,year) +1 && newDay == 1){
     day = 1;    
     setMonth(1, true);
    
  }
  //End Code
  
  if (year == today.getFullYear() && month == today.getMonth()+1) {
    day = enforceBounds(day, 1, today.getDate());
  } else {
    day = enforceBounds(day, 1, daysInMonth(month,year));
  }
  if (day < 10) { 
    dayStr = "0" + day; 
  } else {
    dayStr = day; 
  }
  //Code Added by ALAN 2/18/08
  // ==> TMS 20090821 ==> //
  selectedDay = day;
  selectedMonth = month;
  selectedYear = year;
  // <== TMS 20090821 <== //
  showDays();
  //End code

  loadCameraImage(imgID);
}

function setMonth(newMonth, relative) {
  if(relative == null) {
    month = newMonth;
  } else {
    // relative == true
    month += newMonth;
    
  }

  //Check to see if going back to December from January
  if (month == 0 && year != startYear) {
     month = 12;
     year--;
     setYear(-1, true);
  } else if (month == 0 && year == startYear) {
     month = 1;
  //Check to see if going to January from December
  } else if (month == 13){
     month = 1;
     year++;
     setYear(1, true);
  }

  if(year == today.getFullYear()) {
    month = enforceBounds(month, 1, today.getMonth()+1);
  } else {
    month = enforceBounds(month, 1, 12);
  }
  if (month < 10) { 
    monthStr = "0" + month; 
  } else {
    monthStr = month; 
  }
  loadMonthImage(monthID);
  
  //Code Added By ALAN 2/18/08
  //Check day and change to correct color
  if(month == today.getMonth()+1){
    if (day > today.getDate()){
      day = 1;      
   }
  }
  showDays();
  

  
  //End Code

}

function loadYearImage(id) {
   if (id != undefined && id != ""){
   document.getElementById(id).src = "images/years/" + year + ".png";
   }
}


function loadMonthImage(id) {
   if (id != undefined && id != ""){
   document.getElementById(id).src = "images/months/" + monthDir[month] + ".png";
   }
}


function setYear(newYear, relative) {
  if(relative == null) {
    year = newYear;
  } else {
    // relative == true
    year += newYear;
  }
  year = enforceBounds(year, startYear, today.getFullYear());
  loadYearImage(yearID);
}

function initialYearImageLoad(yearID) {
  this.yearID = yearID;
  loadYearImage(yearID);
}


function initialMonthImageLoad(monthID) {
  this.monthID = monthID;
  loadMonthImage(monthID);
}


function initialLoadImage(imgID) {
  this.imgID = imgID;
  loadCameraImage(imgID);
}


function loadCameraImage(id) {
  // ==> TMS 20090821 ==> //
  if ((selectedDay == today.getDate()) && (selectedMonth == today.getMonth()+1) && (selectedYear == today.getFullYear())) {
  // <== TMS 20090821 <== //
	temp = startPath + cameraDir[cameraNum] + "/" + "Recent" + "/" + "large" + imgType;
  }
  else{
  	temp = startPath + cameraDir[cameraNum] + "/" + year + "/" + monthStr + "/" + dayStr + "/" + "small" + imgType;
  }
  loadImage(id, temp);
}

function loadLargeImage(id) {
  if (day == today.getDate()){
       temp = startPath + cameraDir[cameraNum] + "/" + "Recent" + "/" + "large" + imgType;
  }else{
  temp = startPath + cameraDir[cameraNum] + "/" + year + "/" + monthStr + "/" + dayStr + "/" + "large" + imgType;
  }

  window.open(temp, '_blank', 'height=560, width=800');
}

function loadLargeTehamaRecent(id) {
  temp = startPath + "TehamaCAM/Recent" + "/" + "large" + imgType;
  window.open(temp, '_blank', 'height=560, width=800');
}

function loadLargeButteRecent(id) {
  temp = startPath + "ButteCAM/Recent" + "/" + "large" + imgType;
  window.open(temp, '_blank', 'height=500, width=660');
}


function loadImage(id, src) {
  if (id != undefined && id != "" && id != null && src != "" && src != undefined && src != null){
	var x = document.getElementById(id);
       x.src = src;
  }
}

function loadTimelapse(Num){
   temp = startPath + cameraDir[Num] + "/" + "timelapse.mov";
   window.open(temp, '_blank', 'height=400, width=600');
}

function calculateOffset() {
  theFirst = new Date();
  theFirst.setFullYear(year);
  theFirst.setMonth(month-1);
  theFirst.setDate(1);
         
  offset = theFirst.getDay();
}

function showDays() {
  var tmpSrc;
  var daySrc;

  calculateOffset();

  for (i=0; i<offset; i++) {
    tmpSrc = "images/spacer.gif";

    // CODE ADDED BY ALAN
    daySrc = "";
    dayColorDir[i] = "empty";
    //End Code

    document.getElementById('spot'+(i+1)).style.cursor = "default";

    loadImage("spot"+(i+1), tmpSrc);
  }
 
  for(i=1; i<=42; i++) {
    if(daysInMonth(month,year)+offset >= i) {
      if( ( i > today.getDate() ) && ( month == today.getMonth()+1 ) && ( year == today.getFullYear() ) ) {
        tmpSrc = "images/dayResize/calendarDay" + i + "_black.png";
        dayColorDir[i] = "black";
           
      }else{
      
    
        // if file exists, make red.  Otherwise, make black.
        tmpSrc = "images/dayResize/calendarDay" + i + "_red.png";
        dayColorDir[i] = "red";
	 //BUGS CODE FOR MARCH - ALAN
        //document.getElementById('spot'+(i+offset)).style.cursor = "pointer";
      }
      if( i+offset <= 42 )
      {
        loadImage("spot"+(i+offset), tmpSrc);
      }

    } else {
      tmpSrc = "images/spacer.gif";
      loadImage("spot"+i, tmpSrc);
      dayColorDir[i] = "empty";
    }
  }
 //Code Added by ALAN 2/18/08
  // ==> TMS 20090821 ==> //
  if ((month == selectedMonth) && (year == selectedYear))
  {
    loadImage("spot" + (selectedDay + offset), "images/dayResize/calendarDay" + selectedDay + "_red_highlight.png");
  }
  // <== TMS 20090821 <== //
 //End Code
}

//CODE ADDED BY ALAN
function clickNumber(spotNum){
  if (dayColorDir[spotNum - offset] == "red") {
     setDay(spotNum - offset);
     loadCameraImage(imgID);
  }
}
//End Code     

function enforceBounds(num, low, high) {
  var tempNum;
  
  if(num < low) {
    tempNum = low;
  }  else if(num > high) {
    tempNum = high;
  } else {
    tempNum = num;
  }
  
  return tempNum;
}

function daysInMonth(m,y) {
  var days = [31,28,31,30,31,30,31,31,30,31,30,31];
  if (m != 2) return days[m - 1];
  if (y%4 != 0) return days[1];
  if (y%100 == 0 && y%400 != 0) return days[1];
  return days[1] + 1;
}
