var mouse_wheel;
function change_country_form(url, country) {
	if ($('quick_town')) {
		$('quick_town').innerHTML = '';
	}
	if ($('town_list')) {
		$('town_list').innerHTML = '';
	}
	if ($('subway')) {
		$('subway').innerHTML = '';
	}
	if ($('highway')) {
		$('highway').innerHTML = '';
	}
	if ($('okrug')) {
		$('okrug').innerHTML = '';
	}
	if (country == 0) return false;
	new Ajax.Request(url + '?country='+ country, {
		method: 'get',
		onSuccess: function(transport) {
			$('town_list').innerHTML = transport.responseText;
		}
  	});
}

function change_form(url, index, okr, obl) {
	if(index == 0 || index == 9999 ||  index == 10000) {
		if(index == 0) {okr=1; obl=15;}
		if(index == 9999) {
			okr=2; obl=29;
			if ($('highway')) $('highway').innerHTML = '';
		}
		if (index == 10000) {
			if ($('highway')) $('highway').innerHTML = '';
		}
		new Ajax.Updater('okrug', url + '?okrug='+okr+'&obl='+obl + '&check_query=' +index, {
			method: 'get'
		});
	} else {
		$('okrug').innerHTML = '';
	}
}

function createMarker(point, address, icon) {
	  var marker = new GMarker(point, icon);
	  marker.value = address;
	  if (address != '') {
		  GEvent.addListener(marker, "click", function() {
		    var myHtml = address;
		    map.openInfoWindowTabsHtml(point, myHtml);
		  });
	  }
	  return marker;
}

function change_subway(url, town) {
	if(town != 0 && town != 9999) {
		new Ajax.Updater('subway', url + '?town_id=' + town, {
			method: 'get'
		});
	} else {
		if ($('highway')) $('highway').innerHTML = '';
		$('subway').innerHTML = '';
	}
} 

function change_highway(url, town) {
	if(town != 0 && town != 9999) {
		new Ajax.Updater('highway', url + '?town_id=' + town, {
			method: 'get'
		});
	} else {
		if ($('subway')) $('subway').innerHTML = '';
		$('highway').innerHTML = '';
	}
} 

function change_coords(url, url_search, town, country, opt) {
	if (country == 'country' && town == 1) {
		country = ''; town = 139;
		mouse_wheel = 1; // чтобы не вызывался поиск второй раз, когда меняется коэффициент масштаба
	}
	if (country == 'country') {
		mouse_wheel = 1; // чтобы не вызывался поиск второй раз, когда меняется коэффициент масштаба
		new Ajax.Request(url + '?'+opt+'_id='+ town, {
			method: 'get',
    		onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				gmap_x = json.gmap_x;
				gmap_y = json.gmap_y;
				new_center = new GLatLng(gmap_x, gmap_y);
				if (opt == 'country' && town != 0) {
					var scale = 5;
				} else if (opt == 'country' && town == 0){
					var scale = 3;
					town = 139;
				} else {
					var scale = 10;
				}
				map.setZoom(scale);
		        $('MapScaleInput').value = map.getZoom();
		        map.setCenter(new_center);
		        bounds = map.getBounds();
				$('SouthWestXInput').value = bounds.getSouthWest().lat();
				$('SouthWestYInput').value = bounds.getSouthWest().lng();
				$('NorthEastXInput').value = bounds.getNorthEast().lat();
				$('NorthEastYInput').value = bounds.getNorthEast().lng();
				$('CenterXInput').value = map.getCenter().lat();
				$('CenterYInput').value = map.getCenter().lng();
				search(url_search, town);
    		}
      	});
	} else {
		if(town != 10000) {
			if (town == 0) town = 139;
			if (town == 9999) town = 143;
	    	new Ajax.Request(url + '?town_id='+ town, {
	    		method: 'get',
	    		onSuccess: function(transport) {
					var json = transport.responseText.evalJSON();
					gmap_x = json.gmap_x;
					gmap_y = json.gmap_y;
					new_center = new GLatLng(gmap_x, gmap_y);
			        map.setZoom(10);
			        $('MapScaleInput').value = map.getZoom();
			        map.setCenter(new_center);
			        bounds = map.getBounds();
					$('SouthWestXInput').value = bounds.getSouthWest().lat();
					$('SouthWestYInput').value = bounds.getSouthWest().lng();
					$('NorthEastXInput').value = bounds.getNorthEast().lat();
					$('NorthEastYInput').value = bounds.getNorthEast().lng();
					$('CenterXInput').value = map.getCenter().lat();
					$('CenterYInput').value = map.getCenter().lng();
					search(url_search, town);
	    		}
	      	});
		}
	}
} 

function change_coords_highway(url, url_search, highway) {
	mouse_wheel = 1; // чтобы не вызывался поиск второй раз, когда меняется коэффициент масштаба
	if(highway != 0) {
    	new Ajax.Request(url + '?highway='+ highway, {
    		method: 'get',
    		onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				gmap_x = json.gmap_x;
				gmap_y = json.gmap_y;
				new_center = new GLatLng(gmap_x, gmap_y);
				map.setZoom(12);
				$('MapScaleInput').value = map.getZoom();
		        map.setCenter(new_center);
		        bounds = map.getBounds();
				$('SouthWestXInput').value = bounds.getSouthWest().lat();
				$('SouthWestYInput').value = bounds.getSouthWest().lng();
				$('NorthEastXInput').value = bounds.getNorthEast().lat();
				$('NorthEastYInput').value = bounds.getNorthEast().lng();
				$('CenterXInput').value = map.getCenter().lat();
				$('CenterYInput').value = map.getCenter().lng();
				search(url_search, highway);
    		}
      	});
	}
} 

function change_coords_subway(url, url_search, subway) {
	mouse_wheel = 1; // чтобы не вызывался поиск второй раз, когда меняется коэффициент масштаба
	if(subway != 0) {
    	new Ajax.Request(url + '?subway='+ subway, {
    		method: 'get',
    		onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				gmap_x = json.gmap_x;
				gmap_y = json.gmap_y;
				new_center = new GLatLng(gmap_x, gmap_y);
				map.setZoom(14);
		        map.setCenter(new_center);
		        bounds = map.getBounds();
				$('SouthWestXInput').value = bounds.getSouthWest().lat();
				$('SouthWestYInput').value = bounds.getSouthWest().lng();
				$('NorthEastXInput').value = bounds.getNorthEast().lat();
				$('NorthEastYInput').value = bounds.getNorthEast().lng();
				$('CenterXInput').value = map.getCenter().lat();
				$('CenterYInput').value = map.getCenter().lng();
				search(url_search, subway);
    		}
      	});
	}
} 

function search(url_search, town, pawn) {
	
	if (town != 0 && town != 10000 && town != 'undefined' && town != 9999) {

		new Ajax.Request(url_search + '?' + $('search_flat').serialize(), {
			method: 'get',
			onSuccess: showResults
		});
		if (pawn == undefined) {
			Element.show('processing');
		}
	}
}

function showResults(transport) {
	if ($('processing')) {
		Element.hide('processing');
	}
	json = transport.responseText.evalJSON();
	eval(json.data);
	$('objects_list').innerHTML = json.search_array;
	if ($('obj_num')) {
		$('obj_num').show();
	}
	//setFormValues(json.form_data);
}
function searchUrl(url_search, url_data) {
	
	
		new Ajax.Request(url_search  + url_data, {
			method: 'get',
			onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				$('objects_list').innerHTML = json.search_array;
				eval(json.data);
				
			}
		});
}

function ajax_login_validate_form(f, url) {
	pars = 'ajax=true&';
	pars += f;

	new Ajax.Request(url, {method: 'get', parameters: pars,
		onSuccess: function(transport) {
			eval(transport.responseText);
			if (ajax_valid == 'no_valid') {
				$('login_error_msg').innerHTML='Логин или пароль неправильны'; 
          		$('login_error_msg').show();
          		Windows.focusedWindow.updateHeight();
            	new Effect.Shake(Windows.focusedWindow.getId());
			} else {
				$('login_error_msg').hide();
			}
		},
		onFailure: function(req) {
			alert("Data transmit error:\n" + req.statusText);
		}
	});

	return false;
}

function change_link(display, first, last) {
	if (first == undefined && last == undefined) {
		if (display == 'none') {
	    	$('option_link').innerHTML = "Меньше опций";
		} else {
			$('option_link').innerHTML = "Больше опций";
		}
	} else {
		if (display == 'none') {
	    	$('option_link').innerHTML = last;
		} else {
			$('option_link').innerHTML = first;
		}
	}
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	window.onload = function(){
		if (typeof oldonload == 'function') oldonload();
		func();
	}
}

function ajax_validate_message(f, url) {
	pars = 'ajax=true&';
	pars += Form.serialize(f);
	new Ajax.Request(url, {method: 'get', parameters: pars,
		onSuccess: function(transport) {
			eval(transport.responseText);
			if (valid == true) {
				new Ajax.Updater('message_wrapper', url, {
					parameters: Form.serialize(f),
					method: 'get'
				});
			}
		},
		onFailure: function(req) {
			alert("Data transmit error:\n" + req.statusText);
		}
	});
	
}

function setFormValues(form) {
	
	for (var i=1; i<=5; i++) {
		if (eval('form.flat_number_' + i) == 'on') {
			$('FlatNumber'+ i +'Input').checked = true;
		}
	}
	if (form.area_first || form.area_second || form.cost_first || form.cost_second || form.new_building == 'on' || form.second_building == 'on') {
		Effect.toggle('search_more', 'slide');
		change_link($('search_more').style.display);
	}
	if (form.area_first) {
		$('AreaFirstInput').value = form.area_first;
	}
	if (form.area_second) {
		$('AreaSecondInput').value = form.area_second;
	}
	if (form.cost_first) {
		$('CostFirstInput').value = form.cost_first;
	}
	if (form.cost_second) {
		$('CostSecondInput').value = form.cost_second;
	}
	if (form.quick_town) {
		$('QuickTownInput').value = Number(form.quick_town);
	}
	
	if (form.new_building) {
		$('NewBuildingInput').checked = true;
	}
	
	if (form.second_building) {
		$('SecondBuildingInput').checked = true;
	}
	

	if (form.object_number == 20) {
		$('object_number_20').cheked = true;
	}
	if (form.object_number == 50) {
		$('object_number_50').checked = true;
	}
	if (form.object_number == 100) {
		$('object_number_100').checked = true;
	}
}

function scroll_page_alt(url, page) {
	new Ajax.Request(url + '?page='+ page, {
		method: 'get',
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			$('objects_list').innerHTML = json.search_array;
			eval(json.data);
		}
  	});
}

function changePageNumber(number, url, partid) {
	$('PageHiddenNumberInput').value = number;
	search(url);
	for (var i = 1; i <= number; i++) {
		if (i == number) {
			$(partid + i).className = 'obj_sel';
		} else {
			$(partid + i).className = '';
		}
	}
}


function get_photo(i, el) {
	if (images_ar[i]) {
		$("resultat").innerHTML = '';
		$("resultat").innerHTML = "<img src=/img/obj_images/" + images_ar[i].id + "_" + images_ar[i].rnd + "_std." + images_ar[i].ext + " width=" + images_ar[i].width + " height=" + images_ar[i].height + " border=0>";
		act_class(el);
	}
}

function act_class(element) {
	if (window.current_selected) {
		window.current_selected.className = "";
	}
	window.current_selected = element;
	element.className="obj_img_border_act";
}

function ajax_prevue_validate_form(f, url, url_prevue, id_prevue, action_prevue) {
	pars = 'ajax=true&prevue=true&';
	pars += f;

	new Ajax.Request(url, {method: 'post', parameters: pars,
		onSuccess: function(transport) {
		eval(transport.responseText);
			if (valid == true) {
				Application.evalCode('open_prevue', true);
				prevue(url_prevue, id_prevue, action_prevue);
			}
		},
		onFailure: function(req) {
			alert("Data transmit error:\n" + req.statusText);
		}
	});
return false;
}

function prevue(url, id, action) {
	new Ajax.Request(url + '?prevue_id='+ id + '&obj_action_prevue=' + action + '&' + '\'' + $('obj_form').serialize() + '\'', {
		method: 'post',
		onComplete: showPrevue
  	});
}
function showPrevue(transport) {
	json = transport.responseText.evalJSON();
	if (json.prevue_data) {
		$('prevue_wrapper').innerHTML = json.prevue_data;
		eval(json.function_images);
		eval(json.function_currency);
	}
}

function act_currency(el, currency) {
	if (window.current_selected_curency) {
		window.current_selected_curency.className = "";
	}
	window.current_selected_curency = el;
	el.className = 'currency_act';
	if (currency == 1) {
		$('rubles_price').show();
		$('euro_price').hide();
		$('usd_price').hide();
	} else if (currency == 2) {
		$('rubles_price').hide();
		$('euro_price').hide();
		$('usd_price').show();
	} else if (currency == 3) {
		$('rubles_price').hide();
		$('euro_price').show();
		$('usd_price').hide();
	}
}
