/**
 * H2 Support
 */
var H2 = {
	include: function(filename) {
		var script = document.createElement('script');
		script.src = filename;
		script.type = 'text/javascript';
		document.documentElement.firstChild.appendChild(script);

		return script;
	},

	initLinks: function() {
		for (i in document.links) {
			link = document.links[i];
			if (link.rel) {
				if (link.rel.indexOf('external')!=-1) {
					Event.observe(link, 'click', function(event) {
						window.open(this.href);
						event.stop();
					});
				} else if (link.rel.indexOf('internal')!=-1) {
					Event.observe(link, 'click', function(event) {
						window.open(this.href);
						event.stop();
					});
				} else if (link.rel.indexOf('popup')!=-1) {
					Event.observe(link, 'click', function(event) {
						H2.openPopupWindow(this.href);
						event.stop();
					});
				}
			}
		}
	},

	addFormValidation: function(form) {
		var noFieldValidation = false;
		$$("#" + form.id + " input[name='_noFieldValidation']").each(function(item) {
			noFieldValidation = true;
		});
		if (!noFieldValidation) {
			Form.getElements(form).each(function(item) {
				Event.observe(item, 'blur', function() {
					var data = new Hash();
					data.set('_action', form.action);
					data.set(this.name, this.value);

					new Ajax.Request(form.action + "/fieldvalidation",
						{
							method:'post',
							parameters: data,
							onSuccess: function(transport, json) {
								var result = transport.responseText.evalJSON();

								if (!result.errors || !result.errors[item.name]) {
									item.removeClassName('error');
									item.title = "";
									$(item.id + "-error").remove();
									$$("label[for='" + item.id + "']").each(function(label) {
										label.removeClassName('error');
									});
								}

								if (result.errors) {
									Object.keys(result.errors).each(function(key) {
										result.errors[key].each(function(error) {
											$$("#" + form.id + " *[name='" + key + "']").each(function(item) {
												if (!item.hasClassName('error')) {
													item.addClassName('error');
												}
												$$("label[for='" + item.id + "']").each(function(label) {
													if (!label.hasClassName('error')) {
														label.addClassName('error');
													}
													var errorMessage = error.replace("{label." + key + "}", label.innerHTML.replace(/(.+):([ *]+)?/, "$1")).replace("&nbsp;", " ").replace(/<\/?[^>]+>/g, "");

													item.title = errorMessage;
													var errorMsg = new Element('div', {
														'class': "input-error-message",
														id: item.id + "-error",
													});
													Element.insert(errorMsg, new Element('img', {
															alt: errorMessage,
															src: "/img/icons/exclamation_mark.gif",
															width: 15,
															height: 15
														})
													);
													Element.insert(errorMsg, errorMessage);
													if ($(item.id + "-error")) {
														Element.replace(item.id + "-error", errorMsg);
													} else {
														Element.insert(item, {after: errorMsg});
													}
												});

												// Disable form submiting
												$$("#" + form.id + " input[type='submit']").each(function(submit) {
													Form.Element.disable(submit);
												});
											});
										});
									});
								} else {
									// Enable form submiting if no other errors found
									var anyErrorsInForm = false;
									$$("#" + form.id + " .input-error-message").each(function() {
										anyErrorsInForm = true;
									});
									if (!anyErrorsInForm) {
										$$("#" + form.id + " input[type='submit']").each(function(submit) {
											Form.Element.enable(submit);
										});
									}
								}
							},
							onFailure: function(){ alert('Validation service error...') }
						});
				});
			});
		}
	},

	initFormsValidation: function() {
		$$("form").each(function(form) {
			if (form.id) {
				H2.addFormValidation(form);
			}
		});
	},

	openPopupWindow: function(url, resizable, width, height, left, top) {
	  if (typeof resizable == 'undefined') resizable = true;
	  if (typeof width == 'undefined') width = 1024;
	  if (typeof height == 'undefined') height = 600;

	  var _width = screen.availWidth, _height = screen.availHeight;
	  if (typeof left == 'undefined') {
		  if (_width > width)
		  	left = (_width-width)/2;
		  else
		  	left = 0;
	  }
	  if (typeof top == 'undefined') {
		  if (_height > height)
		  	top = (_height-height)/2;
		  else
		  	top = 0;
	  }

	  if (typeof left == 'undefined') left = 0;

	  return !open(url, "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+(resizable?"yes":"no")+",resizable="+(resizable?"yes":"no")+",copyhistory=no,top="+top+",left="+left+",width="+width+",height="+height);
	}
};


Event.observe(window, 'load', function() {
	H2.initLinks();
	H2.initFormsValidation();

	$('searchinput').observe('focus', function() {
		if(this.value == this.title) {
			this.value = '';
		}
	});
	$('searchinput').observe('blur', function() {
		if(this.value == '') {
			this.value = this.title;
		}
	});
});

