//////////////////////////////////////////////////////////////
function changeCountry(obj, doNotRemove)
{
	var countryID = getSelected(obj);
	//var countryID = obj.selectedIndex;
	var citiesSelect = document.getElementById('cities_list');

	deleteAllCities(citiesSelect);

	if (countryID==-1) {
		setAllCities(citiesSelect);
	} else {
		setCitiesByCountry(citiesSelect, countryID);
	}

	if(doNotRemove) {
		for (var i=0; i < citiesSelect.options['length']; i++) {
			if (citiesSelect.options[i].value == curCityID) {
				//citiesSelect.options[i].setAttribute('selected', true);
				citiesSelect.selectedIndex = i;
			}
		}
	}
}

//////////////////////////////////////////////////////////////
function changeCity(obj)
{
	var cityID = getSelected(obj);
	//var cityID = obj.selectedIndex;
	
	curCityID = cityID; 
	
	if (cityID!=-1) {
		for (var country in countries) {
			for (var city in countries[country].cities) {
				if(countries[country].cities[city].id == cityID) {
					var countryID = country;
					break;
				}
			}
			if (countryID) {
				break;
			}
		}
		
		var countriesSelect = document.getElementById('countries_list');
		for (var i=0; i < countriesSelect.options['length']; i++) {
	
			if (countriesSelect.options[i].value == countryID) {
				//countriesSelect.options[i].setAttribute('selected',true);
				countriesSelect.selectedIndex = i;
				break;
			}
		}
	}
	changeCountry(countriesSelect, true);
}
//////////////////////////////////////////////////////////////
function getSelected(obj)
{
	for (var i=0; i < obj.options['length']; i++) {
		if (obj.options[i].selected) {
			return obj.options[i].value;
		}
	}
}

//////////////////////////////////////////////////////////////
function setAllCities(citiesSelect)
{
	var cities = Array();
	for (var country in countries) {
		for (var city in countries[country].cities) {
			cities[countries[country].cities[city].sort] = createCity(countries[country].cities[city]);
		}
	}

	for (var i=1; i<cities.length; i++) {
		citiesSelect.appendChild(cities[i]);
	}
}

//////////////////////////////////////////////////////////////
function setCitiesByCountry(citiesSelect, countryID)
{
	for (var city in countries[countryID].cities) {
		citiesSelect.appendChild(createCity(countries[countryID].cities[city]));
	}

}

//////////////////////////////////////////////////////////////
function deleteAllCities(citiesSelect)
{
	var citiesNum = citiesSelect.options['length'];
	for (var i=1; i < citiesNum; i++) {
		citiesSelect.removeChild(citiesSelect.options[1]);
	}
}

//////////////////////////////////////////////////////////////
function createCity(city)
{
	var optionElement = document.createElement('option');
	optionElement.setAttribute('value',city.id);
	optionElement.innerHTML = city.name;
	return optionElement;
}


//////////////////////////////////////////////////////////////
function showPhoto(ind)
{
	var photoLayer = document.getElementById('photoLayer');
	var photoBackground = document.getElementById('photoBackground');
	var photoImg = document.getElementById('photoImg');
	var photoCount = document.getElementById('photoCount');

	if (!photoInBody) {
		document.getElementsByTagName( 'body' )[0].appendChild(photoLayer);
		document.getElementsByTagName( 'body' )[0].appendChild(photoBackground);	
		photoInBody = true;
	}
	
//	photoBackground.style.top = document.documentElement.scrollTop + 'px'; 
//	photoLayer.style.top = document.documentElement.scrollTop + 'px'; 
	

//	 alert(document.documentElement.scrollTop);
	photoBackground.style.height = document.documentElement.scrollHeight + 'px';
//	alert(photoLayer.style.height);
	
	
	photoCount.innerHTML = 'Фото ' + (ind+1)+' из ' + photos.length;
	photoImg.innerHTML = '<img src="'+photos[ind].src+'" alt="" class="pic" height="'+photos[ind].height+'" width="'+photos[ind].width+'">';

	curPhoto = ind;

	photoBackground.className = 'photovisible';
	photoLayer.className = 'photovisible';
}

//////////////////////////////////////////////////////////////
function closePhoto()
{
	var photoLayer = document.getElementById('photoLayer');
//	photoLayer.setAttribute('class', 'photoinvisible');
	photoLayer.className = 'photoinvisible';
	var photoBackground = document.getElementById('photoBackground');
//	photoBackground.setAttribute('class', 'photoinvisible');
	photoBackground.className = 'photoinvisible';
}


//////////////////////////////////////////////////////////////
function photoNext()
{
	if((curPhoto+1)<photos.length) {
		showPhoto(curPhoto+1);
	}
}

//////////////////////////////////////////////////////////////
function photoPrev()
{
	if(curPhoto>0) {
		showPhoto(curPhoto-1);
	}
}
//////////////////////////////////////////////////////////////
function photoFirst()
{
	showPhoto(0);
}
//////////////////////////////////////////////////////////////
function photoLast()
{
	showPhoto(photos.length-1);
}
//////////////////////////////////////////////////////////////
function dosearch()
{
/*	citiesSelect = document.getElementById('cities_list');
	if(citiesSelect.selectedIndex==0) {
		alert('Выберите город!');
		return false;
	}
*/	return true;
}
var photoInBody = false;
//////////////////////////////////////////////////////////////
function toogle(id)
{
	var el = document.getElementById(id);
	if(el.style.display == "none") {
		el.style.display = "block";
	}
	else {
		el.style.display = "none";
	} 
	
//	alert(el.style.visibility);
}

