((typeof(jQuery15) != 'undefined') ? jQuery15 : jQuery)(function($) 
{
	// Hash of call back functions that provide custom initialisation for site modal dialogs
	// The modal dialog seems to lose button click events the second time it is popped up, so we attach them on each show
	var _modalInits = {
		// NEWSLETTER REGISTRATION - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		'#register-form': function()
		{
			$('#registerSubmit').click(
				function(e)
				{
					var ok = true;
					var email = $('#registerEmail').val();
					
					// Can do pre-server validations here:
					if(!email)
					{
						ok = false;
						
						alert('You must enter an email address');
					}
					//
					
					if(ok)
					{
						var data = [
							'title=' + escape($('#registerTitle').val()),
							'fn=' + escape($('#registerFirstName').val()),
							'ln=' + escape($('#registerLastName').val()),
							'email=' + escape(email)
						];			
						
						$.ajax({
							url: '/plugins/AbsoluteVacuum/ajax.ashx?handler=MailshotActionSrv&action=Create&' + data.join('&'),
							
							// The AJAX handler fired okay - could still be an error in the data though
							success:function(data, textStatus, jqXHR)
							{	
								var resp = getAjaxData(data);
								if(resp && resp.Data && resp.Data.Success)
								{
									$('#register-form-entry').hide();
									$('#register-form-thanks').show();
								}
								else if(resp && resp.Data)
									alert(resp.Data.ErrorString); 
							},
							
							// System failure of AJAX handler
							error:function(data, textStatus, errorThrown)
							{
							}
						});
					}
				}
			);
		}
		// END NEWSLETTER REGISTRATION - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		,
		// CONTACT US - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		'#contact-us-form': function()
		{
			$('#contactUsSubmit').click(
				function(e)
				{
					var ok = true;
					
					var postdata = {
						Name: 			$('#contactUsName').val(),
						Email: 			$('#contactUsEmail').val(),
						Telephone:	$('#contactUsTelephone').val(),
						Message: 		$('#contactUsMessage').val()
					}					
					
					// Can do pre-server validations here:
					ok = postdata.Name && (postdata.Email || postdata.Telephone) && postdata.Message;
						
					if(!ok)
						alert('Please enter your name, either an email address or a telephone number, and your query so we can help you as quickly as possible');
					//
					
					if(ok)
					{
						$.ajax({
							type: 'POST',
							
							url: '/plugins/AbsoluteVacuum/ajax.ashx?handler=ContactActionSrv',
							
							processData:false,
							data:cFront.Xml.serialize(postdata),
							
							// The AJAX handler fired okay - could still be an error in the data though
							success:function(data, textStatus, jqXHR)
							{	
								var resp = getAjaxData(data);
								if(resp && resp.Data && resp.Data.Success)
								{
									$('#contact-us-form-entry').hide();
									$('#contact-us-form-thanks').show();
								}
								else if(resp && resp.Data)
									alert(resp.Data.ErrorString); 
							},
							
							// System failure of AJAX handler
							error:function(data, textStatus, errorThrown)
							{
							}
						});
					}
				}
			);
		}
		// END CONTACT US - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		
	};

	// Simple modal popups - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	$('.typeof__xsimplemodaltrigger').click(
		function(e)
		{
			$(this.hash).modal({overlayClose:true, onShow:_modalInits[this.hash]});
		
			e.preventDefault();
			e.stopPropagation();
		}
	);
	//
	
	// Search return key handling (needed for IE)
	$('.isearchbox').keydown(
		function(e)
		{
			if(event.keyCode == '13')
			{
				$('.isearchtrigger').click();
				
				e.preventDefault();
				e.stopPropagation();
			}
		}
	);
	
});
