/** 
 * This will be the base class for all Lifetype Javascript code. Right 
 * now only the TableEffects class extends this, but in the future all code
 * will be moved to this framework.
 */

/**
 * Base Lifetype class 
 */
Lifetype = function() 
{
	// nothing yet
}

/**
 * Compatibility check
 */
Lifetype.prototypeCompatibabilityCheck = function( str )
{
    if ( str == '_each' ||
	  	 str == '_reverse' ||
         str == 'all' ||
         str == 'any' ||
         str == 'clear' ||
         str == 'collect' ||
         str == 'compact' ||
         str == 'detect' ||
         str == 'each' ||
         str == 'entries' ||
         str == 'extend' ||
         str == 'find' ||
         str == 'findAll' ||
         str == 'first' ||
         str == 'flatten' ||
         str == 'grep' ||
         str == 'include' ||
         str == 'indices' ||
         str == 'indexOf' ||
         str == 'inject' ||
         str == 'inspect' ||
         str == 'invoke' ||
         str == 'last' ||
         str == 'map' ||
         str == 'max' ||
         str == 'member' ||
         str == 'min' ||
         str == 'partition' ||
         str == 'pluck' ||
         str == 'reject' ||
         str == 'remove' ||
         str == 'removeItem' ||
         str == 'select' ||
         str == 'shift' ||
         str == 'sortBy' ||
         str == 'toArray' ||
         str == 'without' ||
         str == 'zip')
        return true;
    else
    	return false;
}

/**
 * Return the base URL of the script
 *
 * @return Base URL
 */
Lifetype.getBaseURL = function()
{
	// Get document base path
	documentBasePath = document.location.href;
	if (documentBasePath.indexOf('?') != -1)
		documentBasePath = documentBasePath.substring(0, documentBasePath.indexOf('?'));
		
	documentBasePath = documentBasePath.substring(0, documentBasePath.lastIndexOf('/'));

	return( documentBasePath );
}

/**
 * Given a function call, sets the scope of the called
 * function to the given object, so that we can reference the object as 'this'
 * inside it from for example an event handler (where 'this' would be the 
 * object throwin the event instead of our javascript object)
 *
 * Inspired from the Prototype object and http://www.digital-web.com/articles/scope_in_javascript/
 *
 * @param method
 * @param obj
 */
Lifetype.bind = function(method,obj) 
{
	temp = function() {
    	return method.apply(obj, arguments);
	};
  	
	return temp;
}

/**
 * When loading Javascript code and assigning it to a node via
 * innerHTML, it won't be executed by the browser for apparently
 * security reasons. This method will take a given node, look for
 * <script> tags and execute them accordingly.
 *
 * Shamelessly copied from here: 
 * http://kratcode.wordpress.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/
 *
 * @param node A starting node where to look for script
 * tags.
 */
Lifetype.execJS = function(node)
{
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;

  /* IE wants it uppercase */
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;

  for(var i=0;i<st.length; i++)
  {
    if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent = "";
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }

    try {
      var x = document.createElement("script");
      x.type = "text/javascript";

      /* In IE we must use .text! */
      if ((bSaf) || (bOpera) || (bMoz))
        x.innerHTML = strExec;
      else x.text = strExec;

      document.getElementsByTagName("head")[0].appendChild(x);
    } catch(e) {
      alert(e);
    }
  }
}

/**
 * JSon related code
 */
Lifetype.JSon = function() {}

/**
 * Decode the given JSon string and return the result
 *
 * @param code
 */
Lifetype.JSon.decode = function( code )
{
	return( eval('(' + code + ')'));
}

/**
 * Simple code that allows to store and retrieve 
 * pairs of keys and value. This should be used to store
 * variables that would have been global otherwise.
 *
 * There is no need to instantiate objects of this type, as this
 * entire class can be used as static.
 */
Lifetype.Config = function() {}

Lifetype.Config.data = new Array();

/**
 * Stores a value in the configuration registry
 * @param key
 * @param value
 * @return nothing
 * @static
 */
Lifetype.Config.setValue = function( key, value ) 
{
	this.data[key] = value;
}

Lifetype.Config.set = Lifetype.Config.setValue;

/**
 * Retrieves a value from the configuration registry
 * @param key
 * @return The value, or undefined if it does not exist
 * @static
 */
Lifetype.Config.getValue = function( key )
{
	result = this.data[key];
	if( result == undefined )
		result = key;
		
	return( result );
}

/**
 * Alias for Locale.Config
 */
Lifetype.Locale = Lifetype.Config;

/**
 * Alias for Lifetype.Config.setValue
 */
Lifetype.Locale.add = function( key, value )
{
	return( Lifetype.Locale.setValue( key, value ));
}

/**
 * Alias for Lifetype.Config.getValue
 */
Lifetype.Locale.tr = function( key )
{
	return( Lifetype.Locale.getValue( key ));
}

/**
 * It works like Lifetype.Locale.tr but allows us to perform replacements
 * on the string via %s
 *
 * @param key
 * @param replace
 */
Lifetype.Locale.pr = function( key, replace )
{
	var str = Lifetype.Locale.tr( key );
	return( str.replace( "%s", replace ));
}

/**
 * Shorthands for Lifetype.Locale.tr and Lifetype.Locale.pr
 */
var tr = Lifetype.Locale.tr;
var pr = Lifetype.Locale.pr;
