/* A fix for the getElementById bug in IE */
if(window.ie){
	/* Copy of the function */
	document.nativeGetElementById = document.getElementById;

  /* Now redefine it */
	document.getElementById = function(id){
		var elem = document.nativeGetElementById(id);
		if(elem){
			/* Check it's id and see if its the right element */
			if(elem.attributes['id'] && elem.attributes['id'].value == id){
				return elem;
			}else{
				/* not a valid match!
				the non-standard, document.all array has keys for all name'd, and id'd elements
				start at one, because we know the first match, is wrong! */
				for(var i=1;i<document.all[id].length;i++){
					if(document.all[id][i].attributes['id'] && document.all[id][i].attributes['id'].value == id){
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

/* A debugging dump method */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	if(level > 2) return 'max level exceeded';
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

/* Try these from prototype */
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;
	}
}


/*
	Extend Element to add an easy to use JSONifier for forms
*/
Element.extend({
	toJsonString:function(){
		var formElements = {};
		var j=0;
		this.getFormElements().each(function(el){
			var name = el.name;
			var value = el.getValue();
			if (value === false || !name || el.disabled) return;
			if (formElements[name.replace('[]', "")])
				formElements[name.replace('[]', "")].push(value);
			else formElements[name.replace('[]', "")] = [value];
		});
		for (var element in formElements){
			if (formElements[element].length == 1)
				formElements[element] = formElements[element][0];
		}
		return Json.toString(formElements);
	}
});


/*
	Make some changes to the FancyUploader
	We don't want it to start uploading on form submit, rather
	after we've validated the input.
*/
FancyUpload.prototype.initializeFlash = function() {
	this.queue = $(this.options.queueList);
	// $(this.element.form).addEvent('submit', this.upload.bindWithEvent(this));
	if (this.options.createReplacement) this.options.createReplacement(this.element);
	else {
		new Element('input', {
			type: 'button',
			value: 'Browse Files',
			events: {
				click: this.browse.bind(this)
			}
		}).injectBefore(this.element);
		this.element.remove();
	}
}

/*
	Make some changes to AutoCompleter
	We want to support both choice values as well as text
*/
Autocompleter.Base.prototype.updateChoices = function(choices) {
	this.choices.empty();
	this.selected = null;
	if (!choices || !choices.length) return;
	if (this.options.maxChoices < choices.length) choices.length = this.options.maxChoices;
	choices.each(function(choice, i){
		var el = new Element('li').setHTML(this.markQueryValue((choice.label?choice.label:choice)));
		el.inputValue = (choice.value?choice.value:choice);
		this.addChoiceEvents(el).injectInside(this.choices);
	}, this);
	this.showChoices();
}

