/*
	Do all the basics needed for the website
*/


var poll;
DOM.Ready.onDOMReady(function() {
	poll = window.setInterval(function () {
		/*if (mainElements.dynamic) {
			window.clearInterval(poll);
			load();
		}*/
	}, 50);
});


var map;
var clusterer;
var mapMarkers = new Array();
var spotIcon;
var spotIconActive;
var spotIconWaiting;
var spotIconMulti;

var activeSpot, oldSpot;

var preMarker;
var mapElement;
var messageErrorElement, messageWarningElement, messageInfoElement, messageSuccessElement;
var variableElement;
var spotContentElement;

var GET = getUrlVars();

var tagsArr = new Array();
var searchTagsArr = new Array();
var searchBoundsBool = false;
var searchSpotNameStr = "";

var mainVariables = new Object();
var mainElements = new Object();
var userData = new Object();
userData.userID = 0;
userData.username = "";
userData.realname = "";
userData.email = "";
userData.group = 0;

var eventHandlers = new Object();

pageLoaded = 0;


function load() {
	if (document.getElementById("messageError")) {
		messageErrorElement = document.getElementById("messageError");
	}
	
	if (document.getElementById("messageWarning")) {
		messageWarningElement = document.getElementById("messageWarning");
	}
	
	if (document.getElementById("messageSuccess")) {
		messageSuccessElement = document.getElementById("messageSuccess");
	}
	
	if (document.getElementById("messageInfo")) {
		messageInfoElement = document.getElementById("messageInfo");
	}
	
	
	if (GBrowserIsCompatible() && pageLoaded == 0) {
		pageLoaded = 1;
		
		if (mapMode == "") {
			// No map to load, do nothing
		} else {
			if (mapMode == "large") {
				mapElement = document.getElementById("gMapLarge");
			} else {
				mapElement = document.getElementById("gMap");
			}
		
			mapElement.innerHTML = "";
			
			var mapOptions = {};
			map = new GMap2(mapElement, mapOptions);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			
			if (mapStateStr = readCookie("mapState")) {
				var mapStateArr = mapStateStr.split("|");
				var center = new GLatLng(parseFloat(mapStateArr[0]), parseFloat(mapStateArr[1]));
				var zoom = parseInt(mapStateArr[2]);
				
				switch (mapStateArr[3]) {
					case "k":
					var type = G_SATELLITE_MAP;
					break;
					case "h":
					var type = G_HYBRID_MAP;
					break;
					default:
					var type = G_NORMAL_MAP;
				}
			} else {
				var center = new GLatLng(39.639538, -2.460937);
				var zoom = 2;
				var type = G_NORMAL_MAP;
			}
			
			map.setCenter(center, zoom, type);
			
			map.enableScrollWheelZoom();
		
			clusterer = new Clusterer(map);
			clusterer.SetMaxVisibleMarkers(15);
			clusterer.SetMaxLinesPerInfoBox(3);
		
			spotIcon = new GIcon();
			spotIcon.image = cfgSpotIconStr;
			spotIcon.shadow = null;
			spotIcon.iconSize = new GSize(20, 20);
			spotIcon.iconAnchor = new GPoint(11, 11);
			spotIcon.infoWindowAnchor = new GPoint(9, 2);
		
			spotIconActive = new GIcon(spotIcon);
			spotIconActive.image = cfgSpotIconActiveStr;
			
			spotIconWaiting = new GIcon(spotIcon);
			spotIconWaiting.image = cfgSpotIconWaitingStr;
		
			spotIconMulti = new GIcon();
			spotIconMulti.image = cfgSpotIconMultiStr;
			spotIconMulti.shadow = null;
			spotIconMulti.iconSize = new GSize(30, 35);
			spotIconMulti.iconAnchor = new GPoint(15, 18);
			spotIconMulti.infoWindowAnchor = new GPoint(13, 16);
		
			clusterer.icon = spotIconMulti;
			
			querystring = "";
			
			
			if (GET["text"] != undefined) {
				querystring += "text=" + GET["text"];
				if (GET["tags"] != undefined) {
					querystring += "&tags=" + GET["tags"];
				}
			}
			
		
			GDownloadUrl("spots.xml.php?" + querystring, function(data, responseCode) {
				var xml = GXml.parse(data);
				var spots = xml.documentElement.getElementsByTagName("spot");
				for (var i = 0; i < spots.length; i++) {
					var point = new GLatLng(parseFloat(spots[i].getAttribute("lat")), parseFloat(spots[i].getAttribute("lng")));
					var title = spots[i].getAttribute("title");
					var options = {};
					
					
					if (GET["spotID"] == spots[i].getAttribute("id")) {
						options.icon = spotIconActive;
						options.title = title;
					} else if (parseInt(spots[i].getAttribute("state")) == 1) {
						options.icon = spotIconWaiting;
						options.title = "Awaiting approval: " + title;
					} else {
						options.icon = spotIcon;
						options.title = title;
					}
					
					var spot = new GMarker(point, options);
					spot.sid = spots[i].getAttribute("id");
					//spot.description = title;
					
					if (parseInt(spots[i].getAttribute("state")) > 1 || mapMode == "spotEdit") {
						clusterer.AddMarker(spot, title);
						mapMarkers.push(spot);
					}
					
					if (GET["spotID"] == spot.sid) {
						clusterer.AddMarker(spot, title);
						mapMarkers.push(spot);
						spotZoom = (zoom < 14) ? 14 : zoom;
						map.setCenter(point, spotZoom);
						//spotToggleIcon(spot, spotIconActive);
						//viewSpot(spot, spot.sid);
					}
				}
			});
			
			
			
			if (mapMode == "spotEdit") {
				map.disableDragging();
				mapElement.style.cursor = "crosshair";
				eventHandlers.addSpotZoom = GEvent.addListener(map, "zoomend", function(oldLevelInt, newLevelInt) {
					if (newLevelInt < 15) {
						map.zoomIn();
					}
				});
				
				eventHandlers.addSpotClick = GEvent.addListener(map, "click", function(spot, point) {
					
					if (spot) {
						if (spot != mainVariables.newSpot) {
							setMessage("Du får inte skapa en ny spot så nära en gammal", "w");
						}
						//point = spot.getPoint();
						//document.getElementById("spotPoint").value = point.lat() + "|" + point.lng();
					} else {
						mainVariables.currentNewSpotPoint = point;
						document.getElementById("spotPoint").value = point.lat() + "|" + point.lng();
						
						if (mainVariables.newSpot) {
							map.removeOverlay(mainVariables.newSpot);
						}
						
						var options = {};
						options.icon = spotIconActive;
						mainVariables.newSpot = new GMarker(point, options);
						map.addOverlay(mainVariables.newSpot);
						
						// Check if this new point is valid
						GDownloadUrl("spotCheck.txt.php?mode=check&lat=" + point.lat() + "&lng=" + point.lng(), function(data, responseCode) {
							if (responseCode == 200) {
								if (data == "OK") {
									clearMessage("w");
								} else {
									setMessage(data, "w");
								}
							}
						});
					}
				});
			} else {
				mapDefaultBehaviour();
			}
		}
	}
}

