jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

/*
jQUERY
--------------------------------------------------*/
$(document).ready (function() {
	
	// ADD LINK TO HEADER
	$('h1').prepend('<a href="/" title="Ballard*King Home"></a>');
	
	// ENHANCEMENTS TO LINKS
	$('a[href^="mailto"]').addClass('email');
	$('a[href^="http://"],a[href^="https://"]').addClass('external');
	
	// SUBMIT BUTTON ROLLOVER ACTIONS
	$(':image').each(function() {
		
		var $str = $(this).attr("src");
		var $strSpl = $str.split(".");
		
		var $normal = $str;
		var $hover = $strSpl[0]+"-hover."+$strSpl[1];
		
		$.preloadImages($hover);
		
		$(this).css('outline','none');
		
		$(this).hover(
			function(){ $(this).attr("src",$hover); },
			function(){ $(this).attr("src",$normal); }
		);
		
	});
	
	// VALIDATE FORM FIELDS
	$('.required label').append(' <span>*</span>');
	$( '#contact form' ).submit(function() {
		
		$valid = true;
		$('.required').removeClass('input-error');
		$('.input-error-message').remove();
		$('.required input,.required textarea').each(function() {
			
			if ( $(this).val() == this.defaultValue || $(this).val() == "" ) {
				$(this).parent().addClass('input-error');
				$valid = false;
			}
			
		});
	
		if (!$valid) {
			$('p.submit').before('<p class="input-error-message"><strong>Yikes, you missed something!</strong> Please check what you entered to make sure you filled out all the required fields and try submitting the form again.</p>');
			
			return false;
		} else {
			$('.text input,.text textarea').each(function() {
				if ( $(this).val() == this.defaultValue ) {
					$(this).val('');
				}
			});
		}
		
	});
	
	if ($('#successmsg')) {
		setTimeout( function(){ $('#successmsg').fadeOut('slow'); }, 5000 );
	}
	
	// AUTO GROW TEXT AREA
	$('.text textarea').autogrow();
	
	// SIMPLE JUMP MENU
	$('.jumpMenu').change(function() {
		if ($(this).val() != "") {
			window.location = "project-list/view-state/"+$(this).val()+"/";
		}
	});
	
	// ADD LIGHTBOX
	$('.lightBox').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.7,
		imageLoading: '/images/lightbox-ico-loading.gif',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBlank: '/images/lightbox-blank.gif',
		containerBorderSize: 0
	});
	
	// GALLERY ACTIONS
	$('.list-gallery').append('<span class="tl"></span>').after('<p class="info">* Click to view larger image.</p>');
	
	// HOME PAGE
	var slideshow = $("#slideshow");
	var slides = $(".screenshots");
	
	slides.after('<ul id="pnav">').cycle({ 
		fx:     'fade', 
		speed:  500, 
		timeout: 7000,
		pager:  '#pnav',
		pause:  1,
		before: function() {
			//$('#caption').animate({opacity:'0'},1);
			$('#caption').hide();
		},
		after: function() {

			var caption = $(this.innerHTML).attr("alt");
			var captionArray = caption.split('//');
			
			var subCaptionArray1 = captionArray[0].split('~');
			var subCaptionArray2 = captionArray[1].split('~');
			
			var myCaption = '<strong>'+subCaptionArray1[0]+'<br /><a href="/projects/project-detail/'+subCaptionArray1[1]+'/">Read More &raquo;</a></strong><span>'+subCaptionArray2[0]+'<br />'+subCaptionArray2[1]+'</span>';
			
			$('#caption').html('<small>' + myCaption + '</small>').show();
			
		},
		
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) {
			return '<li><a href="#">Click</a><img src="/content/images/th_' + $(slide.innerHTML).attr("rel") + '" width="88" height="50" /><span></span></li>'; 
		} 
	});
		
});