// http://www.crockford.com/javascript/private.html
function AClass(){
	// this code is the constructor executing
	var that = this; // required so that private functions can access this.
	this.privateVariable = 'hmm'; 

	
	// Privileged functions are accessible from public scope, and have access to private members
	this.privilegedFunction = function(){
		privateFunction();
		document.title += this.privateVariable;
	}
	
	// Private functions are NOT accessible from public scope, have access to private members
	var privateFunction = function(){
		document.title += that.privateVariable;
	}
}

// public functions have access to privileged functions, not private functions...
AClass.prototype.publicFunction = function(){
	this.privilegedFunction();
}

AClass.staticFunction = function(){
}

function AutocompleteLocation() {
	
	this.apply = function() {		 
		var optionsAutocompleteLocation = {
			minChars: 0,
			width: 310,
			max: 20,
			selectFirst: false	,
			matchContains: "word",
			autoFill: false,
			parse: function(data) {
				data = window["eval"]("(" + data + ")");
				parsed = new Array();
				for (var i=0; i < data.suggestions.length; i++) {
					var location = data.suggestions[i];	
					
	//				if(window.console){	window.console.log(location);	}
					parsed[parsed.length] = {
						data: location,
						value: "123",
						result: AutocompleteLocation.addressToString(location)
					};
				}
				return parsed;
			},
			formatItem: function(row, i, max) {
				return "<div class='S" + row.Source + "'>" + AutocompleteLocation.addressToString(row) + "</div>";
			},
			f1ormatMatch: function(row, i, max) {
				return row.CountryCode;
			},
			f1ormatResult: function(row) {
				return row.CountryCode;
			}
		};

		$("#address").autocomplete('/location/suggest/format/json/', optionsAutocompleteLocation);
		$("#address").result(function(event, data, formatted) {
				var hsh = $("#location_id");
				var src = $(event.target);
	
				var loc = $("#location");
				var adr = $("#address");
				
				// If the address object found by autocompletion contains a 'venue',
				// then autocomplete the location(=venue) textfield with that value
				// only iff the address object is from the Flogs DB.
				if(data.Source == -1002 /* From Flogs DB */)
				{
					loc.val(data.Venue);
					adr.val(data.Address);
				}
				
				hsh.val(data.Id + "," + data.Source);
				src.data('original_value', jQuery.trim(src.val()));
		});
			
		$("#address").change(function(event) {
				var hsh = $("#location_id");
				var src = $(event.target);
				
				// Determine if the src value has really changed
				if(jQuery.trim(src.val()) != src.data('original_value'))
				{
					// real textual change.
					hsh.val('0,0');
				}
			});

                $("#address_detail").autocomplete('/location/suggest/format/json/googleonly/true', optionsAutocompleteLocation);
		$("#address_detail").result(function(event, data, formatted) {
				var hsh = $("#address_detail_source");
				var src = $(event.target);

				var adr = $("#address_detail");

				// If the address object found by autocompletion contains a 'venue',
				// then autocomplete the location(=venue) textfield with that value
				// only iff the address object is from the Flogs DB.
				if(data.Source == -1002 /* From Flogs DB */)
				{
					adr.val(data.Address);
				}

				hsh.val(data.Source);
				src.data('original_value', jQuery.trim(src.val()));
		});

		$("#address_detail").change(function(event) {
				var hsh = $("#address_detail_source");
				var src = $(event.target);

				// Determine if the src value has really changed
				if(jQuery.trim(src.val()) != src.data('original_value'))
				{
					// real textual change.
					hsh.val('0');
				}
			});


		$("#location").autocomplete('/location/suggest/format/json/localonly/true', optionsAutocompleteLocation);
		
		$("#location").result(function(event, data, formatted) {
			var hsh = $("#location_id");
			var src = $(event.target);
	
			var loc = $("#location");
			var adr = $("#address");
	
			loc.val(data.Venue);
			adr.val(data.Address);
			
			hsh.val(data.Id + "," + data.Source);
			src.data('original_value', jQuery.trim(src.val()));
		});
		
		$("#location").change(function(event) {
			var hsh = $("#location_id");
			var src = $(event.target);
	
			// Determine if the src value has really changed
			if(jQuery.trim(src.val()) != src.data('original_value'))
			{
				// real textual change.
				hsh.val('0,0');
			}
			
		});
		
		var onSelectAddress = function(event, data, formatted) {
			var $parent = $(this).parents('tr');
			var loc = $(".location", $parent);
			var adr = $(".address", $parent);
			var loc_id = $(".location_id", $parent);
			
			// If the address object found by autocompletion contains a 'venue',
			// then autocomplete the location(=venue) textfield with that value
			// only iff the address object is from the Flogs DB.
			if(data.Source == -1002 /* From Flogs DB */)
			{
				loc.val(data.Venue);
				adr.val(data.Address);
				loc_id.val(data.Id);
			}
			
			hsh.val(data.Id + "," + data.Source);
			src.data('original_value', jQuery.trim(src.val()));
		}
		
		var onSelectLocation = function(event, data, formatted) {		
			var $parent = $(this).parents('tr');
			var loc = $(".location", $parent);
			var adr = $(".address", $parent);
			var loc_id = $(".location_id", $parent);

			loc.val(data.Venue);
			adr.val(data.Address);
			loc_id.val(data.Id);
			
/*			var hsh = $("#location_id");
			var src = $(event.target);
	
			var loc = $("#location");
			var adr = $("#address");
	
			
			hsh.val(data.Id + "," + data.Source);
			src.data('original_value', jQuery.trim(src.val()));*/
		}
		
		$('input.location').each(function(){
			$this = $(this);
			$this.autocomplete('/location/suggest/format/json/localonly/true', optionsAutocompleteLocation);
			$this.result(onSelectLocation);
		});
		
		$('input.address').each(function(){
			$this = $(this);
			$this.autocomplete('/location/suggest/format/json/', optionsAutocompleteLocation);
			$this.result(onSelectAddress);
		});
	}
}

AutocompleteLocation.addressToString = function(obj) {
	var result = [];
	if(obj.Venue) {result[result.length] = obj.Venue;}
	if(obj.Address) {
		result[result.length] = obj.Address
	} else {;
		if(obj.Street) {result[result.length] = obj.Street;}
		if(obj.PostalCode) {result[result.length] = obj.PostalCode;}
		if(obj.City) {result[result.length] = obj.City;}
		if(obj.Region) {result[result.length] = obj.Region;}
		if(obj.Country) {result[result.length] = obj.Country;}
	}
	
	return result.join(", ");
}

function LocationFormHandler(frmLocation){
	
	this.frmLocation = frmLocation;

	this.checkboxLocationUnknown = $('#location_unknown', this.frmLocation);
	this.textfieldLocation = $('#address', this.frmLocation);
	this.textfieldAddress = $('#location', this.frmLocation);
	
	this.originalValueLocation = '';
	this.originalValueAddress = '';

	this.onCheckLocationUnknown = function(ev){
		if(this != ev.data) { return ev.data.onCheckLocationUnknown(ev); };
		
 		if(this.checkboxLocationUnknown.attr('checked')){
			this.originalValueLocation = this.textfieldLocation.val();
			this.originalValueAddress = this.textfieldAddress.val();
			this.textfieldLocation.val('').addClass('content-form-text-disabled').attr('disabled','disabled');
			this.textfieldAddress.val('').addClass('content-form-text-disabled').attr('disabled','disabled');
		} else {
			if(this.textfieldLocation.attr('disabled')){
				this.textfieldLocation.val(this.originalValueLocation);
			}
			if(this.textfieldAddress.attr('disabled')){
				this.textfieldAddress.val(this.originalValueAddress);
			}
			this.textfieldLocation.removeClass('content-form-text-disabled').removeAttr('disabled');
			this.textfieldAddress.removeClass('content-form-text-disabled').removeAttr('disabled');
		}
	}
	
	this.apply = function()		{
		this.checkboxLocationUnknown = $('#location_unknown', this.frmLocation);
		this.textfieldLocation = $('#address', this.frmLocation);
		this.textfieldAddress = $('#location', this.frmLocation);
		$(this.checkboxLocationUnknown).bind('click', this, this.onCheckLocationUnknown);
		this.onCheckLocationUnknown({data: this});
	}
}


$(document).ready(function(){
	var acl = new AutocompleteLocation();
	acl.apply();
	
	var lfh = new LocationFormHandler($('form#event_create').get(0));
	lfh.apply();
});