var map;
var marker;
var arrDistrict;
var arrCity = new Array(['0','Krak\u00f3w'],['1','Katowice']);
var globalCitySelectedName;
var zoom;

function getMarkers() {
	objGoogleMap = new GoogleMap;
 	objGoogleMap.getMapMarkers();	
}

function showHideAFSOptions(selectApartmentType) {
	var apartmentTypeSelectedIndex = selectApartmentType.options[selectApartmentType.selectedIndex].value;
	if(apartmentTypeSelectedIndex == 1) {
		document.getElementById("idAFSOptions").style.display = "";
	} else {
		document.getElementById("idAFSOptions").style.display = "none";
		
		var i;
	    var select = document.getElementById('LiveCondType');
	    for(i=1;i<select.options.length;i++) {
	        select.options[i].selected=false;
	    }
	    
	    var i;
	    var select = document.getElementById('ApartType');
	    for(i=1;i<select.options.length;i++) {
	        select.options[i].selected=false;
	    }
	}
}

function selectCity(citySelectedIndex, districtSelectedIndex, bMap) {
	
	if (bMap == 1) {
		var citySelectedName = arrCity[citySelectedIndex][1];
		globalCitySelectedName = citySelectedName; 
		showAddress(citySelectedName +", Poland");
		zoom = 13;
		
		$('District').onchange=function() {
			changePage(this);
		}
	}
	
	if (bMap == 0) {
		$('District').onchange=function() {
			changePageNoMap(this);
		}
	}
	
	if(citySelectedIndex == 0) {
		response = "arrDistrict = new Array(['0','-- wybierz rejon Krakowa --'],['19','Stare Miasto'],['18','\u015ar\u00f3dmie\u015bcie'],['24','Kazimierz'],['9','Grzeg\u00f3rzki'],['10','Kleparz'],['1','Bie\u0144czyce'],['2','Bie\u017can\u00f3w'],['3','Bronowice'],['4','Czy\u017cyny'],['5','Azory'],['6','Salwator'],['7','Krowodrza'],['8','D\u0119bniki'],['11','\u0141agiewniki'],['12','\u0141obz\u00f3w'],['13','Mistrzejowice'],['14','Nowa Huta'],['15','Podg\u00f3rze'],['16','Pr\u0105dnik Czerwony'],['17','Pr\u0105dnik Bia\u0142y'],['20','Ruczaj'],['21','Wola Duchacka'],['22','Wola Justowska'],['23','Zwierzyniec']);";		
	}
	if(citySelectedIndex == 1) {
		response = "arrDistrict = new Array(['0','-- wybierz rejon Katowic --'],['1','Bogucice'], ['2','Bryn\u00f3w'], ['3','Centrum'], ['4','D\u0105b'], ['5','D\u0105br\u00f3wka Ma\u0142a'], ['6','Giszowiec'], ['7','Jan\u00f3w'], ['8','Jan\u00f3w-Nikiszowiec'], ['9','Kostuchna'], ['10','Koszutka'], ['11','Ligota - Panewniki'], ['12','Murcki'], ['13','O\u015b. Paderewskiego-Muchowiec'], ['14','O\u015b. Ptasie'], ['15','O\u015b. Tysi\u0105clecia'], ['16','O\u015b. Witosa'], ['17','Piotrowice-Ochojec'], ['18','Podlesie'], ['19','\u015ar\u00f3dmie\u015bcie'], ['20','Szopienice-Burowiec'], ['21','We\u0142nowiec-J\u00f3zefowiec'], ['22','Za\u0142\u0119\u017ce'], ['23','Zarzecze'], ['24','Zawodzie']);";
	}
	eval(response);
	
	for(i=0;i<arrDistrict.length;i++) {	
		$('District').options[i] = new Option(arrDistrict[i][1],arrDistrict[i][0]);
		
		if (districtSelectedIndex == arrDistrict[i][0]) {
			$('District').options[i].selected = true;				
		}
	};
}

function changePage(selectDistrict) {
	var citySelectedName = globalCitySelectedName;
	var districtSelectedIndex = selectDistrict.options[selectDistrict.selectedIndex].value;
	
	for(i=0;i<arrDistrict.length;i++) {	
		$('District').options[i] = new Option(arrDistrict[i][1],arrDistrict[i][0]);
		if (arrDistrict[i][0] == districtSelectedIndex) {
			districtSelectedName = arrDistrict[i][1];				
		}
		if (districtSelectedIndex == arrDistrict[i][0]) {
			$('District').options[i].selected = true;				
		}
	};
	
	if(districtSelectedIndex != 0) {
		var searchAddress = districtSelectedName + ", " + citySelectedName + ", Poland";		
		showAddress(searchAddress)
		zoom = 13;
	} else {
		var searchAddress = citySelectedName + ", Poland";
		showAddress(searchAddress)
		zoom = 11;
	}
}

function changePageNoMap(selectDistrict) {
	var citySelectedName = globalCitySelectedName;
	var districtSelectedIndex = selectDistrict.options[selectDistrict.selectedIndex].value;
	
	for(i=0;i<arrDistrict.length;i++) {	
		$('District').options[i] = new Option(arrDistrict[i][1],arrDistrict[i][0]);
		if (arrDistrict[i][0] == districtSelectedIndex) {
			districtSelectedName = arrDistrict[i][1];				
		}
		if (districtSelectedIndex == arrDistrict[i][0]) {
			$('District').options[i].selected = true;				
		}
	};
	
}

function showAddress(address) {
	geocoder = new GClientGeocoder();
    geocoder.getLocations(address, function (result) {
           if (result.Status.code == G_GEO_SUCCESS) {
           	 p = result.Placemark[0].Point.coordinates;
             var centerLatitude=p[1];
             var centerLongitude=p[0];
             callBackShowAddress(centerLatitude, centerLongitude);
            }
    	}
    );        
}

function callBackShowAddress(centerLatitude,centerLongitude) {
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), zoom);
	getMapMarkers();
}

function createMarker(icon, point, html) {
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}
