var swapphoto, maxnum, currentnum, inner1, inner2;
var imagearray = new Array();

function smartRollover() {
	if(document.getElementsByTagName) {
		//image swap
		if(document.getElementById("swapphoto")) {
			swapphoto = document.getElementById("swapphoto");
			imagename = swapphoto.getAttribute("src");
			maxnum = parseInt(swapphoto.parentNode.className.substr(3));
			currentnum = 1;
			
			for (i = 0; i < maxnum; i++) {
				imagearray[i] = new Image();
				newname = "_" + (i + 1) + ".jpg";
				imagearray[i].src = imagename.replace("_1.jpg", newname);
			}
	
			var outer = document.createElement("ul");
			document.getElementById("photo").appendChild(outer);
			
			inner1 = document.createElement("li");
			inner1.setAttribute("id", "swapback");
			outer.appendChild(inner1);
			
			var action1 = document.createElement("a");
			action1.setAttribute("href", "#");
			action1.onclick = function() {clickToSwap(true); return false;};
			inner1.appendChild(action1);
			
			var switch1 = document.createElement("img");
			switch1.setAttribute("src", "images/swapback_off.png");
			switch1.setAttribute("width", "90");
			switch1.setAttribute("height", "40");
			action1.appendChild(switch1);
			
			inner2 = document.createElement("li");
			inner2.setAttribute("id", "swapnext");
			outer.appendChild(inner2);
			
			var action2 = document.createElement("a");
			action2.setAttribute("href", "#");
			action2.onclick = function() {clickToSwap(false); return false;};
			inner2.appendChild(action2);
			
			var switch2 = document.createElement("img");
			switch2.setAttribute("src", "images/swapnext_off.png");
			switch2.setAttribute("width", "90");
			switch2.setAttribute("height", "40");
			action2.appendChild(switch2);
			
			inner1.className = "hideswitch";
		}
		//end image swap
		var images = document.getElementsByTagName("img");
		for(var i = 0; i < images.length; i++) {
			if(images[i].getAttribute("src").match("_off.")) {
				images[i].onmouseover = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
				}
				images[i].onmouseout = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
				}
			}
		}
	}
}
//image swap
function clickToSwap(bool) {
	inner1.className = "showswitch";
	inner2.className = "showswitch";
	if(bool) {
		currentnum--;
		newname = "_" + (currentnum) + ".jpg";
		if(currentnum == 1) {
			inner1.className = "hideswitch"; 
		}
	} else {
		currentnum++;
		newname = "_" + (currentnum) + ".jpg";
		if(currentnum == maxnum) {
			inner2.className = "hideswitch";
		}
	}
	swapphoto.setAttribute("src", imagearray[currentnum - 1].getAttribute("src"));
}
// end image swap
if(window.addEventListener) {
	window.addEventListener("load", smartRollover, false);
} else if(window.attachEvent) {
	window.attachEvent("onload", smartRollover);
}
