﻿	var contextPath = "http://www.riversideca.gov:8080/athomews";
	
	function toProperCase(s) {
		return s.toLowerCase().replace(/^(.)|\s(.)/g, 
			function($1) { return $1.toUpperCase(); });
	}

	/* Closure for autocomplete ajax calls. */
	var autocompleteOptions = function() {
		return {
			dataType: 'jsonp',
			formatItem: function(row, i, max, value, term) {
				return toProperCase(value);
			},
			parse: function(data) {
				var properties = new Array();
				for(var i=0; i < data.length; i++) {
					properties[properties.length] = { data:data[i],
						value: toProperCase(data[i].address),
						result: toProperCase(data[i].address)
					};
				}
				return properties;
			},
			max: 100,
			minChars: 4,
			width: 180 /* changed width from 200px to 180px - CL 04.13.10 */
		}
	}

	/* Handles Go button click. */
	function submitAddress() {
		$.ajax({
			type: 'GET',
			dataType: 'jsonp',
			url: contextPath + '/property/searchByAddress',
			data: {q:$('#address').val()},
			error: function(data) {
				alert('No Data Found!');
			},
			success: function(data) {
				$('#address').val(toProperCase(data[0].address));
				populateHomeDataTable(data[0]);
			}
		});
	}

	function populateHomeDataTable(property) {
		$.ajax({
			type: 'GET',
			dataType: 'jsonp',
			url: contextPath + '/property/getPropertyLinks',
			data: {q:property.address},
			error: function(data) {
				alert('No Data Found!');
			},
			success: function(links) {
				$('#ward').html('<a href="http://www.riversideca.gov/council/ward-' + property.ward + '.asp">' + property.ward  + '</a>');
				$('#community').html('<a href="http://www.riversideca.gov/athomeinriverside/neighborhoods-' + property.community.toLowerCase().replace(/ /g, '') + '.asp">' + property.community + '</a></td>');
				$('#library').html('<a href="' + links[property.library] + '">' + property.library + '</a>');
				$('#park').html('<a href="' + links[property.park] + '">' + property.park + '</a>');
				$('#hospital').html('<a href="' + links[property.hospital] + '" target="_blank">' + property.hospital + '</a>');
				$('#shopping').html('<a href="' + links[property.shopping] + '">' + property.shopping + '</a>');
				$('#schoolDistrict').html('<a href="' + links[property.schoolDistrict] + '" target="_blank">' + toProperCase(property.schoolDistrict) + '</a>');
				$('#elementarySchool').html('<a href="' + links[property.elementarySchool] + '">' + toProperCase(property.elementarySchool) + '</a>');
				$('#middleSchool').html('<a href="' + links[property.middleSchool] + '">' + toProperCase(property.middleSchool) + '</a>');
				$('#highSchool').html('<a href="' + links[property.highSchool] + '">' + toProperCase(property.highSchool) + '</a>');
				$('#propertyDataWrapper').slideDown('fast');
				$('#propertyTable').highlightFade();
			}
		});
	}

	$(document).ready(function() {
	    var url = contextPath + '/property/searchByAddress';
		$('#address').autocomplete(url, autocompleteOptions()
		).result(function(e, property, formatted) {
			populateHomeDataTable(property);
		});

		$('#submit').click(function() { submitAddress() });

		$("a#hide img").click(function() {
			$('#propertyDataWrapper').fadeOut();
			return false;
		});
	});
