ClassifierType = new Object();
ClassifierType.streets = 5;
ClassifierType.address = 6;

function AddresserItem(wrapper, type, parentType, container, width, height) {
    var items = new Array(), selectHandler = null, showHandler = null, parentId = -1, selIndex = -1, hlIndex = -1, startLoadingHandler = null, stopLoadingHandler = null, beginHLIndex, endHLIndex;
    var hint = type == 5 ? "Улицы" : "Строения";
    
    var areaBtn = document.createElement('div');
    var text = document.createElement('input');
	container.appendChild(text);
    var dropDown = new DropDown();
    text.style.width = (width - 40) + 'px';
//    var downButton = new ImageButton(areaBtn, 24, 24);
    var showButton = new ImageButton(container, 24, 24);
	var needShow = false;

    areaBtn.className = 'addrBtnArea';
    $(text).addClass("addrTextBox").addClass("addrTextBoxDisable");

	var textBox = $(text).hintTextBox({hint:hint, hintCSS:"addrTextBoxHint"});
    
    //container.appendChild(text);
    //container.appendChild(areaBtn);

	var nodeClick = function(i) { selIndex = i; textBox.val(items[i].text); dropDown.hide(); if (selectHandler) selectHandler(items[i].id); if(needShow) showClick(); needShow = false;}
    var showList = function() {
        var list = dropDown.content();
        var listHeight;
        list.innerHTML = '';
        items = wrapper.PAO(this.responseXML);
        for (var i = 0; i < items.length; i++) {
            items[i].onClick(nodeClick);
            items[i].setIndex(i);
            list.appendChild(items[i].getDOM());
        }
		if(items.length == 1){
			nodeClick(0);
			if(needShow) showClick();
			needShow = false;
			return;
		}
        if (items.length == 0)
            listHeight = 30;
        listHeight = 19 * (items.length <= 8 ? items.length : 8);

        beginHLIndex = 0;
        endHLIndex = 7;

        var pos = getPosition(text);
        var offset = getPageOffset();
        pos.left += offset.left; pos.top += offset.top;
        pos.top += text.offsetHeight
        dropDown.show(pos.left, pos.top, width, listHeight);
        text.focus();
        hlIndex = -1;
        selIndex = -1;
        if (stopLoadingHandler)
            stopLoadingHandler();
    }
	var textKeyUp = function(){/*showButton.enable(textBox.val() != "");*/ selIndex = -1; }
	var textKeyPress = function(event) {
		if (!event) event = window.event;
		if(dropDown.isShow()) {
			var list = dropDown.content();
			if(event.keyCode == 13) { if(hlIndex > -1 && hlIndex < items.length) nodeClick(items[hlIndex].getIndex());else loadData();}
			else if(event.keyCode == 27) { dropDown.hide();} 
			else if(event.keyCode == 40 && hlIndex < items.length - 1) { if( hlIndex > -1) items[hlIndex].highLight(false); items[++hlIndex].highLight(true); if(hlIndex > endHLIndex){endHLIndex++; beginHLIndex++; list.scrollTop += 19;}}
			else if(event.keyCode == 34 && hlIndex < items.length - 1) { if( hlIndex > -1) items[hlIndex].highLight(false);else hlIndex++; hlIndex = (hlIndex + 8) < (items.length - 1) ? hlIndex + 8 : items.length - 1; items[hlIndex].highLight(true); if(hlIndex > endHLIndex){ var h = hlIndex - endHLIndex; endHLIndex += h; beginHLIndex += h; list.scrollTop += 19 * h;}}
			else if(event.keyCode == 35 && hlIndex < items.length - 1) { if( hlIndex > -1) items[hlIndex].highLight(false); hlIndex = items.length - 1; items[hlIndex].highLight(true); if(hlIndex > endHLIndex){ endHLIndex = items.length - 1; beginHLIndex = items.length - 7; list.scrollTop = list.scrollHeight;}} 
			else if(event.keyCode == 38 && hlIndex > 0) { if( hlIndex < items.length) items[hlIndex].highLight(false); items[--hlIndex].highLight(true); if(hlIndex < beginHLIndex){ endHLIndex--; beginHLIndex--; list.scrollTop -= 19;}}
			else if(event.keyCode == 33 && hlIndex > 0) { if( hlIndex < items.length) items[hlIndex].highLight(false); hlIndex = (hlIndex - 8) > 0 ? hlIndex - 8 : 0; items[hlIndex].highLight(true);if(hlIndex < beginHLIndex){var h = (beginHLIndex - hlIndex); endHLIndex -= h;beginHLIndex -= h;list.scrollTop -= 19*h;}} 
			else if(event.keyCode == 36 && hlIndex > 0) {if( hlIndex < items.length)items[hlIndex].highLight(false);hlIndex = 0;items[hlIndex].highLight(true);if(hlIndex < beginHLIndex){beginHLIndex = 0;endHLIndex = 7;list.scrollTop = 0;}}
		} else { if (event.keyCode == 13) { loadData();}}
	}
	var loadData = function() { if (startLoadingHandler) startLoadingHandler(); wrapper.LAD(showList, null, type, textBox.val(), parentType, parentId);}
	var showClick = function() { if(selIndex != -1) { if (showHandler) showHandler(type, items[selIndex].id);}else {needShow = true;loadData();}}

//    downButton.setURL('img/addresser/addr_find.png', 'img/addresser/addr_find.png', 'img/addresser/addr_find.png', 'img/addresser/addr_find_d.png');
//    downButton.onClick(loadData);
//    downButton.enable(true);
    showButton.setURL('img/addresser/addr_show.png', 'img/addresser/addr_show.png', 'img/addresser/addr_show.png', 'img/addresser/addr_show_d.png');
    showButton.onClick(showClick);
	showButton.getDOM().className = "addrBtn";
    showButton.enable(true);

    //var btn = downButton.getDOM();
    //btn.style.margin = '0px 0px 0px 3px';
    //btn = showButton.getDOM();
    //btn.style.margin = '0px 0px 0px 3px';
	
	text.onkeydown = textKeyPress;
	text.onkeyup = textKeyUp;

	this.onSelect = function(handler) {selectHandler = handler;}
	this.onShow = function(handler) {showHandler = handler;}
	this.getParent = function() {return parentId;}
	this.setParent = function(id) {parentId = id;}
	this.enable = function(state) {if (state) $(text).removeClass("addrTextBoxDisable");else if (state) $(text).addClass("addrTextBoxDisable");text.disabled = !state;/*downButton.enable(state);*/showButton.enable(state);}
	this.clear = function() {textBox.val("");selIndex = -1;/*showButton.enable(false);*/}
	this.onStartLoading = function(handler) {startLoadingHandler = handler;}
	this.onStopLoading = function(handler) {stopLoadingHandler = handler;}
}
function Addresser(container, width) {
    var streetsArea = document.createElement('div'), addressArea = document.createElement('div'), wrapper = new WH();
    streetsArea.className = 'addresserItem';
    streetsArea.style.width = (width-12) + 'px';
    addressArea.className = 'addresserItem';
    addressArea.style.width = (width-12)/2 + 20 + 'px';

    var streets = new AddresserItem(wrapper, 5, 3, streetsArea, width-12, 25);
    var addresses = new AddresserItem(wrapper, 6, 5, addressArea, (width-12)/2, 25);

    streets.setParent(1); streets.enable(true); addresses.enable(false);
    streets.onSelect(function(id) { addresses.setParent(id); addresses.enable(true); addresses.clear(); });

    container.appendChild(streetsArea);
    container.appendChild(addressArea);
    $(container).append("<div style=\"clear:both\"></div>");

	this.init = function(mapWrapperURL) { wrapper.MapWrapperURL = mapWrapperURL; wrapper.Host = document.domain;}
	this.onShow = function(handler) {streets.onShow(handler);addresses.onShow(handler);}
	this.onStartLoading = function(handler) {streets.onStartLoading(handler);addresses.onStartLoading(handler);}
	this.onStopLoading = function(handler) {streets.onStopLoading(handler);addresses.onStopLoading(handler);}
	this.setTown = function(id) {streets.setParent(id);streets.enable(true);addresses.enable(false);}
}
