/**
 * Functions for displaying the map element
 * Author: ahermann
 */

/*extern cityData, countryData */

var currentCountry = null;
var currentCity = null;

/** text field that is used to display caption */
var captionField = "caption"

/**
 * search a city in the json array
 * @param {String} cityName Name of a city
 */ 
function getCityByName(cityName)
{
	for (var i = 0; i < cityData.length; i++)
	{
		var c = cityData[i];
		if (cityData[i].name_english.toLowerCase() == cityName.toLowerCase())
		{
			return cityData[i];
		}
	}	
	return null;
}

/**
 * search a country in the json array
 * @param {String} countryName
 */
function getCountryByName(countryName)
{
	for (var i = 0; i < countryData.length; i++)
	{
		var c = countryData[i];
		if (countryData[i].name_english.toLowerCase() == countryName.toLowerCase())
		{
			return countryData[i];
		}
	}	
	return null;
}

/**
 * hide the country image and city markers
 * @param {String} countryName
 */
function unsetCountry(countryName) {
	var cityMarkers = document.getElementById(countryName + '-city-markers');
	var mapImage = document.getElementById('map_image_'+countryName);
    
	if (cityMarkers)
	{
		cityMarkers.style.display = 'none';
	}
    
    if (mapImage) {
        mapImage.style.display = 'none';
    }
}
		
/**
 * swap the map image and caption text
 * @param {String} countryName
 */
function setCountry(countryName) {
	var mapTitle = document.getElementById('map_title');
	var mapCaption = document.getElementById('map_caption');
	var mapImage = document.getElementById('map_image_'+countryName);
	
	var cityMarkers = document.getElementById(countryName + '-city-markers');
	
	// hide previously displayed city markers, show new ones
	if (currentCountry && currentCountry != countryName)
	{
		unsetCountry(currentCountry.name_english);
	}	
	currentCountry = getCountryByName(countryName);
	
	if (currentCountry) {
        if (mapTitle)
        {
		    mapTitle.innerHTML = "Sprachreisen in " + currentCountry.name;
        }
        if (mapCaption)
        {
            mapCaption.style.display = 'none';
        }
	}
	else {
        if (mapCaption)
        {
		    mapCaption.innerHTML = 'N/A';
        }
	}
    
    if (mapImage) {
        mapImage.style.display = '';
    }
	
	if (cityMarkers)
	{	
		cityMarkers.style.display = '';
		var globalCountryMarkers = document.getElementById ( 
"country-markers-simple" );
		globalCountryMarkers.style.display = 'none';
	}
}

/**
 * show the city caption text, set the map name to country - city
 * @param {String} cityName
 */
function setCity(cityName) {

	currentCity = getCityByName(cityName);
	
	if (currentCity) {
		setCountry(currentCity.countryName);
	
		var mapCaption = document.getElementById('map_caption');
        if (mapCaption)
        {
		    mapCaption.innerHTML = '<b>'+currentCity.name+'</b>: ' + currentCity[captionField];
		    mapCaption.style.display = '';
        }
		
		var cityMarker = document.getElementById('city-marker-'+cityName);
		if (cityMarker)
		{
			cityMarker.style.display = '';
		}
	}
}

/**
 * hide the city caption text, set the map name to country only
 * @param {String} cityName
 */
function unsetCity(cityName) {
	// window.setTimeout("unsetCity_func", 100, cityName);
	
    var mapCaption = document.getElementById('map_caption');
    var mapTitle = document.getElementById('map_title');
	var cityMarker = document.getElementById('city-marker-'+cityName);
    
	if (mapCaption) {
        mapCaption.style.display = 'none';
    }
	
	if (cityMarker)
	{
		cityMarker.style.display = 'none';
	}
}

function setCountrySimple ( countryName )
{
    var mapCaption = document.getElementById('map_caption');

    mapCaption.innerHTML = '<b>' + "Sprachaufenthalt in " + countryName + '</b>';
    mapCaption.style.display = '';

    var countryMarker = document.getElementById('country-marker-'+countryName);
    if (countryMarker)
    {
        countryMarker.style.display = '';
    }
}

function unsetCountrySimple ( countryName )
{
    var mapCaption = document.getElementById('map_caption');
    var mapTitle = document.getElementById('map_title');
    var countryMarker = document.getElementById('country-marker-'+countryName);
 
    if (mapCaption) {
        mapCaption.style.display = 'none';
    }

    if (countryMarker)
    {
        countryMarker.style.display = 'none';
    }
}

function destinations_map_init() {
    currentCountry = countryData[0];
}

dojo.addOnLoad(destinations_map_init);