jQuery(document).ready(function(){
		
		var running = false;

		if(jQuery(".home-header").exists()){
			var slideshowImages = new Array();
			var i = 0;
			var size;
			var transitionSpeed = 500;
			var timeDelay = 7000;
			jQuery.getJSON("/slideshow-content",
			{},
			function(data) {
				slideshowImages = data;
				size = slideshowImages.length;
				changeSlideshow();
			});
		}

		jQuery("#quick-links img, #follow-us img").hover(function(){
			cimg = jQuery(this);
			cimg.attr('src',cimg.attr('src').replace('.png','-h.png'));
		},function(){
			cimg = jQuery(this);
			cimg.attr('src',cimg.attr('src').replace('-h.png','.png'));
		});

		function changeSlideshow(){
				currentImg = "/sites/default/files/styles/slideshow/public/" + slideshowImages[i];
				jQuery(".home-header").css('backgroundImage',jQuery(".inner-home-header").css('backgroundImage'));
				jQuery(".inner-home-header").hide().css('backgroundImage', "url('" +currentImg + "')").fadeIn(transitionSpeed,function(){ });
				if(size > 1)
					setTimeout(function(){changeSlideshow(); },timeDelay);
				i++;
				i %= size;
		}

		/*Ensure our li a hover state is maintained as we move out of the a*/
		jQuery("#main-menu li").hover(function(){
			jQuery(this).find('a').addClass("hover");
		},
		function(){
			jQuery(this).find('a').removeClass("hover");
		});


		if(jQuery("#currentImageWrapper").exists()){
			var currentImage = 	0;
			var transition = 	250;
			var totalImages = 	jQuery(".thumbnails img").length;

			showImage(currentImage);
		}

		function showImage(){

			if(currentImage >= totalImages)
				currentImage = 0;
			else if(currentImage < 0)
				currentImage = totalImages - 1;

			thumbs = jQuery(".thumbnails img");
			thumbs = getThumbs();
			inner = jQuery("#innerCurrentImage");
			outer = jQuery("#currentImage");
			currentImg = thumbs[currentImage];
			outer.css('backgroundImage',inner.css('backgroundImage'));
			inner.hide().css('backgroundImage', "url('" +currentImg + "')").fadeIn(transition,function(){ });
		}

		function getThumbs(){
			imageArray = new Array();

			thumbs = jQuery(".thumbnails img");
			jQuery.each(thumbs,function(index,thumb){
				imageArray.push(jQuery(thumb).attr('src').replace('thumb','full'));
				if(index == currentImage){
					jQuery("#currentImageWrapper .description").html(jQuery(thumb).attr('alt'));
				}
			});

			return imageArray;
		}

		jQuery("#next").click(function(){
			currentImage++;
			showImage();
		});

		jQuery("#prev").click(function(){
			currentImage--;
			showImage();
		});

		jQuery(".thumbnails img").click(function(){
			/*Probably a much better way to do this*/
			thumbs = jQuery(".thumbnails img");
			clicked = this;
			jQuery.each(thumbs,function(index,thumb){
				if(jQuery(thumb).attr('src') == jQuery(clicked).attr('src')){
					currentImage = index;
					jQuery("#currentImageWrapper .description").html(jQuery(clicked).attr('alt'));
					showImage();
				}
			});

		});

		//Enter your comma-seperated field ID's below (This should be the only thing you need to alter)
		var textFieldIdentifiers = "#edit-search-block-form--2";

		//Self explanatory
		function getTextFieldValue(element){
			var textValue = element.val();
			if (textValue == "") textValue = element.html();
			return textValue;
		}

		//Self explanatory
		function setTextFieldValue(element,textValue){
			element.val(textValue);
			element.html(textValue);
		}

		//Initialise a form field's blur and click actions
		function initFormFieldAction(fieldIdentifier){

			var textField = jQuery(fieldIdentifier);

			//Store default value (Only works with textareas and textfields)
			var defaultText = getTextFieldValue(textField);

			//On click we clear the field if it contains the default value
			jQuery(fieldIdentifier).focus( function () {
				var currentText = getTextFieldValue(textField);
				if (currentText == defaultText) setTextFieldValue(textField,"");
			});

			//On blur we restore the default value if the field is blank
			jQuery(fieldIdentifier).blur( function () {
				var currentText = getTextFieldValue(textField);
				if (currentText == "") setTextFieldValue(textField,defaultText);
			});
		}

		//Parse the supplied string and initialise each identifier
		function parseFormFieldIdentifiers(identifiersString){
			var identifiersArray = identifiersString.split(",");
			for (var count in identifiersArray) initFormFieldAction(identifiersArray[count]);
		}

		//Initialise all fields identified in the textFieldIdentifiers string
		parseFormFieldIdentifiers(textFieldIdentifiers);

		Cufon.refresh();

		jQuery("#downloads-table a").click(function(){
			link = jQuery(this);
			jQuery("#download-form #edit-file").val(link.attr('id'));
			window.location.href = '#download-form';
			running = true;
			personalInfo(0);
		});

		function personalInfo(times){
			if(times == 0){
				jQuery(".personal-info span").addClass('highlight');
				setTimeout(function(){ personalInfo(times + 1) },3000);
			}else{
				jQuery(".personal-info span").removeClass('highlight');
			}
		}
});

jQuery(document).load(function(){
	Cufon.refresh();
	setTimeout("Cufon.refresh()",1000);
});

jQuery.fn.exists = function(){return jQuery(this).length>0;}
;

