/**
 * ©2011 realmdigital
 * @author rdteam
 */
(function( $ ){
	
	// Work around for jQuery serializeArray method 
	$.fn.serializeJSON=function() {
		var json = {};
		$.map($(this).serializeArray(), function(n, i){
			json[n['name']] = n['value'];
		});
		return json;
	};
	
	// Swap on element with another
	$.fn.swap = function(b){
	    b = $(b)[0];
	    var a = this[0], t = a.parentNode.insertBefore(document.createTextNode(''), a);
	    b.parentNode.insertBefore(a, b);
	    t.parentNode.insertBefore(b, t);
	    t.parentNode.removeChild(t);
	    return this;
	};

})( jQuery );
 
(function($){

	/**
	 * Platform API
	 * @author rdteam
	 * @created 11/01/2011
	 * @version 1.0
	 * @method public object construct throws Exception
	 * @method public void message ( [arguments] ) throws Exception
	 * @method public void call ( [arguments] ) throws Exception
	 * @method public void view ( [arguments] ) throws Exception
	 * @method public object user ( [String property] | [Object user] ) throws Exception
	 **/
	
	// Namespace definition and singleton check
	var _platformAPI = function(){
		if(window.platform != undefined){
			return $.error('Singleton error [platformAPI]');
		};
		return this;
	};

	// Simple Debug, Error and Notification handling on a code level.
	platformAPIConsole = {
		error: function(){ 
			window.console.log(arguments); 
			$(document).trigger('onerror', arguments); throw Array.prototype.slice.call(arguments, 1); 
		},
		info: function(){ 
			window.console.log(arguments); 
			return $(document).trigger('oninfo', arguments); 
		},
		debug: function(){ 
			window.console.log(arguments); 
			return $(document).trigger('ondebug', arguments); 
		},
		log: function(){ 
			window.console.log(arguments); 
		}
	};
	$.error = platformAPIConsole.error; 
	$.info = platformAPIConsole.info; 
	$.debug = platformAPIConsole.debug; 
	$.log = platformAPIConsole.log; 

	// Class definition
	$.extend(_platformAPI.prototype, {
		
		/**
		 * Constructor
		 * @return this
		 **/
		construct: function(){
		},
		
		/**
		 * Notification
		 * @desc Send a notification / message
		 * @return void
		 **/
		message: function(){
			$.log(arguments);
		},
		
		/**
		 * @desc Call an action
		 * @return object
		 **/
		call: function(args){
			var url = (args['url'] == null) ? 'index.php' : args['url'], self = this;
			$.ajax({
				dataType: 'html',
				type: "POST",
				cache: false,
				url: url,
				data: args.params,
				success: function(data){
					self.trigger('oncallsuccess');
					if(typeof args.success == 'function'){
						args.success(data);
					};
				},
				error: function(e){
					self.trigger('oncallerror');
					if(typeof args.error == 'function'){
						args.error(e);
					};
				}
			});
		},
		
		/**
		 * @desc Get a view
		 * @return object
		 **/
		view: function(args){
			try{ delete args.params.id; }catch(e){};
			var url = 'index.php?action=view&id=' + args.id, self = this;
			$.ajax({
				dataType: 'html',
				type: "POST",
				cache: false,
				url: url,
				data: args.params,
				success: function(data){
					self.trigger('onviewsuccess');
					if(typeof args.success == 'function'){
						args.success(data);
					};
				},
				error: function(e){
					self.trigger('onviewerror');
					if(typeof args.error == 'function'){
						args.error(e);
					};
				}
			});
		},
		
		trigger: function(event, args){
			$(this).trigger('platformAPI.'+event, args);
		},
		
		bind: function(event, args){
			$(this).bind('platformAPI.'+event, args);
		}
	
	});
	
	// Auto load. [window.platform]
	try {
		if(window.platform == undefined){
			window.platform = new _platformAPI();
			window.platform.construct();
		};
	}catch(e){};

})(jQuery);
