/* 
 * Homepage js
 */




// form pre-submit callback
function showRequest(formData, jqForm, options)
{
	// show loading graphic
	// send data
	$('div.error').remove();
	
	// check that an option has been ticked, else return false
	if($("input:radio[@name='option']").is(':checked'))
		{
			$('#holiform').fadeOut("slow");
			return true;
		}
		else
		{
			$('#holiform').prepend('<div class="error"><p>Rhaid dewis ateb!</p></div>').hide().fadeIn('fast');
			return false;
		}
   
}

// holi form post-submit callback
function showResponse(responseText, statusText)
{    
	if(responseText.status == 'fail')
	{
		$('#holiform').fadeIn("slow");

		$('#holiform').prepend('<div class="error">'+responseText.reason+'</div>');
	}
	else
	{
		// get rid of the form
		$('#holiform').remove();

		// and display results

		$('#holipoll').append(responseText.html);
		$('#holigraphs').hide().fadeIn('slow');
		animateBars();
	}
}

// animate the holi bars
// get the present horizontal value, pos[1]
// reset all backgrounds to 0%, animate to pos[1]
function animateBars()
{
	var pos = new Array();
	$('div.holiresult div').each(function() {
		pos = $(this).attr('style').split(' ');
		$(this).css('backgroundPosition', '0% 50%');
		$(this).animate({
			backgroundPosition: pos[1] + ' 50%'
		}, 1500);
	})
}
/*
 *	DOCUMENT READY
 */
$(document).ready(function(){

	var options = {
		beforeSubmit:	showRequest,  // pre-submit callback
		success:		showResponse,  // post-submit callback
		dataType:		'json'        // 'xml', 'script', or 'json' (expected server response type)
	};

	$('#holiform').ajaxForm(options);
	// holi result bars
	animateBars();

	// scrollabe portal_menu
	$("#main").scrollable({

		// basic settings
		vertical: true,
		size: 1,
		clickable: false,

		// up/down keys will always control this scrollable
		keyboard: true,

		// assign left/right keys to the actively viewed scrollable
		onSeek: function(event, i) {
			horizontal.scrollable(i).focus();
		}

	// main navigator (thumbnail images)
	}).navigator("#main_navi");

	// horizontal scrollables. each one is circular and has its own navigator instance
	var horizontal = $(".scrollable").scrollable({size: 1,clickable: false}).circular().navigator(".navi");

	// when page loads setup keyboard focus on the first horizontal scrollable
	horizontal.eq(0).scrollable().focus();

	// neges sant ffolant
	// 
	// hide form, and show onclick
	$('div#ffolant form').hide();
	$('div#ffolant').click(function(){
		$(this).children('form').show('fast');
	});
	
	// clear inputs on click
	$('#ffolant_enw').focus(function(){ 
		if($(this).val() == 'Enw')
		{
			$(this).val('');
		}
	});
	$('#ffolant_neges').focus(function(){ 
		if($(this).val() == 'Neges')
		{
			$(this).val('')
		}
	});

	// ajax submit form
	$('#ffolant_submit').click(
		function()
		{
			var ff_enw = $('#ffolant_enw').val();
			var ff_neges = $('#ffolant_neges').val();

			$.post('http://www.uned5.co.uk/hafan/ffolant/', { enw : ff_enw, neges : ff_neges },
				ffolantSuccess,
				'json'
			);
			return false;
		}
	);
});

function ffolantSuccess(data, textStatus, XMLHttpRequest)
{
	//console.log('textstatus '+textStatus);
	//console.log('XMLHttpObject: '+XMLHttpRequest);
	//console.log(data.status);
	if(data.status == 'ok')
	{
		$('div#ffolant form').hide('slow');
		$('div#ffolant').html('<h3>Diolch! Cofia wylio\'r rhaglen Ddydd Sul i weld dy neges!</h3>');
	}
	else if(data.status == 'ko')
	{
		// email not sent
		//console.log('not ok');
		alert('Roedd problem, ceisia eto os gweli yn dda.');
	}
	else
	{
		//console.log(data)
		$('#ffolant form p').remove();
		$('div#ffolant').append(data);
	}

}