var defaultErrorMessage = "";
var closing = false;

$(document).ready(function() 
{
	searchTerm = 'destinationCity';
	getAdwordsCommerce(locationid, searchTerm);

	truncateContent();

	$('#useraddattraction').ajaxForm(options);

	$('.content_inner div.add a').click(function()
	{
		return showAddAttractionOverlay();
	});

	$('#email').blur(function(){
		if (!closing) checkEmail();
	});

	$('.close').click(function(){closing = true; $("#addattractionform").jqmHide(); $.unblockUI(); return false;});

	$('#emailpassword').click(function(){return emailPassword();});

	defaultErrorMessage = $('#error_message').html();
});

function truncateContent()
{
	truncateDom('#local_history', 250);
	truncateDom('#restaurants', 220);
	truncateDom('#bar_clubs', 220);

	$('.touristic_information').each(function(i, e){
		if (i > 0) truncateDom(e, 120);
	});
}

function truncateDom(_domId, _length)
{
	$(_domId).truncate({max_length: _length});
}

var options = {
	beforeSubmit:  showRequest,
	success:       showResponse
};

function emailPassword()
{
	var email = $('#email').val();
	if ( email.length < 4 ) return;

	var url = '/AddUserAttraction.html';
	var data = "action=emailpassword&email=" + encodeURIComponent(email);
	
	$.getJSON(url,data,
		function(response){
			switch(response['result'])
			{
				case 'success':
					$('#emailpassword').fadeOut("fast");
					$('.email_info').fadeIn("fast");
					break;
				case 'error':
					$('#error_message').html("Sorry, we could not send \
												you your password.");
					break;
			}
		});
}

function checkEmail()
{
	showMessage('Please wait...');

	var email = $('#email').val();
	if ( email.length < 4 ) return;

	var url = '/AddUserAttraction.html';
	var data = "action=checkemail&email=" + encodeURIComponent(email) + "&locationid=" + locationid;

	$.getJSON(url,data,
		function(response){
			if (response.emailExists)
			{
				$('#password').attr('disabled', '');
				$('#firstname').attr('disabled', 'true');
				$('#lastname').attr('disabled', 'true');

				$('.password').show();
				$('.firstname').hide();
				$('.lastname').hide();

				$('#error_message').addClass('error');
				$('#error_message').html('This email already exists in our system. Please enter your password');

				$('#emailExists').val('true');
			}
			else
			{
				$('#password').attr('disabled', 'true');
				$('#firstname').attr('disabled', '');
				$('#lastname').attr('disabled', '');

				$('.password').hide();
				$('.firstname').show();
				$('.lastname').show();

				$('#error_message').removeClass('error');
				$('#error_message').html(defaultErrorMessage);

				$('#emailExists').val('false');
			}

			 $.unblockUI();
		});
}

function showAddAttractionOverlay()
{
	closing = false;

	$("#addattractionform").jqm({
			toTop:true,
			overlay: 50
		});

	$('#password').attr('disabled', 'true');
	$('#firstname').attr('disabled', '');
	$('#lastname').attr('disabled', '');

	$("#addattractionform").jqmShow();

	return false;
}

function showRequest(formData, jqForm, options)
{
}

function showResponse(responseText, statusText)
{
	response = eval('(' + responseText + ')');

	switch(response['result'])
	{
		case 'success':
			$("#addattractionform").jqmHide();
			showAlertMessage('Thank your for your review. It will be posted online very soon.');

			break;
		case 'error':
			errors = response.errors;

			resetErrors();

			for (field in errors)
			{
				for ( error in errors[field])
				{
					$('label[for='+field+']').addClass('error');
				}
			}

			if (response['error_message'] != null)
			{
				$('#error_message').html(response['error_message']);
				$('#error_message').fadeIn("slow");
				$('#error_message').addClass('error');
			}

			break;
		default:
			showAlertMessage('Unknown Reponse from Server. Please Try Again.');
			break;
	}
}

function resetErrors()
{
	$('#error_message').hide();
	$('#error_message').html('');

	$('.error').removeClass('error');

	if (errors != null)
	{
		for (field in errors)
		{
			for ( error in errors[field])
			{
				//This loops thru all error messages for a given form field
				//nothing to do so far.
			}
		}
	}
}
