/*
 * $id$
 */

function LocationHintControl(base_input,hint_frame,hint_body) {

    var hinting_enabled = false;
    var last_state = '';
    var hint_selected = -1;
    var hints_total = 0;
    var hint_locations = [];
	var selectSize = null;

    function setHintVisible(state) {
        if (state==true) {
            makeVisible(hint_frame);
			setIE6SelectBoxCoverVisible('route_type',true, hint_body);
        }
        else {
            makeInvisible(hint_frame);
            setIE6SelectBoxCoverVisible('route_type',false, hint_body);
        }
    }

    function onKeyUp(ev) {
        if ((ev.key().code!=38) && (ev.key().code!=40)) {
            search_for = $(base_input).value;
            if (search_for!=last_state) {
                var _action = loadJSONDoc(JSON_REQUESTS_PATH,
                                          { method: "LocationHint",
                                            ref_location : search_for
                                          });
                _action.addCallbacks(onLocationHintResponse,onLocationHintResponseFail);
                last_state = search_for;
            }
            ev.cancelBubble = true;
        }
    }

    function onKeyDown(ev) {
        var new_hint = -1;

        if (hints_total>0) {
            if (ev.key().code==38)
                new_hint = (hint_selected>0) ? (hint_selected-1) : (hints_total-1);
            if (ev.key().code == 40)
                new_hint = (hint_selected == (hints_total-1)) ? 0 : (hint_selected+1);
            if (new_hint>=0) {
                onHintHighlight(new_hint);
                $(base_input).value = hint_locations[new_hint];
            }
        }
        ev.cancelBubble = true;
    }

    function onBlur(ev) {
        setTimeout(partial(setHintVisible,false),200);
        hinting_enabled = false;
    }

    function onFocus(ev) {
        hinting_enabled = true;
        last_state = $(base_input).value;
    }

    function onLocationHintResponse(response) {
		var date = new Date();
        if (hinting_enabled) {
            hints_total = response.length;
            hint_locations = response;
            hint_selected = -1;
            var htmlbody = '';
			
            for (linenr in response) {
                htmlbody += '<a href="#" class="location_hint_line" id="'+base_input+'_location_hint_line_'+linenr+'">'+response[linenr]+'</a>\n';
            }
            $(hint_body).innerHTML = htmlbody;
            setHintVisible(true);
            for (linenr in response) {
                connect(base_input+'_location_hint_line_'+linenr,'onclick',partial(onHintSelected,response[linenr]));
                connect(base_input+'_location_hint_line_'+linenr,'onmouseover',partial(onHintHighlight,parseInt(linenr)));
            }
            if (response.length==0) {
                setHintVisible(false);
            }
            $(hint_frame).style.height = 'auto';
            onHintHighlight(0);
        }
    }

    function onHintHighlight(linenr) {
        if (hint_selected>=0) {
            removeElementClass(base_input+'_location_hint_line_'+hint_selected, "location_hint_highlight");
        }
        hint_selected=linenr;
        if (linenr>=0) {
            addElementClass(base_input+'_location_hint_line_'+linenr, "location_hint_highlight");
        }
    }

    function onHintSelected(hintbody,ev) {
        $(base_input).value = hintbody;
        return false;
    }

    function onLocationHintResponseFail() {
        // silent fail
        $(base_input).value = "dispatcher error";
    }

    connect(base_input,'onkeyup',onKeyUp);
    connect(base_input,'onkeydown',onKeyDown);
    connect(base_input,'onblur',onBlur);
    connect(base_input,'onfocus',onFocus);
    $(base_input).setAttribute('autocomplete','off');
}
