
/*
	Authors:		Dan Nye & Jeff Home, Coedit Limited - http://www.coedit.co.uk/
	Description:	Coedit Limited - Oyster-specific portfolio navigation JavaScript
	Copyright:		Copyright 2006 - Coedit Limited - http://www.coedit.co.uk/

	Not to be reproduced without permission of the authors.
*/


var currentlySelectedPortfolio = '';
var oysterPortfolioLinks = [];
var oysterPortfolioSections = [];

function patchPortfolioThumbnails() {
	// Detect portfolio bookmarks in the URL
	var url = location.href;
	if (url.indexOf('#') != -1) currentlySelectedPortfolio = url.substr(url.indexOf('#') + 1);
	if (!document.getElementById(currentlySelectedPortfolio)) currentlySelectedPortfolio = '';
	if (currentlySelectedPortfolio == 'backToTop') currentlySelectedPortfolio = '';

	// Hide all portfolio entries
	var portfolioDivs = getElementsByClassName('oysterPortfolioEntry', document.getElementById('contentInner'));
	for (var loop=0; loop<portfolioDivs.length; loop++) {
		// Set first entry to be default one, if none specified
		if (currentlySelectedPortfolio == '') currentlySelectedPortfolio = portfolioDivs[loop].id;
		portfolioDivs[loop].style.display = 'none';
	}

	/* Start loop at 1 to avoid invisible "skip sub-nav" link */
	oysterPortfolioLinks = document.getElementById('subNav').getElementsByTagName('a');
	for (var loop=1; loop<oysterPortfolioLinks.length; loop++) {
		oysterPortfolioSections[oysterPortfolioSections.length] = getBookmarkNameFromLink(oysterPortfolioLinks[loop]);
		oysterPortfolioLinks[loop].onclick = function() {
			showPortfolioEntry(getBookmarkNameFromLink(this));
			return(false);
		}
	}

	// Show initial entry (first one by default, unless another valid one has been specified
	showPortfolioEntry(currentlySelectedPortfolio);


	/**********************/
	/* 4th Level Nav Code */
	/**********************/

	// For each portfolio entry section...
	for (var loop=0; loop<oysterPortfolioSections.length; loop++) {
		var tempPortfolioEntry = document.getElementById(oysterPortfolioSections[loop]);

		// Create a placeholder image for the large versions of each associated project thumbnail
		var tempDiv = getElementsByClassName('inner', tempPortfolioEntry)[0];
		if (tempDiv) {
			var newImg = document.createElement('img');
			newImg.id = oysterPortfolioSections[loop] + 'BigImage';
			newImg.className = 'bigImage';
			newImg.width = 397;
			newImg.height = 256;
			newImg.className = 'bigImage';
			tempDiv.appendChild(newImg);

			var rightCol = getElementsByClassName('rightCol', getElementsByClassName('oysterPortfolioContainer', tempPortfolioEntry)[0])[0];

			// Move the associated project heading into the container
			var heading = tempPortfolioEntry.getElementsByTagName('h3')[0];
			if (heading) rightCol.insertBefore(heading, null);

			// Move the associated project thumbnails and links into the container
			var thumbSources = getElementsByClassName('projectThumb', tempPortfolioEntry);

			for (var loop2=0; loop2<thumbSources.length; loop2++) {
				thumbSources[loop2] = thumbSources[loop2].getElementsByTagName('a')[0];
				if (loop2 == 0) thumbSources[loop2].className = 'firstItem';
				rightCol.insertBefore(thumbSources[loop2], null);
			}

			// Stop the thumnbails from linking to the new image, choosing instead to load it into the container
			// Set the image to hold the first large image for each portfolio entry
			// Not only does this save us having to show the image whenever we first switch
			// to an entry, but it speeds up the switch between entries, too
			var setImage = false;

			for (var loop2=0; loop2<thumbSources.length; loop2++) {
				thumbSources[loop2].setAttribute('projectIndex', loop2);
				thumbSources[loop2].setAttribute('parentId', oysterPortfolioSections[loop]);
				thumbSources[loop2].setAttribute('bigImage', oysterPortfolioSections[loop] + 'BigImage');

				if (!setImage) {
					setImage = true;
					newImg.src = thumbSources[loop2].href;
				}

				thumbSources[loop2].onclick = function() {
					var bigImage = document.getElementById(this.getAttribute('bigImage'));
					bigImage.src = this.href;
					bigImage.alt = this.getElementsByTagName('img')[0].alt;
					bigImage.title = this.getElementsByTagName('img')[0].title;
					showAssociatedProject(this.getAttribute('parentId'), parseInt(this.getAttribute('projectIndex'), 10));
					return(false);
				}
			}
		}

		// Show the first associated project by default
		var associatedProjects = getElementsByClassName('associatedProject', tempPortfolioEntry);
		for (var loop2=1; loop2<associatedProjects.length; loop2++) {
			associatedProjects[loop2].style.display = 'none';
		}
	}
}

function showAssociatedProject(whichOne, projectIndex) {
	var tempPortfolioEntry = document.getElementById(whichOne);
	var associatedProjects = getElementsByClassName('associatedProject', tempPortfolioEntry);
	for (var loop2=0; loop2<associatedProjects.length; loop2++) {
		associatedProjects[loop2].style.display = (loop2 == projectIndex) ? 'block' : 'none';
	}
}

function getBookmarkNameFromLink(whichOne) {
	var url = whichOne.href;
	return(url.substr(url.indexOf('#') + 1));
}

function getLinkFromBookmarkName(whichOne) {
	for (var loop=1; loop<oysterPortfolioLinks.length; loop++) {
		url = oysterPortfolioLinks[loop].href;
		var whichSection = url.substr(url.indexOf('#') + 1);
		if (whichOne == whichSection) return(oysterPortfolioLinks[loop]);
	}
}

function showPortfolioEntry(whichOne) {
	// Before changing entry, make sure new entry exists!
	var tempPortfolioEntry = document.getElementById(whichOne);

	if (tempPortfolioEntry) {
		if (document.getElementById(currentlySelectedPortfolio)) {
			document.getElementById(currentlySelectedPortfolio).style.display = 'none';
			getLinkFromBookmarkName(currentlySelectedPortfolio).className = '';
		}
		currentlySelectedPortfolio = whichOne;
		document.getElementById(currentlySelectedPortfolio).style.display = 'block';
			getLinkFromBookmarkName(currentlySelectedPortfolio).className = 'selected';

		// Show the first associated project by default
		var thumbDestination = getElementsByClassName('rightCol', getElementsByClassName('oysterPortfolioContainer', tempPortfolioEntry)[0])[0];
		var firstThumbAnchor = thumbDestination.getElementsByTagName('a')[0];
		if (firstThumbAnchor) firstThumbAnchor.onclick();
	}
}

