
/************************************************************** 
 * Accessible menu
 * 
 * 
 **************************************************************/
	 /**
	 * Adds and removes classes to a list of links to allow keyboard accessibility
	 *
	 * @param string dropDownId
	 * @param string hoverClass
	 * @param int mouseOffDelay
	 */
	function dropdown(dropdownId, hoverClass, mouseOffDelay) {
		if(dropdown = document.getElementById(dropdownId)) {
			var listItems = dropdown.getElementsByTagName('li');
			for(var i = 0; i < listItems.length; i++) {
				listItems[i].onmouseover = function() { this.className = addClass(this); }
				listItems[i].onmouseout = function() {
					var that = this;
					setTimeout(function() { that.className = removeClass(that); }, mouseOffDelay);
					this.className = that.className;
				}
				
				var anchor = listItems[i].getElementsByTagName('a');
				anchor = anchor[0];
				anchor.onfocus = function() { tabOn(this.parentNode); }
				anchor.onblur = function() { tabOff(this.parentNode); }
			}
		}
		
		function tabOn(li) {
			if(li.nodeName == 'LI') {
				li.className = addClass(li);
				tabOn(li.parentNode.parentNode);
			}
		}
		
		function tabOff(li) {
			if(li.nodeName == 'LI') {
				li.className = removeClass(li);
				tabOff(li.parentNode.parentNode);
			}
		}
		
		function addClass(li) { return li.className + ' ' + hoverClass; }
		function removeClass(li) { return li.className.replace(hoverClass, ""); }
	}
	
	$( function() {
		dropdown('mainnav', 'hover', 150);
	} );

/************************************************************** 
 *
 * CUFON
 *
 **************************************************************/
	
	// Cufon setup and cross-site code
	Cufon.DOM.ready(function() {
		Cufon.set('fontFamily', 'hand');
		Cufon.set('fontSize', '19px');
		Cufon.set('textTransform', 'none');
		Cufon.set('letterSpacing', '-1px');
		Cufon.set('fontStretch', '130%');
	
		Cufon.replace('#featured h3');
		Cufon.replace('.cols-1 h1');
		Cufon.replace('.cols-1-3q h1');
		Cufon.replace('.cols-2q h1');
		Cufon.replace('.cols-4 h2');
		Cufon.replace('.cols-2 h1');

		Cufon.set('fontSize', '17px');
		Cufon.set('letterSpacing', '0');
		Cufon.replace('.cols-1 h2');
		Cufon.replace('.cols-1-3q h2');
		Cufon.replace('.cols-2 h2');
	} );

/************************************************************** 
 *
 * Gallery Page Functionality
 *
 **************************************************************/

	var DCgallery = {

		/*************************
		 * Initiate gallery functionality.
		 *************************/
		initGallery : function () {

			// adding preload img element to page
			$('<div id="gallery-loading"></div><img src="" id="gallery-preloader" height="1" width="1" />').appendTo("#galleryImage");
			
			//Add actions to thumb links
			$('#galleryThumbs a').click(function(event) {
				DCgallery.replaceImage($(this).attr('href'));
				DCgallery.changeHighlight(this);
				event.preventDefault();
			});
			
			// Preload next iamge
			DCgallery.postLoad();
		},
		
		/*************************
		 * Load and replace page image.
		 *************************/
		replaceImage : function (ImageURLLeft) {
			var imgContainer, TheImageLeftTemp, TheImageLeft;
			
			//Show loading message
			$("#gallery-loading").show();	// display loading message
			
			//Get Image
			imgContainer = $('#galleryImage img');
	
			//Swap Image Left
			TheImageLeftTemp = new Image();
			$(TheImageLeftTemp).load(function () {
				$('#gallery-preloader').attr('src', ImageURLLeft);
				TheImageLeft = document.createElement('img');
				$(TheImageLeft).load(function () {
					$(imgContainer).attr('src', this.src);
					$('#gallery-loading').hide();
					DCgallery.postLoad();
				});
				$(TheImageLeft).attr('src', ImageURLLeft);
			});
			$(TheImageLeftTemp).attr('src', ImageURLLeft);
		},

		/****************************************************************
		 * Preload Next Image in Sequence
		 ****************************************************************/
		postLoad : function () { //Agressive postload
			var NewImage, postloadNew;
			
			NewImage = $('#galleryThumbs .current').parent().next().children().attr('href');
			//var postloadPath = NewImage;
			postloadNew = new Image();
			$(postloadNew).attr('src', NewImage);
		},


		/*************************
		 * Change highlight
		 *************************/
		changeHighlight : function (currentElem) {
			$('#galleryThumbs .current').removeClass('current');
			$(currentElem).addClass('current');
		}
	};

/************************************************************** 
 *
 * RESPONSIBLE FOR FADING IN THE HEADER IMAGES. 
 *
 * Plain Jane JS is used (before we pull in any JS libraries) 
 * to hide the elements initially.
 *
 **************************************************************/
function fadeImages() {

	var useImage = $('#head-image').css('background-image');
	
	// image urls on non-webkit can be found using split
	var splitImage = new Array();
	splitImage = useImage.split('"');
	if (splitImage[1]) {	// that worked, let's use it.
		useImage = splitImage[1];
	} else { // webkit browsers need an alternative way to find the url
		useImage = useImage.substring(4);
		useImage = useImage.substring(0, useImage.length-1);
	}

	var imgOne = new Image();
	$(imgOne)
		.load(function () {
			$("#head-image").fadeIn('slow');
		})
		.error(function () {
			$("#head-image").fadeIn('slow');
		})
		//.css('background-image', $('#head-image').css('background-image'));
		.attr('src', useImage);
}

$( function() {fadeImages();} );

