﻿/**
*	Core
*/
var Core = AbstractModel.extend(
	{
		/**
		*   init
		*
		*   Constructs the core
		*
		*	@param	p - application prefix
		*/
		init: function(p)
		{
			this._super( "Core", p );
		},
		
		/**
		*   makeAjaxRequest
		*
		*   Makes an ajax request
		*
		*	@param	o - scope (of caller)
		*	@param	a - ajax page
		*	@param	c - command
		*	@param	d - arguments object
		*	@param	s - callback success event
		*	@param	f - callback failure event
		*/
		makeAjaxRequest: function(o, a, c, d, s, f)
		{
			//this.debug( "making ajax request " + o + " : " + a + " : " + c + " : " + d + " : " + s + " : " + f );
			
			// append the command onto the arguments object
			d.cmd = c;
			
			// handle the query
			var obj = this;
			$.post( "" + this.getPrefix() + "ajax/" + a, d, function(json) {
				// handle the returned data
				//obj.debug( "data returned " + json );
				if ( typeof(json) != "object" ) {
				//if the object is malformed
					obj.broadcast( AjaxRequestEvent_JSON_ERROR, null );
				} else {
				// if the object exists and is correctly formatted
					// check if there are any messages
					if ( json.contents.feedback ) {
					// if there has been feedback returned
						if ( json.contents.feedback.messages.length != 0 )
							obj.broadcast( FeedbackEvent_FEEDBACK_RECEIVED, obj.serializeAJAXFeedback(o, json.contents.feedback) );
					}
					// now check the success of the call
					// and broadcast a resultant event with the returned content
					if ( json.contents.success == 0 ) {
					// if the call has failed
						obj.broadcast( f, json.contents.content );
					} else {
					// else, if the call was a success
						obj.broadcast( s, json.contents.content );
					}
				}
				// return false
				return false;
			}, "json");
		},
		
		/**
		*   serializeAJAXFeedback
		*
		*   Serializes AJAX feedback returned from the database
		*
		*	@param	o - scope (of caller)
		*	@param	d - feedback data object
		*/
		serializeAJAXFeedback: function(o, d)
		{
			// serialise feedback
			
			// @@ messages
			
			var messages = new Array();
			
			for ( i=0; i<d.messages.length; i++ )
			{
				var messageData = new MessageData(
					d.messagesType,
					d.messages[i]
				);
					
				messages.push( messageData );
			}
				
			// return the ajax result data
			return new FeedbackData(
				o, d.messagesType, messages				
			);
		}
	}
);

/**
*	@class AjaxResultData
*
*	The AjaxResultData holds all data necessary to esatblish an ajax result object
*
*	@author			Daniel Ivanovic dan@anti-blanks.co.uk
*/

var AjaxResultData = AbstractObject.extend(
	{
		/**
		*   init
		*
		*   Constructs the data object
		*
		*	@param	s - success boolean
		*	@param	f - feedback object
		*	@param	c - content object
		*/
		init: function(s, f, c)
		{
			this._super( "AjaxResultData" );
			
			this.success = 0;
			this.feedback = null;
			this.content = null;
			
			if ( s ) this.success = s;
			if ( f ) this.feedback = f;
			if ( c ) this.content = c;
		}
	}
);

/**
*	@class FeedbackData
*
*	The FeedbackData holds all data necessary to esatblish a feedback object
*
*	@author			Daniel Ivanovic dan@anti-blanks.co.uk
*/

var FeedbackData = AbstractObject.extend(
	{
		/**
		*   init
		*
		*   Constructs the data object
		*
		*	@param	s - feedback scope object
		*	@param	t - messages type
		*	@param	m - messages array
		*/
		init: function(s, t, m)
		{
			this._super( "FeedbackData" );
			
			this.feedbackScope = null;
			this.messagesType = '';
			this.messages = new Array();
			
			if ( s ) this.feedbackScope = s;
			if ( t ) this.messagesType = t;
			if ( m ) this.messages = m;
		}
	}
);

/**
*	@class MessageData
*
*	The MessageData holds all data necessary to esatblish a message object
*
*	@author			Daniel Ivanovic dan@anti-blanks.co.uk
*/

var MessageData = AbstractObject.extend(
	{
		/**
		*   init
		*
		*   Constructs the button dat6a object
		*
		*	@param	t - message type
		*	@param	m - message
		*/
		init: function(t, m)
		{
			this._super( "MessageData" );
			
			this.messageType = '';
			this.message = '';
			
			if ( t ) this.messageType = t;
			if ( m ) this.message = m;
		}
	}
);
