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 selectTransType(transTypeSelectedIndex, priceMaxSelectedIndex) {
	
	if (transTypeSelectedIndex == 0) {
		response = "arrPriceMax = new Array(['0','-- select price below --']);";
		eval(response);
	}
	
	// mieszkanie dla studenta
	if (transTypeSelectedIndex == 1) {
		response = "arrPriceMax = new Array(['0','-- select price below --'],['1000','price below 1 000 PLN'],['1500','price below 1 500 PLN'],['2000','price below 2 000 PLN'],['2500','price below 2 500 PLN'],['1000000','price below 3 000 PLN and more']);";
		eval(response);
	}
	
	// mieszkanie do wynajecia
	if (transTypeSelectedIndex == 2) {
		response = "arrPriceMax = new Array(['0','-- select price below --'],['1000','price below 1 000 PLN'],['1500','price below 1 500 PLN'],['2000','price below 2 000 PLN'],['2500','price below 2 500 PLN'],['1000000','price below 3 000 PLN and more']);";
		eval(response);
	}
	
	// mieszkanie na sprzedaz
	if (transTypeSelectedIndex == 3) {
		response = "arrPriceMax = new Array(['0','-- select price below --'],['200000','price below 200 000 PLN'],['250000','price below 250 000 PLN'],['300000','price below 300 000 PLN'],['350000','price below 350 000 PLN'],['1000000','price below 400 000 PLN and more']);";
		eval(response);
	}
	
	// dom do wynajecia
	if (transTypeSelectedIndex == 4) {
		response = "arrPriceMax = new Array(['0','-- wselect price below --'],['4000','price below 4 000 PLN'],['5000','price below 5 000 PLN'],['6000','price below 6 000 PLN'],['7000','price below 7 000 PLN'],['1000000','price below 8 000 PLN and more']);";
		eval(response);
	}
	
	// dom na sprzedaz
	if (transTypeSelectedIndex == 5) {
		response = "arrPriceMax = new Array(['0','-- select price below --'],['400000','price below 400 000 PLN'],['500000','price below 500 000 PLN'],['600000','price below 600 000 PLN'],['700000','price below 700 000 PLN'],['1000000','price below 800 000 PLN  and more']);";
		eval(response);
	}
	
	// dzialke do kupienia
	if (transTypeSelectedIndex == 6) {
		response = "arrPriceMax = new Array(['0','-- select price below --'],['7000','price below 7 000 PLN / ar'],['14000','price below 14 000 PLN / ar'],['25000','price below 25 000 PLN / ar'],['35000','price below 35 000 PLN / ar'],['1000000','price below 45 000 PLN / ar and more']);";
		eval(response);
	}
	
	// nieruchomosc komercyjna wynajem
	if (transTypeSelectedIndex == 7) {
		response = "arrPriceMax = new Array(['0','-- select price below --'],['2000','price below 2 000 PLN'],['3000','price below 3 000 PLN'],['4000','price below 4 000 PLN'],['5000','price below 5 000 PLN'],['1000000','price below 6 000 PLN and more']);";
		eval(response);
	}
	
	// nieruchomosc komercyjna sprzedaz
	if (transTypeSelectedIndex == 8) {
		response = "arrPriceMax = new Array(['0','-- select Price below --'],['200000','price below 200 000 PLN'],['300000','price below 300 000 PLN'],['400000','price below 400 000 PLN'],['500000','price below 500 000 PLN'],['1000000','price below 600 000 PLN and more']);";
		eval(response);
	}
	
	$('PriceMax').length = 0;
	for(i=0;i<arrPriceMax.length;i++) {	
		$('PriceMax').options[i] = new Option(arrPriceMax[i][1],arrPriceMax[i][0]);
		if (priceMaxSelectedIndex == arrPriceMax[i][0]) {
			$('PriceMax').options[i].selected = true;				
		}
				
	};	
}

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','-- select Krakow district --'],['1','Bie\u0144czyce'],['2','Bie\u017can\u00f3w'],['3','Bronowice'],['4','Czy\u017cyny'],['5','Azory'],['6','Salwator'],['7','Krowodrza'],['8','D\u0119bniki'],['9','Grzeg\u00f3rzki'],['10','Kleparz'],['11','\u0141agiewniki'],['12','\u0141obz\u00f3w'],['13','Mistrzejowice'],['14','Nowa Huta'],['15','Podg\u00f3rze'],['16','Pr\u0105dnik Czerwony'],['17','Pr\u0105dnik Bia\u0142y'],['18','\u015ar\u00f3dmie\u015bcie'],['19','Stare Miasto'],['20','Ruczaj'],['21','Wola Duchacka'],['22','Wola Justowska'],['23','Zwierzyniec'],['24','Kazimierz']);";
	}
	if(citySelectedIndex == 1) {
		response = "arrDistrict = new Array(['0','-- select Katowice district --'],['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],i);
		if (districtSelectedIndex == i) {
			$('District').options[i].selected = true;				
		}
	};
}

function changePage(selectDistrict) {
	var citySelectedName = globalCitySelectedName;
	var districtSelectedIndex = selectDistrict.options[selectDistrict.selectedIndex].value;
	var districtSelectedName = arrDistrict[districtSelectedIndex][1];
	
	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;
	var districtSelectedName = arrDistrict[districtSelectedIndex][1];		
}

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;
}