$(document).ready(function() {

	$('#expand-interest-list-button').click(function(e){
		e.preventDefault();
		expandQuickSignup();
	});

	$('#quick-signup #expand-icon').click(function(e){
		e.preventDefault();
		expandQuickSignup();
	});	
	
	$('#quick-signup #contract-icon').click(function(e){
		e.preventDefault();
		contractQuickSignup();
	});
	
	// validate signup form on keyup and submit
	// Uses jquery validate, which must be loaded before this script

	$("#quick-signup form").validate({
		rules: {
			first_name: "required",
			last_name: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: "Please enter a valid email address"
		},
		submitHandler: function(form) {
			
			// Submit the form to the server for processing
		   	$(form).ajaxSubmit({
				url:'/quicksignup/post',
				dataType: 'json',
				success:showResponse,
				error:showError
			});

			return false;
		}

	});
	
	$('#quick-signup .submit-button').click(function(e){
		e.preventDefault();
		$("#quick-signup form").submit();
	});
	
	$('#interest-list-button').live('click', function(e){
		e.preventDefault();		
		var quick_signup_info = $("#quick-signup form").serialize();
		window.location = '/sign-up?' + quick_signup_info;
	});

	// For both the realtor and working-with-realtor checkboxes, add or remove to the tag list
	// when the checkbox is selected or deselected:
	
	$('#realtor_checkbox').live('click', function(e) {		
		tag_checkbox(11, this);
	});	

	$('#working_with_realtor_checkbox').live('click', function(e) {		
		tag_checkbox(13, this);
	});	
	
});

function showResponse(data)
{
	if (data) 
	{
		if (data.status == 'success') 
		{
			$('#quick-signup form').fadeOut('fast', function(){
				$('#quicksignup-thankyou-message').fadeIn('fast');
			});

			// Tie lead id and email to clicky analytics data
			clicky_custom.session = {
				lead_id: data.id,
				username: data.email,
				signup: "1"
			};
			
			// Fire off a Google Analitics goal hit
			pageTracker._trackPageview('/ajax-goals/quick-signup');
		  
			// Log action in clicky
			clicky.init(66382742); // Let's clicky "see" the new clicky_custom information
			clicky.goal( '113' );  // Logs a "quick-signup" goal hit in clicky
			clicky.log('/quick-signup/thank-you');  // Logs a fake page hit in clicky
			
			// trigger google adwords conversion
			$.ajax({
				url: "http://www.googleadservices.com/pagead/conversion/1067464410/?label=signup&amp;guid=ON&amp;script=0"
			});
		}
		else {
			showError();
		}
	}
	else
	{
		showError();
	}
}

function showError()
{
	$('#quick-signup form').fadeOut('fast',function(){
		$('#quicksignup-error-message').fadeIn('fast');	
	});
	
	// Send error to webmaster
	$.get('/error/report', {
			subject:'Error processing QSU', 
			error:'There was a problem processing a quick signup.\nLead Email: ' + $('#email').val() + '. Page: ' + window.location.href
		}
	)
}

function expandQuickSignup()
{
	$('#expand-interest-list-button').slideUp();
	$('#toggle-form-container').slideDown(function(){
		$('#first_name').focus();
		$('#quick-signup #contract-icon').show();
		$('#quick-signup #expand-icon').hide();
	});
}

function contractQuickSignup()
{
	$('#expand-interest-list-button').slideDown();
	$('#toggle-form-container').slideUp(function(){
		$('#quick-signup #contract-icon').hide();
		$('#quick-signup #expand-icon').show();
	});
}

function tag_checkbox(tag_id, checkbox_element)
{
	// First step, get the existing tag ids into an array
	var tag_list = $('#tag_ids').val();
	var tag_array = [];
	
	if (tag_list != '') tag_array = tag_list.split(',');
	
	// Next, remove the Realtor tag from the array if it exists
	var idx = tag_array.indexOf(tag_id + ''); // The + '' converts the number to a string
	if(idx != -1) tag_array.splice(idx, 1);
	
	// Now add the realtor tag if the checkbox is checked
	if($(checkbox_element).attr("checked") == true)
	{
		tag_array.push(tag_id); // Add the Realtor tag.  11 is the Realtor tag
	}
	
	// And finally, update the tag_ids hidden field
	$('#tag_ids').val(tag_array);
}


