/*------------------Fast search---------------------------------*/
var FastSearch = {
	id: "fast_search",
	found_cnt: 0,
	query_cache: "",
	query_fail: new Array(),
		
	init: function(value, id) {
		if(id) this.id = id;
		if(!$(this.id)) return false;
		
		var handler = function() {
			$(this.id).value = value;
			$(this.id).onfocus = function() {
				if($F(this.id) == value) {
					$(this.id).value = '';
					$(this.id).style.color = '#000';
				}
			};
			$(this.id).onblur = function() {
				if($F(this.id) == '') {
					$(this.id).value = value;
					$(this.id).style.color = '#BBB';
				}
			}
			$(this.id).parentNode.insertBefore(new Element('span', {'id': this.id + '_stat'}).setStyle({fontSize: '14px'}).update("&nbsp;<span id='" + this.id + "_found_cnt' style='font-weight: bold; color: #000;'></span>&nbsp;[" + $$('.' + this.id + '_item_value').length + ']'), $(this.id).nextSibling);
			$(this.id).onkeypress = FastSearch.start;
			$(this.id).onkeyup = FastSearch.start;
		}

		Event.observe(document, "dom:loaded", handler.bindAsEventListener(this));
	},
	
	start: function (event) {
		if(!($F(this.id) && this.query_cache && $F(this.id).toLowerCase().indexOf(this.query_cache.toLowerCase()) == 0)) {
			var parents = $$("." + this.id + "_item_parent_off");
			for(p = 0; p < parents.length; p++) {
				parents[p].style.display = "";
				parents[p].removeClassName(this.id + "_item_parent_off");
				parents[p].addClassName(this.id + "_item_parent");
			}
		}
	
		if($F(this.id)) {
			this.found_cnt = 0;
			var items = $$("." + this.id + "_item_parent ." + this.id + "_item_value");
			for(i = 0; i < items.length; i++) {
				if( items[i].innerHTML.toLowerCase().indexOf($F(this.id).toLowerCase()) > -1 ) {
					this.found_cnt += 1;
				}
				else {
					var ancestors = items[i].ancestors();
					for(a = 0; a < ancestors.length; a++) {
						if(ancestors[a].hasClassName(this.id + "_item_parent")) {
							ancestors[a].style.display = "none";
							ancestors[a].addClassName(this.id + "_item_parent_off");
							ancestors[a].removeClassName(this.id + "_item_parent");
						}
					}
				}
			}
			$(this.id + "_found_cnt").innerHTML = this.found_cnt;
			this.query_cache = $F(this.id);
		}
		else {
			$(this.id + "_found_cnt").innerHTML = '';
		}
	}
};
/*--------------------------------------------------------------*/


Element.Methods.slightVertScrollTo = function(element,step) {
	element = $(element);
	if(typeof(Scriptaculous) != 'undefined') {
		new Effect.ScrollTo(element, {duration: 1.5, offset: -10});
	}
	else {
		var scroll_from = document.viewport.getScrollOffsets();
		var scroll_to  = element.cumulativeOffset();
		for (var i = scroll_from.top; i <= scroll_to.top; i += (step ? step : 5) )
			window.scrollTo(scroll_from.left, i);
		window.scrollTo(scroll_from.left, scroll_to.top);
	}
	return element;
};
Element.addMethods();

