//console.log('main');

var map;
var mgr;
var aMarkers;

function initialize() {

	//reset country select
	if ($('tx_secutauchdb_pi[country]')) {
		$('tx_secutauchdb_pi[country]').options.selectedIndex = 0;
	}

	if (GBrowserIsCompatible() && ($('gmap1'))) {
		map = new GMap2(document.getElementById("gmap1"));
		map.setCenter(new GLatLng(0,0), global_gmap_init_zoomlvl);
		map.enableScrollWheelZoom();
		map.setUIToDefault();
		map.setMapType(G_PHYSICAL_MAP);
		mgr = new MarkerManager(map);
		redrawMarkers();
	}
}

function startSearch() {
	$('tx_secutauchdb_pi[country]').value = false;

	var aCatFilter = $$('input[name=recordfilter]');
	aCatFilter.each(function (e) {
		e.setProperty('checked', true);
	});
	aCatFilter = $$('input[name=objectfilter]');
	aCatFilter.each(function (e) {
		e.setProperty('checked', true);
	});

	$('tx_secutauchdb_pi[country]').value = 0;
	return updateRecordList();
}

function changeCountry() {
	var uid = $('tx_secutauchdb_pi[country]').value;

	if (uid == 0) {
		map.setCenter(new GLatLng(0, 0), global_gmap_init_zoomlvl);
	}
	else {
		map.setCenter(new GLatLng(aCountries[uid].glat, aCountries[uid].glong), aCountries[uid].zoomlvl);
	}
	return updateRecordList();
}

function updateRecordList() {

	var aRecordFilter = $$('input[name=recordfilter]');
	var aRecordValues = new Array();
	aRecordFilter.each(function (e) {
		if (e.getProperty('checked') == true) {
			aRecordValues.push(e.getProperty('value'));
		}
	});

	var aObjectFilter = $$('input[name=objectfilter]');
	var aObjectValues = new Array();
	aObjectFilter.each(function (e) {
		if (e.getProperty('checked') == true) {
			aObjectValues.push(e.getProperty('value'));
		}
	});

	var aData = new Hash({
		country: $('tx_secutauchdb_pi[country]').value,
		sword: $('tx_secutauchdb_pi[sword]').value,
		recordFilter : aRecordValues,
		objectFilter : aObjectValues
	});

	//console.log(aData);

	var req = new Request.HTML({
		url: 'index.php?eID=secu_tauchdb_updateRecordList',
		method: 'post',
		data: aData,
		update: $('secu_tauchdb_catlist'),

/*
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('secu_tauchdb_catlist').empty();
			//Inject the new DOM elements into the results div.
			$('secu_tauchdb_catlist').adopt(html);
		},
*/
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.

		onFailure: function() {
			$('secu_tauchdb_catlist').set('text', 'The request failed.');
		}
	}).send();
}

function redrawMarkers() {

	mgr.clearMarkers();

	aMarkers = new Array();
	var UsrIconPath = 'uploads/tx_secutauchdb/';

	var	noicon = new GIcon(G_DEFAULT_ICON);
		noicon.image = '';
		noicon.iconSize = new GSize(1, 1);
		noicon.shadowSize = new GSize(0, 0);
		noicon.iconAnchor = new GPoint(1, 1);

	var aRecordFilter = $$('input[name=recordfilter]');
	aRecordFilter.each(function (e) {

		//console.log(e.value)
		if (e.getProperty('checked') != 0) {

			//--------------payed user record labels
			for (m in aPayedRecords[e.value]) {
				var obj = aPayedRecords[e.value][m];
				if (!obj.uid) {
					continue;
				}

				var	icon = new GIcon(G_DEFAULT_ICON);
					icon.iconSize = new GSize(25, 22);
					icon.shadowSize = new GSize(0, 0);
					icon.iconAnchor = new GPoint(1, 1);
					icon.LabelOffset = new GSize(25, 0);
				    icon.image = UsrIconPath + obj.caticon;

				var aMarkerOptions = {
					icon: icon,
					labelText: obj.uid,
					labelClass: 'gmap_record_marker',
					labelOffset: icon.LabelOffset
				};

				var oMarker = new LabeledMarker(new GLatLng(obj.glat,obj.glang), aMarkerOptions);
				oMarker.obj = obj;
				aMarkers[obj.uid] = oMarker;
				GEvent.addListener(oMarker, "click", function() {
					this.openInfoWindowHtml(this.obj.infohtml);
				});
				mgr.addMarker(oMarker, global_gmap_payed_zoomlvl);

			}

			//--------------free user record labels
			for (m in aFreeRecords[e.value]) {
				var obj = aFreeRecords[e.value][m];
				if (!obj.uid) {
					continue;
				}

				var icon = new GIcon(G_DEFAULT_ICON);
					icon.iconSize = new GSize(25, 22);
					icon.shadowSize = new GSize(0, 0);
					icon.iconAnchor = new GPoint(1, 1);
					icon.LabelOffset = new GSize(25, 0);
					icon.image = UsrIconPath + obj.caticon;

				var aMarkerOptions = {
					icon: icon,
					labelText: obj.uid,
					labelClass: 'gmap_record_marker',
					labelOffset: icon.LabelOffset
				};

				var oMarker = new LabeledMarker(new GLatLng(obj.glat,obj.glang), aMarkerOptions);
				oMarker.obj = obj;
				aMarkers[obj.uid] = oMarker;
				GEvent.addListener(oMarker, "click", function() {
					this.openInfoWindowHtml(this.obj.infohtml);
				});
				mgr.addMarker(oMarker, global_gmap_record_zoomlvl);

			}

			//--------------country labels
			for (m in aCountries['records'][e.value]) {
				var obj = aCountries['records'][e.value][m];
				if (!obj.uid) {
					continue;
				}
				var aMarkerOptions = {
					icon: noicon,
					labelText: obj.name,
					labelClass: 'gmap_country_marker'
				};
				var oMarker = new LabeledMarker(new GLatLng(obj.glat,obj.glang), aMarkerOptions);

				oMarker.obj = obj;

				GEvent.addListener(oMarker, "click", function() {
					zoomCountry(this.obj.uid, this.obj.glat, this.obj.glang, this.obj.zoomlvl);
				});
				mgr.addMarker(oMarker, 3, 4);

			}
		}
	});

	var aObjectFilter = $$('input[name=objectfilter]');
	aObjectFilter.each(function (e) {

		//console.log(e.value)
		if (e.getProperty('checked') != 0) {

			//--------------wrecks labels
			for (m in aWrecks[e.value]) {
				var obj = aWrecks[e.value][m];
				if (!obj.uid) {
					continue;
				}

				var icon = new GIcon(G_DEFAULT_ICON);
					icon.iconSize = new GSize(25, 22);
					icon.shadowSize = new GSize(0, 0);
					icon.iconAnchor = new GPoint(1, 1);
					icon.LabelOffset = new GSize(25, 0);
					icon.image = UsrIconPath + obj.caticon;

				var aMarkerOptions = {
					icon: icon,
					labelText: obj.uid,
					labelClass: 'gmap_record_marker',
					labelOffset: icon.LabelOffset
				};

				var oMarkerW = new LabeledMarker(new GLatLng(obj.glat,obj.glong), aMarkerOptions);
				oMarkerW.obj = obj;
				GEvent.addListener(oMarkerW, "click", function() {
					this.openInfoWindowHtml(this.obj.infohtml);
				});
				mgr.addMarker(oMarkerW, global_gmap_object_zoomlvl);
			}

			//--------------country labels
			for (m in aCountries['objects'][e.value]) {
				var obj = aCountries['objects'][e.value][m];
				if (!obj.uid) {
					continue;
				}
				var aMarkerOptions = {
					icon: noicon,
					labelText: obj.name,
					labelClass: 'gmap_country_marker'
				};
				var oMarker = new LabeledMarker(new GLatLng(obj.glat,obj.glang), aMarkerOptions);

				oMarker.obj = obj;

				GEvent.addListener(oMarker, "click", function() {
					zoomCountry(this.obj.uid, this.obj.glat, this.obj.glang, this.obj.zoomlvl);
				});
				mgr.addMarker(oMarker, 3, 4);
			}
		}
	});


	//--------------continent labels
	for (m in aContinents) {
		var obj = aContinents[m];
		var aMarkerOptions = {
			icon: noicon,
			labelText: obj.name,
			labelClass: 'gmap_continent_marker'
		};
		var oMarker = new LabeledMarker(new GLatLng(obj.glat,obj.glong), aMarkerOptions);
		oMarker.obj = obj;

		GEvent.addListener(oMarker, "click", function() {
			map.setCenter(new GLatLng(this.obj.glat,this.obj.glong), this.obj.zoomlvl);
		});
		mgr.addMarker(oMarker, 0, 2);

	}
	mgr.refresh();
}

function zoomCountry(uid, glat, glang, zoomlvl) {
	//console.log(uid);
	$('tx_secutauchdb_pi[country]').value = uid;
	map.setCenter(new GLatLng(glat,glang), zoomlvl);
	map.setMapType(G_PHYSICAL_MAP);
	updateRecordList();
}

function showRecord(uid, glat, glang) {
	if (glat && glang && (glat != 0) && (glang != 0) ) {
		var ScrollFx = new Fx.Scroll(window).toElement('main')
		map.setCenter(new GLatLng(glat,glang), global_gmap_preview_zoomlvl);
		map.setMapType(G_PHYSICAL_MAP);
		GEvent.trigger(aMarkers[uid],'click');
	}
	else {
		showRecordDetailWindow(uid);
	}
}

function showRecordDetailWindow(uid) {

		switch (LLkey) {

			case 'en':
				var url='?id=' + global_popup_id  + '&L=1' + '&tx_secutauchdb_pi[recordid]=' + uid;
				break;

			case 'de':
			default:
				var url='?id=' + global_popup_id  + '&L=0' + '&tx_secutauchdb_pi[recordid]=' + uid;
				break;
		}
		hwnd = window.open(url, "Details", "width=" + global_popup_w  +",height="+ global_popup_h  +",menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
	//	hwnd = window.open(url, "Details", "width=" + global_popup_w  +",menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
		hwnd.focus();
}

function showWreckDetailWindow(uid) {

		switch (LLkey) {

			case 'en':
				var url='?id=' + global_popup_id  + '&L=1' + '&tx_secutauchdb_pi[wreckid]=' + uid;
				break;

			case 'de':
			default:
				var url='?id=' + global_popup_id  + '&L=0' + '&tx_secutauchdb_pi[wreckid]=' + uid;
				break;
		}
		hwnd = window.open(url, "Details", "width=" + global_wreck_popup_w  +",height="+ global_wreck_popup_h  +",menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
		hwnd.focus();
}

