var intervalID;
var global_all_doctors_link;
var global_locale;
var global_map_ID = -1;
var global_sex;
var global_sex_id;
var symtom2 = new Array;
var IE = document.all?true:false;
var tempX = 0;
var tempY = 0;

function select_sex(sex, sex_id) {
	var sex_old = sex;
	document.getElementById('human_man_siluet').style.display = 'none';
	document.getElementById('human_woman_siluet').style.display = 'none';
	document.getElementById('human_boy_siluet').style.display = 'none';
	document.getElementById('human_girl_siluet').style.display = 'none';

	document.getElementById('link_woman').className = "link_woman";
	document.getElementById('link_man').className = "link_man";
	document.getElementById('link_boy').className = "link_boy";
	document.getElementById('link_girl').className = "link_girl";
	if (sex_id == 5) {
		sex = 'girl'
	}else if (sex_id == 4) {
		sex = 'boy'
	}
	document.getElementById('link_'+sex).className = "link_"+sex+"_active";
	sex = sex_old;

	document.getElementById('human_'+sex+'_siluet').style.display = 'block';

	//Список симптомов обновить
	global_sex = sex;
	global_sex_id = sex_id;
	global_map_id = 0;
	body_part_selected(0, 0);
	//symptoms_list_renew(sex_id, 0);
}

function body_part_selected(part, map_id) {
	document.getElementById('man_head').className = "man_head";
	document.getElementById('man_nack').className = "man_nack";
	document.getElementById('man_plechi').className = "man_plechi";
	document.getElementById('man_breast').className = "man_breast";
	document.getElementById('man_stomach').className = "man_stomach";
	document.getElementById('man_pah').className = "man_pah";
	document.getElementById('man_legs').className = "man_legs";

	document.getElementById('woman_head').className = "woman_head";
	document.getElementById('woman_nack').className = "woman_nack";
	document.getElementById('woman_plechi').className = "woman_plechi";
	document.getElementById('woman_siski').className = "woman_siski";
	document.getElementById('woman_breast').className = "woman_breast";
	document.getElementById('woman_stomach').className = "woman_stomach";
	document.getElementById('woman_pah').className = "woman_pah";
	document.getElementById('woman_legs').className = "woman_legs";

	if (part) document.getElementById(part).className = part+"_active";
	
	//Список симптомов обновить
	//seeks_list_renew(map_id);
	document.getElementById('simptom_2').value = 'все симптомы и болезни';
	document.forms.search_form.simptom.value = -1;
	global_map_ID = map_id;
	symptoms_list_renew(global_sex_id, map_id);
}

function getBodyScrollTop() {
  return self.pageYOffset || 
    (document.documentElement && document.documentElement.scrollTop) || 
    (document.body && document.body.scrollTop);
}

function getClientHeight(){
  return (document.body.scrollHeight > document.body.offsetHeight)?
    document.body.scrollHeight:
    document.body.offsetHeight;
}

function getClientCenterY(){
  return parseInt(getClientHeight()/2)+getBodyScrollTop();
}

function placeFluidWindow() {
	var i = $(window).height()/2+getBodyScrollTop();
	if (i > 900) i = 900;
	if (i < 470) i = 490;
	i = parseInt(i);
	clearInterval(intervalID);
	intervalID = setInterval('moveTheBlock('+i+')', 7);
}

function moveTheBlock(i) {
	var x = document.getElementById('fluid_block').style.top;
	if (x == '') x = 300;
	x = parseInt(x);
	if (x < i) x = x+1;
	else x = x-1;
	document.getElementById('fluid_block').style.top = x+'px';
	if (x == i) clearInterval(intervalID);
	
}


function showIn( listname) {
	var list = eval( 'document.forms.search_form.' + listname );
	
	// empty the list	
	toselect = document.forms.search_form.city.value;
	while (list.options.length) {
		list.options[0] = null;
	}

	
	i = 0;
	for (x in city) {
		if (city[x][0] == document.forms.search_form.oblast.value) {
			opt = new Option();
			opt.value = city[x][3];
			opt.text = city[x][2];
			list.options[i] = opt;
			if ( opt.value == toselect ) {
				opt.selected = true;
			}
			i++;
		}
	}
	if (i == 0)	{
		opt = new Option();
		opt.value = "null";
		opt.text = "";
		opt.selected = true;
		list.options[i] = opt;
	}

	for (x in oblast) {
		if (oblast[x][0] == document.forms.search_form.oblast.value) document.getElementById('obl_map').innerHTML = '<img src = "/layout_img/maps/'+oblast[x][1]+'">';
	}
	change_city();
}


function symptoms_list_renew(sex_id, map_id) {
	/*
	var list = eval( 'document.forms.search_form.simptom');
	empty the list	
	while (list.options.length) {
		list.options[0] = null;
	}*/
	var used_values = new Array;
	symtom2 = [];
	i = 0;
	//alert (sex_id);
	for (x in symtom) {
		if (symtom[x][2] == sex_id || sex_id == 0 || symtom[x][2] == -1) {
			if (map_id == 0 || symtom[x][3] == map_id || symtom[x][3] == -1)	{
				if (!used_values.in_array(symtom[x][0]))	{
					//opt = new Option();
					//opt.value = symtom[x][0];
					//opt.text = symtom[x][1];
					//list.options[i] = opt;
					//if (i ==0 ) document.forms.search_form.simptom.value = symtom[x][0];
					//i++;
					symtom2.push(symtom[x]);
					used_values.push(symtom[x][0]);
				}
			}
		}
	}
	document.forms.search_form.simptom.value = -1;
	change_specialnost(document.forms.search_form.simptom.value);
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++)	{
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

function change_specialnost(id) {
	var list = eval( 'document.forms.search_form.specialnost');
	// empty the list	
	while (list.options.length) {
		list.options[0] = null;
	}
	var used_values = new Array;
	i = 0;
	for (x in specialnost) {
		if (specialnost[x][2] == id || id == -1 || specialnost[x][2] == -1) {
			if (!used_values.in_array(specialnost[x][0]) && specialnost[x][0] &&  symtom2.length > 1)	{
				opt = new Option();
				opt.value = specialnost[x][0];
				opt.text = specialnost[x][1];
				list.options[i] = opt;
				i++;
				used_values.push(specialnost[x][0]);
			}
		}
	}
	count_doctors();
}

function change_city() {
	if (document.forms.search_form.city.value != 'all') global_locale = 'http://www.'+document.forms.search_form.city.value+'medukr.com/';
	else global_locale = 'http://www.medukr.com/';
	//global_locale = 'http://www.'+document.forms.search_form.city.value+'medukr.com/';
	global_all_doctors_link = global_locale + 'base_spec/';
	var domen_hospital;
	for (x in city) {
		if (city[x][3] == document.forms.search_form.city.value) {
			domen_hospital = city[x][4];
			document.getElementById('oblast name').innerHTML = city[x][2];
		}
	}
	if (domen_hospital == ''){
		domen_hospital = city[1][4];
	}
	document.getElementById('medhouse_link').innerHTML = '<a href="'+domen_hospital+'">Медучреждения</a>';
	document.getElementById('medhouse_link_2').innerHTML = '<a href="'+domen_hospital+'">Медучреждения</a>';
	count_doctors();
	reload_banners(global_locale);
}

function go_to_doctors() {
	//alert (global_locale);
	if (document.forms.search_form.specialnost.value > 0) {
		var action = global_locale + 'doctors/speciality/'+document.forms.search_form.specialnost.value+'/start/1/';
	}else {
		var action = global_all_doctors_link;
	}

	for (x in city) {
		if (city[x][3] == document.forms.search_form.city.value) {
			var cityID = city[x][1];
		}
	}


	action = global_locale + 'searchdoctors/city/'+cityID+'/seek/'+document.forms.search_form.simptom.value+'/bodypart/'+global_map_ID+'/speciality/'+document.forms.search_form.specialnost.value+'/sex/'+global_sex_id+'/sort/rank/1';

	document.forms.search_form.action = action;
	document.search_form.submit();
}



function count_doctors_all() {
	$.ajax({
		url: '/counter_doctors.php?city=-1&seek=-1&bodypart=-1&speciality=-1&sex=-1',
		success:function(answ){show_all_count(answ)} 
	});
}

function reload_banners(locale) {
	for (x in city) {
		if (city[x][3] == document.forms.search_form.city.value) {
			var cityID = city[x][1];
		}
	}
	
	$.ajax({
		url: '/get_site_banners.php?locale='+locale+'&city_id='+cityID,
		success:function(answ){load_banners(answ)} 
	});
}


function count_doctors() {
	for (x in city) {
		if (city[x][3] == document.forms.search_form.city.value) {
			var cityID = city[x][1];
		}
	}
	var cpecialnost = document.forms.search_form.specialnost.value;
	if (!cpecialnost) cpecialnost = 0;
	$.ajax({
		url: '/counter_doctors.php?city='+cityID+'&seek='+document.forms.search_form.simptom.value+'&bodypart='+global_map_ID+'&speciality='+cpecialnost+'&sex='+global_sex_id,
		success:function(answ){show_count(answ)} 
	});
	//loadXMLDoc('/counter_doctors.php?city='+cityID+'&seek='+document.forms.search_form.simptom.value+'&bodypart='+global_map_ID+'&speciality='+cpecialnost+'&sex='+global_sex_id, 'show_count');
}

function loadXMLDoc(url, func) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function (){processReqChange(func)};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function (){processReqChange(func)};
            req.open("GET", url, true);
            req.send();
        }
    }
}

function loadXMLDoc2(url, func) {
    if (window.XMLHttpRequest) {
        req2 = new XMLHttpRequest();
        req2.onreadystatechange = function (){processReqChange2(func)};
        req2.open("GET", url, true);
        req2.send(null);
    } else if (window.ActiveXObject) {
        req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req2) {
            req2.onreadystatechange = function (){processReqChange2(func)};
            req2.open("GET", url, true);
            req2.send();
        }
    }
}

function processReqChange(funcname) {
	ab = window.setTimeout("req.abort();", 5000);
	if (req.readyState == 4) {
        clearTimeout(ab);        
        // only if "OK"
        if (req.status == 200) {
           eval (funcname+'("'+req.responseText+'")');
        }
    }
}

function processReqChange2(funcname) {
	ab2 = window.setTimeout("req2.abort();", 5000);
	if (req2.readyState == 4) {
        clearTimeout(ab2);        
        // only if "OK"
        if (req2.status == 200) {
           eval (funcname+'("'+req2.responseText+'")');
        }
    }
}

function show_count(count) {
	document.getElementById('count_doctors3').innerHTML = '<a href = "doctors/" onclick = "go_to_doctors(); return false;">'+count+'</a>';
	document.getElementById('count_doctors2').innerHTML = count;
	document.getElementById('count_doctors').innerHTML = count;
}

function load_banners (string) {
	var main_arr = string.split('<--/--!--/-->');	
	var arr = main_arr[0].split('<--//-->');
	var row = 4;

	$(function() {
		var rollingDiv = $("#banners");
		rollingDiv.rolling("up", 502, 50, 2); // direction, item width, item height, visible item count
		
		
		for (var i = 0; i < arr.length; i++) {
			if (i == 0) document.getElementById('freezed_banners').innerHTML = arr[i];
			else rollingDiv.addRollingItem(arr[i]);
		}
		if (arr.length > 3){
			rollingDiv.startRolling(50, 1000, 10); // rolling time, viewing time, animation frame count
		}
		rollingDiv.bind("mouseover", function() {
		rollingDiv.stopRolling();
		});
		rollingDiv.bind("mouseout", function() {
		rollingDiv.resumeRolling();
		});
	});

	document.getElementById('top_banners').innerHTML = main_arr[1]; //var top_banners = main_arr[1];
	document.getElementById('left_banners').innerHTML = main_arr[2]; //var left_banners = main_arr[2];
	document.getElementById('right_banners').innerHTML = main_arr[3]; //var right_banners = main_arr[3];
}


var tiptext = 'empty';
function sendtooltip() {
	if (document.getElementById('simptom_2').value != tiptext) {
		if (document.getElementById('simptom_2').value == 'все симптомы и болезни') document.getElementById('simptom_2').value = '';
		document.getElementById('tipsfield').style.display = 'none';
		tiptext = document.getElementById('simptom_2').value;
		var tipvalues = '<iframe><html><head></head><body></body></html></iframe>';
		
		var used_values = new Array;
		for (var x = 0; x < symtom2.length; x++) {
			if (symtom2[x][1].toLowerCase().indexOf(tiptext.toLowerCase()) == 0 || tiptext == '' || tiptext == 'все симптомы и болезни' || symtom2[x][0] == -1) {
				if (!used_values.in_array(symtom2[x][0])) {
					tipvalues = tipvalues + '<a href = \'\' class = \'tiplink\' onClick = \'changevalue(\"'+symtom2[x][1]+'\", '+symtom2[x][0]+'); return false;\'>'+symtom2[x][1]+'</a>';
					used_values.push(symtom2[x][0]);
				}
			}
		}
		document.forms.search_form.simptom.value =document.forms.search_form.simptom.value + '1-';
		showtooltip(tipvalues);
	}
}
function showtooltip(value) {
	if (value !='')	{
		document.getElementById('tipsfield').style.display = 'block';
		document.getElementById('tipsfield').innerHTML = value;
	}
}
function hidetooltip(event) {
	
	var divX = obtenerPosicionX(document.getElementById('tipsfield'));
	var divY = obtenerPosicionY(document.getElementById('tipsfield'));
	var divW = $('#tipsfield').width();
	var divH = $('#tipsfield').height();

	//document.getElementById('otladka').innerHTML = 'X: '+tempX+' Y: '+tempY + ' X2: '+ divX + ' Y2: ' + divY + ' W: '+ divW + ' H: ' + divH;
	if ( (tempX < divX || tempX > divX+divW) || (tempY < divY || tempY > divY+divH) ) { 
		document.getElementById('tipsfield').style.display = 'none';
		tiptext = 'empty';
		if (document.getElementById('simptom_2').value == '') {
			document.getElementById('simptom_2').value = 'все симптомы и болезни';
			document.forms.search_form.simptom.value = -1;
			change_specialnost(-1);
			//body_part_selected(0, 0);
		}
	}else { 
		document.getElementById('simptom_2').focus();
	}
}
function changevalue(text, seekid) {
	document.getElementById('simptom_2').value = text;
	document.forms.search_form.simptom.value = seekid; 
	change_specialnost(seekid);
	document.getElementById('tipsfield').style.display = 'none';
}

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop+getBodyScrollTop();
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  return true
}

function obtenerPosicionX(elemento) {
	var x=0;
	while(elemento) {
		x += elemento.offsetLeft;
		elemento=elemento.offsetParent;
	}
	return x;
}

function obtenerPosicionY(elemento) {
	var y=0;
	while(elemento) {
		y += elemento.offsetTop;
		elemento=elemento.offsetParent;
	}
	return y;
}
