if ((typeof Prototype == 'undefined') || Prototype.Version < '1.6') {
    throw new Error('Loom requires Prototype 1.6.0 or greater');
}

Object.extend(String.prototype, {

	/**
	 * Original implementation by by kangax and henrah 
	 * at http://thinkweb2.com/projects/prototype/namespacing-made-easy/
	 */
	namespace: function(separator) {
	  this.split(separator || '.').inject(window, function(parent, child) {
	    return parent[child] = parent[child] || { };
	  })
	},
	
	/**
	 * Transform a String to a DOM node tree.
	 * If the String includes a <body> tag, only its contents will be processed.
	 * Any <script> tag will be removed before processing
	 * @param rootNode the node where the DOM objects will be inserted. If null, a new div will be created and returned
	 * @return the root div with the inserted contents	 
	 */
	toDOM: function(rootNode) {
		var s = this.match(/<body[^>]*>(.*)<\/body>/);
		s = (s? s[1] : this).stripScripts();
		return (rootNode || new Element('div', { style: 'display:none' })).insert(s);
	}
	
});

Object.extend(Function.prototype, {
	
	/**
	 * Implementation of the pattern showcased here:
	 * http://ajaxian.com/archives/delaying-javascript-execution
	 * Usage: function.throttle(delay_in_seconds)(arg1, arg2...)
	 */
	throttle: function() {
		var __method = this, timeout = arguments[0] * 1000;
		var executionTimer;
		return function() {
			var args = arguments;
            if (executionTimer) {
                clearTimeout(executionTimer);
            }
            executionTimer = setTimeout(function() {
            	return __method.apply(__method, args);
            }, timeout);
        };
	}

});

var loom = Object.extend(window['loom'] || {}, {

	/** loom GET params prefix */
	PARAM_PREFIX: '_', 
	
	/** template pattern used for ${parameter} substitutions */
	TEMPLATE_PATTERN: /(^|.|\r|\n)(\$\{(.*?)\})/,
	
	/** true if this is a IE <7 */ 
	ie6: Prototype.Browser.IE && navigator.appVersion < "4.0 (compatible; MSIE 7",
  
	/**
	 * Original implementation by Carlos Reche:
	 * http://wiki.script.aculo.us/scriptaculous/show/Cookie 
	 */
	cookies: {
	
		// TODO: missing "secure"
	  set: function(name, value, daysToExpire, path) {
	    var expire = '';
	    if (daysToExpire != undefined) {
	      var d = new Date();
	      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
	      expire = '; expires=' + d.toGMTString();
	    }
	    path = '; Path=' + (path || '/');
	    return (document.cookie = escape(name) + '=' + escape(value || '') + path + expire);
	  },
	  
	  get: function(name) {
	    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
	    return (cookie ? unescape(cookie[2]) : null);
	  },
	  
	  remove: function(name) {
	    var cookie = loom.cookies.get(name);
	    loom.cookies.set(name, '', -1);
	    return cookie;
	  },
	  
	  /**
	   * return true if the browser has cookies enabled
	   */
	  accept: function() {
	    if (typeof navigator.cookieEnabled == 'boolean') {
	      return navigator.cookieEnabled;
	    }
	    loom.cookies.set('_test', '1');
	    return (loom.cookies.remove('_test') === '1');
	  }
    
	},
	
	/**
	 * Return the full path of the provided js script without the script name, e.g.<b>
	 * loom.getScriptPath('prototype.js') returns 'http://prototype.org/js/script' if the page 
	 * includes http://prototype.org/js/script/prototype.js?something.
	 * If the script is not found, returns null.  
	 */
	getScriptPath: function(filename) {
	    var path = null;
	    $A(document.getElementsByTagName("script")).any( function(s) {
	       var src = s.getAttribute('src');
	       if (src) {
		       var pos = src.indexOf(filename);
	           return pos != -1 && (path = src.substring(0, pos));
           }
        });
        return path;
	}	
});

