/**
 * AstonishMe Helper plugin javascript
 *
 * This plugin provides methods for other plugins
 *
 * @package AstonishMe General
 *
 * @copyright (c)2008 by AstonishMe - {@link http://astonishme.co.uk/}.
 *
 * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
 * @author Yabba	- {@link http://www.astonishme.co.uk/}
 * @author Stk		- {@link http://www.astonishme.co.uk/}
 *
 * {@internal License choice
 * - If you have received this file as part of a package, please find the license.txt file in
 *   the same folder or the closest folder above for complete license terms.
 * - If you have received this file individually
 *   then you must choose one of the following licenses before using the file:
 *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
 *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
 * }}
 *
 */

if( typeof( console ) == 'undefined' )
{ // firebug console disabled
	function _console_null()
	{
		return {
			log:function(){},
			error:function(){},
			info:function(){}
		}
	}
	var console = _console_null();
}

/**
 *@object amHelper
 */
var amHelper = _amHelper();

function _amHelper()
{
	/**
	 * @internal : count of open screens
	 */
	var amhlprWindowCount = 0;

	/**
	 * @internal : array server calls
	 */
	var amhlprServerCalls = new Array();

	/**
	 *@internal : reference to self
	 */
	var self;

	var trans = Array();

	/**
	 * @internal : our url
	 */
	var url;

	return {
		/**
		 * Initialises the helper object
		 */
		init:function()
		{
			// set available params to defaults
			params = jQuery.fn.extend({
					// no comma after final entry or IE barfs
					url: '', // our url
					key: '' // our key
					}, ( arguments.length ? arguments[0] : '' ) );
			self = this;// set our reference
			url = params.url+'&amhlpr_key='+params.key+'&amhlpr_plug_evt='; // used for our server calls
			jQuery( '#amhlprMessageBox' ).addClass( 'amhlprHideObject' );
			jQuery( '#amhlprMessageClose' ).click( function(){
				return self.MessageClose();
			});
			console.info( 'AM Helper plugin : initialised' );
			// init T_() strings
		},

		/**
		 * Display a message screen
		 * @params
		 *    (string) title : the title for the window
		 *		(string) message : message to display
		 *		(string) button : text for the button ( if any )
		 *		(function) callback : function to call on button click ( if any )
		 */
		Message: function()
		{
			// set available params to defaults
			params = jQuery.fn.extend({
					title: '', // message box title
					message: '', // messge to display
					button: '', // optional button text
					close:true, // does it have a close button
					// no comma after final entry or IE barfs
					callback: '' // optional callback funtion on close
					}, ( arguments.length ? arguments[0] : '' ) );
			if( !arguments.length || !( params.title || params.message || params.button || params.callback ) )
			{ // nothing to do;
				return false;
			}
			jQuery( '#amhlprTitle' ).html( params.title );
			jQuery( '#amhlprMessageText' ).html( params.message );
			jQuery( '#amhlprMessageButtons' ).empty();
			if( params.callback )
			{ // we have a callback_function
				if( params.button )
				{ // we have a button for the function
					var btnObj = jQuery('<input type="button" value="'+params.button+'" />' );
					jQuery( btnObj ).data( 'params', params );
					jQuery( btnObj ).click( function(){
						params = jQuery( this ).data( 'params' );
						return self.MessageClose( params );
					});
				}
				else
				{ // we have a function but no button
					var btnObj = jQuery('<input type="button" value="Close" />' );
					jQuery( btnObj ).click( function(){
						return self.MessageClose();
					});
				}
				jQuery( btnObj ).appendTo( '#amhlprMessageButtons' ); // add the button
			}
			else if( params.button )
			{ // button but no function
				jQuery( '#amhlprMessageButtons' ).empty();
				var btnObj = jQuery('<input type="button" value="'+params.button+'" />' );
				jQuery( btnObj ).click( function(){
					return self.MessageClose();
				});
				jQuery( btnObj ).appendTo( '#amhlprMessageButtons' ); // add the button
			}

			if( params.close )
			{ // we want a close icon
				jQuery( '#amhlprMessageClose' ).removeClass( 'amhlprHideObject' );
			}
			else
			{ // we don't want a close icon
				jQuery( '#amhlprMessageClose' ).addClass( 'amhlprHideObject' );
			}

			if( jQuery( '#amhlprMessageBox' ).hasClass( 'amhlprHideObject' ) === false )
			{
				self.MessageClose();
			}
			jQuery( '#amhlprMessageBox' ).draggable({
				handle:jQuery( '#amhlprMessageTitleWrap' ),
				containment:jQuery( '#amhlprWindow' )
			});
			self.WindowOpen();
			jQuery( '#amhlprScreen' ).addClass( 'amhlprHideObject' );
			jQuery( '#amhlprMessageBox' ).removeClass( 'amhlprHideObject' );
			console.log( 'AM Helper plugin : Message window opened' );
		},


		/**
		 * Close the message screen
		 *
		 * @param (function) callback : function to call after closing ( if any )
		 *
		 * @return value from callback function ( if any )
		 */
		MessageClose: function()
		{
			// set available params to defaults
			params = jQuery.fn.extend({
					callback: '' // no comma after final entry or IE barfs
				}, ( arguments.length ? arguments[0] : '' ) );
			if( jQuery( '#amhlprMessageBox' ).hasClass( 'amhlprHideObject' ) )
			{ // we're not open
				return false;
			}
			self.WindowClose();
			jQuery( '#amhlprMessageBox' ).addClass( 'amhlprHideObject' );
			jQuery( '#amhlprScreen' ).removeClass( 'amhlprHideObject' );
			if( typeof( params.callback ) == 'function' )
			{
				console.log( 'AM Helper plugin : Message window closed, callback' );
				return params.callback(params);
			}
			console.log( 'AM Helper plugin : Message window closed, no callback' );
			return false;
		},


		/**
		 * Opens the window if required
		 */
		WindowOpen: function()
		{
			jQuery( '#amhlprWindow' ).removeClass( 'amhlprHideObject' );
			jQuery( 'html' ).addClass( 'amhlprOpen' );
			amhlprWindowCount++;
			console.log( 'AM Helper plugin : Window open' );
		},


		/**
		 * Close the window if no more open screens
		 */
		WindowClose: function()
		{
			amhlprWindowCount--;
			if( amhlprWindowCount == 0 )
			{
				jQuery( '#amhlprWindow' ).addClass( 'amhlprHideObject' );
				jQuery( 'html' ).removeClass( 'amhlprOpen' );
				console.log( 'AM Helper plugin : Last window closed' );
			}
			else
			{
				console.log( 'AM Helper plugin : Window close' );
			}
		},

		/**
		 * Forcibly close all windows
		 */
		WindowCloseAll:function()
		{
			amhlprWindowCount = 0;
			jQuery( '#amhlprScreen' ).html('');
			jQuery( '#amhlprWindow' ).addClass( 'amhlprHideObject' );
			console.log( 'AM Helper plugin : All Windows closed' );
		},

		OpenScreen: function()
		{
			var newScreen = jQuery( '<div class="amhlprInnerScreen"></div>' );
			newScreen.appendTo( jQuery( '#amhlprScreen' ) );
			self.WindowOpen();
			return newScreen;
		},

		OpenPage:function()
		{
			// set available params to defaults
			params = jQuery.fn.extend({
					title:'', // title for the page
					// no comma after final entry or IE barfs
					url:'' // page url
					}, ( arguments.length ? arguments[0] : '' ) );
			var newScreen = self.OpenScreen(); // open a screen
			jQuery( '<h3 class="amhlprPageTitle"><span class="amhlprPageClose">x</span>'+params.title+'</h3>' ).appendTo( newScreen );
			jQuery( '<iframe class="amhlprPage" src="'+params.url+'" />' ).appendTo( newScreen );
			newScreen.find( '.amhlprPageClose' ).click(function(){
				self.CloseScreen( jQuery( this ).parent().parent() );
			});
			return newScreen;
		},

		CloseScreen:function( screen )
		{
			jQuery( screen ).remove();
			self.WindowClose();
		},

		/**
		 * Sends a request to the server
		 *
		 * @params
		 *		(string) url : the url to call
		 *		(string) type : type of call ( js || html || form )
		 *		(function) success : function to call on success
		 *		(function) failure : function to call on failure
		 *
		 * @params optional : @ Message()
		 *    (string) title : the title for the window
		 *		(string) message : message to display
		 *		(string) button : text for the button ( if any )
		 *		(function) callback : function to call on button click ( if any )
		 */
		ServerCall: function()
		{
			// set available params to defaults
			params = jQuery.fn.extend({
					event:'', // event to trigger on the server
					data:'', // data to send
					type:'js', // js || htm || form
					success: '', // function to call on success
					// no comma after final entry or IE barfs
					failure: '' // function to call on failure
					}, ( arguments.length ? arguments[0] : '' ) );
			self.Message( params );
			switch( params.type )
			{
				case 'js' :
					var call = jQuery( '<script src="'+self.EventUrl()+params.event+'&amhlpr_mode=js&'+params.data+'"></script>' );
					if( typeof( params.success ) == 'function' )
					{ // add success function
						jQuery( call ).data( 'success', params.success );
						jQuery( call ).ready( function(){
							params.success();
						});
					}
					if( typeof( params.failure ) == 'function' )
					{ // add error function
						jQuery( call ).error( function(){
							params.failure();
						});
					}
					jQuery( call ).appendTo( jQuery( 'head' ) );
					break;
			}
			return false;
		},

		EventUrl:function()
		{
			return url.replace( '&amp;', '&' );
		},


		/**
		 * Adds a translation string
		 */
		AddTranslation:function( untranslated_text, translated_text )
		{
			trans[ untranslated_text.toLowerCase() ] = translated_text;
			jQuery( self ).data( 'translations', trans );
		},


		/**
		 * Translates a string if translation available
		 */
		T_: function( untranslated_text )
		{
			if( typeof( trans ) == 'object' && typeof( trans[ untranslated_text.toLowerCase() ] ) != 'undefined' )
			{ // we have a translation
				return trans[ untranslated_text.toLowerCase() ];
			}

			// we don't have a translation
			return '*** '+untranslated_text;
		}
	}
}
