//--------------------------------------------------------------
// Copyright (C) 2006 Michael Schwarz (http://www.ajaxpro.info).
// All rights reserved.
//--------------------------------------------------------------

// prototype.js
var ajaxProPrototypes = {};

ajaxProPrototypes.Object = {
    extend : function(dest, source, replace) {
	    for(var prop in source) {
		    if(replace == false && dest[prop] != null) { continue; }
		    dest[prop] = source[prop];
	    }
	    return dest;
    }
};

ajaxProPrototypes.Function = {
	bind: function(fnc, o) {
		if(!ajaxProPrototypes.Function.__objs) {
			ajaxProPrototypes.Function.__objs = [];
			ajaxProPrototypes.Function.__funcs = [];
		}
		var objId = o.__oid;
		if(!objId) {
			ajaxProPrototypes.Function.__objs[objId = o.__oid = ajaxProPrototypes.Function.__objs.length] = o;
		}

		var me = fnc;
		var funcId = me.__fid;
		if(!funcId) {
			ajaxProPrototypes.Function.__funcs[funcId = me.__fid = ajaxProPrototypes.Function.__funcs.length] = me;
		}

		if(!o.__closures) {
			o.__closures = [];
		}

		var closure = o.__closures[funcId];
		if(closure) {
			return closure;
		}

		o = null;
		me = null;

		return ajaxProPrototypes.Function.__objs[objId].__closures[funcId] = function() {
			return ajaxProPrototypes.Function.__funcs[funcId].apply(ajaxProPrototypes.Function.__objs[objId], arguments);
		};
	},
	argumentsToArray: function(fnc) {
		var args = [];
		for(var i=0; i<fnc.arguments.length; i++) {
			args.push(fnc.arguments[i]);
		}
		return args;
	}
};

ajaxProPrototypes.Array = {
	addRange: function(a, items) {
		if(items.length > 0) {
			for(var i=0; i<items.length; i++) {
				a.push(items[i]);
			}
		}
	},
	clear: function(a) {
		a.length = 0;
		return a;
	}
};

ajaxProPrototypes.String = {
	trimLeft: function(str) {
		return str.replace(/^\s*/,"");
	},
	trimRight: function(str) {
		return str.replace(/\s*$/,"");
	},
	trim: function(str) {
	    return(ajaxProPrototypes.String.trimLeft(ajaxProPrototypes.String.trimRight(str)));
	},
	endsWith: function(str, s) {
		if(str.length == 0 || str.length < s.length) { return false; }
		return (str.substr(str.length - s.length) == s);
	},
	startsWith: function(str, s) {
		if(str.length == 0 || str.length < s.length) { return false; }
		return (str.substr(0, s.length) == s);
	},
	
	format: function(s) {
		for(var i=1; i<arguments.length; i++) {
			s = s.replace("{" + (i -1) + "}", arguments[i]);
		}
		return s;
	},
	isNullOrEmpty: function(s) {
		if(s == null || s.length == 0) {
			return true;
		}
		return false;
	}
};


if(typeof addEvent == "undefined")
	addEvent = function(o, evType, f, capture) {
		if(o == null) { return false; }
		if(o.addEventListener) {
			o.addEventListener(evType, f, capture);
			return true;
		} else if (o.attachEvent) {
			var r = o.attachEvent("on" + evType, f);
			return r;
		} else {
			try{ o["on" + evType] = f; }catch(e){}
		}
	};
	
if(typeof removeEvent == "undefined")
	removeEvent = function(o, evType, f, capture) {
		if(o == null) { return false; }
		if(o.removeEventListener) {
			o.removeEventListener(evType, f, capture);
			return true;
		} else if (o.detachEvent) {
			o.detachEvent("on" + evType, f);
		} else {
			try{ o["on" + evType] = function(){}; }catch(e){}
		}
	};
