$(document).ready(function() {
	
	$('#slideShow').cycle({
		fx:     'fade', 
		timeout: 4000
	});

	$('#recaptcha_table').find('tr').each(function() {
		$($(this).children('td')[2]).remove();
	});

	$('a[rel*=facebox]').facebox();

	// Load options
	$.getJSON("/state/get-regions/",{ state: $("#state").val()}, function(j){
		var myOptions = '';
		var region = $("#oldRegion").val();
		for (var i = 0; i < j.length; i++) {
			try {
				myOptions += '<option value="' + j[i].optionValue + '"';
				if (j[i].optionValue==region)
				{
					myOptions += " selected='selected'";
				}
				myOptions += '>' + j[i].optionDisplay + '</option>';
			} catch (exc) {
				//No one likes IE6
			}
		}
		// Add in the "other"
		myOptions += '<option value="-1" style="font-weight: bold; color: #444444;"';
		if (region=='-1')
		{
			myOptions += ' selected="selected"';
		}
		myOptions += '>Other, please contact me</option>';
		$("#region").html(myOptions);
	});
	
	// Listing Registration Back button
	$("#alterListingButton").click( function() {
		window.location='/listing/submit-listing-step1/';
	});

	// Advertisement Back button
	$("#alterAdvertisementButton").click( function() {
		window.location='/advertise/submit-step1/';
	});

	// Listing Registration Back button
	$("#proceedListingButton").click( function(e) {
		e.preventDefault();
		if($("#acceptListingCheck").attr("checked")==0) {	// If the check box is not checked
			alert("You must accept the terms and conditions before proceeding.");
			return false;
		}
		window.location='/listing/submit-listing-step3/';
	});

	// Listing Registration Back button
	$("#proceedReservationButton").click( function(e) {
		e.preventDefault();
		if($("#acceptReservationCheckbox").attr("checked")==0) {	// If the check box is not checked
			alert("You must accept the terms and conditions before proceeding.");
			return false;
		}
		$("#reserveForm").submit();
	});

	// Listing Advertisement Back button
	$("#proceedAdvertisementButton").click( function(e) {
		e.preventDefault();
		if($("#acceptAdvertisementCheck").attr("checked")==0) {	// If the check box is not checked
			alert("You must accept the terms and conditions before proceeding.");
			return false;
		}
		window.location='/advertise/submit-step3/';
	});

	$("#state").change( function() {
		$.getJSON("/state/get-regions/",{ state: $(this).val()}, function(j){
			var myOptions = '';
			for (var i = 0; i < j.length; i++) {
				try {
					myOptions += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				} catch (exc) {
					//No one likes IE6
				}
			}
			myOptions += '<option value="-1">Other, please contact me</option>';
			$("#region").html(myOptions);
		});
	});

	$("#otherState").change( function() {
		$.getJSON("/state/get-regions/",{ state: $(this).val()}, function(j){
			var myOptions = '';
			for (var i = 0; i < j.length; i++) {
				try {
					myOptions += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				} catch (exc) {
					//No one likes IE6
				}
			}
			$("#otherRegion").html(myOptions);
		});
	});

	function attachAjaxDeletePhotoEvent(e) {
		e.preventDefault();
		var parentRow = $(this).parents("td")[0];
		if (confirm("Really Delete?")) {
			$.get($(this).attr("href"), function(d) {
				if (d=="Success" || d=="") {
					$(parentRow).children(".photo").remove();
				}
			});
		}
	}

	// Apply delete links
	$(".deletePhoto").click(attachAjaxDeletePhotoEvent);
	
	// Enforce state to be selected on search button
	$("#searchButton").click(function(e) {
		e.preventDefault();

		if($("#otherState").val()!="") {
			$("#search").submit();
		} else {
			alert("Please select a state.");
		}
	});

	/*
	** Listing photo swapper
	*/
	for(i=1; i<7; i++) {
		$("#smallPhoto"+i).click(function(){
			// Swap this photo with the main photo
			var mainSrc = $("#mainPhoto").attr("src");
			$("#mainPhoto").attr("src", $(this).attr("src"));	// Main photo becomes little one
			$(this).attr("src", mainSrc);
		});
	}

	/*
	**
	** Number of words test (used in listing submission step 1
	**
	*/
	var wordLen = 110; // Maximum word length

	$("#description").keyup(function(e) {
		var len = $(this).val().split(/[\s]+/);
		if(len.length > wordLen){
			if ($(this).val()!=$(this).attr("rel"))
			{
				$(this).val($(this).attr("rel"));
			}
			return false;
		} else {
			$(this).attr("rel",$(this).val());
		}
		return true;
	});

});