/*******************************************************************************************
 * imageSwitcher
 * Written by Craig Francis
 * Allow the
 *******************************************************************************************/

	var imageSwitcher = new function() {

		//--------------------------------------------------
		// Old browsers

			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Initialisation

			this.init = function() {

				//--------------------------------------------------
				// Debug

					console.log('imageSwitcher.js: Initialisation');

				//--------------------------------------------------
				// Get the references

					var divs = document.getElementsByTagName('div');
					for (var k = (divs.length - 1); k >= 0; k--) {
						if (cssjs('check', divs[k], 'jsImageSwitcher')) {
							imageSwitcher.setup(divs[k]);
						}
					}

			}

		//--------------------------------------------------
		// Setup

			this.setup = function(switcherHolderRef) {

				//--------------------------------------------------
				// Debug

					console.log('imageSwitcher.js: Found switcher (' + switcherHolderRef.id + ')');

				//--------------------------------------------------
				// Images

					switcherHolderRef.imageSwitcherHolder = null;

					var imgs = switcherHolderRef.getElementsByTagName('img');
					for (var k = (imgs.length - 1); k >= 0; k--) {

						if (cssjs('check', imgs[k], 'jsImageSwitcherHolder')) {

							//--------------------------------------------------
							// Found the holder - the main image

								switcherHolderRef.imageSwitcherHolder = imgs[k];

								console.log('imageSwitcher.js: Found holder');

						} else {

							//--------------------------------------------------
							// Found an image as a link

								var parentLink = getParent(imgs[k], 'a');
								if (parentLink) {

									parentLink.imageSwitcherRef = switcherHolderRef;

									parentLink.onclick = function() {
										if (this.imageSwitcherRef.imageSwitcherHolder) {
											this.imageSwitcherRef.imageSwitcherHolder.src = this.href;
											return false;
										}
										return true;
									}

									console.log('imageSwitcher.js: Found link');

								}

						}

					}

			}

		//--------------------------------------------------
		// On page load

			addLoadEvent(function() {
				imageSwitcher.init();
			});

	}
