//
// images.js
// Copyright 2006 Tomoko Kinoshita and Stuart Jones
// SAJ  2006.07.10  Added fade in and out
// SAJ 2006.07.27 Consolidated code and added page numbering for 24
//

//
// Global variables
//
var photoOpacity = 100;
var continuefadeout = true;

//
// registerHandlers()
// Assign relevant bits and bobs to various elements on page load
//
function registerHandlers(){
  if (document.getElementById("thumbnails")){
    var t = document.getElementById("thumbnails");
    var tds = t.getElementsByTagName("IMG");
    for (var i = 0; i < tds.length; i++){
      // Assign event handlers
      tds[i].onclick = function() {        
        // Start fading out, and load the next image
        loadImage(getMainPhotoPath(this.src));
      }
      tds[i].onmouseover = function() {
        this.style.cursor = "pointer";
        this.style.border =  "1px solid gray";
      }
      tds[i].onmouseout = function() {
        this.style.border =  "1px solid #fff";
      }
    }
    // Set the photo to that of the first thumbnail
    getPhoto().onload = photoLoaded;
    getPhoto().src = getMainPhotoPath(tds[0].src);
  } else {
    if (getPhoto() != null){
      getPhoto().onload = photoLoaded;
    }
  }
}

//
// getMainPhotoPath(thumbPath)
// Derive the path of a photo from its thumbnail
//
function getMainPhotoPath(thumbPath){
  var k = thumbPath.indexOf('t_');
  var s = '';
  if (k > 1){
    s = thumbPath.substring(0, k);
    s = s + thumbPath.substring(k + 2);
  }
  return s;
}

//
// getPhoto()
// Return the IMG object that is the main photo on a page
//
function getPhoto(){
  if (document.getElementById("photo"))
    return document.getElementById("photo");
  else
    return null;
}

//
// setPhoto(url)
// Assign the main photo's source as url 
//
function setPhoto(url){
  getPhoto().src = url;
}

//
// photoLoaded()
// Start the fade in in a separate thread
//
function photoLoaded(){
	setTimeout("fadein()", 20);
}

//
// fadeIn()
//
function fadein(){
	if (photoOpacity < 100){
		continuefadeout = false;
		photoOpacity += 5;
	  var p = getPhoto();
		p.style.opacity = (photoOpacity / 100);
		// p.style.MozOpacity = (photoOpacity / 100);
		p.style.filter = "alpha(opacity=" + photoOpacity + ")";
		setTimeout("fadein()", 20);
	}
}

//
// fadeOut()
//
function fadeout(){
	if ((photoOpacity > 0) && (continuefadeout == true)){
    var p = getPhoto();
		photoOpacity -= 5;
		p.style.opacity = (photoOpacity / 100);
		// p.style.MozOpacity = (photoOpacity / 100);
		p.style.filter = "alpha(opacity=" + photoOpacity + ")";
		setTimeout("fadeout()", 20);
	}
}

function loadImage(url){
  // Start fading out, and load the next image
  continuefadeout = true;
  var p = getPhoto();
  setTimeout("fadeout()", 20);
  setTimeout("setPhoto('" + url + "')", 20);
}

// Code for Twenty Four image rotation
var t4curr = 0;
var t4path = '../portfolio/album/album/';
var t4arr = [ '24_1.jpg', '24_2.jpg', '24_3.jpg','24_4.jpg', '24_5.jpg', '24_6.jpg','24_7.jpg', '24_8.jpg', '24_9.jpg'
,'24_10.jpg', '24_11.jpg', '24_12.jpg','24_13.jpg', '24_14.jpg', '24_15.jpg','24_16.jpg', '24_17.jpg', '24_18.jpg' ];

function t4next(){
  if (t4curr == t4arr.length - 1)
    return;
    
  t4curr++;
  loadImage(t4path + t4arr[t4curr]);
  
  updatePageNumber(t4curr + 1, t4arr.length);  
}

function t4back(){
  if (t4curr == 0)
    return;
  
  t4curr--;
  loadImage(t4path + t4arr[t4curr]);  
  
  updatePageNumber(t4curr + 1, t4arr.length);    
}

function updatePageNumber(page, totalPages){
  if (document.getElementById('pageNum')){
    document.getElementById('pageNum').innerHTML = page + '/' + totalPages;
  }
}

