jQuery(document).ready(function($) {
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
});

//Forms
jQuery(document).ready(function($) {
		
	//auto clear forms and validate
	$('form').each(function() {
		var form = $(this);
		form.find('input[type=text], input[type=password], textarea').each(function() {
			var d = $(this).val();      
			if(d != '') $(this).addClass('v-default');  
			$(this).focus(function() { if($(this).val() == d) { $(this).val(''); $(this).removeClass('v-default'); } });
			$(this).blur(function() { if($(this).val() == "") { $(this).val(d); $(this).addClass('v-default'); } });
		});
		
		form.submit(function(e) {
			var errors = false, pattern= /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
			
			form.find('.v-email, .v-noempty').each(function() {
				
				if($(this).hasClass('v-email')) {
					if($(this).hasClass('v-default') || !pattern.test($(this).val())) {
						errors = true;
						setError($(this));
					}
				}
				
				if($(this).hasClass('v-noempty')) {
					if($(this).hasClass('v-default') || $(this).val() == "") {
						errors = true;
						setError($(this));
					}
				}
				
			});
			
			if(!errors)
				return true;
			else
				return false;
			
		});
		
	});
	
	function setError(o) {
		o.fadeOut().fadeIn();
	}
	
	
});

jQuery(document).ready(function($) {
								
	//home slideshow
	// redefine Cycle's updateActivePagerLink function 
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
		switch(currSlideIndex) {
			case 0:
				$(pager).find('a').removeClass('p3-active').filter('a:eq('+currSlideIndex+')').addClass('p1-active');
				break;
			case 1:
				$(pager).find('a').removeClass('p1-active').filter('a:eq('+currSlideIndex+')').addClass('p2-active');
				break;
			case 2:
				$(pager).find('a').removeClass('p2-active').filter('a:eq('+currSlideIndex+')').addClass('p3-active');
				break;
		}
	};
	
	$('#slideshow .images').cycle({
						  timeout:4000,
						  speed:1000,
						  fx:'fade',
						  cycle:true,
						  pager:'#pager',
						  pagerAnchorBuilder:function(index, slide) {
							  var c = index+1;
							  switch(c) {
								  case 1:
								  	return "<a href='http://jamesgrant2010.com/about/' class='p"+c+"'></a>";
								  	break
								  case 2:
								  	return "<a href='#' class='p"+c+"'></a>";
								  	break;
								  case 3:
								  	return "<a href='http://jamesgrant2010.com/volunteer/' class='p"+c+"'></a>";
								  	break;
							  }
						  }
						  });
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
	//fancybox
	$('a.fancyimage').fancybox({
		'showNavArrows':true,
		'hideonContentClick':true
	});
	
	//form validation
	$('.x-form').each(function() {
		var wrap = $(this);
		var error = false;
		
		//auto clear defaults
		wrap.find('input[type=text], input[type=password], textarea').each(function() {
			var d = $(this).val();
			$(this).focus(function() { if($(this).val() == d) { $(this).val(''); } });
			$(this).blur(function() { if($(this).val() == '') { $(this).val(d); } });
		});
		
		//no empty
		jQuery.fn.v_noEmpty = function() {
			if($(this).val() == '' || $(this).val().length < 2) {
				$(this).setError();
			}
		}
		
		//email
		jQuery.fn.v_email = function() {
			if(!$(this).val().match('/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/')) {
				$(this).setError();
			}
		}
		
		//error
		jQuery.fn.setError = function() {
			error = true;
			$(this).fadeOut('fast').fadeIn('fast');
		}
		
		wrap.find('.x-send').click(function(e) {
			
			wrap.find('.x-noempty').v_noEmpty();
			wrap.find('.x-email').v_email();
			
			if(!error) { wrap.submit(); }
			
		});
		
	});
	
	//media page
	$('.media .photos').each(function() {
		var prev = $(this).find('.prev'), next = $(this).find('.next');
		$(this).find('.hide').serialScroll({
							 items:'li',
							 prev:prev,
							 next:next,
							 duration:500,
							 cycle:false,
							 exclude:3
							 });
	});
	
	$('a.lbox').fancybox();
	
	//fonts
	Cufon.replace('h1,h2,h3,h4,h5,h6');
	
	//social icon effect
	$('.home .s-links a').each(function() {
		var a = $(this);
		a.hover(function() {
			//on over
			$(this).find('img').animate({top:'-10', queue:false}, 100);
		}, function() {
			//on out
			$(this).find('img').animate({top:'0', queue:false}, 100);
		});
	});
								
});
