$(document).ready(function(){
	if($('.slideshow').length > 0){		
		$('.slideshow').cycle({ 
			speed:  800 
		}); 
	}
	$('#nav li').hover(
		function(){
			var leftPos = $(this).position().left;
			$(this).children('ul').css('left',leftPos);
			$(this).children('ul').fadeIn('100');
		}, function(){
			$(this).children('ul').fadeOut('20');
		}
	);
	$("a.fancy").fancybox({
		'titleShow': false
	});
	$("a.inlinefancy").fancybox({
		'titleShow': false,
		'autoDimensions': true,
		'autoScale': false
	});
	$(".newsfeed a.readmore").fancybox({
		'titleShow': false,
		'autoScale' : false
	});
	if($('.interactivemap').length > 0){
		$('.releaselisting h2').hover(
			function(){
				var myClass = $(this).attr("class");
				if(myClass != ''){
					var overlaypointer = "overlaymap-"+myClass;
					$('.interactivemap .'+overlaypointer).fadeIn(100);
				}
			}, function(){
				$('.overlaymap').fadeOut(50);
			}
		);
	}
		
	$('div.homes-for-sale form, form.ajax-submit').submit(function(){
		var form = $(this);
			
		if(form.hasClass('submitting')){
			return false;	
		}
		
		form.addClass('submitting');
		form.find('input, textarea').attr("readonly","readonly");
				
		formAction = form.attr('action');
		
		postData = new Object;
		postData['json'] = true;
		form.find('input, textarea').each(function(){
			if($(this).attr('name')){
				postData[$(this).attr('name')] = $(this).val();	
			}
		});
	
		$.ajax({
			type: "POST",
			url: formAction,
			data: postData,
			dataType:"json",
			error: function(jqXHR, textStatus, errorThrown){
				form.removeClass('submitting');
				form.find('input, textarea').removeAttr("readonly","readonly");
				
				if(textStatus !== "abort"){
					alert('An unexcepted error occurred. Try again.');
					return;
				}
			},
			success: function(response){
				if(response.success){				
//					form.html(response.replace_form_html);
					if(response.go_to_url){
						window.location.href = response.go_to_url;	
					}
				}
				else {
					form.removeClass('submitting');
					form.find('input, textarea').removeAttr("readonly","readonly");

					if(response.errors){
						error = "";
						
						for(i in response.errors){
							error += " - " + response.errors[i] + "\n";	
						}
						
						alert("The following errors occurred:\n" + error);
					}
					else {
						alert("Failed to save your details. Please try again");	
					}
				}
			}
		});
		
		return false;
	});
});
