/**
 * Removes an initial value when input field is focused and set initial value when field keep blank.
 * If the element has a class 'realValue' this functionality won't be enabled.
 *
 * Constructor fields:
 * -------------------
 * element  the element to observe
 */
var initialInputValueHandler = Class.create({
	initialize: function(element) {
		if (element && !element.hasClassName('realValue')) {
			this.inputField = element;
			this.inputField.initialInputValueHandler = this;
			this.initialValue = this.inputField.value;
			this.inputField.observe('focus', this.focusHandler);
			this.inputField.observe('blur', this.blurHandler);
			this.inputField.form.initialInputValueHandler = this;
			
			//IE workaround to set form event
			var temp = this.inputField.parentNode;
			while (temp.tagName != 'FORM') {
				temp = temp.parentNode;
			}
			$(temp).observe('submit', function(event){
			  	var bindObj = this.initialInputValueHandler;
				if (bindObj.inputField != null && bindObj.initialValue != null
						&& bindObj.inputField.value == bindObj.initialValue) {
					bindObj.inputField.value = '';
					}
				 });
		}
	},

	focusHandler: function() {
		var bindObj = this.initialInputValueHandler;
		if (bindObj.inputField != null && bindObj.initialValue != null
				&& bindObj.inputField.value == bindObj.initialValue) {
			bindObj.inputField.value = '';
		}
	},
	
	blurHandler: function() {
		var bindObj = this.initialInputValueHandler;
		if (bindObj.inputField != null && bindObj.initialValue != null
				&& bindObj.inputField.value == '') {
			bindObj.inputField.value = bindObj.initialValue;
		}
	},
	
	submitHandler: function() {
		var bindObj = this.initialInputValueHandler;
		if (bindObj.inputField != null && bindObj.initialValue != null
				&& bindObj.inputField.value == bindObj.initialValue) {
			bindObj.inputField.value = '';
		}
	}
});

/**
 * Helps a form that will be submitted by a <input type="image" .../> element using GET method
 * to produce a cachable request. (without clicking coordinates)
 * Currently the name of the button will not be attached to the request.
 *
 * Constructor fields:
 * -------------------
 * form  the form to extend
 */
var cachedSubmit = Class.create({
	submitButton : null,
	
	initialize: function(form) {
		if (form && form.method == 'get') {
			this.form = form;
			var inputImages = form.getInputs('image');
			for (var i = 0; i < inputImages.length; i++) {
				var button = inputImages[i];
				button.cachedSubmitHandler = this;
				if (this.submitButton == null) {
					this.submitButton = new Element('input', {type: 'submit'});
					this.submitButton.hide();
					button.insert({after: this.submitButton});
				}
				button.onclick = this.clickHandler;
			}
		}
	},
	
	clickHandler: function() {
		if (this.disabled == false) {
			var bindObj = this.cachedSubmitHandler;
		    bindObj.submitButton.click();
		}
		return false;
	}
});

var NonFunctionalLink = Class.create({
	initialize: function(element) {
		if (!element.nonFunctionalLink) {
			element.onclick = this.clickHandler;
			element.nonFunctionalLink = true;
		}
	},
	
	clickHandler: function() {
		return false;
	}
});

var URLSelectingBox = Class.create({
	initialize: function(element) {
		if (element) {
			element.onchange = this.onChangeHandler;
		}
	},
	
	onChangeHandler: function() {
		window.location.href = this.value; 
	}
});

if ('observe' in document) {
	document.observe('dom:loaded', function() {
		var iivhFields = $$('.initialInputField');
		iivhFields.each(function(element) {
			new initialInputValueHandler(element);
		});

		new cachedSubmit($('SimpleSearchForm'));
		
		var urlSelectingBoxes = $$('select.urlSelectingBox');
		urlSelectingBoxes.each(function(element) {
			new URLSelectingBox(element);
		});
	});
}
