var map = null;
var poi = new Array();
var start_zoom_location = 9;
var start_zoom_widget = 8;
var start_center = null;
var detailCall = null;
var action = null;
var widgetMarker_ClickUid = null;

// create marker icon
var templateIcon = new GIcon();
templateIcon.shadow = 'http://'+window.location.host+'/fileadmin/templates/img/locations_shadow.png';
templateIcon.iconSize = new GSize(20,34);
templateIcon.shadowSize = new GSize(37,34);
templateIcon.iconAnchor = new GPoint(9,34);
templateIcon.infoWindowAnchor = new GPoint(9,2);
templateIcon.infoShadowAnchor = new GPoint(18,25);

//build XML object for MapData
if (window.XMLHttpRequest) {
  http = new XMLHttpRequest();
} 
else if (window.ActiveXObject) {
  try {
    http = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e) {
    try {
      http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {}
  } 
}

//build XML object for start information
if (window.XMLHttpRequest) {
  http_start = new XMLHttpRequest();
} 
else if (window.ActiveXObject) {
  try {
    http_start = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e) {
    try {
      http_start = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {}
  } 
}

//init function for map
function load(type){
	action = type;
	start_url = 'http://'+window.location.host+'/fileadmin/templates/scripts/locations_getIpLocatorXml.php';
	if (http_start != null) {
	  http_start.open("GET", start_url, true);
	  http_start.send(null);
	  http_start.onreadystatechange = function(){
			if (http_start.readyState == 4) {
				response_start = http_start.responseXML;
				startinfo = response_start.getElementsByTagName("IPLOCATION");
				start_lat = startinfo[0].getElementsByTagName("LATITUDE")[0].firstChild.nodeValue;
	      start_lng = startinfo[0].getElementsByTagName("LONGITUDE")[0].firstChild.nodeValue;
				start_city = startinfo[0].getElementsByTagName("CITY")[0].firstChild.nodeValue;
				start_center = new GLatLng(start_lat, start_lng);
				if(action=='widget'){
					map = new GMap2(document.getElementById('location_widgetMap'));
					map.setCenter(start_center ,start_zoom_widget);
					map.addControl(new GSmallMapControl()); //Small/Large als Alternative
					document.getElementById('location_widgetSearchbox').value = start_city;
					getXMLData();
				} else{
					map = new GMap2(document.getElementById('locations_map'));
					map.addControl(new GLargeMapControl()); //Small/Large als Alternative
  				map.addControl(new GMapTypeControl());
					if(document.getElementById('locations_widgetClickId')){
						widget_address = document.getElementById('locations_searchbox').value;
						widgetMarker_ClickUid = document.getElementById('locations_widgetClickId').value;
						geocode_searchPoint();
					}
					else{
						map.setCenter(start_center ,start_zoom_location);
						getXMLData();
					}
				}
			}
		};
	}
}

//geocode users searchpoint, center map and call xml function
function geocode_searchPoint(type){
	action = type;
	if(action=="add"){
		search_address = document.getElementById('locations_editbox').value;
		document.getElementById('locations_searchbox').value = 'Adresse';
	} else if(action=="widget"){
		search_address = document.getElementById('location_widgetSearchbox').value;
	} else{
		search_address = document.getElementById('locations_searchbox').value;
		if(document.getElementById('locations_editbox')){
			document.getElementById('locations_editbox').value = 'Adresse';
		}
	}
	if(search_address=='' || search_address=='Adresse'){
		alert('Bitte geben Sie eine Adresse ein, nach der Sie suchen wollen.');
	}
	else{
		geocoder = new GClientGeocoder();
    geocoder.getLatLng(search_address, function(point) {
      if (!point){
      	alert('Leider konnte die angegebene Adresse nicht gefunden werden.');
      }
      else{
				map.clearOverlays();
				map.setCenter(point ,start_zoom_location);
				if(action=="add"){
					addLocation(point);
				}
				else if(action=="widget"){
					getXMLData();
					map.setCenter(point ,start_zoom_widget);
				}
				else{
					getXMLData();
				}
      }
    });
	}
}

// build array for each poi
function buildDataArray(){
  if (http.readyState == 4) {
    response = http.responseXML;
    poiliste = response.getElementsByTagName("location");
    poi_anzahl = poiliste.length;
    if(poi_anzahl>0){
       for(var i=0; i < poi_anzahl; i++) {
        poi[i] = new Array();
        //for GMarker Objekt
        poi[i][0] = null;
        //lat lng of point
        tmp_lat = poiliste[i].getElementsByTagName("lat")[0].firstChild.nodeValue;
        tmp_lng = poiliste[i].getElementsByTagName("lng")[0].firstChild.nodeValue;
        poi[i][1] = new GLatLng(tmp_lat,tmp_lng);
				//uid of item
				poi[i][2] = poiliste[i].getElementsByTagName("id")[0].firstChild.nodeValue;
				//additional params
				poi[i][3] //katorie name
				poi[i][4] //kategorie_id
				poi[i][5] //name
				poi[i][6] //strasse
				poi[i][7] //plz
				poi[i][8] //ort
				poi[i][9] //email
				poi[i][10] //internet
				poi[i][11] //telefon
				poi[i][12] //bild
				poi[i][13] //beschreibung
				for (var j=0; j < poiliste[i].childNodes.length; j++) {
					with (poiliste[i].childNodes[j]) {
						if (nodeName == 'kategorie') {
            	poi[i][3] = firstChild.nodeValue;
            }
						else if (nodeName == 'kategorie_id') {
            	poi[i][4] = firstChild.nodeValue;
            }
						else if (nodeName == 'name') {
            	poi[i][5] = firstChild.nodeValue;
            }
						else if (nodeName == 'strasse') {
            	poi[i][6] = firstChild.nodeValue;
            }
						else if (nodeName == 'plz') {
            	poi[i][7] = firstChild.nodeValue;
            }
						else if (nodeName == 'ort') {
            	poi[i][8] = firstChild.nodeValue;
            }
						else if (nodeName == 'email') {
            	poi[i][9] = firstChild.nodeValue;
            }
						else if (nodeName == 'internet') {
            	poi[i][10] = firstChild.nodeValue;
            }
						else if (nodeName == 'telefon') {
            	poi[i][11] = firstChild.nodeValue;
            }
						else if (nodeName == 'bild') {
            	poi[i][12] = firstChild.nodeValue;
            }
						else if (nodeName == 'beschreibung') {
            	poi[i][13] = firstChild.nodeValue;
            }
					}
				}
        //create marker for each point and add to map
        poi[i][0] = createMarker(poi[i][1], poi[i][4], poi[i][5], i, poi[i][2]);
        map.addOverlay(poi[i][0]);
				if(widgetMarker_ClickUid==poi[i][2]){
					GEvent.trigger(poi[i][0], "click");
				}
      }  
    }
  }
}

// create marker object
function createMarker(latlng, category_id, name, idx, poi_uid) {
	var Icon = new GIcon(templateIcon);
	//vereinsheime
	if(category_id==2){
		Icon.image = 'http://'+window.location.host+'/fileadmin/templates/img/locations_verein.png';
	}
	//sportbar
	else if(category_id==3){
		Icon.image = 'http://'+window.location.host+'/fileadmin/templates/img/locations_sportbar.png';
	}
	//fussballwurst
	else if(category_id==4){
		Icon.image = 'http://'+window.location.host+'/fileadmin/templates/img/locations_wurst.png';
	}
	else{
		Icon.image = 'http://'+window.location.host+'/fileadmin/templates/img/locations_plaetze.png';
	}
	onMap = new GMarker(latlng, {icon: Icon, title: name});
	GEvent.addListener(onMap, "click", function() {
		if(action=='widget'){
			document.getElementById('widget_markeruid').value = poi_uid;
			document.location_widgetSearchform.submit();
		}
		else{
  		showPoiDetail(idx);
		}
  }); 
  return onMap;
}

// open infowindow for selected marker
function showPoiDetail(idx){
	if(!detailCall){
		url = window.location.href+'#locationdetail';
		detailCall = true;
	}	else{
		url = window.location.href;
	}
	info_cont = '<strong>'+poi[idx][5]+'</strong><br /><em>'+poi[idx][3]+'</em><br /><br /><a onclick="openDetailFrame('+poi[idx][2]+')" href="'+url+'">&raquo;&nbsp;mehr Details</a>';
	poi[idx][0].openInfoWindowHtml(info_cont);
}

