// Tea Council UK - Javascript required for widget functionality
//
// This file assumes the precense of jQuery Javascript Framwork
// @depends http://www.jquery.com
// @author Paul Rooney

$(document).ready(function(){
	
	// Survey opening behavior
	if(!$.cookie('widget_survey')){$.cookie('widget_survey', 'q');} // no cookie? drop one
	if($.cookie('widget_survey') == 'q'){ $('.surveypercent').hide(); 			// hide the scores
	}//else{$('.widget_survey_p').hide();}										// or hide the intro
	
});


function widget_yoursay_form(){
	$('#yoursayform_message').empty();
	
	if ($('#suyviews').text() != 'back') {
		$('#suyviews').text('back');
		$('#widget-yoursay-content').slideUp();
		$('#widget-yoursay-form').slideDown();
	}
	else {
		$('#suyviews').text('Send us your views');
		$('#widget-yoursay-form').slideUp();
		$('#widget-yoursay-content').slideDown();
	}
}

function widget_yoursay_form_submit(){
	if (!$('#yoursayform_firstname').val().length || !$('#yoursayform_lastname').val().length || !$('#yoursayform_comments').val().length || !$('#yoursayform_location').val().length || !$('#yoursayform_email').val().length) {
		$('#yoursayform_message').css("color","red").html("Please fill out the form completely");
	}
	else if (!checkemail($('#yoursayform_email').val())) {
		$('#yoursayform_message').css("color","red").html("Please provide a valid email address");
	}
	else {
		$.ajax({
			   type: "POST",
			   url: "/",
			   data: "w_action=your_say_comment&"+$('#yoursayform').serialize(),
			   dataType: "json",
			   success: function(dat) {
					if (dat.status == 'ok') {
						$('#yoursayform_message').css("color","green").html("Thanks we have submitted your comments.");
						$('#widget-yoursay-form').slideUp();
					}
					else {
						$('#yoursayform_message').css("color","red").html("Something went wrong...<br />Please refresh the page and try again.");
					}
			   }
		 });	
	}
}

function widget_newsletter_subscribe() {
	if (!$('#w_nl_name').val().length || !$('#w_nl_email').val().length) {
		$('#w_nl_cont_message').css("color","red").html("Please complete both your name and email");
	}
	else if (!checkemail($('#w_nl_email').val())) {
		$('#w_nl_cont_message').css("color","red").html("Please provide a valid email address");
	}
	else {
		$.ajax({
			   type: "POST",
			   url: "/",
			   data: "w_action=newsletter_subscribe&name="+$('#w_nl_name').val()+"&email="+$('#w_nl_email').val(),
			   dataType: "json",
			   success: function(dat) {
					if (dat.status == 'created') {
						msg = "Thank you - we have sent a message to the email address you supplied. You must click a 'confirmation' link within that email for your registration to be complete.";
					}
					else if (dat.status == 'exists') {
						msg = "The email address " + $('#w_nl_email').val() + " is already subscribed.";
					}
					else if (dat.status == 'failed') {
						msg = "We could not send an email to you at the moment.<br />Please refresh the page and try again.";
					}
					
					$('#w_nl_cont').html('<p>'+msg+'</p>');
			   }
		 });
	}
}

function widget_survey_vote(i,d){
	if($.cookie('widget_survey') == 'q') {
		 $.ajax({
			   type: "POST",
			   url: "/",
			   data: "w_action=survey_vote&i="+i+"&d="+d,
			   dataType: "json",
			   success: function(dat){
					for(j=1;j<7;j++){
						var newP = eval("dat.votes.percent_"+j);
						$('#widget_survey_p'+j).text(newP + '%');
					}
					$('#widget_survey_tot').text(dat.total_votes);
					$.cookie('widget_survey', i);
					$('.widget_survey_p').slideUp('slow');
					$('.surveypercent').fadeIn('slow');
			   }
		 });
	}
}
