// Great Basin Community Food Co-op, www.greatbasinfood.coop, JQuery Functions; Author, Manny Becerra, PACE Creative Media Co., www.pacecreativemedia.com; Spring 2010; October 2010
$(document).ready(function() {

// Search Form
if($("#s").val()=="") $("#s").val("Search the site");
$("#s").blur(function(){
  if($(this).val()=="") $(this).val("Search the site");
});
$("#s").click(function(){
  if($(this).val()=="Search the site") $(this).val("");
});

// Donate From
if($("#item_price_1").val()=="") $("#item_price_1").val("10");
$("#item_price_1").blur(function(){
  if($(this).val()=="") $(this).val("10");
});
$("#s").click(function(){
  if($(this).val()=="10") $(this).val("");
});

// Preload Images/Graphics
//$.preloadCssImages();

// Nav Menu
$("ul.sf-menu").superfish({ 
		pathClass:  'current',
		pathLevels: 1,
		delay:       1200, // one second delay on mouseout 
		animation:   {opacity:'show',height:'show'}, // fade-in and slide-down animation 
		speed:       'fast',  // faster animation speed 
		autoArrows:  false,  // disable generation of arrow mark-up 
}); 

// Scroll effect on nav
$.localScroll({
	queue:true,
	duration:850,
	hash:true,
});

// Flickr Feed
$('.flickr').jflickrfeed({
	limit: 6,
	qstrings: {
		id: '28401178@N03'
	},
	itemTemplate:
		'<a href="http://www.flickr.com/photos/greatbasinfoodcooperative/" title="{{title}}">' + '<img src="{{image_s}}" alt="{{title}}" />' + '</a>'
}, function(data) {
	$('.flickr a[rel*=flickr]').colorbox();		
});

/*$('.flickr').jflickrfeed({
	limit: 14,
	qstrings: {
		id: '28401178@N03'
	},
	itemTemplate: '<li><img src="{{image}}" alt="" /><div>{{title}}</div></li>'
}, function(data) {
	$('.flickr div').hide();
	$('.flickr').cycle({
		timeout: 5000
	});
	$('.flickr li').hover(function(){
		$(this).children('div').show();
	},function(){
		$(this).children('div').hide();
	});
});*/

// Tool Tips
$('#colwrapper a').tipsy({gravity: $.fn.tipsy.autoWE});
$('#colwrapper span').tipsy({gravity: $.fn.tipsy.autoWE});
$('#footer a.pacelogo').tipsy({gravity: $.fn.tipsy.autoWE});
$("#colwrapper .col3 form [title]").tipsy({trigger: 'focus', gravity: 'w'});


// Live Tweets
//$('#livetweets').liveTwitter('gbfoodcoop', {limit: 5, refresh: false, mode: 'user_timeline', showAuthor: true});

// Photo ColorBox
$("a[rel^='colorbox']").colorbox();

// MailChimp Sign-up Form
$("a.mcsignuplink").colorbox({href:"/wp-content/themes/gbcfc/includes/mc_signupform.php",speed:0,transition:"none"});

// Auto Grow
$('textarea').autogrow();

// FAQs
$('h4.faq-title a').click(function() {
$(this).parent().next("div").toggle("fast");	
  return false;
});

// Video
$('a.videomodal').colorbox();

// Contact Form Validation
var validator = $("form#formContact").validate({

// If Valid, Submit via AJAX
submitHandler: function(form) {

//Ajax Contact Form Options
var options = { 
	dataType:  'script',
	target: '#confirmmessage', 
	//beforeSubmit:  showRequest,  // pre-submit callback 
	type: 'get',
	success: showResponse,  // post-submit callback
	data: { magictoken: '7206' },
	clearForm: true
	//resetForm: true  
};  

// Bind to the form's submit event 
	$(form).ajaxSubmit(options); 
	return false; 
},

// Validation Rules
rules: {
 contactName: "required",
 contactEmail: {
  required: true,
  email: true
 },
 contactSubject: "required",  
 contactTo: "required",   
 contactMessage: "required" 
},

// Custom Error Messages
messages: {
 contactName: {
  required: "First and last name are required"
 },
 contactEmail: {
  required: "E-mail is required.",
  email: "Valid e-mail please, like name@domain.org"
 },	
/* contactTelephone: {
  required: "Telephone is required"
 },*/
 contactMessage: {
  required: "A message/comment is required"
		//rangelength: "Please keep your comments between 5 and 250 characters"
 },

},

}); // End of Contact Form Validation and AJAX Submit

// RSVP Form Validation
var validator = $("form#formRSVP").validate({

// If Valid, Submit via AJAX
submitHandler: function(form) {

//Ajax Contact Form Options
var options = { 
	dataType:  'script',
	target: '#confirmmessage', 
	//beforeSubmit:  showRequest,  // pre-submit callback 
	type: 'get',
	success: showRSVPResponse,  // post-submit callback
	data: { magictoken: '7206' },
	clearForm: true
	//resetForm: true  
};  

// Bind to the form's submit event 
	$(form).ajaxSubmit(options); 
	return false; 
},

// Validation Rules
rules: {
 rsvpName: "required",
 rsvpEmail: {
  required: true,
  email: true
 },
 rsvpTelephone: "required",  
 rsvpDate: "required",  
},

// Custom Error Messages
messages: {
 rsvpName: {
  required: "First and last name are required"
 },
 rsvpEmail: {
  required: "E-mail is required.",
  email: "Valid e-mail please, like name@domain.org"
 },	
 rsvpTelephone: {
  required: "Your contact number is required"
 },
 rsvpDate: {
  required: "Please select a presentation date"
		//rangelength: "Please keep your comments between 5 and 250 characters"
 },

},

}); // End of RSVP Form Validation and AJAX Submit

//Validate Google Checkout Form
var validator = $("#BB_BuyButtonForm").validate({

// Validation Rules
rules: {
 item_price_1: {
			required: true,
   //maxlength: 8,
			//minlength: 2,
			digits: true,			
			min: 10
	}
},

// Custom Error Messages
messages: {
 item_price_1: {
  required: "Please specify an amount",
		//maxlength: "",
		//minlength: "",
		digits: "Numbers only (no commas, alpha-characters, etc.)",
		min: "Minimum of $10"
 }			
},

// Error Placement
errorPlacement: function(error, element) {
 if (element.attr("name") == "item_price_1")
  error.insertBefore("#errorcontainer");
}

}); // End of Google Checkout AJAX Form Submit


// End of JQuery Doc Ready
});

//Load Colorbox (Modal)
function loadcolorbox() {
jQuery.colorbox({ href: this.href }) ;
return false;
}

// pre-submit callback 
function showRequest(formData, jqForm, options) { 

} 

//*/ Post-Submit Callback 
function showResponse(responseText, statusText)  { 
 //alert(responseText);
	$('#confirmmessage').fadeIn('slow');
	$('#confirmmessage').animate({opacity: 1.0}, 2500);

	if(responseText=="successsuccess") {
		//alert(responseText);
		$('#confirmmessage').html('Thanks. Your message was successfully sent! We will be in touch soon.');
		$('#confirmmessage').fadeOut('slow');    
		
	} else {
		$('#captchanotice').html('Sorry, you\'re message was not sent. Please try again. If the problem persists, we will get this resolved ASAP. We\'ve been notified of this error.');
	}
	
} 

//*/ Post-Submit Callback (RSVP Form)
function showRSVPResponse(responseText, statusText)  { 
 //alert(responseText);
	$('#confirmmessage').fadeIn('slow');
	$('#confirmmessage').animate({opacity: 1.0}, 25000);

	if(statusText=="success") {
		//alert(responseText);
		$('#confirmmessage').html('Your RSVP was successfully sent. <br /><br />Thank You for getting involved in the future success of your community food co-op!  We look forward to meeting you on: ' + responseText +  ' at 250 Court St. Reno, NV -- The Garden Pavilion across the street from the proposed, new Great Basin Community Food Co-op location.<br /><br />Please let us know if you have a change in plans and can no longer attend, there is limited space and we would like as many people as possible to be able to attend.<br /><br />Amber and Nicole Sallaberry<br />Co-Founders and Moving Project Coordinators' );
		$('#confirmmessage').fadeOut('slow');    
		
	} else {
		$('#captchanotice').html('Sorry, you\'re RSVP was not sent. Please try again. If the problem persists, we will get this resolved ASAP. We\'ve been notified of this error.');
	}
	
} 

//Pre-load Images
newimage0 = new Image();
newimage0.src = "/wp-content/themes/gbcfc/images/nav-hover-bg.png";


