/* 
 * Context bind function and helpers from Prototype Javascript framework,
 * © 2006-2007 Prototype Core Team
 * Under MIT License, http://dev.rubyonrails.org/browser/spinoffs/prototype/trunk/LICENSE?format=raw
 */
Function.prototype.update = function(array, args)
{
	var
		arrayLength = array.length,
		length = args.length;
	
	while (length--) {
		array[arrayLength + length] = args[length];
	}
	
	return array;
}

Function.prototype.merge = function(array, args)
{
	array = Array.prototype.slice.call(array, 0);
	return Function.prototype.update(array, args);
}

Function.prototype.ctxBind = function(context)
{
	if (arguments.length < 2 && typeof arguments[0] == 'undefined') {
		return this;
	}
	
	var
		__method = this,
		args = Array.prototype.slice.call(arguments, 1);
	
	return function() {
		var a = Function.prototype.merge(args, arguments);
		return __method.apply(context, a);
	}
};

Object.extend = function(destination, source)
{
	for (key in source) {
		destination[key] = source[key];
	}
}



var Peli = {};
Peli.objectValues = function(object)
{
	var retval = [];
	$.each(object, function(key, value) {
		retval.push(value);
	});
	return retval;
}

Peli_ElementDefaultValue = function(config)
{
	// element, value
	Object.extend(this, config);
	
	this.onFocus = function()
	{
		if (this.element.val() == this.value) {
			this.element.val('');
			this.element.removeClass('defaultValue');
		}
		
		return true;
	};
	
	this.onBlur = function()
	{
		if (this.element.val() == '') {
			this.element.addClass('defaultValue');
			this.element.val(this.value);
		}
		
		return true;
	};
	
	this.onBlur();
	this.element.bind('focus', this.onFocus.ctxBind(this));
	this.element.bind('blur', this.onBlur.ctxBind(this));
}

Peli.equalizeHeight = function(elements)
{
	var maxHeight = 0;
	elements.each(function(key, element) {
		var height = $(element).height();
		maxHeight = Math.max(maxHeight, height);
	});
	elements.height(maxHeight);
}

