//------------------------------------
//	SIGNUP.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.3.2
//				cufon.js
//				gazz.js
//------------------------------------

Cufon.replace('#steps .step h2', {
	textShadow: '1px 1px #ff9260'
});

Cufon.replace('#steps .step h2 span', {
	textShadow: '1px 1px #561e06'
});

Cufon.replace('#step_2 dt, .loading span', {
	textShadow: '1px 1px #FFF'
});

Cufon.replace('.step label:not(.terms_label), #share h3', {
	textShadow: '1px 1px #9f3302'
});

// Button click
var buttonClicked = false;

$(function(){
//BEGIN jQuery

	///////////////////////////
	// FORM RESET
	
	$('#email').val('').focus();
	$('#location option').removeAttr('selected');
	
	$('#start_over').click(function(){

		window.location.reload();

		// Back to step one
		/*
		slideTo('#step_1');
		
		// Hide this button
		$(this).fadeOut();
		
		// Remove the classes from go button
		$('#form_1_submit').removeClass('accepted').removeClass('waiting');
		
		// Reset email field
		$('#email').val('');
		
		// Clear voucher container
		$('#voucher_container').html('');
		
		// Reset vouchers
		$('#offer_list').html('<p class="none_selected initial">Please choose a location from the list above<br/>to view available offers</p>');
		$('#location').find('option:first').attr('selected', 'selected').parent('select');
		*/
		return false;
	});
	

	///////////////////////////
	// STEP ONE
	
	$('#form_1_submit').live('click', function() { 
		
		if(buttonClicked) return false;
		
		buttonClicked = true;
		
		$.ajax({
			type: "POST",
		   	url: "/signup/checkEmail",
		   	data: {
				email : $('#email').val()
			},
			dataType: "json",
			beforeSend: function(){
				$('#form_1_submit').addClass('waiting');
			},
		   	success: function( data, textStatus ){
				
				$('#missing').val(data.missing);
				
				if(data.status == 'error') {
					$('#form_1_submit').removeClass('waiting');
					$('#email_error').css({opacity:0}).slideDown('easeInOutExpo').animate({opacity:1});
					setTimeout(function() { $('#email_error').animate({opacity:0}).slideUp(); }, 2000);
				}else{

					$('#form_1_submit').addClass('accepted');
					
					slideTo('#step_2');
					
					$('#start_over').fadeOut();
					setTimeout(function() { $('#start_over').fadeIn(); }, 1000);
									
				} 
				
				buttonClicked = false;
				
			}
		});
		
		return false;
	});
	
	
	///////////////////////////
	// STEP TWO
	
	$('#location').change(function() {
		
		$('.initial').hide();
		$(this).children('option:first').attr('disabled','disabled');
		
		$.ajax({
			type: "POST",
			url: "/signup/loadOffers",
			data: {
				location_id : $('#location').val(),
				email : $('#email').val()
			},
			dataType: "json",
			beforeSend: function(){
				showLoader($('#offer_container'), 'Loading offers!');
				$('#offer_list').hide();
			},
			success: function( data, textStatus ){
								
				if(data.status == 'error') {
					$('#offer_list').html('<p class="none_selected">' + data.msg + '</p>').show();
					
					if($('#missing').val() == '1') {
						$('#step_3 h2').html('<span>3</span>Join the hug club!');
						Cufon.replace('#step_3 h2');
						$('#step_3 .intro').html('Please fill in all your details below to join the Hug Club and we&rsquo;ll keep you up-to-date with all our latest news and offers!');
						$('#offer_container').animate({height: 118},600,'easeInOutExpo',function(){
							hideLoader();
							$('#offer_list').html('<p class="none_selected">Sorry, there are no vouchers available for this location.<br/><a href="#" id="new_member">Click here</a> to join the Hug Club and we&rsquo;ll let you know next time one is available!</p>').show();
							mTop = '-' + ( $('#offer_list').children('p.none_selected').outerHeight() / 2 ) + 'px';
							$('#offer_list').children('p.none_selected').css({marginTop:mTop });
						});

						$('#new_member').live('click',function(){
							loadStep3('skip');
							return false;
						});
					}else{
						$('#step_3 .intro').html('It looks like you&rsquo;re a new member! Please fill in some extra details before downloading your voucher.');
						$('#offer_container').animate({height: 118},600,'easeInOutExpo',function() {
							hideLoader();
						});
					}
				}else{
					$('#offer_list').html(data.html);
					Cufon.replace('#step_2 dt, .loading span', {
						textShadow: '1px 1px #FFF'
					});
					setTimeout(function() {
						$('#offer_container').animate({height: $('#offer_list').height()}, 'normal', function() {
							$('#offer_list').show();
							hideLoader();
							$(this).removeAttr('style');
						});	
					}, 200);
				}
				
			}
		});
		
	});
	
	// Showing terms
	$('#step_2 dl .toggle_terms').live('click',function(){
		$ul = $(this).prev('ul');
		if( !$ul.is(':visible') ){
			$(this).text('Hide terms & conditions');
			$ul.closest('dl').siblings('dl').find('ul.terms').animate({opacity:0}).slideUp('easeInOutExpo').next('.toggle_terms').text('Show terms & conditions');
			$ul.css({opacity:0}).slideDown('easeInOutExpo').animate({opacity:1});
		}else{
			$(this).text('Show terms & conditions');
			$ul.animate({opacity:0}).slideUp('easeInOutExpo');
		}
		return false;
	});
	
	// Offer selecting
	$('#step_2 dl span').live('click', function(){							
				
		if($(this).closest('dl').hasClass('selected')) return false;		
				
		$(this).closest('dl').addClass('selected').siblings('dl').removeClass('selected');
	
		if($('#step_4').is(':visible')) $('#step_4').slideUp();
	
		if($('#missing').val() == '1') {
						
			loadStep3($('#offer_list .selected dd a').attr('rel'));
			
		}else{
			
			$.ajax({
				type: "POST",
				url: "/signup/preLoadVoucher",
				data: {
					email : $('#email').val(), 
					offer_id : $('#offer_list .selected dd a').attr('rel'),
					location_id : $('#location').val()
				},
				dataType: "json",
				beforeSend: function(){
					if($('#step_3').is(':visible')) $('#step_3').slideUp();
					$('#step_4 h2 span').text('3');
					Cufon.replace('#steps .step h2 span', {
						textShadow: '1px 1px #561e06'
					});
				},
				success: function( data, textStatus ){
					slideTo('#step_4',function(){
						loadVoucher();
					});
				}
			});

		}
		
	});
	
	
	///////////////////////////
	// STEP THREE
	
	$('#form_3_submit').live('click', function() {
		
		showLoader($('#step_3 form'), 'Saving your details!',function(){
		
			$.ajax({
				type: "POST",
				url: "/signup/saveDetails",
				data: $('#step_3 form').serialize(),
				dataType: "json",
				success: function( data, textStatus ){
					
					if(data.status == 'error') {
						
						$('#step_3 form .showOnce').remove();
						
						$('#error_msg').slideDown();
						
						$.each( data.toRemove, function(i, val){
													
							if( val != '' ) {
								var $completeInput = $( '#' + val + ' input, #' + val + ' select' );
								
								$completeInput.each(function() {
									$('#step_3 form').append('<input class="showOnce" type="hidden" name="' + $(this).attr('name') + '" value="' + $(this).val() + '" />');
								});
								
								$completeInput.closest('.field').slideUp();
							}
						
						});
					
					}
					else {
						
						$('#missing').val('0');
					
						$('#step_3 form input, #step_3 form select').attr('disabled', 'disabled');
						
						$('#error_msg').slideUp();
						
						if(data.status == 'step4') {
							slideTo('#step_4',function(){
								loadVoucher();
							});
						}
						else if(data.status == 'step5') {
							slideTo('#step_5');
						}
						
					}	
					
					setTimeout(hideLoader,1000);
				}
			});
		});
		
		return false;
	});
	
	function loadStep3(offer_id) {
		
		showLoader($('#step_3 form'), 'Loading form!');
		slideTo('#step_3');
		
		$('#signup_form')
			.load('/signup/loadStep3', { 
				email : $('#email').val(), 
				offer_id : offer_id, 
				location_id : $('#location').val()	
			}, 
			function() {
				Cufon.replace('#steps .step h2', {
					textShadow: '1px 1px #ff9260'
				});
				Cufon.replace('#steps .step h2 span', {
					textShadow: '1px 1px #561e06'
				});
				Cufon.replace('.step label:not(.terms_label), #share h3', {
					textShadow: '1px 1px #9f3302'
				});
		
				setTimeout(function() {
					$('#signup_form').slideDown('normal', function() {
						hideLoader();
					});						
				}, 200);
			}
		);
		
	}
	
	
	///////////////////////////
	// STEP FOUR
	
	$('#print_button').css({opacity:0});

	$('#print_button').click(function(){
		return false;
	});

	// Load the voucher into the hidden div
	function loadVoucher(){
		
		num = new Date().getTime();
		
		$('#voucher_container').load('/signup/display#' + num, function(response, status, xhr) {
			if( status == 'success' ){
				$('#print_button').animate({opacity:1});
				
				// Print button
				$('#print_button').click(function(){
					window.print();
					return false;
				});
			}
		});
	}
	
	///////////////////////////
	// SLIDE FUNCTION
	
	function slideTo(step,cback){
		$step = $(step);
		$old = $('#steps .step:visible');
		
		cHeight = $('#steps').height();
		
		$('#steps').height(cHeight);
		
		// Current step
		$old.css({position:'absolute',top:0,left:0}).animate({top:'-' + cHeight},800,'easeInOutExpo',function(){
			$old.removeAttr('style').hide();
		});
		
		// Next step
		$step.css({position:'absolute',top:cHeight,left:0}).show().animate({top:0},800,'easeInOutExpo',function(){
			$(this).removeAttr('style').show();
		});
		
		// Container
		$('#steps').animate({height:$step.outerHeight()},800,'easeInOutExpo',function(){
			$(this).removeAttr('style');
			if( cback ){
				cback.call(this);
			}
		});
	}
	
	///////////////////////////
	// 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;
	});
	
	
	///////////////////////////
	// LOADING SPINNER
	
	// Show
	function showLoader($parent,text,cBack){
		$parent.find('.loading span').html(text);
		Cufon.replace('.loading span');
		
		if( $.browser.msie && $.browser.version <= 6 ){
			$parent.find('.loading').height($parent.innerHeight());
			$parent.find('select').css({marginLeft:-5000}).addClass('ieSelectFix');
		}
		
		$parent.find('.loading').fadeIn(function(){
			if(cBack){
				cBack.call(this);
			}
		});
	}
	
	// Hide
	function hideLoader(){		
		$('.loading').fadeOut(function(){
			$(this).removeAttr('class').addClass('loading')
		});
		if( $.browser.msie && $.browser.version <= 6 ){
			$('.ieSelectFix').css({marginLeft:0});
		}
	}
	
//END jQuery
});