/*
ajaxselect.js

VERSION 0.1
Last Modified: 13/05/2008
*/

var AjaxSelect = Class.create();

AjaxSelect.prototype = {

    initialize: function(url, entity, divSearch, searchInput)
    {
	this.url = url;
	this.entity = entity;		// type of request

	this.divSearch = divSearch;	// div for Search
	this.searchInput = searchInput;	// input for search
    
	this.pressedKey = 0;		// code of pressed button
    
	this.nodeList = new Array();	// the list of all nodes
	this.searchIndex = -1;		// current index of combo box
	this.userText = "";		// user's text in search textbox
    
	this.thHeader = '<table cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="#004477" height=1 colspan=3><IMG SRC="/Images/transp.gif" WIDTH=10 HEIGHT=1></td></tr><tr><td bgcolor="#004477" width="1"><IMG SRC="/Images/transp.gif" WIDTH=1></TD><td>';
	this.thFooter = '</td><td bgcolor="#004477" width=1><IMG SRC="/Images/transp.gif" WIDTH=1></td></tr><tr><td bgcolor="#004477" height=1 colspan=3><IMG SRC="/Images/transp.gif" WIDTH=10 HEIGHT=1></TD></TR></table>';

	this.isMozilla = false;
	if(document.implementation && document.implementation.createDocument)
	    this.isMozilla=true;
	
	this.timer = null;
	
	this.searchId	= -1;		// id of founded node
    },
    
    IsIE: function()
    {
	if (navigator.userAgent.indexOf('MSIE') != -1)
	    return true;
	else
	    return false;
    },
    
    HideSearch: function()
    {
	this.nodeList = new Array();
	this.index = -1;
	var area = $(this.divSearch);
	area.style.visibility = "hidden";
    },
    
    ClearTimer: function()
    {
	if (this.timer) {
	    this.ClearTimeout(this.timer);
	    this.timer = null;
	}
    },
	
    SetTimer: function()
    {
//	this.timer = window.setTimeout(this.HideSearch(), 0);
    },
    
    WaitSearch: function(evt)
    {
	this.ClearTimer();
	var area = $(this.divSearch);
	if (this.isMozilla) event = evt;
	
	var pos = getElementPosition($(this.searchInput));

	area.style.left	= pos.left;
	area.style.top = pos.top + 20;

	area.innerHTML = this.thHeader + 'Загрузка...' + this.thFooter;
        area.style.visibility = "visible";
    },
    
    LoadNodeList: function(evt)
    {
	var key = this.key;
	
	if (key == 13)
	    return;

	var searchText = $(this.searchInput).value;
	if (searchText == "" || key == 27 || searchText.length < 3)
	{
	    this.SetTimer();
	    this.HideSearch();
	    return;
	}
	
	if (key == 38 || key == 40)
	{
	    if (this.IsIE())
		this.Move();

	    return false;
	}
	
	this.WaitSearch(evt);
	
	var win1251 = 'false';

	var pars = "action=treelist&entity=" + this.entity + "&keyword=" + $(this.searchInput).value + "&win1251=" + win1251;
	var myAjax = new Ajax.Request(
    	    this.url,
    	    {method: "post", parameters: pars, onComplete: this.ProcessNodeList.bindAsEventListener(this)}
	);
	
    },
    
    ProcessNodeList: function(originalRequest)
    {
	this.nodeList = new Array();
	this.index = -1;
        var rows = originalRequest.responseText.split("\n");

	var html = "";
	for (i = 0; i < rows.length - 1; i++)
	{
	    var t = rows[i].split("|");
	    var o = {};
	    o.id = t[0];
	    o.name = t[1];
	    this.nodeList[i] = o;

	    var s = 'div_' + o.id;
	    html += '<div name="' + s + '" id="' + s + '"><nobr>&nbsp;' + o.name + '&nbsp;</nobr></div>';
	}
	
	if (html == "")
	    this.HideSearch();
	else
	    html += "<div align=\"right\"><a href=\"\" name=\"hrefclose\" id=\"hrefclose\" onClick=\"return false;\">Закрыть</a></div>";
	    
	$(this.divSearch).innerHTML = this.thHeader + html + this.thFooter;
	
	for (i = 0; i < this.nodeList.length; i++)
	{
	    var s = 'div_' + this.nodeList[i].id;
	    Event.observe($(s), 'mousemove', this.MoveByMouse.bindAsEventListener(this, i), false);
	    Event.observe($(s), 'click', this.SelectNode.bindAsEventListener(this), false);
	}
	Event.observe($('hrefclose'), 'click', this.HideSearch.bindAsEventListener(this), false);
    },

    Move: function()
    {
	var key = this.key;
	
	// Move down
	if (key == 40)
	{
	    if (this.index == -1)
		this.userText = $(this.searchInput).value;
	    if (this.index < this.nodeList.length - 1)
	    {
		if (this.index >= 0)
		    this.ClearStyle('div_' + this.nodeList[this.index].id);
		this.index++;
		this.SetStyle('div_' + this.nodeList[this.index].id);
		$(this.searchInput).value = this.nodeList[this.index].name;
	    }
	    
	    return false;
	}

	// Move up
	if (key == 38) {
	    if (this.index == 0)
		$(this.searchInput).value = this.userText;
	
	    if (this.index >= 0)
	    {
		this.ClearStyle('div_' + this.nodeList[this.index].id);
		this.index--;
		if (this.index >= 0)
		{
		    this.SetStyle('div_' + this.nodeList[this.index].id);
		    $(this.searchInput).value = this.nodeList[this.index].name;
		}
	    }	        

	    return false;
	}

    },
    
    KeyDown: function(evt)
    {
	this.key = evt.keyCode;
	
	if (this.key == 13)
	    this.HideSearch();
    },
    
    MoveByMouse: function(e, newIndex)
    {
	var curObj = $('div_' + this.nodeList[newIndex].id);
	curObj.style.cursor = 'pointer';
	var obj = $(this.searchInput);
//        if (this.index == -1)
//	    this.userText = obj.value;
//	else
//	    this.ClearStyle('div_' + this.nodeList[this.index].id);
	if (this.index >= 0)
	    this.ClearStyle('div_' + this.nodeList[this.index].id);
	this.index = newIndex;
	this.SetStyle('div_' + this.nodeList[this.index].id);
//	obj.value = this.nodeList[this.index].name;
    },
    
    SelectNode: function(e)
    {
	if (this.index >= 0)
	    $(this.searchInput).value = this.nodeList[this.index].name;
	this.GetNodeId();
    },
    
    SetStyle: function(elemId)
    {
	var d = document.getElementById(elemId);
	d.style.background = '#3366cc';
	d.style.color = '#FFFFFF';
    },

    ClearStyle: function(elemId)
    {
	var d = $(elemId);
	d.style.background = '#FFFFFF';
	d.style.color = '#000000';
    },
    
    GetNodeId: function()
    {
	var text = $(this.searchInput).value;

        var pars = 'action=treelist&entity=' + this.entity + '&name=' + text;
        var myAjax = new Ajax.Request(
            this.url,
            {method: 'get', parameters: pars, onComplete: this.ProcessNodeId.bindAsEventListener(this)}
        );
    },
									    
    ProcessNodeId: function(originalRequest)
    {
	var id = originalRequest.responseText;
	
	if (id.length > 0)
	{
	    this.searchId = id;
	    this.HideSearch();
	}
	else
	{
	    this.searchId = "";
	    alert("Элемента с названием '" + $(this.searchInput).value + "' не существует");
	}
    },

    SubmitForm: function(form)
    {
	if (this.searchId.length > 0)
	    $('did').value = this.searchId;

	form.submit();
	return true;
//	else
//	{
//	    this.hideSearch();
//	    this.GetNodeId();
//	    form.submit();
//	    return true;

//	}    
//	return false;
    }

}

