var Prototype = {
Version: '1.4.0',
	 ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
	 emptyFunction: function() {},
	 K: function(x) {return x}
}
var Class = {
create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
};

var Abstract = new Object();

Object.extend = function(destination, source) {
	for (property in source) {
		destination[property] = source[property];
	}
	return destination;
};
Object.extend(String.prototype, {
  toQueryParams: function() {
    var pairs = this.match(/^\??(.*)$/)[1].split('&');
    return pairs.inject({}, function(params, pairString) {
      var pair = pairString.split('=');
      params[pair[0]] = pair[1];
      return params;
    });
  }
});

Object.inspect = function(object) {
	try {
		if (object == undefined) return 'undefined';
		if (object == null) return 'null';
		return object.inspect ? object.inspect() : object.toString();
	} catch (e) {
		if (e instanceof RangeError) return '...';
		throw e;
	}
}

Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}

var Try = {
these: function() {
	       var returnValue;

	       for (var i = 0; i < arguments.length; i++) {
		       var lambda = arguments[i];
		       try {
			       returnValue = lambda();
			       break;
		       } catch (e) {}
	       }

	       return returnValue;
       }
};

var Enumerable = {
each: function(iterator) {
	      var index = 0;
	      try {
		      this._each(function(value) {
				      try {
				      iterator(value, index++);
				      } catch (e) {
				      if (e != $continue) throw e;
				      }
				      });
	      } catch (e) {
		      if (e != $break) throw e;
	      }
      },

include: function(object) {
		 var found = false;
		 this.each(function(value) {
				 if (value == object) {
				 found = true;
				 throw $break;
				 }
				 });
		 return found;
	 },
inject: function(memo, iterator) {
		this.each(function(value, index) {
				memo = iterator(memo, value, index);
				});
		return memo;
	}
};

var $A = Array.from = function(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) {
		return iterable.toArray();
	} else {
		var results = [];
		for (var i = 0; i < iterable.length; i++)
			results.push(iterable[i]);
		return results;
	}
};

Object.extend(Array.prototype, Enumerable);
Object.extend(Array.prototype, {
_each: function(iterator) {
for (var i = 0; i < this.length; i++)
iterator(this[i]);
}
});

function $H(object) {
	var hash = Object.extend({}, object || {});
	Object.extend(hash, Enumerable);
	Object.extend(hash, Hash);
	return hash;
}

var Ajax = {
getTransport: function() {
		      return Try.these(
				      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
				      function() {return new ActiveXObject('Microsoft.XMLHTTP')},
				      function() {return new XMLHttpRequest()}
				      ) || false;
	      },

activeRequestCount: 0
}

Ajax.Responders = {
responders: [],

	    _each: function(iterator) {
		    this.responders._each(iterator);
	    },

register: function(responderToAdd) {
		  if (!this.include(responderToAdd))
			  this.responders.push(responderToAdd);
	  },

unregister: function(responderToRemove) {
		    this.responders = this.responders.without(responderToRemove);
	    },

dispatch: function(callback, request, transport, json) {
		  this.each(function(responder) {
				  if (responder[callback] && typeof responder[callback] == 'function') {
				  try {
				  responder[callback].apply(responder, [request, transport, json]);
				  } catch (e) {}
				  }
				  });
	  }
};

Object.extend(Ajax.Responders, Enumerable);

Ajax.Responders.register({
onCreate: function() {
Ajax.activeRequestCount++;
},

onComplete: function() {
Ajax.activeRequestCount--;
}
});

Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) {
		    this.options = {
method:       'post',
	      asynchronous: true,
	      parameters:   ''
		    }
		    Object.extend(this.options, options || {});
	    },

responseIsSuccess: function() {
			   return this.transport.status == undefined
				   || this.transport.status == 0
				   || (this.transport.status >= 200 && this.transport.status < 300);
		   },

responseIsFailure: function() {
			   return !this.responseIsSuccess();
		   }
}

Ajax.Request = Class.create();
Ajax.Request.Events =
['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];

Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
initialize: function(url, options) {
this.transport = Ajax.getTransport();
this.setOptions(options);
this.request(url);
},

request: function(url) {
var parameters = this.options.parameters || '';
if (parameters.length > 0) parameters += '&_=';

try {
this.url = url;
if (this.options.method == 'get' && parameters.length > 0)
this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;

Ajax.Responders.dispatch('onCreate', this, this.transport);

this.transport.open(this.options.method, this.url,
	this.options.asynchronous);

if (this.options.asynchronous) {
	this.transport.onreadystatechange = this.onStateChange.bind(this);
	setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
}

this.setRequestHeaders();

var body = this.options.postBody ? this.options.postBody : parameters;
this.transport.send(this.options.method == 'post' ? body : null);

} catch (e) {
	this.dispatchException(e);
}
},

setRequestHeaders: function() {
			   var requestHeaders =
				   ['X-Requested-With', 'XMLHttpRequest',
			   'X-Prototype-Version', Prototype.Version];

			   if (this.options.method == 'post') {
				   requestHeaders.push('Content-type',
						   'application/x-www-form-urlencoded');
				   if (this.transport.overrideMimeType)
					   requestHeaders.push('Connection', 'close');
			   }

			   if (this.options.requestHeaders)
				   requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);

			   for (var i = 0; i < requestHeaders.length; i += 2)
				   this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
		   },

onStateChange: function() {
		       var readyState = this.transport.readyState;
		       if (readyState != 1)
			       this.respondToReadyState(this.transport.readyState);
	       },

header: function(name) {
		try {
			return this.transport.getResponseHeader(name);
		} catch (e) {}
	},

evalJSON: function() {
		  try {
			  return eval(this.header('X-JSON'));
		  } catch (e) {}
	  },

evalResponse: function() {
		      try {
			      return eval(this.transport.responseText);
		      } catch (e) {
			      this.dispatchException(e);
		      }
	      },

respondToReadyState: function(readyState) {
			     var event = Ajax.Request.Events[readyState];
			     var transport = this.transport, json = this.evalJSON();

			     if (event == 'Complete') {
				     try {
					     (this.options['on' + this.transport.status]
					      || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
					      || Prototype.emptyFunction)(transport, json);
				     } catch (e) {
					     this.dispatchException(e);
				     }

				     if ((this.header('Content-type') || '').match(/^text\/javascript/i))
					     this.evalResponse();
			     }

			     try {
				     (this.options['on' + event] || Prototype.emptyFunction)(transport, json);
				     Ajax.Responders.dispatch('on' + event, this, transport, json);
			     } catch (e) {
				     this.dispatchException(e);
			     }
			     if (event == 'Complete')
				     this.transport.onreadystatechange = Prototype.emptyFunction;
		     },

dispatchException: function(exception) {
			   (this.options.onException || Prototype.emptyFunction)(this, exception);
			   Ajax.Responders.dispatch('onException', this, exception);
		   }
});
if ( ! Object.prototype.toJSONString) {
	Array.prototype.toJSONString = function () {
		var a = ['['], 
		    b, 
		    i, 
		    l = this.length,
		    v;

		function p(s) {
			if (b) {
				a.push(',');
			}
			a.push(s);
			b = true;
		}
		for (i = 0; i < l; i ++) {
			v = this[i];
			switch (typeof v) {
				case 'undefined':
				case 'function':
				case 'unknown':
					break;
				case 'object':
					if (v) {
						if (typeof v.toJSONString === 'function') {
							p(v.toJSONString());
						}
					} else {
						p("null");
					}
					break;
				default:
					p(v.toJSONString());
			}
		}
		a.push(']');
		return a.join('');
	};

	Boolean.prototype.toJSONString = function () {
		return String(this);
	};

	Date.prototype.toJSONString = function () {

		function f(n) {
			return n < 10 ? '0' + n : n;
		}

		return '"' + this.getFullYear() + '-' +
			f(this.getMonth() + 1) + '-' +
			f(this.getDate()) + 'T' +
			f(this.getHours()) + ':' +
			f(this.getMinutes()) + ':' +
			f(this.getSeconds()) + '"';
	};

	Number.prototype.toJSONString = function () {

		return isFinite(this) ? String(this) : "null";
	};

	Object.prototype.toJSONString = function () {
		var a = ['{'], 
		    b, 
		    k, 
		    v;

		function p(s) {
			if (b) {
				a.push(',');
			}
			a.push(k.toJSONString(), ':', s);
			b = true;
		}
		for (k in this) {
			if (this.hasOwnProperty(k)) {
				v = this[k];
				switch (typeof v) {
					case 'undefined':
					case 'function':
					case 'unknown':
						break;
					case 'object':
						if (v) {
							if (typeof v.toJSONString === 'function') {
								p(v.toJSONString());
							}
						} else {
							p("null");
						}
						break;
					default:
						p(v.toJSONString());
				}
			}
		}
		a.push('}');
		return a.join('');
	};

	(function (s) {
	 var m = {
	 '\b': '\\b',
	 '\t': '\\t',
	 '\n': '\\n',
	 '\f': '\\f',
	 '\r': '\\r',
	 '"' : '\\"',
	 '\\': '\\\\'
	 };

	 s.parseJSON = function (filter) {
	 try {
	 if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.
		 test(this)) {
	 var j = eval('(' + this + ')');
	 if (typeof filter === 'function') {

	 function walk(k, v) {
	 if (v && typeof v === 'object') {
	 for (var i in v) {
		 if (v.hasOwnProperty(i)) {
			 v[i] = walk(i, v[i]);
		 }
	 }
	 }
	 return filter(k, v);
	 }

	 j = walk('', j);
	 }
	 return j;
	 }
	 } catch (e) {
	 }
	 throw new SyntaxError("parseJSON");
	 };

	 s.toJSONString = function () {
		 var _self = this.replace("&", "%26");

		 if (/["\\\x00-\x1f]/.test(this)) {
			 return '"' + _self.replace(/([\x00-\x1f\\"])/g, function(a, b) {
					 var c = m[b];
					 if (c) {
					 return c;
					 }
					 c = b.charCodeAt();
					 return '\\u00' +
					 Math.floor(c / 16).toString(16) +
					 (c % 16).toString(16);
					 }) + '"';
	 }
	 return '"' + _self + '"';
	 };
	})(String.prototype);
}

var _nativeExtensions = false;
if (!window.Element)
  var Element = new Object();
Element.extend = function(element) {
	if (!element) return;
	if (_nativeExtensions) return element;

	if (!element._extended && element.tagName && element != window) {
		var methods = Element.Methods, cache = Element.extend.cache;
		for (property in methods) {
			var value = methods[property];
			if (typeof value == 'function')
				element[property] = cache.findOrStore(value);
		}
	}

	element._extended = true;
	return element;
}
function $() {
	var results = [], element;
	for (var i = 0; i < arguments.length; i++) {
		element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		results.push(Element.extend(element));
	}
	return results.length < 2 ? results[0] : results;
}
