$(document).ready(function() {
	$("a.nav").hover(
				// over
				function(){
					// store the original image src
					$(this).children(":first-child").attr( "originalsrc" , $(this).children(":first-child").attr("src") );
					
					// add -over to the image unless it is already -over
					if (!$(this).children(":first-child").attr("src").match(/-over/) ) {
						$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/\./, "-over.") );
					}
					
				},
				
				// out
				function(){
					// replace the original src
					$(this).children(":first-child").attr(
						"src", $(this).children(":first-child").attr("originalsrc")
					);
					
					$(this).children(":first-child").attr(
						"originalsrc", null
					);
				}
			);
	
	
	
});

window.onload = function() {
		// preload
		$("a.nav img").each(
			function() {
				if (!$(this).attr("src").match(/-over/) ) {
					$("<img>").attr( "src", $(this).attr("src").replace(/\./, "-over.") );
				}
			}
		);
}




var selected;
function setImage(button, newPhoto) {
	// clear
	var thumbs = document.getElementById('thumbs').childNodes;
	for (var i = 0; i < thumbs.length; i++) {
		if (thumbs[i].nodeType == 1) {
			thumbs[i].style.backgroundPosition = "0px 0px";
		}
	}
	
	selected = button;
	document.getElementById(button).style.backgroundPosition = "0px -55px";

	$('#mainphoto').attr('src',"/images/photo_swap/" + newPhoto);
	
}



function thumbOver(button) {
	document.getElementById(button).style.backgroundPosition = "0px -55px";
}

function thumbOut(button) {
	if (selected != button) {
		document.getElementById(button).style.backgroundPosition = "0px 0px";
	}
}

function thumbs() {
	document.write('<div id="thumbs">');
	var mythumbs = "";
	var thumbCSS = "";
	var first = "background-position : 0px -55px;";
	for (var i = 0; i < arguments.length; i++) {
			if (first) {
				selected = "t" + i;
			}
			document.write('<a id="t' + i + '" onclick="setImage(this.id, \'' + arguments[i] + '\');" onmouseover="thumbOver(this.id)" onmouseout="thumbOut(this.id)" style="background : url(\'/images/photo_swap/thumbs/' + arguments[i] + '\'); ' + first + ';"></a>\n');
			first = "";
			// set up preload
			//$("<img>").attr( "src", "/images/photo_swap/" + arguments[i]);
	}
	document.write('</div>');

	
}

function clearField(field, value) {
	if (field.value == value) {
		field.value = '';
	}
}

function setField(field, value) {
	if (field.value == '') {
		field.value = value;
	}
}