window.onload = init;

var DefaultCSText = '';  // the default text for the case study text box
var DefaultLocation = 0; // the default location of the case study text box
var currentWidth = 300;  // width of the floating case study text box
var offset = 30;         // the number of pixels from the edge of the box at which the Bump should kick in
var bumpAmount = 20;     // the number of pixels to bump text near the edge toward the center
var offSetFromEdge = 40; // the number of pixels the icons sit from the left edge
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
var CyclingInterval = 3; // The interval between images cycling on the page.
PreloaderImage = new Image(); // An image object to hold the preloaded mouseover images


function init() {
	if (!document.getElementsByTagName)
		return;
	InitImageRollovers();
	SetDefaults();
	InitCSHighlighting();
	InitFormValidation();
}

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' || thisA.className == 'imgrollover_text')) {
			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;
	}
	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);
		}
		// set cycling for indicated images
		if (thisimg.className && thisimg.className == 'cycling') {
			PreloadImage(thisimg);
			ImageCycling(thisimg);
		}
	}
}

function ImageCycling(target) {
	try {
		StartCyclingImage = target.src.split('_')[1].split('.')[0];
		CurrentCyclingImage = StartCyclingImage;
		setInterval(function() { NextImage(target) },CyclingInterval * 1000);
	}
	catch(e) {
		return false;
	}
}

function NextImage(target) {
	try {
		CurrentCyclingImage--;
		if (CurrentCyclingImage == 0)
			CurrentCyclingImage = StartCyclingImage;
		var CycleImageSrc = target.src.replace(/_[1-9](\.[^.]+)$/, '_' + CurrentCyclingImage + '$1');
		target.src = CycleImageSrc;
	}
	catch(e) {
		return false;
	}
}

function SetDefaults() {
	var all_LI = document.getElementsByTagName("li");
	for (var i = 0;i < all_LI.length; i++) {
		var thisLI = all_LI[i];
		if (thisLI.className && thisLI.className == 'current') {
			DefaultCSText = thisLI.childNodes[1].firstChild.nodeValue;
			DefaultLocation = GetBoxNumber(thisLI.firstChild.firstChild);
			var destination = setTextDestination(thisLI.firstChild.className);
			DeleteCurrentPNode(destination);
			SetTextInPNode(destination, DefaultCSText);
			SetBoxPosition(getIconTextBoxLocation(DefaultLocation));
		}
	}
}

function InitCSHighlighting() {
	var all_LI = document.getElementsByTagName("li");
	for (var i = 0;i < all_LI.length; i++) {
		var thisLI = all_LI[i];
		if (thisLI.className && (thisLI.className == 'current' || thisLI.className == 'highlight')) {
			var img_tag = thisLI.firstChild.firstChild;
			img_tag.src = img_tag.src.replace(/(\.[^.]+)$/, '_over$1');
		}
	}
}

function InitFormValidation() {
	var all_Forms = document.getElementsByTagName("form");
	for (var i = 0;i < all_Forms.length; i++) {
		var thisForm = all_Forms[i];
		thisForm.onsubmit = ValidateForm;
	}
}

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');
	if (target.parentNode.className == 'imgrollover_text') {
		ImageRollover_Text(e);
		MoveRolloverTextBox(target);
	}
}

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');
	if (target.parentNode.className == 'imgrollover_text') {
		ImageRollout_Text(e);
		MoveRolloverTextBoxBack();
	}
}

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;
	}
	// Preload cycling images
	if (img_tag.className == 'cycling') {
		for (var i = img_tag.src.split('_')[1].split('.')[0];i > 0; i--) {
			var CycleImage = img_tag.src.replace(/_[1-9](\.[^.]+)$/, '_' + i + '$1');
			PreloaderImage.src = CycleImage;
		}
	}
}

function ImageRollover_Text(e) {
	var target = find_target(e);
	var destination = setTextDestination(target.parentNode.className);
	DeleteCurrentPNode(destination);
	if (target.parentNode.parentNode.childNodes[1].firstChild)
		SetTextInPNode(destination, target.parentNode.parentNode.childNodes[1].firstChild.nodeValue);
}

function ImageRollout_Text(e) {
	var target = find_target(e);
	var destination = setTextDestination(target.parentNode.className);
	DeleteCurrentPNode(destination);
	if (DefaultCSText.length)
		SetTextInPNode(destination, DefaultCSText);
}

function DeleteCurrentPNode(destination) {
	// See if there is already a P node in place. If so, delete it.
	if (document.getElementById(destination).firstChild) {
		var thisOldP = document.getElementById(destination).firstChild;
		thisOldP.parentNode.removeChild(thisOldP);
		thisOldP = null;
	}
}

function SetTextInPNode(destination, newText) {
	var newp = document.createElement('p');
	newp.appendChild(document.createTextNode(newText));
	document.getElementById(destination).appendChild(newp);
}

function MoveRolloverTextBox(target) {
	var distanceLeft = getIconTextBoxLocation(GetBoxNumber(target));
	SetBoxPosition(distanceLeft);
}

function MoveRolloverTextBoxBack() {
	if (DefaultLocation > 0)
		var distanceLeft = getIconTextBoxLocation(DefaultLocation);
	else
		var distanceLeft = -1000;
	SetBoxPosition(distanceLeft);
}

function SetBoxPosition(distanceLeft) {
	var ThisBox = document.getElementById('icontextdisplay');
	distanceLeft = distanceLeft - (currentWidth/2);
	if (distanceLeft <= 0 - offset)
		distanceLeft = distanceLeft + bumpAmount;
	if (distanceLeft + currentWidth >= 900 + offset)
		distanceLeft = distanceLeft - bumpAmount;
	try {
		ThisBox.style.left = distanceLeft + 'px';
	}
	catch(e) {
		return false;
	}
}

function getIconTextBoxLocation(BoxNumber) {
	var centerLocation = 35 * BoxNumber + offSetFromEdge - 17;
	return centerLocation;
}

function GetBoxNumber(target) {
	var all_Items = target.parentNode.parentNode.parentNode.childNodes;
	var counter = 0;
	for (var i = 0;i < all_Items.length; i++) {
		var thisItem = all_Items[i];
		if (thisItem.nodeName.toLowerCase() == 'li') {
			counter++;
			if (thisItem == target.parentNode.parentNode)
				break;
		}
	}
	return counter;
}

function setTextDestination(thisClass) {
	if (thisClass == 'imgrollover_text')
		var destination = 'icontextdisplay';
	return destination;
}

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 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;
}

// Form Validation
function ValidateForm(e) {
	var target = find_target(e);
	var all_inputs = target.elements;
	for (var i = 0;i < all_inputs.length; i++) {
		var thisInput = all_inputs[i];
		if (thisInput.className && thisInput.className == 'requiredform') {
			if (thisInput.value.length == 0) {
				alert('The ' + thisInput.title + ' field is required, please fill in that information.');
				thisInput.focus();
				return false;
			}
			// validate email addresses
			if (thisInput.name == 'email') {
				if (!thisInput.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)) {
					alert('The email address you provided does not apprear to be valid.')
					thisInput.focus();
					return false;
				}
			}
			// validate phone numbers
			if (thisInput.name == 'phone') {
				if (!thisInput.value.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)) {
					alert('The phone number you provided does not apprear to be valid.')
					thisInput.focus();
					return false;
				}
			}
		}
	}
	return true;
}