window.onload = init;

var popupFeatures = 'location=0,status=0,menubar=0,toolbar=0,directories=0,scrollbars=1,width=450,height=400'; // The specifications for the default popup page
PreloaderImage = new Image(); // An image object to hold the preloaded mouseover images


function init() {
	if (!document.getElementsByTagName)
		return;
	InitImageRollovers();
}

function InitImageRollovers() {
	var all_A = document.getElementsByTagName("a");
	for (var i = 0;i < all_A.length; i++) {
		var thisA = all_A[i];
		if (thisA.className && thisA.className == 'imgrollover') {
			if (thisA.childNodes && thisA.childNodes.length == 1 && thisA.childNodes[0].nodeName.toLowerCase() == 'img') {
				thisA.onmouseover = ImageRollover;
				thisA.onmouseout = ImageRollout;
				PreloadImage(thisA);
			}
		}
		if (thisA.className && (thisA.className == 'popup'))
			thisA.onclick = event_popup;
		if (thisA.className && (thisA.className == 'postcardnumber')) { // Manage the Event postcard swapping.
			thisA.onclick = postcardswap;
			if (thisA.getAttribute('href')) { // Set the default image when the page loads for each Event postcard, it will use the image ending with "_1"
				var thisSwapImage = thisA.getAttribute('href');
				var thisSwapImageNum = thisSwapImage.split('_')[thisSwapImage.split('_').length - 1].split('.')[0];
				if (thisSwapImageNum == 1) {
					var swapBox = thisSwapImage.split('_')[0].split('/')[thisSwapImage.split('/').length - 1];
					thisSwapBox = document.getElementById(swapBox + '_image');
					thisSwapBox.style.background = '#606864 url(' + thisSwapImage + ') center top no-repeat';
					thisA.style.background = '#4FA600'; // Set the background color for the selected image box to green
				}
			}
		}
		if (thisA.className && (thisA.className == 'thumbnailicon')) { // Manage the Portfolio and Press image swapping.
			thisA.onclick = portfolioswap;
		}

	}
	var all_img = document.getElementsByTagName("img");
	for (var i = 0;i < all_img.length; i++) {
		var thisimg = all_img[i];
		if (thisimg.className && thisimg.className == 'imgrollover') {
			thisimg.onmouseover = ImageRollover;
			thisimg.onmouseout = ImageRollout;
			PreloadImage(thisimg);
		}
	}
}

function ImageRollover(e) {
	var target = find_target(e);
	if (!target) return;
	if (target.nodeName.toLowerCase() == 'a')
		var img_tag = target.childNodes[0];
	else
		var img_tag = target;
	if (target.parentNode.parentNode.className != 'highlight' && target.parentNode.parentNode.className != 'current')
		img_tag.src = img_tag.src.replace(/(\.[^.]+)$/, '_over$1');
}

function ImageRollout(e) {
	var target = find_target(e);
	if (!target) return;
	if (target.nodeName.toLowerCase() == 'a')
		var img_tag = target.childNodes[0];
	else
		var img_tag = target;
	if (target.parentNode.parentNode.className != 'highlight' && target.parentNode.parentNode.className != 'current')
		img_tag.src = img_tag.src.replace(/_over(\.[^.]+)$/, '$1');
}

function PreloadImage(target) {
	if (target.nodeName.toLowerCase() == 'a')
		var img_tag = target.childNodes[0];
	else
		var img_tag = target;
	// Preload mouseovers
	if (img_tag.parentNode.parentNode.className != 'highlight' && img_tag.parentNode.parentNode.className != 'current') {
		var ImgToLoad = img_tag.src.replace(/(\.[^.]+)$/, '_over$1');
		PreloaderImage.src = ImgToLoad;
	}
}

function event_popup(e) {
	var eventTarget = find_target(e);
	if (!eventTarget) return;
	var features = popupFeatures;
    if (eventTarget.getAttribute('popup'))
		var features = eventTarget.getAttribute('popup');
	var thisTarget = '_blank';
    if (eventTarget.getAttribute('target'))
		var thisTarget = eventTarget.getAttribute('target');
    var theWindow = window.open(eventTarget.getAttribute('href'), thisTarget, features);
    theWindow.focus();
    return false;
}

function postcardswap(e) {
	var target = find_target(e);
	if (!target) return;
    if (target.getAttribute('href')) {
		var thisSwapImage = target.getAttribute('href');
		var SwapImageName = thisSwapImage.split('/')[thisSwapImage.split('/').length - 1];
		var SwapImageNumber = SwapImageName.split('_')[SwapImageName.split('_').length - 1];
		var swapBox = SwapImageName.split('_')[0];
		thisDestinationBox = document.getElementById(swapBox + '_image');
		thisDestinationBox.style.background = '#606864 url(' + thisSwapImage + ') center top no-repeat';
		for(var i=1;i < 100;i++) { // Loop through all numbers 1 to 100 and set those boxes to black to erase the previously selected green box.
			if (document.getElementById(swapBox + '_' + i)) {
				thisOtherA = document.getElementById(swapBox + '_' + i);
				thisOtherA.style.background = '#000';
			}
			else break; // Break as soon as we find a missing image in the list; the images must be numbered sequentially.
		}
		target.style.background = '#4FA600'; // Set the just-clicked box to green.
	}
    return false;
}

function portfolioswap(e) {
	var target = find_target(e);
	if (!target) return;
	if (target.nodeName.toLowerCase() == 'img')
		var a_tag = target.parentNode;
	else
		var a_tag = target;
    if (a_tag.getAttribute('href')) {
		var thisSwapImage = a_tag.getAttribute('href');
		thisDestinationImg = document.getElementById('imageswap');
		thisDestinationImg.src = thisSwapImage;
	}
    return false;
}

function find_target(e) {
	var target; 
	if (window.event && window.event.srcElement) 
		target = window.event.srcElement;
	else if (e && e.target)
		target = e.target;
	if (!target)
		return null;
	return target;
}