/**
 * @author Dan
 */
$(document).ready(function(){

	$('.ajaxForm').each(function()
	{
		$(this).ajaxForm({
		dataType:		'json',
		beforeSubmit:	hideErrors,
		success:		checkJson
	});
	});

	function hideErrors(){
		$('.error').removeClass('error');
		$('.onError').hide();
		$('.addedOnError').remove();
	}

	function checkJson(json)
	{
		if(json.success == false){
			for(var i=0; i<json.fields.length; i++){
				$('#'+json.fields[i]).addClass('error');
			}
            $('#_error_result').trigger('ajaxForm:failure');
			$('#_error_result').html('<p id="form_result" class="error" style="display: block;">'+json.emailError+'</p>');
		}
		else
		{
			$('#_error_result').trigger('ajaxForm:success');
			$('#_error_result').html('<div id=form_result style="margin:10px;display:block"><h3>Request Sent</h3><p>We will contact you shortly. Thanks</p></div>');
		}
	}
	
	$('#brochure_form').ajaxForm(
	{
		dataType:		'json',
		beforeSubmit:	hideErrors,
		success:		checkJsonBr
	});

	function hideErrors()
	{
		$('.error').removeClass('error');
		$('.onError').hide();
		$('.addedOnError').remove();
	}

	function checkJsonBr(json)
	{
		if(json.success == false)
		{
			for(var i=0; i<json.fields.length; i++)
			{
				$('#'+json.fields[i]).addClass('error');
			}
			$('#b_error_result').html('<p id="form_result" class="error" style="display: block;">'+json.emailError+'</p>');
			return;
		}
		else
		{
			$.fancybox.close;
			$('#b_error_result').html('<div id=form_result style="margin:10px;display:block">Thankyou for downloading the brochure.</div>');
		}
		$.fancybox.close();
		window.location.href = json.pdf;
	}

	$('#delivery_method').change(function()
	{
		$('#updateBtn').click();
	});
	
	$('#updateBtn').bind('click', function()
	{
		$(this).parent('form').submit();
	});
	
	$('#checkOutBtn').bind('click', function()
	{	
		if($('#delivery_error') && $('#delivery_method').val() == 0)
		{
			$('#delivery_error').text('Please select an option...');
			return false;
		}
		else
		{
			$(this).parent('form').submit();
		}
	});
	

	$(".more").next("em").hide();
	$(".more").hover(function() 
	{
		$(this).next("em").stop(true, true).animate({opacity: "show", right: "-120"}, "slow");
	}, function() 
	{
		$(this).next("em").animate({opacity: "hide", right: "-110"}, "fast");
	});

	
	$(".popUp").fancybox(
	{
		'scrolling'		: 'no',
		'titleShow'		: false,
		'onClosed'		: function() 
		{
			$("#b_error_result").hide();
		}
	});
});

