/*
 * Culturele Kaart Rotterdam - All custom JavaScript that is related to the Google Map.
 * @version: 200712-01
 * @API-Key: ABQIAAAAIUl653TLoCd1KPIiyWIWUBT7TfnnUDL2lqOUSPoifmQikLIvaBST0JlhYCv9z_Wm12MdcCLjZh3nHw
 */

/************************************************************************************************************************************************
 *																																				*
 *														C O U N T Y																				*
 *																																				*
 ************************************************************************************************************************************************/

/**
 * constructor:
 * + n, String name
 * + c, GLatlng, center of county
 * + o, lat-lng[], outline of county
 */
var County = function(n, c, o){
	this.name = n;
	this.id = n.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
	this.center = c;
	this.hoods = {};
	this.outline = o||[];
	this.polygon = null;
	this.loaded = false;
}
/**
 * + addHood: void, add a neighbourhood to this county
 * + getCenter: GLatLng, get the center of county
 * + getHoodMarkerSelection
 * + getHood: Hood, get a single hood by its id
 * + getHoods: Hood[], get all hoods in this county
 * + getId: String
 * + getPolygon: GPolygon, outline of county
 * + showOutline: void, set the outline
 * + 
 * + 
 */
County.prototype = {
	addHood: function(h){this.hoods[h.getId()] = h},
	getCenter: function(){return this.center},
	getHoodMarkerSelection: function(filter){ // get the markers for this county
		if (this.loaded) return $.each(this.hoods, function(){this.showOnMap(filter)});
		Filter.startLoading();
		var c = this;
		$.getJSON('/includes/ckr/getMapData.inc.php?county='+this.getId(), function(j){
			try{
				if (!j.markers){
					return Filter.showEmpty();
				}
				Filter.results.innerHTML = '';
				var i = 0, m = j.markers, l, n;
				for (; i < m.length; i++){
					if (!m[i].neighbourhood || m[i].neighbourhood == '' || !c.getHood(m[i].neighbourhood)){
						continue;
					}
					l = m[i].lat && m[i].lng? {lat: parseFloat(m[i].lat), lng: parseFloat(m[i].lng)}:m[i].address+', '+m[i].postal+' Rotterdam Nederland';
					n = new Marker(m[i].name, l, m[i].tags, m[i].desc, m[i].url, m[i].id, m[i].county);
					c.getHood(m[i].neighbourhood).addMarker(n);
					if (!filter || !filter.hood || filter.hood == m[i].neighbourhood){
						n.showOnMap(filter);
					}
				}
				c.loaded = true;
	//			Labels.show();
			}catch (e){alert(e.message+'\n'+e.lineNumber)}
			Filter.stopLoading();
		});
		return;
	},
	getHood: function(h){return this.hoods[h]},
	getHoods: function(){return this.hoods},
	getId: function(){return this.id},
	getName: function(){return this.name},
	getPolygon: function(){
		if (!this.polygon){
			var i = 0, latlnglist = [], latlng = this.outline;
			for (;i < latlng.length; i++) latlnglist.push(new GLatLng(latlng[i][0], latlng[i][1]));
			this.polygon = new GPolygon(latlnglist, "#53311c", 3, .7, "#FFF", 0);//57b6dd
		}
		return this.polygon;
	},
	showOutline: function(){Map.setOutline(this.getPolygon())},
	showOnMap: function(filter){
		$('option[@value = \''+this.id+'\']').get(0).selected = true;
		this.getHoodMarkerSelection(filter);
		var hoods = this.getHoods(), h = $('#neighbourhoods').empty(), i;
		h.append('<option value="alle">Filter op wijk</option>');
	
	//	for (i in hoods) h.append('<option value="'+i+'">'+hoods[i].getName()+'</option>');
		Filter.resetHoods();
		
		if (filter && filter.hood) $('option[@value = \''+filter.hood+'\']').get(0).selected = true;
		var t = this;
		setTimeout(function(){
			t.showOutline();
			Map.setCenter(t.getCenter());
		}, 1000);
	}
}
/**
 * Create all counties
 * Add hoods to the right county
 */
County.register = function(){
	var hoods, county, i, j, list = {
		charlois: new County('Charlois', new GLatLng(51.880942, 4.477615),[[51.877869, 4.453669],[51.877286, 4.453068],[51.874318, 4.453411],[51.873312, 4.453325],[51.872411, 4.451866],[51.871987, 4.451694],[51.871987, 4.453840],[51.871616, 4.455557],[51.871033, 4.458389],[51.869390, 4.462166],[51.868224, 4.465513],[51.867111, 4.467745],[51.866051, 4.470148],[51.865309, 4.473495],[51.863772, 4.477530],[51.863348, 4.481821],[51.865627, 4.480963],[51.867270, 4.487057],[51.868807, 4.493151],[51.869072, 4.494352],[51.869231, 4.515209],[51.871404, 4.514694],[51.875325, 4.510918],[51.877551, 4.505854],[51.880836, 4.500790],[51.883114, 4.497442],[51.884915, 4.492035],[51.888465, 4.492722],[51.889365, 4.494181],[51.894239, 4.493752],[51.897575, 4.495468],[51.899164, 4.492893],[51.895881, 4.476671],[51.898476, 4.469290],[51.900329, 4.468002],[51.899535, 4.459333],[51.899747, 4.451609],[51.901442, 4.438562],[51.899323, 4.432812],[51.897311, 4.425774],[51.896463, 4.418650],[51.895722, 4.419165],[51.895404, 4.418650],[51.893126, 4.420195],[51.891325, 4.415131],[51.890001, 4.421310],[51.887034, 4.419594],[51.885710, 4.425688],[51.886028, 4.426546],[51.888889, 4.424400],[51.889736, 4.425344],[51.891166, 4.425859],[51.893815, 4.423885],[51.895828, 4.422083],[51.896516, 4.422855],[51.897364, 4.425688],[51.899376, 4.432983],[51.901442, 4.438562],[51.899747, 4.451437],[51.899535, 4.459333],[51.895245, 4.459419],[51.894450, 4.462938],[51.877869, 4.453669]]),
		kralingencrooswijk: new County('Kralingen-Crooswijk', new GLatLng(51.927966, 4.516068), [[51.942307, 4.486198], [51.941355, 4.487314], [51.939556, 4.485855], [51.938550, 4.485769], [51.935799, 4.487743], [51.935322, 4.489803], [51.934423, 4.489889], [51.933576, 4.488602], [51.932412, 4.487057], [51.930612, 4.485855], [51.929766, 4.486113], [51.927913, 4.484653], [51.926537, 4.484911], [51.925055, 4.485512], [51.923732, 4.495983], [51.918015, 4.500790], [51.917326, 4.505167], [51.915579, 4.510403], [51.913885, 4.513493], [51.911397, 4.515638], [51.907902, 4.516497], [51.904778, 4.517269], [51.901600, 4.520445], [51.900012, 4.524307], [51.900488, 4.532118], [51.902501, 4.538212], [51.903772, 4.542933], [51.915103, 4.535122], [51.917591, 4.534092], [51.926590, 4.538555], [51.928972, 4.538469], [51.946593, 4.531603], [51.949979, 4.525766], [51.947651, 4.515381], [51.945693, 4.502764], [51.944847, 4.495125], [51.942307, 4.486198]]),
		delfshaven: new County('Delfshaven', new GLatLng(51.914044, 4.445686), [[51.910258, 4.411440],[51.909808, 4.409895],[51.910417, 4.409380],[51.912111, 4.408951],[51.913356, 4.412127],[51.914282, 4.412298],[51.914123, 4.415517],[51.920212, 4.417191],[51.920688, 4.422684],[51.921085, 4.426160],[51.921403, 4.433498],[51.922144, 4.441781],[51.922779, 4.448004],[51.922964, 4.451480],[51.922647, 4.452639],[51.922620, 4.453926],[51.922885, 4.456844],[51.923070, 4.458261],[51.923282, 4.462552],[51.921588, 4.463110],[51.919444, 4.463367],[51.917485, 4.462938],[51.916373, 4.462724],[51.915050, 4.463367],[51.913647, 4.463711],[51.912244, 4.463539],[51.911767, 4.463582],[51.910973, 4.461007],[51.910152, 4.459376],[51.908988, 4.460449],[51.908802, 4.462681],[51.905863, 4.464569],[51.903083, 4.466071],[51.900435, 4.468002],[51.899429, 4.460707],[51.899429, 4.453626],[51.900780, 4.442596],[51.902262, 4.432082],[51.902209, 4.424272],[51.901150, 4.415174],[51.902315, 4.413071],[51.903904, 4.414959],[51.905334, 4.413500],[51.906658, 4.412341],[51.908034, 4.413199],[51.910258, 4.411440]]),
		hillegersbergschiebroek: new County('Hillegersberg-Schiebroek', new GLatLng(51.956115, 4.490490), [[51.939397, 4.460449], [51.939820, 4.464483], [51.944318, 4.496584], [51.947810, 4.521303], [51.948762, 4.529200], [51.949397, 4.528170], [51.949979, 4.527311], [51.950825, 4.527140], [51.951460, 4.527397], [51.951777, 4.528084], [51.952042, 4.528856], [51.952148, 4.529371], [51.954687, 4.528341], [51.958072, 4.526882], [51.964524, 4.524736], [51.963784, 4.522333], [51.963731, 4.521303], [51.963678, 4.520874], [51.963149, 4.520359], [51.961827, 4.519157], [51.963096, 4.515553], [51.962991, 4.513750], [51.971927, 4.495897], [51.967327, 4.495468], [51.969548, 4.491005], [51.973249, 4.483194], [51.975258, 4.479418], [51.979752, 4.470491], [51.981444, 4.467230], [51.980175, 4.465342], [51.979382, 4.466286], [51.977056, 4.461737], [51.975629, 4.462252], [51.974941, 4.461479], [51.974042, 4.461651], [51.971875, 4.461479], [51.971822, 4.460449], [51.970923, 4.461308], [51.970077, 4.461908], [51.969495, 4.462080], [51.968543, 4.461994], [51.967803, 4.461479], [51.967168, 4.460535], [51.966640, 4.459162], [51.966269, 4.458132], [51.965582, 4.458990], [51.964260, 4.460707], [51.963043, 4.461479], [51.961827, 4.461651], [51.959870, 4.461651], [51.957754, 4.461565], [51.955797, 4.461308], [51.953364, 4.460278], [51.952095, 4.460020], [51.950931, 4.459934], [51.948444, 4.459934], [51.946540, 4.459848], [51.944159, 4.459848], [51.941778, 4.459848], [51.939397, 4.460449]]),
		hoekvanholland: new County('Hoek van Holland', new GLatLng(51.978536, 4.136524), [[51.955268, 4.161243], [51.960346, 4.167767], [51.961510, 4.178753], [51.964683, 4.187508], [51.965741, 4.188023], [51.967115, 4.187679], [51.969971, 4.179783], [51.973461, 4.169827], [51.976633, 4.158840], [51.977691, 4.155064], [51.979594, 4.155064], [51.981285, 4.146824], [51.984457, 4.148369], [51.984774, 4.147167], [51.986783, 4.148369], [51.990166, 4.136696], [51.992280, 4.133434], [51.994817, 4.131546], [51.998199, 4.130688], [51.999362, 4.131889], [52.001898, 4.122448], [51.993866, 4.103565], [51.992174, 4.094124], [51.983611, 4.083309], [51.977585, 4.107170], [51.973355, 4.124165], [51.968385, 4.136009], [51.963837, 4.146481], [51.955268, 4.161243]]),
		hoogvliet: new County('Hoogvliet', new GLatLng(51.861970, 4.362259), [[51.871881, 4.331274], [51.874636, 4.342861], [51.875537, 4.347410], [51.876491, 4.356594], [51.877286, 4.362688], [51.877551, 4.374790], [51.878081, 4.385004], [51.875113, 4.386206], [51.873047, 4.386206], [51.867959, 4.386377], [51.868383, 4.387321], [51.868277, 4.387836], [51.867853, 4.388094], [51.865097, 4.386549], [51.861228, 4.386120], [51.861175, 4.384403], [51.860009, 4.384317], [51.857411, 4.384060], [51.855662, 4.384489], [51.854283, 4.385176], [51.853594, 4.385519], [51.852905, 4.383717], [51.846596, 4.382515], [51.846065, 4.383717], [51.842354, 4.383287], [51.842725, 4.377623], [51.843043, 4.370842], [51.844103, 4.366121], [51.846171, 4.360027], [51.848876, 4.353333], [51.851686, 4.348440], [51.854707, 4.344578], [51.858418, 4.341059], [51.861599, 4.337540], [51.865044, 4.335136], [51.867323, 4.332991], [51.871881, 4.331274]]),
		feijenoord: new County('Feijenoord', new GLatLng(51.897522, 4.506025), [[51.902871, 4.518385], [51.901918, 4.513063], [51.899217, 4.514608], [51.886823, 4.525423], [51.886505, 4.522591], [51.885233, 4.522505], [51.882849, 4.521646], [51.882107, 4.516068], [51.882637, 4.515209], [51.882637, 4.514008], [51.882266, 4.512892], [51.881260, 4.511433], [51.878928, 4.508600], [51.877551, 4.505939], [51.879140, 4.503193], [51.880836, 4.500961], [51.882955, 4.498043], [51.884968, 4.492035], [51.888412, 4.492550], [51.889312, 4.494095], [51.891484, 4.493837], [51.894186, 4.493752], [51.897575, 4.495468], [51.899217, 4.493322], [51.895828, 4.476843], [51.898476, 4.469290], [51.900647, 4.469633], [51.902183, 4.474096], [51.903719, 4.478216], [51.904460, 4.480019], [51.903348, 4.481993], [51.903189, 4.487400], [51.904778, 4.492979], [51.904301, 4.495468], [51.906896, 4.498816], [51.909649, 4.492378], [51.909173, 4.490490], [51.910497, 4.486113], [51.913038, 4.488859], [51.915156, 4.492207], [51.916797, 4.495983], [51.917909, 4.499245], [51.917909, 4.502163], [51.917591, 4.504566], [51.917009, 4.507227], [51.916268, 4.509716], [51.914891, 4.512291], [51.913356, 4.514093], [51.911979, 4.515209], [51.910497, 4.516153], [51.908590, 4.516582], [51.906843, 4.516840], [51.905201, 4.517097], [51.903931, 4.517698], [51.902871, 4.518385]]),
		ijsselmonde: new County('IJsselmonde', new GLatLng(51.888041, 4.548855), [[51.904142, 4.517956], [51.902130, 4.512978], [51.886875, 4.525166], [51.886346, 4.522591], [51.882849, 4.521561], [51.882107, 4.516239], [51.882637, 4.515209], [51.882213, 4.512978], [51.879140, 4.508686], [51.877445, 4.505768], [51.875537, 4.510059], [51.873524, 4.512978], [51.871086, 4.514694], [51.866634, 4.515381], [51.865786, 4.527225], [51.866952, 4.535809], [51.868966, 4.536324], [51.872252, 4.543705], [51.874795, 4.550400], [51.874901, 4.551601], [51.878399, 4.562073], [51.878504, 4.567394], [51.878610, 4.569283], [51.886452, 4.571857], [51.892702, 4.574089], [51.899800, 4.576149], [51.901495, 4.575977], [51.902765, 4.576492], [51.903613, 4.569969], [51.904672, 4.561901], [51.905837, 4.555550], [51.905413, 4.550743], [51.904354, 4.545250], [51.902130, 4.536495], [51.900541, 4.529629], [51.900012, 4.526024], [51.900118, 4.523964], [51.900541, 4.522076], [51.902024, 4.519672], [51.903931, 4.518127], [51.904142, 4.517956]]),
		noord: new County('Noord', new GLatLng(51.932359, 4.465084), [[51.926167, 4.432554], [51.931883, 4.442682], [51.934264, 4.445944], [51.934846, 4.449291], [51.935905, 4.454184], [51.939503, 4.464226], [51.939979, 4.466801], [51.941672, 4.480276], [51.941990, 4.485512], [51.941196, 4.486971], [51.940455, 4.486885], [51.939820, 4.486113], [51.939344, 4.485598], [51.938603, 4.485769], [51.938021, 4.486284], [51.937439, 4.486971], [51.936540, 4.487228], [51.936010, 4.487486], [51.935693, 4.488001], [51.935534, 4.489117], [51.935270, 4.489803], [51.934687, 4.489803], [51.933735, 4.489031], [51.933206, 4.487743], [51.932041, 4.486713], [51.930612, 4.485855], [51.929554, 4.485941], [51.928601, 4.484825], [51.927437, 4.484482], [51.926960, 4.484482], [51.926325, 4.485340], [51.925002, 4.485168], [51.925584, 4.482250], [51.925584, 4.480276], [51.925849, 4.478645], [51.926061, 4.475384], [51.926219, 4.472036], [51.926114, 4.470491], [51.924684, 4.464226], [51.924420, 4.461393], [51.924367, 4.458475], [51.924102, 4.455214], [51.922832, 4.444141], [51.921985, 4.437704], [51.926167, 4.432554]]),
		overschie: new County('Overschie', new GLatLng(51.940879, 4.432983), [[51.967750, 4.456329], [51.965635, 4.459076], [51.964154, 4.461136], [51.962039, 4.461479], [51.959183, 4.461823], [51.956009, 4.461479], [51.952306, 4.459934], [51.947915, 4.459763], [51.945164, 4.460020], [51.943418, 4.459934], [51.941249, 4.460020], [51.939503, 4.460535], [51.939238, 4.459591], [51.938656, 4.456329], [51.938074, 4.454784], [51.937175, 4.453154], [51.935957, 4.451694], [51.935322, 4.451265], [51.934423, 4.446630], [51.933153, 4.444742], [51.931512, 4.442167], [51.926114, 4.432297], [51.937492, 4.417448], [51.938233, 4.416933], [51.940349, 4.414186], [51.943154, 4.414959], [51.944477, 4.415131], [51.945535, 4.414358], [51.948074, 4.412041], [51.946857, 4.406805], [51.945693, 4.398222], [51.944318, 4.393501], [51.943947, 4.389982], [51.960452, 4.381142], [51.962462, 4.392986], [51.966692, 4.403286], [51.976527, 4.428005], [51.962567, 4.446802], [51.963625, 4.448519], [51.965952, 4.451180], [51.967750, 4.456329]]),
		prinsalexander: new County('Prins Alexander', new GLatLng(51.953999, 4.547310), [[51.905943, 4.541388],[51.913144, 4.536409],[51.914944, 4.535208],[51.917538, 4.534178],[51.920767, 4.535122],[51.920132, 4.537096],[51.919762, 4.540958],[51.919709, 4.544992],[51.919867, 4.547997],[51.920132, 4.551945],[51.920714, 4.555378],[51.922726, 4.561987],[51.924843, 4.565592],[51.926114, 4.566278],[51.929660, 4.565420],[51.940879, 4.560614],[51.942413, 4.570484],[51.943524, 4.572802],[51.949503, 4.565763],[51.956485, 4.562244],[51.957173, 4.566364],[51.957807, 4.568939],[51.966534, 4.587736],[51.972033, 4.602070],[51.975787, 4.593573],[51.981708, 4.595890],[51.987999, 4.598637],[51.994341, 4.594088],[51.983876, 4.557352],[51.972139, 4.570227],[51.971240, 4.567480],[51.972033, 4.564304],[51.972403, 4.563189],[51.972403, 4.562073],[51.971293, 4.559069],[51.970130, 4.560270],[51.969759, 4.560442],[51.969442, 4.559927],[51.969125, 4.558210],[51.968490, 4.556494],[51.967697, 4.555378],[51.968966, 4.553576],[51.969971, 4.551430],[51.971240, 4.549456],[51.971822, 4.548512],[51.972139, 4.547310],[51.972615, 4.546795],[51.972192, 4.545250],[51.971293, 4.543276],[51.969971, 4.540100],[51.970077, 4.538383],[51.970553, 4.536753],[51.970764, 4.535723],[51.970394, 4.534950],[51.969865, 4.535208],[51.968808, 4.535122],[51.968226, 4.534607],[51.968014, 4.533749],[51.967803, 4.532118],[51.967591, 4.530573],[51.967221, 4.529457],[51.966904, 4.528513],[51.966587, 4.527740],[51.966111, 4.527826],[51.965529, 4.527912],[51.965317, 4.527225],[51.964894, 4.526196],[51.964418, 4.524736],[51.952095, 4.529200],[51.951936, 4.528255],[51.951142, 4.527311],[51.950190, 4.527483],[51.949291, 4.528084],[51.948497, 4.529457],[51.947439, 4.530916],[51.945217, 4.532118],[51.936222, 4.535980],[51.928813, 4.538641],[51.926696, 4.538469],[51.921138, 4.535208],[51.917538, 4.534006],[51.912667, 4.536667],[51.905254, 4.542074],[51.908273, 4.555206],[51.909967, 4.556494],[51.914785, 4.556665],[51.914838, 4.554434],[51.916479, 4.554176],[51.915262, 4.551773],[51.909120, 4.549456],[51.907373, 4.548597],[51.906419, 4.546022],[51.905943, 4.541388]]),
		stadscentrum: new County('Stadscentrum', new GLatLng(51.914997, 4.474783), [[51.906208, 4.464140], [51.908802, 4.462767], [51.909173, 4.460449], [51.910338, 4.459419], [51.911714, 4.463625], [51.913462, 4.463797], [51.914944, 4.463196], [51.916109, 4.462767], [51.916956, 4.462681], [51.919020, 4.463282], [51.920079, 4.463196], [51.922408, 4.462852], [51.923308, 4.462509], [51.924684, 4.462252], [51.926378, 4.471264], [51.926272, 4.476671], [51.925584, 4.482164], [51.924420, 4.488773], [51.923838, 4.495897], [51.922038, 4.497356], [51.920185, 4.499073], [51.918173, 4.500704], [51.917750, 4.498215], [51.917273, 4.496241], [51.916585, 4.494610], [51.915738, 4.493237], [51.914362, 4.490919], [51.912456, 4.488001], [51.910497, 4.486198], [51.908802, 4.490576], [51.909596, 4.492292], [51.907055, 4.498558], [51.906102, 4.498129], [51.904619, 4.496326], [51.904725, 4.492464], [51.902977, 4.487572], [51.903136, 4.483280], [51.904142, 4.479675], [51.902130, 4.474268], [51.900435, 4.467659], [51.906208, 4.464140]])
	};
	hoods = {
		charlois: ['Tarwewijk','Carnisse','Zuidwijk','Oud-Charlois','Wielewaal','Zuidplein','Pendrecht', 'Zuiderpark','Heijplaat'],
		kralingencrooswijk: ['Rubroek','Nieuw-Crooswijk','Oud-Crooswijk','Kralingen-West','Kralingen-Oost','Kralingse Bos','De Esch','Struisenburg'],
		delfshaven: ['Delfshaven','Bospolder','Tussendijken','Spangen','Nieuwe Westen','Middelland','Oud-Mathenesse','Witte Dorp','Schiemond','Lloyd Kwartier','Havengebied'],
		hillegersbergschiebroek: ['Schiebroek','Kleiwegkwartier','Terbregge','Molenlaankwartier','Oud-Hillegersberg','110-Morgen'],//'Hillegersberg-Noord',
		hoekvanholland: ['Strand en Duin','Dorp','Rijnpoort'],
		hoogvliet: ['Tussenwater & Gadering','Middengebied en stadshart','Oudeland','Nieuw-Engeland','Westpunt','Boomgaardshoek','Meeuwenplaat','Zalmplaat'],
		feijenoord: ['Feijenoord','Noordereiland','Vreewijk','Bloemhof','Hillesluis','Katendrecht','Afrikaanderwijk','Kop van Zuid'],
		ijsselmonde: ['Oud-IJsselmonde','Lombardijen','Groot-IJsselmonde','Beverwaard'],
		noord: ['Agniesebuurt','Provenierswijk','Bergpolder','Blijdorp','Liskwartier','Oude-Noorden','Blijdorpse polder'],
		overschie: ['Overschie-oude-dorpskern','Kleinpolder','Noord-Kethel','Schieveen','Zestienhoven','Landzicht'],
		prinsalexander: ['Kralingse Veer','Prinsenland','Het Lage Land','Ommoord','Zevenkamp','Oosterflank','Nesselande'],
		stadscentrum: ['CS Kwartier', 'Cool','Dijkzigt','Oude Westen','Scheepvaartkwartier','Stadsdriehoek','Kop van Zuid'] //'Stadscentrum',
	};
	for (i in hoods){
		county = list[i];
		for (j = 0; j < hoods[i].length; j++) county.addHood(new Hood(hoods[i][j]))
	}
	return list;
}
/**
 * save a latlng to the server
 */
County.saveLatLng = function(id, lat, lng){
	if(id != '' && lat != '' && lng != '') $.get('/includes/ckr/saveLatLng.inc.php?id='+id+'&lat='+lat+'&lng='+lng+'', function(data){
		//alert('Lat Lng ('+lat+'-'+lng+') for id '+id+' saved: ' + data);
	});
}

/************************************************************************************************************************************************
 *																																				*
 *															H O O D																				*
 *																																				*
 ************************************************************************************************************************************************/

/**
 * Hood in a county
 * + n: String name
 */
var Hood = function(n){
	this.name = n;
	this.id = n.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
	this.markers = {};
}
/**
 * + addMarker: void, add a marker to this hood
 * + getName: String
 * + getId: String
 * + showOnMap: void, show this hood on the map, provided it passes an optional filter
 */
Hood.prototype = {
	addMarker: function(m){this.markers[m.getId()] = m},
	getName: function(){return this.name},
	getId: function(){return this.id},
	showOnMap: function(filter){
		if (!filter || !filter.hood || filter.hood == this.id)
			$.each(this.markers, function(i,j){j.showOnMap(filter)})
	}
}

/************************************************************************************************************************************************
 *																																				*
 *															M A R K E R																			*
 *																																				*
 ************************************************************************************************************************************************/

/**
 * Wrapper for information of contacts
 * + n: String, name
 * + l: lat-lng
 * + t: String[], tags
 * + d: String, description
 * + u: String, url
 * + id: int, database id, used when no lat-lng is found
 */
var Marker = function(n,l,t,d,u,id,c){
	this.name = n;
	this.label = t||[];
	this.desc = d;
	this.url = u;
	this.county = c;
	this.id = n.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
	if (l && l.lat && l.lng){
		this.latlng = l;
	}else{
		var g = new GClientGeocoder(), m = this;
		g.getLatLng(l, function(point){
			if (!point) return false;
			else{
				County.saveLatLng(id, point.lat(), point.lng());
				return m.setLatLng({lat: point.lat(), lng: point.lng()});
			}
		});
	}
}
/**
 * 
 */
Marker.prototype = {
	marker: null,
	timeoutcount: 0,
	getId: function(){return this.id},
	setLatLng: function(l){this.latlng = l},
	showOnMap: function(filter){
		if (filter &&  ((filter.name && this.id != filter.name) || (filter.tags && $.inArray(filter.tags, this.label) < 0) )) return;
		$.each(this.label, function(){Labels.add(this)});
		var desc = '<strong>'+this.name+'</strong><br/>'+this.desc+'<br/><a href="'+this.url+'" style="float: right">Meer informatie &raquo;</a>';
		if (!this.marker){ // create marker only once
			if(!this.latlng || !this.latlng.lat || !this.latlng.lng){
				var t = this;
				if (this.timeoutcount++ < 5) setTimeout(function(){t.showOnMap()}, 1000);
				return;
			}
			var point = new GLatLng(this.latlng.lat, this.latlng.lng);
			this.marker = new GMarker(point, icon);//, {title: this.name});
			GEvent.addListener(this.marker, 'click', function(){
				this.openInfoWindowHtml(desc, {maxWidth:'300'});
				Map.getMap().panTo(point);
			});
		}
		Map.manager.addMarker(this.marker, Map.manager.getMarkerCount(9) < Map.MAX_LEVEL_9? 9:14);
		if (filter && filter.name && filter.name == this.id){
			this.marker.openInfoWindowHtml(desc, {maxWidth:'300'});
			Map.getMap().panTo(this.marker.getLatLng());
		}
		Filter.results.innerHTML += '<dl><dt><a href="#" title="'+this.id+'" rel="'+this.county+'">'+this.name+'</a></dt><dd class="content"><p>'+this.desc+'</p></dd></dl>';
		return;
	}
}


/**
 * GIcon used for markers
 */
var icon = new GIcon();
icon.image = '/images/ckr/icon/marker.png';
icon.iconSize = new GSize(19, 32);
icon.iconAnchor = new GPoint(16, 18);
icon.infoWindowAnchor = new GPoint(20, 0);

/************************************************************************************************************************************************
 *																																				*
 *															F I L T E  R																			*
 *																																				*
 ************************************************************************************************************************************************/

var Filter = {
	countylist: {},
	hoodSelect: null,
	results: null,
	change: function(ev){ // event handler
		var target = ev.target, l = null;
		if ($.inArray(target.id, ['county', 'hood', 'label'])){
			Labels.reset();
			Map.clear();
			Filter.results.innerHTML = '';
			switch (target.id){
				case 'counties': // Countyhas just been selected
					var c = target.options[target.selectedIndex].value;
					if (!c || c == ''){
						Filter.resetHoods();
						break;
					}
					window.location = '/culturele-kaart-rotterdam/?county='+target.options[target.selectedIndex].value;
					return;
/*					Map.zoom(13);
					var c = target.options[target.selectedIndex].value;
					if (!c || c == '')
						Filter.resetHoods();
					else if (c && Filter.countylist[c]){
						Filter.countylist[c].showOnMap(Map.getMap());
					}
					break;*/
				case 'neighbourhoods':
					var c = $('#counties').get(0), v = c.options[c.selectedIndex].value, c = Filter.countylist[v], h = target.options[target.selectedIndex].value;
				//	if (!c){
						//c = Filter.countylist[target.options[target.selectedIndex].parentNode.id];
						c = target.options[target.selectedIndex].parentNode.id;
						window.location = '/culturele-kaart-rotterdam/?county='+c+'&hood='+h;
						return;
//						return c.showOnMap({hood: h});
				//	}
					if (c && h){
						if (h == 'alle')
							window.location = '/culturele-kaart-rotterdam/?county='+v;
//							c.showOnMap();
						else{
							window.location = '/culturele-kaart-rotterdam/?county='+v+'&hood='+h;
//							c.showOutline();
//							c.getHood(h).showOnMap();
						}
						return;
					}
					break;
				case 'labels':
					var c = $('#counties').get(0), c = c.options[c.selectedIndex].value, c = Filter.countylist[c], l = target.options[target.selectedIndex].value, h; 
					if (c){
						h = $('#neighbourhoods').get(0), h = h.options[h.selectedIndex].value;
						if (h && h != 'alle'){
							c.showOutline();
							c.getHood(h).showOnMap(l==Labels.ALL?null:{tags: l});
						}else{
							c.showOnMap(l==Labels.ALL?null:{tags: l});
						}
					}else{ // no county
						Filter.startLoading();
						return $.getJSON('/includes/ckr/getMapData.inc.php?label='+l, Filter.handleSearch);
					}
					break;
			}
		//	Labels.show(l);
			Map.manager.refresh();
			Filter.showEmpty();
		}
		return false;
	},
	handleSearch: function(j){
		Map.zoom(12);
		Map.clear();
		if (!j.markers){
			return Filter.showEmpty();
		}
		var i, ma = j.markers, c, h, i =0, m;
		for (;i < ma.length; i++){
			var l = ma[i].lat && ma[i].lng? {lat: ma[i].lat, lng: ma[i].lng}:ma[i].address+', '+ma[i].postal+' Rotterdam Nederland';
			c = Filter.countylist[ma[i].county];
			if (c){
				h = c.getHood(ma[i].neighbourhood);
				if (h){
					m = new Marker(ma[i].name, l, ma[i].tags, ma[i].desc, ma[i].url, ma[i].id, ma[i].county);
					h.addMarker(m);
					m.showOnMap();
				}
			}
		}
		Filter.stopLoading();
	//	Labels.show(l);
		Map.manager.refresh();
	},
	register: function(){
		Map.register();
		Filter.countylist = County.register(); // setup list of county oblect, containing available hoods
		Filter.hoodSelect = $('#filterform #neighbourhoods').clone(true);
		$('#filterform'+$.browser.msie?' select':'').change(Filter.change); // geen onchange in ie# op een form, logisch... %^#$^(*$^@Bill
		$('#filterform').submit(Filter.submit);
		$('#search').focus(function(){if(this.value=='Vul een zoekterm in')this.value=''}).blur(function(){if(this.value=='')this.value='Vul een zoekterm in'});
		Filter.results = $('#results').click(function(ev){
			var c = ev.target.rel;
			if (c){
				c = Filter.countylist[c];
				Map.clear();
				c.showOutline();
				c.showOnMap({name: ev.target.title});
			}
			return false;
		}).get(0);
		var hash, vars = [], hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'), i = 0; // setup according to GET vars
		for(; i < hashes.length; i++){
			hash = hashes[i].split('=');
			if (hash[1]) vars[hash[0]] = hash[1];
		}
		if (vars['label']){ // label specified in GET
			$.getJSON('/includes/ckr/getMapData.inc.php?label='+vars['label'], Filter.handleSearch);
		}else if(vars['county'] && Filter.countylist[vars['county']]){ // county requested in the GET
			var c = Filter.countylist[vars['county']];
			Filter.results.innerHTML = '';
			c.showOnMap(Map.getMap());
			if(vars['hood']){
				$('#neighbourhoods option[@value = '+vars['hood']+']').attr('selected', 'selected');
				Filter.results.innerHTML = '';
				setTimeout(function(){
					Labels.reset();
					Map.clear();
					Filter.results.innerHTML = '';
					c.showOutline();
					c.getHood(vars['hood']).showOnMap();
				}, 2000);
			}
		}
	},
	resetHoods: function(){ // replace the option list for the hoods to only show all hoods
		var n = $('#filterform #neighbourhoods'), c = Filter.hoodSelect.clone(true);
		n.replaceWith(c);
		$('#filterform'+$.browser.msie?' select':'').change(Filter.change);
	},
	showEmpty: function(){
		$('#loading-img').remove();
		if (Filter.results.innerHTML.length == 0){
			Filter.results.innerHTML = '<dl><dt>Geen resultaten</dt><dd>Er zijn geen culturele instellingen gevonden die aan uw zoekcriteria voldoen.</dd></dl>';
		}
	},
	startLoading: function(){
		Filter.results.innerHTML = '<dl id="loading-img"><dt><img src="/images/ckr/icon/loading.gif" alt="" class="inline"/> Loading</dt></dl>';
	},
	stopLoading: function(){
		Filter.showEmpty();
	},
	submit: function(){
		Filter.startLoading();
		$.getJSON('/includes/ckr/getMapData.inc.php?search='+$('#search').get(0).value.replace(' ', '+'), Filter.handleSearch);
		return false;
	}
}

/************************************************************************************************************************************************
 *																																				*
 *															L A B E L																			*
 *																																				*
 ************************************************************************************************************************************************/
 
var Labels = {
	ALL: 'alle',
	labels: {},
	add: function(l){this.labels[l] = l},
	reset: function(){this.labels = {}},
	show: function(l){
		var i, t = '';
		t = '<option value="'+Labels.ALL+'">Filter op label</option>';
		for (i in this.labels){
			t += '<option value="'+i+'"'+(l==i?' selected="selected"':'')+'>'+i+'</option>';
		}
		$('#labels').empty().append(t);
	}
}

/************************************************************************************************************************************************
 *																																				*
 *															M A P 																				*
 *																																				*
 ************************************************************************************************************************************************/

/**
 * Map contains the map to be used in the map.
 * public static singleton object
 * 
 * + instance: GMap2 object containing the "physical" map
 * + rotterdam: GLatLng center of town
 *
 * functions:
 * + register: initiate the map
 * + setCenter: reset the center to given latlng,
 * + setOutline: show a polygon,
 * + setupClusters: initiate MarkerManager, called when markers have been loaded (ajax), see ...
 * + 
 */
var Map = {
	addMarkers: function(markers){
		var i, m = Map.instance;
		for (i = 0; i < markers.length; i++) m.addOverlay(markers[i]);	
	},
	getMap: function(){return Map.instance},
	
	MAX_LEVEL_9: 100,
	instance: null,
	manager: null,
	rotterdam: new GLatLng(51.922038, 4.470406),
	clear: function(){
		Map.manager.clearMarkers();
		Map.instance.clearOverlays();
	},
	register: function(){
		if(GBrowserIsCompatible()){
			var m = new GMap2($('#map').get(0));
			m.setCenter(Map.rotterdam, 13);
			m.addControl(new GLargeMapControl());
			m.addControl(new GMapTypeControl());
			m.addMapType(G_PHYSICAL_MAP);
			m.enableContinuousZoom();
			m.enableDoubleClickZoom();
			m.enableScrollWheelZoom();
			m.setMapType(G_PHYSICAL_MAP);
			Map.manager = new MarkerManager(m, {borderPadding:75});
			Map.instance = m;
		}
	},
	setCenter: function(latlng){Map.instance.panTo(latlng)},
	setOutline: function(polygon){Map.instance.addOverlay(polygon)},
	zoom: function(i){
		Map.instance.setZoom(i);
	}
}
$(Filter.register);
window.onunload = GUnload;