$(document).ready(function(){
	$("#list a, #listings a, #item a").click(function(){
		window.scrollTo(0, 0);
		// obtener las dimensiones de la pagina //
		var pageDimensions = getPageDimensions();
		var viewportSize = getViewportSize();
		if(viewportSize[1] > pageDimensions[1]){
		pageDimensions[1] = viewportSize[1];
		}
		
		$("#list_form select").hide();
		// insertar la ventana modal //
		$('<div id="modal"></div>').appendTo($("body"));
		$("#modal").css("position", "absolute");
		$("#modal").css("left", "0");
		$("#modal").css("top", "0");
		$("#modal").width(pageDimensions[0] + "px");
		$("#modal").height(pageDimensions[1] + "px");
		
		// meter el div que va a servir de placeholder para la propiedad //
		$("body").append('<div id="property"></div>');
		$("#property").css("left", (pageDimensions[0] - parseInt($("#property").css("width"))) /2 + "px");
		$.get($(this).attr("href"), function(data){
			$("#property").html(data);
			
			// boton de close //
			$("a.close").click(function(){
				$("#property").hide("slow");
				$("#modal").hide();
				$("#property ").remove();
				$("#list_form select").show();
				$("#modal").remove();
				
				return false;
			});
			
			// Minigakleria de imagenes de cada propiedad //
			$("ul#thumbs_prop a:first").attr("id", "current");
			$("ul#thumbs_prop a").click(function(){
				this.blur();
				$("#placeholder_prop").attr("src", $(this).attr("href"));
				$("#placeholder_prop").attr("alt", $(this).attr("title"));
				$("ul#thumbs_prop a").removeClass("current");
				$(this).addClass("current");
				return false;
			});
			
			$("ul#thumbs_prop a").click(function(){
				this.blur();
				$("#placeholder").attr("src", $(this).attr("href"));
				$("#placeholder").attr("alt", $(this).attr("title"));
				$("ul#thumbs_prop a").removeAttr("id", "current");
				$(this).attr("id", "current");
				return false;
			});
			$("#placeholder_prop").after('<a href="#" id="next" title="Next">Next</a>');
			$("#placeholder_prop").after('<a href="#" id="back" title="Previous">Previous</a>');
			
			$("a#next").click(function(){
				$("#placeholder_prop").attr("src", $("#current").parent("li").next("li").children("a").attr("href"));
				$("#current").parent("li").next("li").children("a").attr("id", "current");
				$("#current:first").removeAttr("id");
				tback = setTimeout(function(){if($("ul#thumbs_prop a:first").attr("id") == "current")
				{
					$("#back").hide();	
				} else {$("#back").show();}},20);
				tnext = setTimeout(function(){
				if($("ul#thumbs_prop a:last").attr("id") == "current")
				{
					$("#next").hide();													
				} else {$("#next").show();}}, 20);
				return false;
			});
			$("a#back").click(function(){
				$("#placeholder_prop").attr("src", $("#current").parent("li").prev("li").children("a").attr("href"));
				$("#current").parent("li").prev("li").children("a").attr("id", "current");
				$("#current:last").removeAttr("id");
				tback = setTimeout(function(){if($("ul#thumbs_prop a:first").attr("id") == "current")
				{
					$("#back").hide();	
				} else {$("#back").show();}},20);
				tnext = setTimeout(function(){
				if($("ul#thumbs_prop a:last").attr("id") == "current")
				{
					$("#next").hide();													
				} else {$("#next").show();}}, 20);
				return false;
			});
			tback = setTimeout(function(){if($("ul#thumbs_prop a:first").attr("id") == "current")
			{
				$("#back").hide();	
			} else {$("#back").show();}},20);
			tnext = setTimeout(function(){
			if($("ul#thumbs_prop a:last").attr("id") == "current")
			{
				$("#next").hide();													
			} else {$("#next").show();}}, 20);
			
			$("#property a.ask").attr("href", "interested.php?head=no");
			$("#property a.ask").click(function(){
				$.get($(this).attr("href"), 
				function(data){
					$("#property").append(data).show();
					$("#ask form").append('<input type="hidden" name="the_property_name" id="the_property_name"');
					$("#the_property_name").attr('value', $("#property_name").html());
					$("input.close").click(function(){
						$("div#property, #modal").hide().remove();
					});
					$("#askform").submit(function(){
						if($('#name').attr('value') == ''){
							alert('You Must Provide your Full Name');
							document.getElementById("name").focus();
							return false;
						}
						if($("#email").val() == "" || $("#email").val().indexOf("@")==-1)
						{
							alert("You Must Provide a valid e-mail address");
							$("#email").focus();
							return false;
						}
						if($("#email").attr("value") != $("#confirm_email").attr('value')) {
							alert('Emails entered do not match');
							document.getElementById('email').focus();
							return false;
						}
						if($('#question').attr('value') == ''){
							alert('You Must Fill in your Question');
							document.getElementById("question").focus();
							return false;
						}
					})
				});
				$("div#info_prop").toggle("slow");
				$("ul.clear_prop").toggle();
				$("#property h1").toggle();
				return false;
			});
			$("#property a.sold").attr("href", "sold.php?head=no");
			$("#property a.sold").click(function(){
				$.get($(this).attr("href"), 
				function(data){
					$("#property").append(data).show();
					$("#ask form").append('<input type="hidden" name="the_property_name" id="the_property_name"');
					$("#the_property_name").attr('value', $("#property_name").html());
					$("input.close").click(function(){
						$("div#property, #modal").hide().remove();
					});
					$("#askform").submit(function(){
						if($('#name').attr('value') == ''){
							alert('You Must Provide your Full Name');
							document.getElementById("name").focus();
							return false;
						}
						if($("#email").val() == "" || $("#email").val().indexOf("@")==-1)
						{
							alert("You Must Provide a valid e-mail address");
							$("#email").focus();
							return false;
						}
						if($("#email").attr("value") != $("#confirm_email").attr('value')) {
							alert('Emails entered do not match');
							document.getElementById('email').focus();
							return false;
						}
						if($('#question').attr('value') == ''){
							alert('You Must Fill in your Question');
							document.getElementById("question").focus();
							return false;
						}
					})
				});
				$("div#info_prop").toggle("slow");
				$("ul.clear_prop").toggle();
				$("#property h1").toggle();
				return false;
			});
		});
		return false;											  
	});						   
});
/*
$(document).ready(function(){
	// cargar el contenido que devuelva la pagina thelist
	$.get('thelist.php', function(data){	
		// cargar el contenido generado por "thelist.php" en el div correspondiente //
		$("#list").html(data);
	});						   
});
*/
$(function(){
	$('#list_form').submit(function(){
		var inputs = [];
		$('select', $("#list_form")).each(function(){
			inputs.push(this.name + '=' + escape(this.value));																
		})
		$('input[@type=checkbox]').each(function(){
			if($(this).attr('checked') == true)
			{
				inputs.push(this.name + '=' + escape(this.value));	
			}
		})
		jQuery.ajax({
			data: inputs.join('&'),
			url: $("#list_form").attr('action'),
			timeout: 2000,
			success: function(r){
				$("#list").html(r);
					// adjuntarle la funcionalidad a los links de las propiedades en los listings //
					$("#list a, #listings a, #item a").click(function(){
						window.scrollTo(0, 0);
						// obtener las dimensiones de la pagina //
						var pageDimensions = getPageDimensions();
						var viewportSize = getViewportSize();
						if(viewportSize[1] > pageDimensions[1]){
						pageDimensions[1] = viewportSize[1];
						}
						
						$("#list_form select").hide();
						// insertar la ventana modal //
						$('<div id="modal"></div>').appendTo($("body"));
						$("#modal").css("position", "absolute");
						$("#modal").css("left", "0");
						$("#modal").css("top", "0");
						$("#modal").width(pageDimensions[0] + "px");
						$("#modal").height(pageDimensions[1] + "px");
						
						// meter el div que va a servir de placeholder para la propiedad //
						$("body").append('<div id="property"></div>');
						$("#property").css("left", (pageDimensions[0] - parseInt($("#property").css("width"))) /2 + "px");
						$.get($(this).attr("href"), function(data){
							$("#property").html(data);
							
							// boton de close //
							$("a.close").click(function(){
								$("#property").hide("slow");
								$("#modal").hide();
								$("#property ").remove();
								$("#list_form select").show();
								$("#modal").remove();
								
								return false;
							});
							
							// Minigakleria de imagenes de cada propiedad //
							$("ul#thumbs_prop a:first").attr("id", "current");
							$("ul#thumbs_prop a").click(function(){
								this.blur();
								$("#placeholder_prop").attr("src", $(this).attr("href"));
								$("#placeholder_prop").attr("alt", $(this).attr("title"));
								$("ul#thumbs_prop a").removeAttr("id");
								$(this).attr("id", "current");
								return false;
							});
							
							/* $("ul#thumbs_prop a").click(function(){
								this.blur();
								$("#placeholder").attr("src", $(this).attr("href"));
								$("#placeholder").attr("alt", $(this).attr("title"));
								$("ul#thumbs_prop a").removeAttr("id");
								$(this).attr("id", "current");
								return false;
							}); */
							$("#placeholder_prop").after('<a href="#" id="next" title="Next">Next</a>');
							$("#placeholder_prop").after('<a href="#" id="back" title="Previous">Previous</a>');
							
							$("a#next").click(function(){
								$("#placeholder_prop").attr("src", $("#current").parent("li").next("li").children("a").attr("href"));
								$("#current").parent("li").next("li").children("a").attr("id", "current");
								$("#current:first").removeAttr("id");
								tback = setTimeout(function(){if($("ul#thumbs_prop a:first").attr("id") == "current")
								{
									$("#back").hide();	
								} else {$("#back").show();}},20);
								tnext = setTimeout(function(){
								if($("ul#thumbs_prop a:last").attr("id") == "current")
								{
									$("#next").hide();													
								} else {$("#next").show();}}, 20);
								return false;
							});
							$("a#back").click(function(){
								$("#placeholder_prop").attr("src", $("#current").parent("li").prev("li").children("a").attr("href"));
								$("#current").parent("li").prev("li").children("a").attr("id", "current");
								$("#current:last").removeAttr("id");
								tback = setTimeout(function(){if($("ul#thumbs_prop a:first").attr("id") == "current")
								{
									$("#back").hide();	
								} else {$("#back").show();}},20);
								tnext = setTimeout(function(){
								if($("ul#thumbs_prop a:last").attr("id") == "current")
								{
									$("#next").hide();													
								} else {$("#next").show();}}, 20);
								return false;
							});
							tback = setTimeout(function(){if($("ul#thumbs_prop a:first").attr("id") == "current")
							{
								$("#back").hide();	
							} else {$("#back").show();}},20);
							tnext = setTimeout(function(){
							if($("ul#thumbs_prop a:last").attr("id") == "current")
							{
								$("#next").hide();													
							} else {$("#next").show();}}, 20);
							
							$("#property a.ask").attr("href", "interested.php?head=no");
							$("#property a.ask").click(function(){
								$.get($(this).attr("href"), 
								function(data){
									$("#property").append(data).show();
									$("#ask form").append('<input type="hidden" name="the_property_name" id="the_property_name"');
									$("#the_property_name").attr('value', $("#property_name").html());
									$("input.close").click(function(){
										$("div#property, #modal").hide().remove();
										$("#list_form select").show();
									});
									$("#askform").submit(function(){
										if($('#name').attr('value') == ''){
											alert('You Must Provide your Full Name');
											document.getElementById("name").focus();
											return false;
										}
										if($("#email").val() == "" || $("#email").val().indexOf("@")==-1)
										{
											alert("You Must Provide a valid e-mail address");
											$("#email").focus();
											return false;
										}
										if($("#email").attr("value") != $("#confirm_email").attr('value')) {
											alert('Emails entered do not match');
											document.getElementById('email').focus();
											return false;
										}
										if($('#question').attr('value') == ''){
											alert('You Must Fill in your Question');
											document.getElementById("question").focus();
											return false;
										}
									})
								});
								$("div#info_prop").toggle("slow");
								$("ul.clear_prop").toggle();
								$("#property h1").toggle();
								return false;
							});
							$("#property a.sold").attr("href", "sold.php?head=no");
							$("#property a.sold").click(function(){
								$.get($(this).attr("href"), 
								function(data){
									$("#property").append(data).show();
									$("#ask form").append('<input type="hidden" name="the_property_name" id="the_property_name"');
									$("#the_property_name").attr('value', $("#property_name").html());
									$("input.close").click(function(){
										$("div#property, #modal").hide().remove();
										$("#list_form select").show();
									});
									$("#askform").submit(function(){
										if($('#name').attr('value') == ''){
											alert('You Must Provide your Full Name');
											document.getElementById("name").focus();
											return false;
										}
										if($("#email").val() == "" || $("#email").val().indexOf("@")==-1)
										{
											alert("You Must Provide a valid e-mail address");
											$("#email").focus();
											return false;
										}
										if($("#email").attr("value") != $("#confirm_email").attr('value')) {
											alert('Emails entered do not match');
											document.getElementById('email').focus();
											return false;
										}
										if($('#question').attr('value') == ''){
											alert('You Must Fill in your Question');
											document.getElementById("question").focus();
											return false;
										}
									})
								});
								$("div#info_prop").toggle("slow");
								$("ul.clear_prop").toggle();
								$("#property h1").toggle();
								return false;
							});
						});
						return false;											  
					});						   
			}
		});
		return false;
	});					 
})
$(document).ready(function(){
	$("#resort_links a").click(function(){
			// obtener las dimensiones de la pagina //
		var pageDimensions = getPageDimensions();
		var viewportSize = getViewportSize();
		if(viewportSize[1] > pageDimensions[1]){
		pageDimensions[1] = viewportSize[1];
		}
		
		// insertar la ventana modal //
		$('<div id="modal"></div>').appendTo($("body"));
		$("#modal").css("position", "absolute");
		$("#modal").css("left", "0");
		$("#modal").css("top", "0");
		$("#modal").width(pageDimensions[0] + "px");
		$("#modal").height(pageDimensions[1] + "px");
		window.scrollTo(0,0);
		// meter el div que va a servir de placeholder para la propiedad //
		$("body").append('<div id="guide"></div>');	
		var centerDiv = (pageDimensions[0]/2) - $("#guide").width() / 2;
		$("#guide").css("margin-left", centerDiv + "px");
		// hacer el request para insertar el contenido en el div placeholder de la propiedad //
		$.get($(this).attr("href"), function(data){$("#guide").html(data)
			$("#guide").prepend('<a id="close" class="close" href="#">Close</a>');
			$("#close").click(function(){
				$("#guide").hide().remove();
				$("#modal").hide().remove();
			})																														
		})
		return false;
	})
	return false;
})
 window.onload = function(){
	$("#list_form select").change(function(){
		$("#list_form").submit();
		window.focus();
	})
	$("#list_form input[@type=checkbox]").click(function(){
		$("#list_form").submit();											 
	})
} 
/*window.onload = function(){
	$("#list_form").click(function(){
		find_list = setInterval(function(){
			$("#list_form").submit();
		}, 1000);	
	});
}
*/
