var isMozilla = browser_type() == 'ns';

function search__search_form_class(countries_is_load)
{
	var countries_is_load = countries_is_load;
	var form = $('search_form');
	var _this = this;
	var select_geo = false;

	this.select_search_type = function(id)
	{
		switch (id) {
			case 'search_cityname':
				$('geo_div').style.display = 'none';
				$('search_zipcode').style.display = 'none';
				$('search_distance').style.display = 'block';
				break;
			case 'search_zipcode':
				$('geo_div').style.display = 'none';
				$('search_cityname').style.display = 'none';
				$('search_distance').style.display = 'block';
				break;
			case 'geo_div':
				if (!countries_is_load)
					this.geo_update(null, 'country')
				$('search_zipcode').style.display = 'none';
				$('search_cityname').style.display = 'none';
				$('search_distance').style.display = $('geo_city_div').style.display != 'none' && $('geo_city').value ? 'block' : 'none';
				break;
		}

		$(id).style.display = 'block';
	}

	this.geo_update = function(select_box, to_update)
	{
		this._block_form(true);

		var value = to_update == 'country' ? null : select_box.options[select_box.selectedIndex].value;

		switch (to_update){
			case 'country':
				$('geo_region_div').style.display = 'none';
				$('geo_city_div').style.display = 'none';
				select_box = {id: 'geo'};
				countries_is_load = true;
				break;
			case 'region':
				$('geo_region_div').style.display = value ? 'block' : 'none';
				$('geo_city_div').style.display = 'none';
				break;
			case 'city':
				$('geo_city_div').style.display = value ? 'block' : 'none';
				break;
		}

		this.select_search_type('geo_div');
		geo_update_selectbox(value, select_box.id, null, _this._block_form);
	}

	this.set_radio = function(id)
	{
		id = id || 'listbox';
		$('by_' + id).checked = true;
	}

	this.change_search_geo = function(selectbox)
	{
		$('warning_country').style.display = 'none';
		$('search_geo_region_block').style.display = 'none';

		if (!selectbox.value)
			return;

		geo_clear_selectbox('search_geo_region');

		this.check_form(true);
	}

    this.check_search_text = function()
    {
        var search_text = $('cityname').value.replace(/\s/g, '');

		if (/^[a-z]?\d/.test(search_text)) {
            $('by_zipcode').checked = true;
			$('zipcode').value = $('cityname').value;
        } else
            $('by_cityname').checked = true;

		return this.check_form();
    }

	this.check_form = function(select_country)
	{
		select_geo = select_country || false;

		if (form.by_listbox && form.by_listbox.checked)
			return true;

		var search_by = form.by_cityname.checked ? 'cityname' : 'zipcode';

		if (search_by == 'cityname' && !form.cityname.value
			|| form.zipcode && search_by == 'zipcode' && !form.zipcode.value)
			return true;

		this._block_form(true);

		var params = {
			search_by:	search_by,
			country: 	form.search_geo_country.value,
			region:		form.search_geo_region.value,
			cityname:	form.cityname.value,
			zipcode:	form.zipcode.value,
			action:		'check_form'};

		request_module('search:search_form', params, {method: 'POST', caching: true, receiver: this._receiver_check_form})
		return false;
	}

	this._receiver_check_form = function(resp)
	{
		resp = resp.responseJS;

		if ((resp == 'ok' || resp.ok) && !select_geo) {
			if (resp.country)
				_this._set_search_geo(resp, 'country');
			if (resp.region)
				_this._set_search_geo(resp, 'region');

			_this._block_form(false);
			form.submit();
			return;
		} else if (!resp) {
			$('warning_' + (form.by_cityname.checked ? 'cityname' : 'zipcode')).style.display = 'block';
		} else {
			if (resp.countries) {
				_this._show_search_select(resp.countries, 'search_geo_country');
				if (resp.country)
					$('search_geo_country').selectedIndex = 1;
				else
					$('warning_country').style.display = 'block';
			}

			if (resp.regions) {
				$('warning_country').style.display = 'none';
				var country = $('search_geo_country');
				$('warning_country_name').innerHTML = country.options[country.selectedIndex].text;
				$('warning_region').style.display = 'block';
				_this._show_search_select(resp.regions, 'search_geo_region');
				country = null;
			}
		}
		_this._block_form(false);
		search__search_form_city_list_obj.set_onclick();
	}

	this._show_search_select = function(geo, id)
	{
		geo_create_options(geo, id);
		$(id + '_block').style.display = 'block';
	}

	this._set_search_geo = function(resp, id)
	{
		var geo = $('search_geo_' + id);
		geo.options.length = 1;
		geo.options[0].value = resp[id];
		geo.selectedIndex = 0;
		geo = null;
	}

	this.hide_zipcode_warning = function()
	{
		$('warning_zipcode').style.display = 'none';
	}

	this._hide_search_block = function()
	{
		$('search_geo_region_block').style.display = 'none';
		$('search_geo_country_block').style.display = 'none';
		$('warning_cityname').style.display = 'none';
		geo_clear_selectbox('search_geo_country');
		geo_clear_selectbox('search_geo_region');
	}

	this.set_with_webcam = function()
	{
		var checked = $('withWebcam').checked;
		if (checked) {
			$('onlineOnly').checked = true;
			$('onlineOnly').disabled = true;
		} else {
			$('onlineOnly').disabled = false;
		}
	}

	this._block_form = function(block)
	{
		if (block)
			show_loading({el: form, init_delay: 500});
		else
			hide_loading();
		$('cityname').disabled = block;
		$('search_form_button').disabled = block;
		try{
			geo_block(block, 'search_');
			$('zipcode').disabled = block;
			$('by_listbox').disabled = block;
			$('by_cityname').disabled = block;
			$('by_zipcode').disabled = block;
		} catch(e) {}
	}
}

function search__search_form_city_list_class()
{
	var cityname = $('cityname');
	var citylist = $('citylist');
	var req;
	var load_timer = null;
	var _this = this;
	var options = null;
	var curr_option = -1;
	var deleted = false;

	this.check = function(e)
	{
		search__search_form_obj._hide_search_block();

		if (/^[a-z]?\d/.test(cityname.value))
			return;

		e = e || window.event;
		var key = e.charCode || e.keyCode;

		deleted = false;

		if (key == 38 || key == 40) {
			this._select(key == 38 ? -1: 1, true);
			return;
		} else if (key == 8 || key == 46)
			deleted = true;

		var length = cityname.value.length;

		if (length <= 0) {
			this.hide();
			return;
		} else if (length < 3)
			return;

		clearTimeout(load_timer);
		load_timer = setTimeout(function(){_this.load(cityname.value)}, 10);
	}

	this.load = function(text)
	{
		//if (search_req) search_req.abort();

        var options = {caching: true, receiver: this._draw};
		var params = {text: text};
		var query = '/ajax/search/suggest/U' + ((_USER && _USER.id) ? _USER.id: 0) + '/';

		search_req = ajax_request_(query, params, options)
	}

	this.show = function(e)
	{
		if (!citylist.innerHTML)
			return;

		var click = e && e.type == 'click';

		this._reset();

		if (!deleted && !click && options) {
            var med = cityname.value;
            cityname.value = options[0].innerHTML;

            if (cityname.createTextRange) {
                var range = cityname.createTextRange();
                range.moveStart('character', med.length);
                range.select();
            } else if (cityname.setSelectionRange)
                cityname.setSelectionRange(med.length, cityname.value.length);
        }

		document.body.old_onclick = document.body.onclick;
		stop_event(e);
		document.body.onclick = this.hide;

		var coords = getAbsCoords($('cityname'));
		citylist.style.left = coords.left;
		citylist.style.top = coords.top + $('cityname').offsetHeight + (isMozilla ? -1 : 1);
		citylist.style.display = 'block';
	}

	this._reset = function(){
        if (curr_option == -1)
            return;
		if (citylist.childNodes[curr_option])
			citylist.childNodes[curr_option].className = 'no_sel';
        curr_option = -1;
    }

	this.hide = function()
	{
		citylist.style.display = 'none';
		document.body.onclick = document.body.old_onclick;
	}

	this._draw = function(resp)
	{
		resp = resp.responseJS[0];

		var resp_options = resp.options;

		citylist.innerHTML = '';
        var k, option;

        if (!resp_options) {
            option = document.createElement('DIV');
            option.className = 'no_sel';
            option.innerHTML = _loc('search', 'no_such_city');
            citylist.appendChild(option);
            options = null;
			_this.show();
            return;
        }

        resp_options.each(function(o, k) {
            option = document.createElement('DIV');
            option.className = 'no_sel';
            option.innerHTML = o;
            option.onclick = function(){
                _this._insert(this.innerHTML);
                _this.hide();
            };

            _this._add_onmouseover(option, k);

            citylist.appendChild(option);
        });

        options = citylist.childNodes;

        _this.show();
	}

    this._select = function(pos, shift, no_ins)
	{
        if (!options)
            return;

        if (shift)
            pos += parseInt(curr_option);

        if (curr_option > -1 && pos < options.length)
            options[curr_option].className = 'no_sel';

        if (pos > -1 && pos < options.length)
            options[pos].className = 'sel';

        if (pos >= -1 && pos < options.length) {
            curr_option = pos;

            if (pos > -1 && !no_ins)
                this._insert(options[pos].innerHTML);
        }
    }

	this._add_onmouseover = function(option, k){
        option.onmouseover = function(){
            _this._select(k, null, true);
        }
    }

	this._insert = function(city)
	{
        cityname.value = city;
    }

	this.set_onclick = function()
	{
		cityname.onclick = function(e){
			stop_event(e);
			cityname.onclick = function(){
				_this.show()
			}
		}
	}
}
