// JavaScript Document

// adds class to body for IE8 formatting
jQuery(function($){
	if ($.browser.msie) {
		$("body").addClass("trident");
	    $('#navGlobal li:last-child').addClass('last-child');
	}
});

jQuery(function($){
	$('#navSearch input')
		.val('Search')
		.focus(function(){
			if($(this).val() == 'Search') {
				$(this).val('');
			}
			$(this).parent().addClass('focus');
		})
		.blur(function(){
			if($(this).val() == '') {
				$(this).val('Search');
			}
			$(this).parent().removeClass('focus');
		});
});


jQuery(function($){
	$('#navMain li').hover(
		function(){
			$(this).addClass('hover').find('div').fadeIn('fast');
		},
		function(){
			$(this).removeClass('hover').find('div').hide();
		}
	);
});

// potential future use for sliding left side nav
//jQuery(function($){
//	$('#contentNav li').hover(
//		function(){
//			$(this).addClass('hover').find('div').fadeIn('fast');
//		},
//		function(){
//			$(this).removeClass('hover').find('div').hide();
//		}
//	);
//});


 jQuery(function($){
	$('div.slideshow').each(function(i){
		var id = 'slideshowContainer' + i;
		
		var slideshow = {};
		
		slideshow.vars = {
			nav: '<div class="slideshowNav"><a class="slideshowNavPrev"><img src="/_res/img/slideshowArrowPrev.png" alt="previous slide" /></a><div class="slideshowNavPager"></div><a class="slideshowNavNext"><img src="/_res/img/slideshowArrowNext.png" alt="next slide" /></a></div>',
			config: {
				fx:               'fade',
				speed:            4000,
				timeout:          10000,
				pager:            '#' + id + ' div.slideshowNavPager',
				prev:             '#' + id + ' a.slideshowNavPrev',
				next:             '#' + id + ' a.slideshowNavNext',
				prevNextClick:    function(){ slideshow.functions.pause(); },
				pagerClick:       function(){ slideshow.functions.pause(); },
				pause:            true,
				activePagerClass: 'slideshowHighlight'
			},
			obj:          '#' + id + ' div.slideshow'
		};
		
		slideshow.functions = {
			pause: function(){
				$(slideshow.vars.obj).cycle('pause');
				$(slideshow.vars.objPlayPause).removeClass(slideshow.vars.classPlay).addClass(slideshow.vars.classPause);
				return false;
			},
			play: function(){
				$(slideshow.vars.obj).cycle('resume', true);
				$(slideshow.vars.objPlayPause).removeClass(slideshow.vars.classPause).addClass(slideshow.vars.classPlay);
				return false;
			}
		};

		$(this)
			.wrap('<div class="slideshowContainer" id="' + id + '"></div>')
			.after(slideshow.vars.nav)
			.cycle(slideshow.vars.config);
			
		$(slideshow.vars.objPlayPause)
			.click(function() {
				if($(this).is('.' + slideshow.vars.classPlay) === true) {
					slideshow.functions.pause();
				}
				else {
					slideshow.functions.play();
				}
			});
	});
});


jQuery(function($){
		$(".gallery a[rel^='prettyPhoto']")
			.prettyPhoto(
				{
					animationSpeed:		'slow',
					theme:				'light_square',
					slideshow:			2000
				}
			);
	});

jQuery(function($){
	function emailValidation(str){
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (str.match(emailRegEx)) {
			return 1;
		}
		else {
			return 0;
		}
	}
	/* generic form validation */
	$('form').submit(function(){
		$('div.error').removeClass('error');
		
		$(this).find('input,select,textarea').each(function(i){
			if($(this).parent().find('span.error').size() > 0 && $(this).attr('value') === ''){
				$(this).parent().addClass('error');
			}
			if($(this).parent().find('input.email').size() > 0) {
				if(emailValidation($(this).attr('value')) === 0) {
					$(this).parent().addClass('error');
				}
			}
			if($('div.error:visible').size() === 1) {
				$(this).focus();
			}
		});
		
		/* if we no DIV.erros are visible, we don't have any errors */
		if($('div.error:visible').size() === 0) {
			return true;
		}
		else {
			scroll(0,0);
			$('p.errorMessage').slideDown();
			return false;
		}
	});
});
