/*
 * ENABLE JS-ONLY STYLES
 */
$('html').addClass('js-enabled');

/*
 * ONLOAD EVENTS
 */
$(document).ready(function() {
	//do IE NAV hover
	if($.browser.msie && parseInt($.browser.version) === 6) {
		$('#nav-about, #nav-childhood').hover(
			function() {
				$(this).addClass('hover_');
			},
			function() {
				$(this).removeClass('hover_');
			}
		);
	}
	
	//
	$('a.external').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	
	//rejigger CONTACT form
	$('#submit-button').click(function() {
		return validateForm();	
	});

	//hook up gallery
	$('div.thumbs a').click(function() {
		return false;		
	});
	
	//preload all the images
	var preImgs = new Image();
		preImgs.src = ('_images/gallery/bkgd_gallery_land.png');
		preImgs.src = ('_images/gallery/bkgd_gallery_port.png');

	$('div.thumbs img').each(function() {
		preImgs.src = ($(this).attr('src'));
	});
	
	$('div.thumbs a').mouseover(function() {
		$('div.thumbs a').removeClass('current');
		$(this).addClass('current');
		var newsrc = $(this).attr('href');
		var newcla = $(this).children('img').attr('class');
		//alert(newsrc);
		$('#gallery-featured img').attr('src',newsrc);
		$('#gallery-featured').attr('class','').addClass(newcla)//.children('img').attr('src',newsrc);
		//do captions
		var caption = $(this).children('img').attr('alt');			
		setCaption(caption);
	});
	//set initial link state and caption
	if($('#gallery-photo').length != 0) {
		var firstthumb = $('div.thumbs a')[0];
		//get alt
		var caption = $(firstthumb).children('img').attr('alt');
		setCaption(caption);
	}
});
function validateForm() {
	//validate for empty values
	$('#message').remove();
	var _isValid = true;
	$('form .required').each(function() {
		var $el = $(this);
		$el.css('background-color','');
		if($el.val() === '') {
			$el.css('background-color','#ffff99');
			_isValid = false;
		} 
		if($el.attr('id') === 'email' && ($el.val().indexOf('.') < 0 && $el.val().indexOf('@') < 0)) {
			$el.css('background-color','#ffff99');
			_isValid = false;
		}
	});
	var $p = $('<p>').attr('id','message');
	var msg;
	if(!_isValid) {
		msg = 'Highlighted fields below are required.';
		$p.addClass('error').html(msg);
	}
	$('#feedback-form').prepend($p);
	return _isValid;
}

function setCaption(str) {
	$('#gallery-caption').html(str);
};

 /*
	Executes proprietary command to force IE to cache all background images 
		which prevents the browser from fetching the same image repeatedly when using CSS hover background repositioning
	(http://support.microsoft.com/?scid=kb;en-us;823727&spid=2073&sid=global)
*/
if($.browser.msie && parseInt($.browser.version) === 6) {
	try {
		document.execCommand("BackgroundImageCache",false,true);
	} catch(e) {
		// just in case
	}
}