//------------------------------------
//	SIGNUP.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.3.2
//				cufon.js
//				gazz.js
//------------------------------------


// LOADING SPINNER SHOW AND HIDE
function showLoader(text){
	$('#loading p').text(text);
	Cufon.replace('#loading p');
	$('#loading').fadeIn();
}
function hideLoader(){		
	$('#loading').fadeOut();
}

// GLOBAL EMAIL
// Passed between the different steps
var sGlobalEmail = "";

$(function(){
//BEGIN jQuery

	//Set the height of the steps containers
	$('#steps').css({height:$('#steps').height()});
	
	//We don't need cufon on load
	isCufon = false;
	
	//Resize steps and next time ask for cufon
	function resize(newH){
		$('#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');
		});
	}

	//SUBMIT FORM 1
	$('#step_1 form').submit(function(){
		
		var sStr = $(this).serialize();

		$.ajax({
			type: "POST",
		   	url: "/signup/extradata/",
		   	data: sStr,
			dataType: "json",
			beforeSend: function(){
				showLoader('generating awesomeness');
			},
		   	success: function( data, textStatus ){
				if( data.status != 'error'){
					$('#step_1').hide();
					
					if( data.show == 'step2' ){
						$('#step_2').append( data.data );
						$('#step_2').show();

						if($('#step_2').height()){
							if(isCufon == false){
								Cufon.replace('.field label[class!=noCufon]');
								Cufon.replace('legend.cufon');
								isCufon = true;
							}
							resize($('#step_2').height());
						}
					}
					
					if( data.show == 'step3' ){
						
						$( '#step_3' ).append( data.data );
						resize($('#step_3').height());
					}
				
					sGlobalEmail = $('#email').attr('value');
				}else{
					showLoader('Please enter a valid email address');
					setTimeout('hideLoader()',1500);
				}
		   	}
		 });

		return false;
	});
	
	// SUBMIT FORM 2
	$('#form_2_submit').live('click',function(){

		// ERROR!
		if($('#subscribe-checkbox:checked').val() == undefined){
			$('.form_checkbox').addClass('error');
			alert('Please accept the terms and conditions');
			return false;
		}

		// GET DATA
		var sStr = $('#step_2 form').serialize();

		// SEND DATA
		$.ajax({
			type: "POST",
		   	url: "/signup/send/",
		   	data: sStr,
			dataType: "json",
			beforeSend: function(){
				showLoader('Generating voucher');
			},
		   	success: function(data,textStatus){
				if(data.status == 'error'){
					$('p.error').show();
					
					// Only leave in those that have validated
					$.each( data.toRemove, function(i, val){
						if( val != '' ) $( '#' + val ).hide();
					});
					
					$('#step_2').show();
					resize($('#step_2').height());
				}
				if(data.status == 'continue'){
					$('#step_1, #step_2').remove();
					$('#step_3').load('/signup/step3', {'email': sGlobalEmail}, function(){
						resize($('#step_3').height());
					});
					
					sGlobalEmail = "";
				}
		   	}
		 });
		return false;
	});
	
	
	//VIEW VOUCHER NOW LINK
	$('a.static').live('click', function(){
  		$('#step_3 form').submit();
  		return false;
  	});
	
	// CHECKBOX
	$('.checkbox, label[for=subscribe-checkbox]').live('click', function(){
		$(this).parent().removeClass('error');
		if($('.checkbox').hasClass('checked')){
			$('.checkbox').removeClass('checked');
			if($(this).hasClass('checkbox')){
				$('#subscribe-checkbox').removeAttr('checked');
			}
		}else{	
			$('.checkbox').addClass('checked');
			if($(this).hasClass('checkbox')){
				$('#subscribe-checkbox').attr('checked','checked');
			}
		}
	});
	
	// FACEBOOK SHARE
	$('a.facebook_link').live('click', function(){
		u=location.href;
		t=document.title;
		window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
		return false;
	});

//END jQuery
});