ChiliBook.recipeFolder     = "/wp-content/themes/trentrichardson/javascript/chili/"; 

$(function(){
	
	$('#commentform').submit(validateCommentForm);
	
	//$('.sidebar .Links ul li a').yoyo('paddingLeft',{ range: 6,speed:'fast' });
	s_cleared = false;
	$('#s').focus(function(){
		$s = $(this);
		if($s.val() == 'search' && !s_cleared){
			$s.val('');
			s_cleared = true;
		}
	});
});

function validateCommentForm(){
	var notAdmin = $('#comment_author,#comment_email').length;
	var name = $.trim($('#comment_author').val());
	var email = $.trim($('#comment_email').val());
	var comment = $.trim($('#comment_comment').val());
	var error = "";
	
	$("#commentform .errorBlock").remove();
	
	if (notAdmin > 0) {
		if (name == "") 
			error += "Your name cannot be blank<br />";
		if (email == "") 
			error += "You must provide a valid email address<br />";
	}
	if(comment == "")
		error += "Please provide a comment<br />";
	
	if(error != ""){
		$("#commentform").prepend('<div class="errorBlock" style="display:none;">'+error +'</div>').children('.errorBlock').show('slow');
		return false;
	}
	return true;
}

function showContactForm(){
	var msg = '<div class="row"><label for="cname">Name</label><input type="text" id="cname" name="cname" value="" /></div><div class="clear"></div>' + 
		'<div class="row"><label for="cemail">Email</label><input type="text" id="cemail" name="cemail" value="" /></div><div class="clear"></div>' + 
		'<div class="row"><label for="cphone">Phone</label><input type="text" id="cphone" name="cphone" value="" /></div><div class="clear"></div>'+
		'<div class="row"><label for="cmsg">Message</label><textarea id="cmsg" name="cmsg"></textarea></div><div class="clear"></div>';
	
	$.prompt(msg,{
			 	buttons: { Send:true, Cancel:false},
				useiframe: true,
				submit: function(v,m,f){
					var na = m.find('#cname');
					var em = m.find('#cemail');
					var ph = m.find('#cphone');
					var ms = m.find('#cmsg');
					var e = false;
					
					if(v==true){
							if($.trim(f.cname) == ""){
								na.addClass('inputerror');
								e = true;
							}
							else na.removeClass('inputerror');
							
							if(f.cemail.toString().match(/^(("[\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 ) == null){
								em.addClass('inputerror');
								e = true;
							}
							else em.removeClass('inputerror');
							
							if($.trim(f.cmsg) == ""){
								ms.addClass('inputerror');
								e = true;
							}
							else ms.removeClass('inputerror');
							
							return !e;
					}
					else return true;
				},
				callback: function(v,m,f){
					if(v==true){
						
						$.post('contactemail.php',f, function(data){
							if($.trim(data) == 'true'){						
									 $.prompt('Thank you!<br /><br />Contact Request successfully sent.');
							}else{ $.prompt('Unable to successfully send contact request.' + data); }
						});
					}
				}
				
			 });
}

jQuery.fn.yoyo = function(attr,o){
	o = jQuery.extend({},{ range:20, speed:400, easing: 'linear' },o);
	var r = { hover:{ attr: o.range }, out:{} };
	
	r['hover'][attr] = '+='+ o.range;
	r['out'][attr] = '-='+ o.range;
	
	jQuery(this).hover(function() { //mouse in
		jQuery(this).animate(r.hover, o.speed, o.easing);
	}, function() { //mouse out
		jQuery(this).animate(r.out, o.speed, o.easing);
	});
}
