$(document).ready(function(){		
	if ($("#propertyTypeID").length) {
		togglePropertyTypeFields();
		$("#propertyTypeID").change (function () {
			togglePropertyTypeFields();
		});
	}
});

$(document).ready(function(){	
	if ($("#advancedPropertySearch").length) {		
		showPropertyFieldSearchContent();
		$("#propertyTypeID").change(function() {
			showPropertyFieldSearchContent();
		});
	}
	
});

//Load fancybox for property photos.
$(document).ready(function() { 
	
	if ($("#property-details a.fancybox").length) {
	
		 $("a.fancybox").fancybox({
			'overlayShow' : true,
			'overlayOpacity' : 0.3,
			'overlayColor' : '#666',
			'zoomSpeedIn' : 600,
			'zoomSpeedOut' : 500,
			'easingIn' : 'easeOutBack',
			'easingOut' : 'easeInBack',
			'showCloseButton' : true,
			'centerOnScroll' : true
		 }); 
	}
}); 

//Navigate through the photos for a specific property.
$(document).ready(function(){
	
	if ($("#property-details").length) {
		
		//When the next button is clicked, hide the current photo and show the next one.
		$("#property-details .photo-navigation .next").click( function () {
			
			//Get a reference to the next photo, if there is one.
			var next = $("#property-details .active-photo").next(".inactive-photo");
			
			//Only proceed if there is another photo after this one.
			if (next.length) {
				
				//Hide the currently active photo.
				$("#property-details .active-photo").removeClass("active-photo").addClass("inactive-photo");;
								
				//Show the next photo.
				next.removeClass("inactive-photo").addClass("active-photo");
			}
		});
		
		//When the previous button is clicked, hide the current photo and show the last one.
		$("#property-details .photo-navigation .prev").click( function () {
			
			//Get a reference to the previous photo, if there is one.
			var prev = $("#property-details .active-photo").prev(".inactive-photo");
			
			//Only proceed if there is another photo before this one.
			if (prev.length) {
				
				//Hide the currently active photo.
				$("#property-details .active-photo").removeClass("active-photo").addClass("inactive-photo");;
								
				//Show the previous photo.
				prev.removeClass("inactive-photo").addClass("active-photo");
			}
		});			
	}
	
});

//Navigate through the featured listings.
$(document).ready(function(){	

	if ($("#featured-listings").length) {
		
		//When the next button is clicked, hide the current listing and show the next one.
		$("#featured-listings .featured-arrows .next").click( function () {

			//Get a reference to the next listing, if there is one.
			var next = $("#featured-listings .active-property").next(".featured-content");
			
			//Only proceed if there is another listing after this one.
			if (next.length) {
				
				//Hide the currently active listing.
				$("#featured-listings .active-property").removeClass("active-property").addClass("inactive-property");;
								
				//Show the next listing.
				next.removeClass("inactive-property").addClass("active-property");
			}
		});
		
		//When the previous button is clicked, hide the current listing and show the last one.
		$("#featured-listings .featured-arrows .prev").click( function () {
			
			//Get a reference to the previous listing, if there is one.
			var prev = $("#featured-listings .active-property").prev(".featured-content");
			
			//Only proceed if there is another listing before this one.
			if (prev.length) {
				
				//Hide the currently active listing.
				$("#featured-listings .active-property").removeClass("active-property").addClass("inactive-property");;
								
				//Show the previous listing.
				prev.removeClass("inactive-property").addClass("active-property");
			}
		});		
	}

});

function showPropertyFieldSearchContent() {
	
	var propertyTypeID = $("#propertyTypeID").attr("value");
	url = SETTINGS.webRoot + "properties/getpropertytypefields&allFieldsRequired=0&&propertyTypeID="+propertyTypeID;
	$.ajax({
	    url: url,
	    type: 'GET',
	    success: function(html){
	    	$("#advancedPropertySearch tr:last").before($(html));
	    }
	});	
}

function togglePropertyTypeFields() {
	
	var propertyTypeID = $("#propertyTypeID").attr("value");

	if ($("#propertyID").length) {
		var propertyID = $("#propertyID").attr("value");
	}
	
	//Delete out the old fields if necessary.
	$("tr.propertyTypeField").each (function() {
		$(this).remove();
	});
	
	
	url = SETTINGS.webRoot + "admin/properties/getpropertytypefields&allFieldsRequired=1&propertyTypeID="+propertyTypeID;
	if (propertyID) {
		url += "&propertyID="+propertyID;
	}
	
	$.ajax({
	    url: url,
	    type: 'GET',
	    success: function(html){
		
			//Only continue if there is content to place.
			if (html.length) {
	    	
		    	//Inser the new content.
		    	$("#propertyAgentID").parent().parent().after($(html));
		    	
		    	//Loop over each input within a tr and get the id.
		    	var requiredFields = new Array();
		    	var i = 0;
		    	$("tr.field input", $("#propertyTypeID").parent().parent().parent()).each (function() {
		    		requiredFields[i] = $(this).attr("id");
		    		i++;
		    	});
		    	requiredFields = requiredFields.join(",");
		    	
		    	//Append the required fields.
		    	$("#propertyTypeID").parent().parent().parent().parent().parent().prepend($("<div><input type='hidden' name='required_fields' id='required_fields' value='"+requiredFields+"' /></div>"));
			}
		}
	});	
}
