/**********
*
*   Copyright (c) 2004, 2005 Alwin Blok
*
*   This file is part of JSB, a javascript to ruby bridge 
*
*   JSB is free software; you can redistribute it and/or
*   modify it under the terms of the GNU General Public License,
*   version 2, as published by the Free Software Foundation.
*   
*   JSB is distributed in the hope that it will be
*   useful, but WITHOUT ANY WARRANTY; without even the implied
*   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
*   PURPOSE. See the GNU General Public License for more details.
*   
*   You should have received a copy of the GNU General Public
*   License along with JSB; if not, write to the Free
*   Software Foundation, Inc., 59 Temple Place, Suite 330,
*   Boston, MA 02111-1307 USA.
*
**********/


// purpose: create a new http request object in a cross-browser way 
HttpRequest = function(){
  var type = typeof(XMLHttpRequest);
  return (type == 'function' || type == 'object') ? 
    new XMLHttpRequest() : 
    new ActiveXObject('Microsoft.XMLHTTP');
}


// purpose: create a new object with it's lookup slot set to the 'cloned' object
Object.prototype.clone = function(){
  var fn = (function(){});
  fn.prototype = this;
  var o = new fn();
	o.__proto__ = o.__proto__ ? o.__proto__ : this;
  return o;
}


// purpose: copy all properties of object _obj_ to object _this_

Object.prototype.extendWith = function(obj){
  for(var a in obj) this[a] = obj[a];
}


// purpose: return a function that will execute the function _fn_ at the object _obj_

Call = function(obj, fn, args){
  if(typeof(fn) != 'function')
    fn = obj[fn];
  return function(){
    fn.apply(obj, (args ? args : (arguments || []) ));
  }
}


// purpose: return a function that will execute the function _fn_ at the object _this_

Object.prototype.call = function(fn, args){
  return new Call(this, fn, args);
}


// purpose: provide a better typeof function

_typeof = function(obj){
  // safari's `arguments` have the __proto__ Arguments.prototype
  var Arguments = typeof(Arguments) == 'undefiened' ? Array : Arguments
  var type = typeof(obj);
  if(type == 'object'){
    if(obj == null) 
      return 'null';
    switch(obj.constructor){
      case Array:
        return 'array';
      break;
      case Arguments:
        return 'array';
      break;
      case DistantObject:
        return 'distant_object';
      break;
      case String:
        return 'string';
      break;
      case Number:
        return 'number';
      break;
      default:
        return 'object'
      break;
    }
  }
  return type;
}

