/* Freestyler Ajax Object
 * ----------------------
 * Purpose: To provide common client-side functionality to any page or widget
 * requiring AJAX... Requires the following libraries and objects:
 *
 * prototype.js
 * domloaded.js
 *
 * Some CSS Assets are also required:
 * fs_ajax.css  	- Contains some standard interface elements and styles
 *
 * To extend, use Prototype's Object.extend functionality: e.g
 *
 * Object.extend(fs_ajax object, your object);
 */

// Class declaration begins:
var fs_ajax = Class.create();          // Use Prototype's Class Object
fs_ajax.prototype = {
	debugMode: true,
	name: "",
	errors: "",
	outputId: "",
	ajaxUrl: "",
	ajaxPars: "",
	loadingOutputId: "",
	errorOutputId: "",
	oldHtml: "",
	newHtml: "",
	defaultAjaxPars:"fs_ajaxUpdate=true",
  	initialize: function(options) {
 	    // Constructor...
 	    // Set private variables
 		// Create a reference to the output Id
		this.debug("fs_ajax initialize");
		this.debug(options.ajaxUrl);
 	    this.outputId=options.outputId;
 	    // Unique Name
 	    this.name=options.name;
 	    // This should probably append our .ajax extension... MVC Change
 	    this.ajaxUrl=options.ajaxUrl;
 	    // Default 'loading' to given outputId;
 	    this.loadingOutputId=this.outputId;
 	    // Default errors to given outputId;
 	    this.errorOutputId=this.outputId;
 	    // Get a copy of the outputId innerHTML for safe keeping
		var outputId=$(this.outputId);
		this.oldHTML=outputId.innerHTML;
		// Default Freestyler ajax update parameter
		this.ajaxPars="fs_ajaxUpdate=true";
	},
 	setLoadingOutputId: function(outputId) {
 	    // Set the location of 'loading' output e.g fs_widgetZone
 	    this.loadingOutputId=outputId;
 	},
 	setErrorOutputId: function(outputId) {
 	    // Set the location of error message output e.g fs_widgetZone
 	    this.errorOutputId=outputId;
 	},
 	setAjaxPars: function(pars) {
		// Append the parameters passed in the ajax call
 	    this.ajaxPars=this.ajaxPars+"&"+pars;
 	},
 	resetAjaxPars: function() {
 	    this.ajaxPars=this.defaultAjaxPars;
	},
 	error: function(message) {
 	    // Callback function to display any errors
 	    this.debug("error while updating - "+message);
 	    var output=$(this.errorOutputId);
		output.innerHTML="<div class=\"fs_error\">An Error Has Occurred</div>";
  	},
 	loading: function() {
 	    // Function to display loading messages or throbber animations
 	    this.debug("loading...");
		var output = $(this.loadingOutputId);
		output.innerHTML="<div class=\"fs_throbber\">Loading...</div>";
 	},
  	loaded: function(outputId,responseText) {
 	    // Determine if there was any Freestyler errors in the returned HTML
 	    // which should be located inside this.newHtml
 	    this.debug("loaded");
		//var output=$(this.outputId);
		// Look for any fs_ajaxError... Assume none
		this.newHtml=responseText;
		this.success();
 	},
 	success: function() {
 	    // If the ajax request has completed without error, then this is the
		// final function called. It populates the outputdiv with a successful
		// ajax update
		this.debug("success");
		var output=$(this.outputId);
		output.innerHTML=this.newHtml;
 	},
 	submit: function(formId) {
 	    // Serialize the given form and return in a param string
 	    // Append to internal ajaxPars
 	    var pars=Form.serialize($(formId));
		this.debug(pars);
		this.ajaxPars="fs_ajaxSubmit=true&"+pars;
		this.update();
 	},
 	update: function() {
 	    this.loading();
 	    // Update the DOM, calling the objects Ajax Url with this.ajaxPars
 	    var fsAjax=new Ajax.Updater({success:this.outputId},this.ajaxUrl,{method:'post',parameters:this.ajaxPars,onFailure:this.error.bind(this),insertion:this.loaded.bind(this),evalScripts:true});
 	},
 	execute: function() {
 	    // Custom internal processing goes here
	},
	animateIn: function() {
	    // How to present output
	},
	animateOut: function() {
	    // How to remove or hide the widget
	},
	shade: function() {
		// Put the current window into modal input by shading out all
	},
	exit: function() {
	    // Tidy up the DOM and call destruct
	    // Remove any html created from the outputId
	    // Restore the saved html
	},
	debug: function(messageObject) {
	    if (this.debugMode) {
	        console.log(messageObject);
		} else return;
	}
};

