/**
 * <p>Title: prototypeHelper.js</p>
 * <p>Description: prototype.js helper javascript library.</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: Kreber Graphics, Inc.</p>
 * @author Charlie Reading
 */

// Includes
var prototypeHelper_js = 1;
var includes = ((typeof Prototype == 'undefined') ? "prototype.js\n" : "");
if (includes != "")
{
	alert("prototypeHelper.js: you must first include:\n" + includes);
}

if (!window.Object.extendDeep)
{
	Object.extendDeep = function(destination, source)
	{
	  for (var property in source)
		{
			if (typeof source[property] == 'object')
			{
				destination[property] = Object.extendDeep({}, source[property]);
			}
			else
			{
				destination[property] = source[property];
			}
	  }
	  return destination;
	}
}

if (!window.Object.clone)
{
	Object.extend(Object,
	{
		clone: function(object)
		{
			return Object.extend({}, object);
		}
	});
}

if (!window.Object.cloneDeep)
{
	Object.extend(Object,
	{
		cloneDeep: function(object)
		{
			return {}.extendDeep({}, object);
		}
	});
}



if (!window.Object.dump)
{
	Object.dump = function ()
	{
		var xtypeof;
		
		var out = "";
    for (var i=0; i<arguments.length; i++)
		{
			xtypeof = Object.xtypeof(arguments[i]);
//debug("xtypeof arguments["+i+"] == "+xtypeof);
			var isArguments = (xtypeof == 'arguments');
			
			var isArray = (xtypeof == 'array');
			var arg = ((isArguments) ? $A(arguments[i]) : ((isArray) ? arguments[i] : [ arguments[i] ]));
			var argNames = null;
			var argCallee = null;
			var sArgCallee = "";
			if (isArguments)
			{
				argCallee = arguments[i].callee;
				sArgCallee = argCallee.toString();
				var posOpen = sArgCallee.indexOf("(")+1;
				var posClose = sArgCallee.indexOf(")");
				var sArgs = sArgCallee.substring(posOpen,posClose).replace(/\s/g,"");	// returns: "parm1,parm2,parm3"
				var argNames = sArgs.split(",");
			}
			
			var a = "";
			for (var j=0; j<arg.length; j++)
			{
				var argName = (((isArguments) && (j<argNames.length)) ? argNames[j]+":" : "");
				xtypeof = Object.xtypeof(arg[j]);
				var s = "";
				if (xtypeof == 'undefined')
				{
					s = argName + "undefined";
				}
				else if (xtypeof == 'null')
				{
					s = argName + "null";
				}
				else if ((xtypeof == 'string')
						|| (xtypeof == 'number'))
				{
					s = argName + arg[j];
				}
				else if (xtypeof == 'domelement')
				{
					s = argName + "domelement";
				}
				else
				{
				  for (var property in arg[j])
					{
						xtypeof = Object.xtypeof(arg[j][property]);						
						var val;
						if (xtypeof == 'undefined')
						{
							val = "undefined";
						}
						else if (xtypeof == 'null')
						{
							val = "null";
						}
						else
						{
							val = arg[j][property];
						}
						try
						{
							val = "" + val;
						}
						catch(e)
						{
							val = "unknown";
						}
						
//debug("xtypeof arguments["+i+"]["+j+"]["+property+"] == "+xtypeof+" ("+val+")");
//debug("xtypeof arguments["+i+"]["+j+"]["+property+"] == "+xtypeof+" ("+val+")");
						s += ((s != "") ? ", " : "") + property + ":" + val;
					}
					s = argName + "{" + s + "}";
				}
				a += ((a != "") ? ", " : "") + s;
			}
			
			out += ((out != "") ? ", " : "");
			if (isArray)
			{
				out += "[" + a + "]";
			}
			else if (isArguments)
			{
				if (arg.length < argNames.length)
				{
					for (j=arg.length; j<argNames.length; j++)
					{
						a += ((a != "") ? ", " : "") + argNames[j] + ":unpassed";
					}
				}
				out += argCallee.name + "(" + a + ")";
			}
			else
			{
				out += a;
			}
			 + ((isArray) ? "["+a+"]" : ((isArguments) ? argCallee.name+"("+a+")": a));
		}
		
		return out;
	}
}


if (!window.Object.xtypeof)
{
	Object.xtypeof = function (arg)
	{
		if (arg == null)
		{
			return "null";
		}
		
		if (arg.callee != null)
		{
			return 'arguments';
		}
		
		var xto = typeof arg;
		if (xto == "object")
		{
			var ctor = arg.constructor;
			if (ctor != null)
			{
				//debug("xtypeof: ctor="+ctor);
				switch (ctor)
				{																	
					case Array:
						xto = "array";
						break;
					case Date:
						xto = "date";
						break;
					case Number:
						xto = "number";
						break;
					case RegExp:
						xto = "regexp";
						break;
					case ReferenceError:
						xto = "error";
						break;
					default:
						if (arg.nodeType != null) xto = "domelement";
						break;
				}
			}
		}
		return xto;
	}
}


if (false) // (! String.toHash)
{
console.debug("extending String");
	Object.extend(String,
	{
		toHash: function(obj, keySeparator, pairSeparator)
		{
			var hash = {};
			
			var pairs = obj.split(pairSeparator);
			for (var i=0,l=pairs.length; i<l; i++)
			{
				var keyvalue = (pairs[i]+keySeparator).split(keySeparator);		// force at least 2
				hash.set(keyvalue[0].strip(), keyvalue[1].strip());
			}
			
			return $H(hash);
		},

		toStyleHash: function(obj)
		{
			return String.toHash(obj, ':', ';');
		}
	});

	Object.extend(String.prototype,
	{
		toHash: function(keySeparator, pairSeparator)
		{
			return String.toHash(this, keySeparator, pairSeparator);
		},
		
		toStyleHash: function()
		{
			return String.toStyleHash(this);
		}
	});
}

if (false) //(! Hash.toString)
{
console.debug("extending Hash");

	Object.extend(Hash,
	{
	  toString: function(obj, keySeparator, pairSeparator)
		{
	    var str = "";
	
	    this.prototype._each.call(obj, function(pair)
			{
	      str += (pair.key + keySeparator + (pair.value || "''") + pairSeparator);
	    });
	
	    return s;
	  },
	  
	  toStyleString: function(obj)
		{
			return Hash.toString(obj, ':', ';');
	  }
	});

	Object.extend(Hash.prototype,
	{
	  toString: function(keySeparator, pairSeparator)
		{
	    return Hash.toString(this, keySeparator, pairSeparator);
	  },

	  toStyleString: function(keySeparator, pairSeparator)
		{
	    return Hash.toStyleString(this);
	  }
	});
}


if (!window.Object.inherit)
{
	Object.inherit = function(subClass, baseClass, baseFuncName)
	{ 
		function inheritance() {}
		inheritance.prototype = baseClass.prototype;
		Object.extend(subClass.prototype, new inheritance());
		subClass.prototype.constructor = subClass;
		if ((! baseFuncName) || (baseFuncName == ""))
		{
			baseFuncName = "base";
		}
		subClass[baseFuncName] = baseClass.prototype.initialize;
	}
}
