/***************************************************************
input.js - form submit/sync via AJAX

Copyright 2010 - Simon Strandgaard <simon@bee3.com>

This file depends on:
  1. jQuery
  2. misc.js
  3. htmlentities.js
  4. get_html_translation_table.js
  5. typewatch.js

***************************************************************/

var SingleTextFields = {};
var SingleTableFields = {};

// our constructor
function SingleLineTextInputController(options) {
	// console.log('SingleLineTextInputController', options);


	this.m_content_uid = dictionary_fetch(
		options,
		'content_uid', 
		'0'
	);
	
	this.m_write_to_database_url = dictionary_fetch(
		options,
		'write_to_database_url', 
		'write_to_database.php'
	);
	this.m_read_from_database_url = dictionary_fetch(
		options,
		'read_from_database_url', 
		'read_from_database.php'
	);
	
	this.m_id_textfield = dictionary_fetch(
		options,
		'id_textfield', 
		'#the_textfield'
	);
	
	this.m_id_button = dictionary_fetch(
		options,
		'id_button', 
		'#the_button'
	);
	
	this.m_id_form = dictionary_fetch(
		options,
		'id_form', 
		'#the_form'
	);
	
}

// prototype assignment
SingleLineTextInputController.prototype = (function() {
	return {
		constructor: SingleLineTextInputController,
		
		setup: function() {
			// console.log("setup");
			var thisObj = this;
			
			$(this.m_id_button).click(function() {     
				thisObj.forceSync();
			});
			
			$(this.m_id_form).submit(function() {
				thisObj.forceSync();
				return false;
			});

			$(this.m_id_textfield).typeWatch({ 
				callback: $.proxy(thisObj.typewatchNotify, thisObj) 
			});
			
			// this.readFromDatabase();
			
			return this;
		},
		
		toString: function() {
			return '<SinglelineInputController>';
		},

		writeToDatabase: function() {
			// console.log('writeToDatabase', this.m_content_uid, this.m_id_textfield);
			
			alert (this.m_write_to_database_url);
			var value = $(this.m_id_textfield).val();
			// console.log('value', value);
			
			var data = {
				value: value
			};

			$.ajax({
				url: this.m_write_to_database_url,
				cache: false,
				context: this,
				data: data,
				success: function(text) { this.writeToDatabaseCallback(text); }
			});

		},

		writeToDatabaseCallback: function() {
			// console.log('callback', this.m_content_uid);
			
			// NICE: indicate that the textfield has been saved
		},

		forceSync: function() {
			this.writeToDatabase();
		},
			             
		typewatchNotify: function() {
			// console.log('typewatchNotify', this.m_content_uid);
			this.writeToDatabase();
		},
		
		readFromDatabase: function() {
			// console.log("reading from database");
			$.ajax({
				url: this.m_read_from_database_url,
				cache: false,
				context: this,
				// data: data,
				success: function(text) { this.readFromDatabaseCallback(text); }
			});
		},
		
		readFromDatabaseCallback: function(text) {
			// console.log('callback', this.m_content_uid, text);
			$(this.m_id_textfield).val(text);
		}

	};
})();

// factory method
SingleLineTextInputController.setup = function(options) { return new SingleLineTextInputController(options).setup(); };

