//+-----------------------------------------------------------------------+
//| THIS IS A COMMERCIAL PACKAGE!										  |
//|	You may not remove the copyright or use and redistribute the script	  |
//|	in any form without commercial license.								  |
//|	To obtain permission for commercial use, contact the copyright owner. |
//+-----------------------------------------------------------------------+

/**
 * @author Martinas Sadzevicius <m.sadzevicius@hexa.lt>
 * @package HexaCmsJs Library (commercial)
 * @version 1.0
 * @copyright © 2010 Martinas Sadzevicius. All rights reserved.
 */
Hexa = {};
Hexa.LiveSearch = Class.create({
    string: false,
    delayId: false,
    divEl: false,
    inputEl: false,
    options: {
	formId: 'search',
	inputId: 'searchfield',
	divId: 'livesearch',
	delayed: 0.5,
	url: '/search/live',
	currItemClass: 'hls_curritem',
	text: 'Greita prekių paieška',
	height: '170px'
    },
    initialize: function(options) {
	Object.extend(this.options, options);
	var el = $(this.options.inputId);
	if(!el) return;
	this.inputEl = el;
	this.divEl = $(this.options.divId);
	this.divEl.hide();
	this.divEl.setStyle({
	    width: (el.getWidth()-4)+'px',
	    top: el.positionedOffset().top+el.getHeight()+'px',
	    position: 'absolute',
	    'z-index': '9999',
	    height: this.options.height
	});

	el.observe('focus', this.showResult.bind(this));
	el.observe('blur', function(){
	    if(!this.inputEl.value)
		this.inputEl.value = this.options.text;
	}.bind(this));

	el.observe('keyup', function(e){
	    if (e.keyCode == Event.KEY_UP)
		this.keyUp();
	    else if(e.keyCode == Event.KEY_DOWN)
		this.keyDown();
	    else
		this.search(el.value);
	}.bind(this));

	$(this.options.formId).onkeypress =  function(e){
	    var key;
	    if(window.event)
		key = window.event.keyCode;
	    else
		key = e.which;

	    if(key == Event.KEY_RETURN)
		return this.keyEnter();
	    return true;
	}.bind(this);
    },
    search: function(string){
	if(string.length < 3) return;
	if(string == this.string) return;
	this.string = string;

	if(this.delayId)
	    window.clearTimeout(this.delayId);

	this.delayId = this.doSearch.bind(this).delay(this.options.delayed, string);
    },
    //private 
    doSearch: function(string){
	document.body.observe('click', function(e){
	    if(e.target.id != this.options.inputId)
		this.divEl.hide();
	}.bind(this))

	new Ajax.Updater(this.divEl, this.options.url, {
	    parameters: {
		string: string,
		request: 'ajax'
	    },
	    onSuccess: function(res){
		if(res.responseText != ''){
		    this.divEl.show();
		}else{
		    this.divEl.hide();
		}
	    }.bind(this),
	    onComplete: function(){ 
		var nodes = this.divEl.descendants();
		    nodes.each(function(item){
			item.observe('click', function(){
			    this.inputEl.value = item.innerHTML;
			}.bind(this))
		    }.bind(this))
	    }.bind(this)
	});
    },
    keyUp: function(){
	var currItem = $$('.'+this.options.currItemClass);
	var el;
	if(currItem == '') return;
	el = currItem[0];
	if(el.previous()){
	    el.removeClassName(this.options.currItemClass);
	    el = el.previous();
	}else{
	    return;
	}
	var itemHeight = el.getHeight();
	if(el.positionedOffset().top - el.cumulativeScrollOffset().top + itemHeight - 5 <= 0)
	    this.divEl.scrollTop -= itemHeight;
	el.addClassName(this.options.currItemClass);
    },
    keyDown: function(){
	var currItem = $$('.'+this.options.currItemClass);
	var el;
	if(currItem == ''){
	    el = this.divEl.down();
	}else{
	    el = currItem[0];
	    if(el.next()){
		el.removeClassName(this.options.currItemClass);
		el = el.next();
	    }else{
		return;
	    }
	}
	var itemHeight = el.getHeight();
	if(el.positionedOffset().top+itemHeight > this.divEl.getHeight())
	    this.divEl.scrollTop += itemHeight;
	el.addClassName(this.options.currItemClass);
    },
    keyEnter: function(){
	var currItem = $$('.'+this.options.currItemClass);
	if(currItem != ''){
	    if(this.string == currItem[0].innerHTML){
		return true;
	    }
	    this.string = this.inputEl.value = currItem[0].innerHTML;
	    return false;
	}
	return true;
    },
    showResult: function(){
	if(this.divEl.innerHTML){
	    this.divEl.show();
	}
	if(this.inputEl.value == this.options.text)
	    this.inputEl.value = '';
    }
});


window.onload = function(){
    var s = new Hexa.LiveSearch({});
}
