// <script type="text/javascript">
<!--  to hide script contents from old browsers

$(document).ready(function()
{
	display_urhere();
	setup_email();
});

/* This function alters the style of the navigation bar to indicate urhere.
I added code from the original to shorten the href string so that it does
not include any bookmarks (....#bookmark). Otherwise, the strings would not
match and the urhere formatting would not be applied.
******************************************************************************/

function display_urhere()
{	var list; var page; var currentHref; var href; var anchorPosition;

	if (!document.getElementById)
	{
		return true;
	}

	list = document.getElementById("navbar");
	page = list.getElementsByTagName("a");
	currentHref = document.location.href;

	anchorPosition = currentHref.indexOf("#");
	if (anchorPosition >= 0)
	{	currentHref = currentHref.substring(0, anchorPosition);
	}

	currentHref = getSimpleHref(currentHref);

	for (var i = 0; i < page.length; i++)
	{	href = getSimpleHref(page[i].href)

		if (href == currentHref)
		{
			//page[i].style.backgroundColor = "#d6b160";
			page[i].style.color = "#000";
			break;
		}
	}

	//display_subnav_urhere();
}

/*
This function was added because Mac Safari does not include the directory structure
before the href, so there was never a match. This function strips the beginning directory structure
away and just leaves the end part--such as about_us.htm
**************************************************************************************************/

function getSimpleHref(s)
{	var length;
	var anchorPosition = 0;

	while (anchorPosition >= 0)
	{	anchorPosition = s.indexOf('/');
		length = s.length;

		if (anchorPosition >= 0)
		{	s = s.substring(anchorPosition + 1, length);
		}
	}

	return(s);
}

/*******************************************************************************
This function sets the urhere for the sub navigation menu for the portfolio pages.
If there is no sub-nav ID on the page, then the function simply returns.
*******************************************************************************/

function display_subnav_urhere()
{
	var href;

	var list = document.getElementById('sub-nav');

	if (list == null)
	{
		return;
	}

	var page = list.getElementsByTagName("a");
	var currentHref = document.location.href;

	var anchorPosition = currentHref.indexOf("#");
	if (anchorPosition >= 0)
	{	currentHref = currentHref.substring(0, anchorPosition);
	}

	currentHref = getSimpleHref(currentHref);

	for (var i = 0; i < page.length; i++)
	{
		href = getSimpleHref(page[i].href)

		if (href == currentHref)
		{
			page[i].style.color = "#f8dfb2";
			page[i].style.backgroundColor = "#6e3631";
			//page[i].parentNode.style.background = "#fff";			// Parent node is <div>

			break;
		}
	}
}

/**************************************************************************************************
This function sets up a links associated with class names to send email. Email address cannot
be read by spambots. email_array is an associative array. Each array corresponds to 1 email address.

To use in html code, as an exampke, add <span class="emailMichele"></span> where each
email address has an email**** associated with it.

To add another email address, add to email_array.
**************************************************************************************************/

function setup_email()
{
	// Get all <span> tags
	var tags = document.getElementsByTagName("span");

	for (var i = 0; i < tags.length; i++)
	{
		// Get className of <span> tag
		var cname = tags[i].className;

		// Now look for classes that begin with 'email'
		var s = cname.indexOf('email');

		// If <span> tag and class starts with 'email'
		if (s == 0)
		{
			// Get index to email_array (strip off 'email')
			index = cname.substring(5);
			insert_email(tags[i], index);
		}
	}
}

/**************************************************************************************************
This function sets up a links associated with class names to send email. Email address cannot
be read by spambots. email_array is an associative array. Each array corresponds to 1 email address.

To use in html code, as an exampke, add <span class="emailMichele"></span> where each
email address has an email**** associated with it.

To add another email address, add to email_array and add another case statement.
**************************************************************************************************/
function insert_email(tag, index)
{
	var email_array = new Array();
	email_array['John'] = 'john, swpatternworks, com';
	email_array['Jason'] = 'jason, swpatternworks, com';

	var temp = email_array[index];

	// Remove any spaces, leave commas
	while (temp.indexOf(' ') != -1)
		temp = temp.replace(' ', '');

	// Split email string by commas
	var temp = temp.split(',');

	address = temp[0] + '@' + temp[1] + '.' + temp[2];

	tag.innerHTML = '<a href="mailto:' + address + '">' + address + '</a>';
}


/*******************************************************************************
It’s simple. It works just how you think getElementsByClass would work, except better.

1. Supply a class name as a string.

2. (optional) Supply a node. This can be obtained by getElementById, or simply by just
throwing in “document” (it will be document if don’t supply a node)). It’s mainly useful
if you know your parent and you don’t want to loop through the entire D.O.M.

3. (optional) Limit your results by adding a tagName. Very useful when you’re toggling
checkboxes and etcetera. You could just supply “input“. Or, if you’re like me, and you
said Good Bye to IE5, you can use the “*” asterisk as a catch-all (meaning ‘any element).
*******************************************************************************/
function getElementsByClass(searchClass, node, tag)
{
	var classElements = new Array();

	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';

	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;

	var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
	for (i = 0, j = 0; i < elsLen; i++)
	{
		if ( pattern.test(els[i].className) )
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


/******************************************************************************
Here's the start of the picture slide show.
/*****************************************************************************/

var folder = '/images/';			// Folder where all images are kept
var current_image_number = 0;		// Global variable to indicate what image is currently displayed
var NUMBER_OF_IMAGES;
var current_image_array = new Array;
var main_image = new Array();
var main_image_loaded = new Array();

//var mainPicture = new Image();

/******************************************************************************
 Names of image files
/*****************************************************************************/

var images_foundry = new Array(
	new Array('aerospace_coreboxes.jpg', 'Aerospace coreboxes'),
	new Array('castings_007.jpg', 'Castings'),
	new Array('core_set_fixture.jpg', 'Core set fixture'),
	new Array('prototype_coreboxes.jpg', 'Prototype core boxes'),
	new Array('ship_large_job_2.jpg', 'Ship large job'),
	new Array('vane_corebox.jpg', 'Vane core box'),
	new Array('cope_and_drag_set.jpg', 'Cope and drag set'),
	new Array('pattern_for_drop_hammer_die.jpg', 'Pattern for drop hammer die')
);

var images_composite = new Array(
	new Array('metal_mold_close_up.jpg', 'Metal mold close up'),
	new Array('composite_part_2.jpg', 'Composite part'),
	new Array('finishing_staves.jpg', 'Finishing staves'),
	new Array('finished_tank_plug.jpg', 'Finished tank plug'),
	new Array('foam_plug_layout.jpg', 'Foam plug layout'),
	new Array('finished_lid_plug.jpg', 'Finished lid plug'),
	new Array('finished_tank_assembly.jpg', 'Finished tank assembly')
);

var images_mold = new Array(
	new Array('aluminum_mold.jpg', 'Aluminum mold'),
	new Array('copy_of_red_pump.jpg', 'Red pump'),
	new Array('epoxy_vacuum_mold.jpg', 'Epoxy vacuum mold'),
	new Array('molded_air_duct_2.jpg', 'Molded air duct'),
	new Array('molded_part.jpg', 'Molded part'),
	new Array('urethanr_prototypes.jpg', 'Urethane prototypes')
);

/*********************************************************************************
This function sets up the images for the Foundry Tooling page.
*********************************************************************************/
function setup_foundry_images()
{
	current_image_array = images_foundry;
	setup_images();
}

function setup_composite_images()
{
	current_image_array = images_composite;
	setup_images();
}

function setup_mold_images()
{
	current_image_array = images_mold;
	setup_images();
}

/*********************************************************************************
This function sets up the images for any of the pages with a slideshow.
*********************************************************************************/
function setup_images()
{
	current_image_number = 0;

	NUMBER_OF_IMAGES = current_image_array.length;

	//Initialize array for preloading next image to 0.
	for (var i = 0; i < NUMBER_OF_IMAGES; i++)
	{
		main_image[i] = new Image();
		main_image[i].src = folder + current_image_array[i][0];
		main_image_loaded[i] = 1;
	}

	var image_file = folder + current_image_array[0][0];
	var description = current_image_array[0][1];

	$("#previousArrow").hide();
	$("#targetImage").attr("src", image_file);
	$("div#imageDescription").text(description);
}

/******************************************************************************
This function selects the next image.
/*****************************************************************************/

function get_next_image()
{
	current_image_number++;

	if (current_image_number == NUMBER_OF_IMAGES - 1)
		$("#nextArrow").hide();
	else
		$("#nextArrow").show();

	$("#previousArrow").show();

	display_image(current_image_number);
}

/******************************************************************************
This function selects the previous image.
/*****************************************************************************/

function get_previous_image()
{
	current_image_number--;

	if (current_image_number == 0)
		$("#previousArrow").hide();
	else
		$("#previousArrow").show();

	$("#nextArrow").show();

	display_image(current_image_number);
}

/******************************************************************************
This function selects the previous image.
/*****************************************************************************/
function display_image(img_index)
{
	var image_file = folder + current_image_array[img_index][0];
	var description = current_image_array[img_index][1];

	// Fade out old image and description
	$("#main-image").fadeOut("slow", function()
		{
			// When done fading out, set new image file
			$("#targetImage").attr("src", image_file);

			// Replace with new description
			$("div#imageDescription").text(description);

			// Now fade in new image and description
			$("#main-image").fadeIn("slow");
			//$("#main-image").slideDown("slow");
		});
}

// end hiding contents from old browsers  -->
// </script>