/*============= Міста ====================*/

var citiesString = '';
var gotData = 0;
var GlInputObj = null;
var GlEvent = null;
var cityIsSelected = 0;
var GCitiesList = null;


function cities_by_keyword (value) {
	//$.get( '/users/getCitiesList/city/'+value+'/', {}, set_cities_string);
	$.ajax({
		url: "/users/getCitiesList/",
		type: "POST",
		data: {city: value},
		success: function(answ){
			set_cities_string(answ);
		}
	});
}
function set_cities_string (data) {
	var citiestr = data;
	var citiesArr = new Array();
	if (citiestr != '') citiesArr = citiestr.split(",");
	var count = (citiestr != '') ? citiesArr.length : 0;
	count = (count < 50) ? count : 50;
	onthescreen = (count <= 7) ? count : 7;
	var div_height = onthescreen * 40;
	GCitiesList.style.height = div_height + 'px';
	if (count > 0) {
		var listHTML = '';
		var names = new Array();
		for (var i = 0; i < count; i++) {
			names = citiesArr[i].split("##");
			if (i == 0) {
				listHTML += '<div class="city_item_on" id="city_item0" style="border-bottom: 1px solid #999999" onmouseover="this.className=\'city_item_on\'" onmouseout="this.className=\'city_item_off\'" onclick="set_selected_city(\''+names[0]+'\', '+names[1]+', '+names[3]+', '+names[5]+');setSPXY();"><div id="city_name0">'+ names[0] +'</div><div style="font-size:12px;color: #999999;">'+names[2]+', '+names[4]+' обл.</div></div>';
				document.getElementById('cities_item_index').value = 0;
			}
			else {
				listHTML += '<div class="city_item_off" style="border-bottom: 1px solid #999999" id="city_item'+ i +'" onmouseover="this.className=\'city_item_on\'" onmouseout="this.className=\'city_item_off\'" onclick="set_selected_city(\''+names[0]+'\', '+names[1]+', '+names[3]+', '+names[5]+');setSPXY();"><div id="city_name'+ i +'">'+ names[0] +'</div><div style="font-size:12px;color: #999999;">'+names[2]+'</div><div style="font-size:12px;color: #999999;">'+names[4]+' обл.</div></div>';
			}
		}
		GCitiesList.innerHTML = listHTML;
        GCitiesList.style.display = "block";
		show_city_list(GlInputObj, GlEvent);
	}
	else {
		hide_city_list();
	}
}

function setSPXY(){}
function getCitiesList (inputObj, e) {
	// down key = 40
	// up key = 38
	/*if (e.keyCode==38 || e.keyCode==40) {
		var listObj = document.getElementById('cities_list');
		//if (listObj.style.display == 'block') {
			var item_index = document.getElementById('cities_item_index').value;
			if (e.keyCode==40) {
				item = document.getElementById('city_item'+item_index);
				item.className = 'city_item_off';
				item_index++;
				item = document.getElementById('city_item'+item_index);
				item.className = 'city_item_on';
			}
			else if (e.keyCode==38) {
				item = document.getElementById('city_item'+item_index);
				item.className = 'city_item_off';
				item_index--;
				item = document.getElementById('city_item'+item_index);
				item.className = 'city_item_on';
			}
			document.getElementById('cities_item_index').value = item_index;
		//}
		return;
	}
	else if (e.keyCode==13 || e.keyCode==9) {
		var listObj = document.getElementById('cities_list');
		//if (listObj.style.display == 'block') {
			item_index = document.getElementById('cities_item_index').value;
			item = document.getElementById('city_name'+item_index);
			city_value = item.innerHTML;
			//alert('set_selected_city('+item_index+', '+city_value+')');
			set_selected_city(city_value);
			return false;
		//}
	}*/

	if(e.keyCode==13 || e.keyCode==9) return;
	if(inputObj.value.length < 1) return;
	if(list_currentLetters[inputObj.name]==inputObj.value) return;

	list_currentLetters[inputObj.name] = inputObj.value;
	GlInputObj = inputObj;
	GlEvent = e;
	if(GlInputObj.id == 'other_city2')
		GCitiesList = document.getElementById('cities_list2');
	else{
		GCitiesList = document.getElementById('cities_list');
		document.getElementById("city_id").value="";
		document.getElementById("city_id_name").value="";
	}
	cities_by_keyword(inputObj.value);
}

function show_city_list (inputObj, e) {
	return;
	//alert(inputObj);
	var listObj = document.getElementById('cities_list');
	listObj.style.top = getAbsoluteTop(inputObj) - 300;
	listObj.style.left = getAbsoluteLeft(inputObj);
	listObj.style.display = 'block';
}

function set_selected_city (citystr, city_id, rayon_id, obl_id) {
	GlInputObj.value = citystr;
	if(GlInputObj.id == 'other_city2'){
		$('#now_live_city_id').val(city_id);
		$('#now_live_city_name').val(citystr);
		hide_city_list();
	}
	else{
		$('#other_city_info').removeClass('info');
		document.getElementById('city_id').value = city_id;
		document.getElementById('city_id_name').value = citystr;
		cityIsSelected = 1;
		setSchool(city_id);
		hide_city_list();
		$('#city+span').removeClass('info').empty().html('<span class="agree"></span>');
	}
}

function hide_city_list () {
	if (GCitiesList) GCitiesList.style.display = 'none';
}

function checkClick(e) {
	var target = (e && e.target) || (event && event.srcElement);
	var obj = document.getElementById('cities_list');
	var obj2 = document.getElementById('other_city');
	if (checkParent(target)) {
		obj.style.display='none';
		if (target != obj2 && !cityIsSelected)obj2.value = 'Оберіть місто';
	}
	else {
		null;
	}
}

function checkParent(t){
	while(t.parentNode) {
		if(t==document.getElementById('cities_list')){
			return false
		}
		t=t.parentNode
	}
	return true
}

function onChangeCityList (el,school_register) {
    if(school_register=="yes"){
    	setSchool(el.value,false,0);
    }

	if (el.value=='other') {
		$('#other_city_div').fadeIn("slow");
        document.getElementById("other_city_div").style.display = "block";

        $("#school_div").hide();
        $("#known_schools").hide();
	}
	else {
		document.getElementById('other_city_div').style.display = 'none';
        document.getElementById("city_id_name").value="";
        document.getElementById("city_id").value=el.value;
	}
	
	if(el.value == 'choose_city'){
		$('#city+span').removeClass('info').empty().html('<span class="info">є обовязковим </span>');
	}
	else if(el.value == 'other'){
		
	}
	else{
		$('#city+span').removeClass('info').empty().html('<span class="agree"></span>');
	}
}
function onChangeLivingCity(el){
	if (el.value=='other') {
		$('#other_city_div2').fadeIn("slow");
        document.getElementById("other_city_div2").style.display = "block";
	}
	else if(el.value == 'false'){
		$('#now_live_city_id').val('');
		$('#now_live_city_name').val('');
		$('#other_city_div2').fadeOut('fast');
	}else {
		$('#other_city_div2').fadeOut('fast');
		$('#now_live_city_id').val(el.value);
		$('#now_live_city_name').val('');
	}
}
