/*
 * Limb PHP Framework
 *
 * @link http://limb-project.com 
 * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
 * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
 */

if(Limb == undefined) var Limb = {};

Limb.get = function(id) {
  return document.getElementById(id);
}

Limb.trim = function(str)
{   
  str = str.replace(/^\s+/,'');
  return str.replace(/\s+$/,'');
}

Limb.toArray = function(pseudoArray) {
  var result = [];
  for (var i = 0; i < pseudoArray.length; i++)
    result.push(pseudoArray[i]);
  return result;
}

if(!Function.prototype.bind)
{
  Function.prototype.bind = function(object)
  {
    var method = this;
    var oldArguments = toArray(arguments).slice(1);
    return function () {
         var newArguments = Limb.toArray(arguments);
         return method.apply(object, oldArguments.concat(newArguments));
     };
  };
}

if(!Function.prototype.bindAsEventListener)
{
  Function.prototype.bindEventListener = function (object) {
    var method = this;
    var oldArguments = toArray(arguments).slice(1);
    return function (event) {
      return method.apply(object, event || window.event, oldArguments);
    }
  };
}

Limb.Exception = function()
{
  if(arguments.length == 1 && Limb.isObject(arguments[0]))
  {
    this.type = arguments[0].type || 'LimbException';
    this.message = arguments[0].message;
    this.stack = arguments[0].stack || 'Stack is not available';
    this.file_name = arguments[0].fileName || 'File name is not available';
    this.line_number = arguments[0].lineNumber || 'Line number is not available';
  }
  else
  {
    this.type = arguments[0] || 'LimbException';
    this.message = arguments[1] || 'Unknown error';
    if(typeof(arguments[2]) == 'object')
    {
      this.stack = arguments[2].stack || e.stack;
      this.file_name = arguments[2].fileName || e.fileName;
      this.line_number = arguments[2].lineNumber || e.lineNumber;
    }
    else
    {
      this.stack = 'Stack is not available';
      this.file_name = 'File name is not available';
      this.line_number = 'Line number is not available';
    }
  }
}

Limb.Exception.prototype =
{
  getMessage: function() { return this.message; },
  getType: function() { return this.type; },
  getStack: function() { return this.stack; },
  getFileName: function() { return this.file_name; },
  getLineNumber: function() { return this.line_number; },
  toString: function() { return '[ exception ' + this.type + ' ]'; }
}

Limb.define = function(name, value)
{
  var parts = name.split('.');
  var var_name = parts.pop();

  var namespace = Limb.namespace(parts.join('.'));
  namespace[var_name] = value;
}

Limb.namespace = function(name)
{
  var parts = name.split('.');
  var parent = window;
  for(var i=0; i<parts.length; i++)
  {
    if(!parts[i])
      continue;

    if(!parent[parts[i]])
      parent[parts[i]] = {};

    parent = parent[parts[i]];
  }

  return parent;
}

Limb.require = function(package_name)
{
  //no working function for now. It needs to be written from the scratch or may be ported from 2.x
}

Limb.isset = function(variable)
{
  return typeof(variable) != 'undefined' && variable != null;
}

Limb.isObject = function(variable)
{
  return typeof(variable) == 'object';
}

Limb.isFunction = function(variable)
{
  return typeof(variable) == 'function';
}

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example Limb.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example Limb.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
 * @desc Create a cookie with all available options.
 * @example Limb.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example Limb.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value.
 */
Limb.cookie = function(name, value, options)
{
  if(typeof value != 'undefined') // name and value given, set cookie
  {
    options = options || {};
    if(value === null)
    {
      value = '';
      options.expires = -1;
    }
    var expires = '';
    if(options.expires && (typeof options.expires == 'number' || options.expires.toUTCString))
    {
        var date;
        if(typeof options.expires == 'number')
        {
          date = new Date();
          date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
        }
        else
          date = options.expires;
        expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    var path = options.path ? '; path=' + options.path : '';
    var domain = options.domain ? '; domain=' + options.domain : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  }
  else
  { // only name given, get cookie
    var cookieValue = null;
    if(document.cookie && document.cookie != '')
    {
      var cookies = document.cookie.split(';');
      for(var i = 0; i < cookies.length; i++)
      {
        var cookie = Limb.trim(cookies[i]);
        // Does this cookie string begin with the name we want?
        if(cookie.substring(0, name.length + 1) == (name + '='))
        {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }
}

Limb.namespace('Limb.Browser');

var agt = navigator.userAgent.toLowerCase();
Limb.Browser.is_ie = (agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1);
Limb.Browser.is_gecko = navigator.product == "Gecko";
Limb.Browser.is_opera  = (agt.indexOf("opera") != -1);
Limb.Browser.is_mac    = (agt.indexOf("mac") != -1);
Limb.Browser.is_mac_ie = (Limb.Browser.is_ie && Limb.Browser.is_mac);
Limb.Browser.is_win_ie = (Limb.Browser.is_ie && !Limb.Browser.is_mac);


