function isValidEmailAddress(emailAddress) {
	// http://www.reynoldsftw.com/2009/03/live-email-validation-with-jquery/
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
function isValidAmount(string) {
	// http://regexlib.com/REDetails.aspx?regexp_id=131
	var pattern = new RegExp(/^\d+\.?\d{0,2}$/i);
	return pattern.test(string);
}



$(document).ready(function(){
	
	// alter the attribute on all external links
	$('a.external').attr("target","_blank");
	
	// CRM form gubbinry
	// $('form#crm :checkbox').wrap('<span class="check-wrap"></span>');

	// toggle help text
	$('form#crm span.help:not(:has(a))').addClass('js-help');
	$('form#crm span.js-help').hide();
	$("p input, p textarea, p select").bind("focus", function(){
		$(this).siblings('span.js-help').fadeIn('fast');
	}).blur(function() {
		$(this).siblings('span.js-help').hide();
	});
	
	// apply focus to first element
	$("form#crm input:first").focus();
	
	// checkbox effect
	
		$('form#crm :checkbox').each(function() {
		var this_id = $(this).attr('id');
		var this_name = $(this).attr('name');
		var this_value=$(this).attr('value');
		var this_checked = $(this).attr('checked');
		var disabled ='';
		if (!this_checked)
			disabled='disabled="disabled"';
		$(this).parents('form').append('<input type="hidden" '+disabled+' name="'+this_name+'" id="'+this_id+'" value="'+this_value+'" />');
		$(this).next('label').wrapInner('<a href="" title="'+this_value+'" rel="'+this_id+'" class="checkbox"></a>')
		$(this).remove();
	});
	$("form#crm a.checkbox").bind("click", function(){
		var this_rel = $(this).attr('rel'); 
		//var this_val = $('input#'+this_rel).val();
		//var this_name = $('input#'+this_rel).attr('name');
		var this_disabled =$('input#'+this_rel).attr('disabled');
		if (this_disabled) {
			$('input#'+this_rel).removeAttr('disabled');	
			$('label:has(a[rel="'+this_rel+'"])').removeClass('unchecked').addClass('checked');
		} else {
			$('input#'+this_rel).attr('disabled',true);
			$('label:has(a[rel="'+this_rel+'"])').removeClass('checked').addClass('unchecked');
		}
		return false;
	});
	
	// hide show for gift recipient
	$("form#crm a.checkbox[rel='extras_is_a_gift']").parent().parent().siblings("p").hide();
	$("form#crm label.checked a.checkbox[rel='extras_is_a_gift']").parent().parent().siblings("p").show();
	$("form#crm a.checkbox[rel='extras_is_a_gift']").bind("click", function(){
		$(this).parent().parent().siblings("p").toggle();
	});

	// hide show for US State drop down
	$("form#crm #country_id[value!='227']").parent().next().hide();
	$("form#crm #country_id").bind("change", function(){
		if($(this).val()=='227'){
			$(this).parent().next().show();
		}
		else{
			$(this).parent().next().hide();
		}
	});

	
	// add required fields notice
	//$('form#crm fieldset:eq(0)').before('<p class="warning">Fields denoted with an asterisk (*) are required</p>');
	
	$("p input#gift_recipient_email").bind("blur", function(){
		var f_val = $(this).val();
		if (isValidEmailAddress(f_val) == false) $(this).parent('p').removeClass('hit').addClass('missed');
		else $(this).parent('p').removeClass('missed').addClass('hit');		
	});
	
	$("p.required input, p.required textarea").bind("blur", function(){
		var f_val = $(this).val();
		var f_id = $(this).attr('id');
		if (f_id == 'donation') {
			if (isValidAmount(f_val) == true && f_val > 0) $(this).parent('p').removeClass('missed').addClass('hit');
			else $(this).parent('p').removeClass('hit').addClass('missed');
		} else if (f_id == 'email') {
			if (isValidEmailAddress(f_val) == false) $(this).parent('p').removeClass('hit').addClass('missed');
			else $(this).parent('p').removeClass('missed').addClass('hit');			
		} else if (f_val.length == 0) {
			$(this).parent('p').removeClass('hit').addClass('missed');			
		} else {
			$(this).parent('p').addClass('hit').removeClass('missed');
		}		
		var missed_q = $("p.missed").size();
		var hit_q = $("p.hit").size();
		var required_q = $('p.required:not(:has(select))').size(); 
		if (hit_q == required_q) $('input[type="submit"]').removeAttr('disabled'); 		
	});	
	var fieldset_q = $('form#crm fieldset').size();
	$('form#crm input[type="submit"]').attr('disabled','disabled');
	$('form#crm p.required').each(function() {
		$(this).children('label').append(' *');
	});	
	
	// home page gubbinry
	$('#home').addClass('home-js');
	$('#slideshow').cycle({
		pause: 1,  
		speed: 1000
	});
	$('#slideshow li').each(function() {
		$(this).children('div').children('h3').prependTo(this);
		$(this).children('div').fadeTo(1,0);
		$(this).hover(function() {
			$(this).children('div').fadeTo('slow',1);
		},function() {
			$(this).children('div').fadeTo('slow',0);
		});		
	});
	
	$('#home-about div').fadeTo(1,0);
	$('#home-about').children('div').children('h3').prependTo('#home-about');
		
	$('#home-about').hover(function() {
		$(this).children('div').fadeTo('slow',1);
	},function() {
		$(this).children('div').fadeTo('slow',0);
	});
	
	
	// book help section
	$('div.help').after('<a href="" class="helpToggle">Show Help</a>').hide();
	var help = $('div.help').height();
	$('a.helpToggle').toggle(function() {
		$('div.help').slideDown();
		$(this).text('Hide Help');		
	},function() {
		$('div.help').slideUp();
		$(this).text('Show Help');		
	});
	
	// book search
	if(($('#book_search_form input#keywords').attr("value")=="")||($('#book_search_form input#keywords').attr("value")=="Enter name")){
		$('#book_search_form input#keywords').attr("value","Enter name");
		$('#book_search_form input#keywords').click(function () { 
			$(this).attr("value","");
		});
	}



	// lightbox
	$('a.lightbox').lightBox();
	
	
	// fancybox
	$("a[rel=fancybox_gallery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a.fancybox").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + title + '</span>';
		}
	});
	
	
	// character counts
	if ($('#body_text').length != 0){
		$('#body_text').after('<p class="character_count">You have <span id="body_text_count"></span> characters left.</p>');
		$('#body_text').simplyCountable({
		counter: '#body_text_count',
		        countType: 'characters',
		        maxCount: '800',
		        countDirection: 'down'
		});
	}
	if ($('#message').length != 0){
		$('#message').after('<p class="character_count">You have <span id="message_count"></span> characters left.</p>');
		$('#message').simplyCountable({
		counter: '#message_count',
		        countType: 'characters',
		        maxCount: '140',
		        countDirection: 'down'
		});
	}
	if ($('#inscription').length != 0){
		$('#inscription').after('<p class="character_count">You have <span id="inscription_count"></span> characters left.</p>');
		$('#inscription').simplyCountable({
		counter: '#inscription_count',
		        countType: 'characters',
		        maxCount: '102',
		        countDirection: 'down'
		});
	}
	if ($('#gift_certificate_text').length != 0){
		$('#gift_certificate_text').after('<p class="character_count">You have <span id="gift_certificate_text_count"></span> characters left.</p>');
		$('#gift_certificate_text').simplyCountable({
		counter: '#gift_certificate_text_count',
		        countType: 'characters',
		        maxCount: '150',
		        countDirection: 'down'
		});
	}
	
	if ($('#interpretation_panel_text').length != 0){
		$('#interpretation_panel_text').after('<p class="character_count">You have <span id="interpretation_panel_text_count"></span> words left.</p>');
		$('#interpretation_panel_text').simplyCountable({
		counter: '#interpretation_panel_text_count',
		        countType: 'words',
		        maxCount: '40',
		        countDirection: 'down'
		});
	}


});
