window.onload = prepLinks;
var flipswitch = 1;
var currentpath = null;
var test;
var flag = 1;

function prepLinks(){
	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	if (!document.getElementById("thumbs")) return false;
	
	var thumbs = document.getElementById("thumbs");
	var links = thumbs.getElementsByTagName("a");
	for ( var i=0; i < links.length; i++) {
		links[i].onclick = function() {
			return switcher(this);
		}
	}
}

function switcher(source)
{
	if (flag == 1){
		flag = 0;
		imagepath = source.getAttribute("href");
		if (imagepath != currentpath ){
			test = new Image();
			test.onabort = function(){
				alert("Image Load aborted");
			}
			test.onload = function(){
				switchImages(imagepath);
			}
			test.onerror = errorFunc;
			test.src=imagepath;
			if (flipswitch == 1){
				var mayne2 = document.getElementById("main2");
				mayne2.setAttribute("src", imagepath);
				currentpath = imagepath;
			} else {
				var mayne = document.getElementById("main") 
				mayne.setAttribute("src", imagepath);
				currentpath = imagepath;
			}
		} else if (imagepath == currentpath){
			flag = 1;
		}
	}
	return false;
}

function errorFunc(){
	alert("There was an error loading this image");
}

function switchImages() {
  if (flipswitch == 1){
  		crossFadeUp(100);
  		flipswitch = 2;
  } else {
  		crossFadeDown(100);
  		flipswitch = 1;
  }
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}


var fadeStep = 50;

function crossFadeUp(opacity) {
	var picture1 = document.getElementById("main");
	var picture2 = document.getElementById("main2");
	if (opacity == 100){
		setOpacity(picture2, 0);
		picture2.style.visibility = "visible";
	}
	if (opacity == 0){
		picture1.style.visibility = "hidden";
		flag = 1;
	}
	if (opacity <= 100 && opacity >= 0){
		setOpacity(picture1, opacity);
		setOpacity(picture2, 100-opacity);
		opacity = opacity - 10;
		window.setTimeout("crossFadeUp("+opacity+")", fadeStep);
	}
}

function crossFadeDown(opacity) {
	var picture1 = document.getElementById("main");
	var picture2 = document.getElementById("main2");
	
	if (opacity == 100){
		setOpacity(picture1, 0);
		picture1.style.visibility = "visible";
	}
	if (opacity == 0){
		picture2.style.visibility = "hidden";
		flag = 1;
	}
	
	if (opacity <= 100  && opacity >= 0){
		setOpacity(picture2, opacity);
		setOpacity(picture1, 100-opacity);
		opacity = opacity - 10;
		window.setTimeout("crossFadeDown("+opacity+")", fadeStep);
	}
}