var totals = -1;
var pos = 1;
var arrows;
var arrowlinks;
var links;

function setNewLinks() {
	var realtotals = totals - 1;
	var newleftlink, newlefttitle, newrightlink, newrighttitle, prevlink, nextlink;
	
	if (pos != 1) {
		prevlink = pos - 2;
		newleftlink = links[prevlink].getAttribute("href");
		newlefttitle = links[prevlink].getAttribute("title");
	} else {
		newleftlink = links[realtotals].getAttribute("href");
		newlefttitle = links[realtotals].getAttribute("title");
	}
	
	if (pos >= totals) {
		newrightlink = links[0].getAttribute("href");
		newrighttitle = links[0].getAttribute("title");
	
	} else {		
		nextlink = pos;
		newrightlink = links[nextlink].getAttribute("href");
		newrighttitle = links[nextlink].getAttribute("title");
	}
	
	arrowlinks[0].setAttribute("href", newleftlink);
	arrowlinks[0].setAttribute("title", newlefttitle);
	arrowlinks[1].setAttribute("href", newrightlink);
	arrowlinks[1].setAttribute("title", newrighttitle);
}


function showPic(whichpic, comingfrom) {
  if (!document.getElementById("placeholder")) return true;
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  if (!document.getElementById("description")) return false;
  if (!document.getElementById("worktitle")) return false;
  
  if (whichpic.getAttribute("title")) {
    var text = whichpic.getAttribute("title");
  } else {
    var text = "";
  }
  var worktitle = document.getElementById("worktitle");
  var description = document.getElementById("description");
  var parts = text.split("-");
  var titlestring = parts[0];
  var titletext = titlestring.slice(0, -1);
  if (description.firstChild.nodeType == 3 && worktitle.firstChild.nodeType == 3) {
    worktitle.firstChild.nodeValue = titletext+",";
	description.firstChild.nodeValue = parts[1];
  }
  /*if (description.firstChild.nodeType == 3) {
    description.firstChild.nodeValue = text;
  }*/
  
  var indexname = whichpic.getAttribute("name");
  if (comingfrom == 'B') {
	  if (indexname == "left") {
		  if (pos > 1) {pos = --pos;}
		  else {pos = totals}
	  } else {
		  if (pos < totals) {pos = ++pos;}
		  else {pos = 1}
	  }
  } else if (comingfrom == 'A') {
	  	pos = parseInt(indexname,10);
  }	 
  setNewLinks();
  return false;
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("imagegallery")) return false;
  var gallery = document.getElementById("imagegallery");
  links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
    links[i].onclick = function() {
      var galleryindicator = 'A';
	  return showPic(this, galleryindicator);
	}
    links[i].onkeypress = links[i].onclick;
		
  }
  totals = i;
}

/* it would be good to initialize the arrow values dynamically - do this last */

function prepareArrows() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("arrows")) return false;
  arrows = document.getElementById("arrows");
  arrowlinks = arrows.getElementsByTagName("a");
  for ( var i=0; i < arrowlinks.length; i++) {
    arrowlinks[i].onclick = function() {
      var arrowindicator = 'B';
	  return showPic(this, arrowindicator);
	}
    arrowlinks[i].onkeypress = arrowlinks[i].onclick;
  }
  arrowlinks[0].setAttribute("name", "left");
  arrowlinks[1].setAttribute("name", "right");
  
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


addLoadEvent(prepareGallery);

addLoadEvent(prepareArrows);



