//console = loadFirebugConsole();

function initially_reset_form() {
		if (this.value == this.initial_value)
				this.value = "";
}
function initially_restore_form() {
		if (!this.value)
				this.value = this.initial_value;
}

function show_quicksearch_results() {
		$('quicksearch-results').setStyle("display", "block");
}
function hide_quicksearch_results() {
		$('quicksearch-results').setStyle("display", "none");
}

function do_inline_results() {
		var q = this.value;
		if (q.length<3) {
				hide_quicksearch_results();
				return;
		}
		var req = new Request.HTML({'url':'/search/quick/', 
																'onComplete':show_quicksearch_results,
																'onError':hide_quicksearch_results,
																'update':'quicksearch-results'});
		req.get({'q':q});
}

function init_search () {
		var search_form = $('search-form');
		var input = $(search_form['q']);

		input.initial_value = input.value;
		input.addEvent("focus", initially_reset_form);
		input.addEvent("blur", initially_restore_form);
		input.addEvent("keyup", do_inline_results);
}

function init_datasheets() {
		$$("DIV.item").each(function (item) {
						var buttons = item.getElements('A.show-details');
						if (!buttons) return;
						var button = buttons[0];
						var specs = item.getElements("DIV.product-details");
						if (!specs || !specs[0]) return;
						specs = specs[0];
						button.specs = specs;
						specs.setOpacity(0);
					        specs.injectInside(document.body);
					        specs.setStyles({'display':'none'});	
						specs.set('tween', { duration: 300, fps: 10 })
						function _position(div) {
								var scroll = document.body.getScroll();
								div.setStyles({'right':10,'top':scroll.y+200});
						}
						function _show_specs(ev) {
					                        specs.setStyles({'display':'block'});	
								this.specs.fade(0.95);
								_position(this.specs);
						}

						function _hide_specs(ev) {
								this.specs.fade("out");
					                        specs.setStyles.delay(500, {'display':'block'});	
						}

						button.addEvent("mouseover", _show_specs);
						button.addEvent("mouseout", _hide_specs);
				});
		//alert("OK");
}


window.addEvent("domready", init_search);
window.addEvent("domready", init_datasheets);
