function showLoader(text){
	$('#loading div.content p').text(text);
	
	//If anything cocks up, it's this bit that's doing it.
	Cufon.replace('#loading p');
	
	$('#loading').show().children('span.background').css({opacity:0}).animate({opacity:1}, 300,function(){
		$(this).siblings('div.content').show().css({opacity:0}).animate({opacity:1}, 300);
	});
}
function hideLoader(){		
	$('#loading div.content').animate({opacity:0}, 600, function(){
		$(this).siblings('span.background').animate({opacity:0}, 300,function(){
			$(this).parent('#loading').hide();
		});
	});
}

var sGlobalEmail = "";

$(function(){

	//Set the height of the steps containers
	$('#steps').css({height:$('#steps').height()});
	
	isCufon = false;
	
	function resize(newH){
		if(isCufon == false){
			Cufon.replace('.field label');
			Cufon.replace('.checkbox label.cufon');
			Cufon.replace('legend.cufon');
			isCufon = true;
		}

		$('#steps').animate({height:newH},1000,function(){
			hideLoader();
			//Then strip out the height so that errors don't break things by increasing the height of the content.
			//$(this).removeAttr('style');
		});
	}

	$( "#form1" ).submit(function(){
		
		var sStr = $( this ).serialize();

		$.ajax({
			type: "POST",
		   	url: "/index.php/tootsies/extradata/",
		   	data: sStr,
			beforeSend: function(){
				showLoader( 'Generating voucher' );
			},
		   	success: function( data, textStatus ){
				if( data != 'error'){
					$( '.step1' ).hide();
					$( '#steps' ).append( data );
					$( '.step2' ).show();
					if( $( '.step2' ).height() ){
						resize( $( '.step2' ).height() );
					}
					if( $( '.step3' ).height() ){
						resize( $( '.step3' ).height() );
					}
					sGlobalEmail = $( "#email" ).attr( "value" );
				}
				else{
					showLoader( 'please enter a valid email address' );
					setTimeout( "hideLoader()", 1500 );
				}
		   	}
		 });

		return false;
	});
	
	
	$( "#step2-submit" ).live( "click", function(){
	
		if( $( "#subscribe-checkbox:checked" ).val() == undefined ){
			alert( "Please accept the terms and conditions" );
			return false;
		}
		var sStr = $( "#form2" ).serialize();

		$.ajax({
			type: "POST",
		   	url: "/index.php/tootsies/send/",
		   	data: sStr,
			dataType: "json",
			beforeSend: function(){
				showLoader( 'generating voucher' );
			},
		   	success: function(data,textStatus){
				//console.log( data.status );
				if( data.status == 'error'){
					$( "p.error" ).show();
					// only leave in those that have validated
					$.each( data.toRemove, function(i, val){
						if( val != '' ) $( '#' + val ).hide();
					});
					$( '.step2' ).show();
					resize( $( '.step2' ).height() );
				}
				if( data.status == 'continue' ){
					$( '.step2' ).hide();
					$( '#steps .step1' ).remove();
					$( '#steps .step2' ).remove();
					$( '#step3_holder' ).load( "/index.php/tootsies/step3", { 'email' : sGlobalEmail }, function(){
						resize($( '.step3' ).height());
					});
					
					sGlobalEmail = "";
				}
		   	}
		 });
		return false;
	});
	
	$("a.static").live( "click", function(){
  		document.form3.submit();
  		return false;
  	});
});