var map = '';
var zoom = 13;
var marker = '';
var center = '';
var marker = new Array();
var shortlist = new Array();
var icons= new Array();
var mainMarker = null;



function centerOnMarker(id,typ){
//	if(marker[id])map.setCenter(marker[id]['marker'].getPoint());
	
	if(mainMarker == null)marker[id]['marker'].openInfoWindowHtml(''+marker[id]['html']+'');
	

  //  for ( j in marker ) {
	//	  if((marker[j]['typ'])==typ) map.removeOverlay(marker[j]['marker']);
	//	}
		
//		map.addOverlay(marker[id]['marker']);
}


	
	
		function wheelevent(e) {
			if (!e){ e = window.event }
			if (e.preventDefault){ e.preventDefault() }
			e.returnValue = false;
		}


function load(lat, lon) {
  if (GBrowserIsCompatible() && document.getElementById("map")) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
	map.enableScrollWheelZoom();
    center = new GLatLng(lat,lon);
    map.setCenter(center); 
    map.setZoom(zoom);
 	
 	/***********************/
GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
		map.getContainer().onmousewheel = wheelevent; 


    if(mainMarker!=null){
	  map.addOverlay(mainMarker['marker']);	
	  
	    for ( j in shortlist ) {
	       	map.addOverlay(shortlist[j]);
	   	}
    	for ( j in marker ) {

        	if(marker[j]['typ']!=mainMarker['typ'])map.addOverlay(marker[j]['marker']);

    	}
//	  mainMarker['marker'].openInfoWindowHtml(''+mainMarker['html']+'');
	  map.setCenter(mainMarker['marker'].getPoint());
	  if(mainMarker['openWin']==1) mainMarker['marker'].openInfoWindowHtml(mainMarker['html']);
    }
    else{
    	var bounds = new GLatLngBounds();
    	for ( j in marker ) {
  	//	if(marker[j]['typ'] != mainMarker['typ'] ){
        	map.addOverlay(marker[j]['marker']);
    		bounds.extend(marker[j]['marker'].getPoint());
    		if(marker[j]['openWin']==1) marker[j]['marker'].openInfoWindowHtml(marker[j]['html']);

   // 	}
    	}

    	
    	
  /*  	map.setZoom(map.getBoundsZoomLevel(bounds));
   		map.setCenter(bounds.getCenter());*/
    }

   
  }
}
/*
function load_navi() {
  if (GBrowserIsCompatible()) {
	G_NORMAL_MAP.getTileLayers()[0].getOpacity = function () {return 0.4;};
    G_SATELLITE_MAP.getTileLayers()[0].getOpacity = function () {return 0.4;};
    G_HYBRID_MAP.getTileLayers()[0].getOpacity = function () {return 0.4;};
    G_HYBRID_MAP.getTileLayers()[1].getOpacity = function () {return 0.4;};
    map = new GMap2(document.getElementById("map_navi"));
    map.addControl(new GSmallMapControl());
	document.getElementById("map_navi").style.backgroundColor="#F4F4F4";
	
	lat = 21.289374;
	lon = -23.96875;
    center = new GLatLng(lat,lon);
    map.setCenter(center); 
	alert('typ');
	var bounds = new GLatLngBounds();
    for ( j in marker ) {
      if(marker[j]['typ'] != marker[1]['typ'] && j != 1){
  		  map.addOverlay(marker[j]);
  		  bounds.extend(marker[j].getPoint());
  		}
		
    }
   map.setZoom(map.getBoundsZoomLevel(bounds));
   map.setCenter(bounds.getCenter());
  }
}
*/

function createMarker(lat, lon,typID, html,openWin) {
	
  if(typID!=1)var marker = new GMarker(new GLatLng(lat,lon),{icon:createNewIcon("/img/map/"+typID+".gif","main")});
  else var marker = new GMarker(new GLatLng(lat,lon),{icon:createNewIcon("/img/map/standard.gif","standard")});
  if(openWin>-1)GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(''+html+'');
  });
  return marker;
}



function initMarker(lat,lon,id,typID,tooltip,titel,text,bild,city,openWin){	
  marker[id] = new Array();
  link='#';
	if(typID.charAt(0)=="E"){
    linktag='<div style="background:url(/img/new/'+typID+'.gif) no-repeat;height:25px;"><a href="'+city+'/event.php?event='+id+'" style="padding-left:30px;font-weight:bold;color:#84B3D8;text-decoration:underline">'+titel+'</a></div>';
    marker[id]['typ']='E';
    icon='<!--img src="/img/bg_icon_ubivo_event_small.GIF"-->';
    link=city+'/event.php?event='+id;
  }
	else if(typID.charAt(0)=="I"){
    linktag='<div style="background:url(/img/new/'+typID+'.gif) no-repeat;height:25px;"><a href="'+city+'/insidedet.php?event='+id+'" style="padding-left:30px;font-weight:bold;color:#99B0C3;text-decoration:underline">'+titel+'</a></div>';
    marker[id]['typ']='I';
    icon='<!--img src="img/bg_icon_ubivo_inside_small.GIF" -->';
    link=city+'/insidedet.php?event='+id;
  }
	else if(typID.charAt(0)=="H"){
	  marker[id]['typ']='H';
      linktag='<div style="background:url(/img/new/'+typID+'.gif) no-repeat;height:25px;"><a href="'+city+'/hotel.php?event='+id+'" style="padding-left:30px;font-weight:bold;color:#427298;text-decoration:underline">'+titel+'</a></div>';
  	  icon='<!--img src="img/bg_iicon_ubivo_vote_small.GIF"-->';
      link=city+'/hotel.php?event='+id;
  }
	else if(typID.charAt(0)=="V"){
	  marker[id]['typ']='H';
    linktag='<img src="img/bg_icon_ubivo_hotel.gif"><div style="background-color: RGB(255,88,0); padding:3px;margin-bottom:6px;"><a style="font-weight:bold;color:#FFFFFF;">'+titel+'</a></div>';
  }
	else linktag='<span style="font-weight:bold">'+titel+'</span>';
	
  imagetag = '';
  if(bild !='' && bild !=0){
    if(typID.charAt(0)=="V")imagetag='<img src="'+bild+'" style="float:left;width:60px;height:60px;margin-right:10px;outline:1px solid invert; padding:2px; ">';
    else imagetag='<img src="http://www.ubivo.de/media/rsdb/'+bild+'" style="float:left;width:200px;height:150px;margin-right:10px;outline:1px solid #DEDEDE ; padding:5px; ">';
  }
	
	if(typID == 'Emain' || typID == 'Imain' || typID == 'Hmain' || typID == 'Vmain'){
    typID = 'main';
  }

  	splitText=text.split(":");
  	if(splitText.length == 2){
  		text2='<p style="font-weight:bold!important;">'+splitText[0]+':</p>'+splitText[1];
  	}
  	else{
  		text2=text;
  	}
  	typID='M'+typID;
	html =icon+'<div style="width:340px;color:#636464;line-height:1.4;">'+linktag+'<a href="'+link+'">'+imagetag+'</a><div>'+text2+' <a href="'+link+'" style="color:#84B3D8;font-weight:bold;text-decoration:underline">mehr</a></div></div>';
	marker[id]['html']=html;
	marker[id]['marker']=createMarker(lat,lon,typID,html,openWin);
	marker[id]['openWin']=openWin;
		
}

function initShortlistMarker(lat, lon, id){
	shortlist[id]=new GMarker(new GLatLng(lat,lon),{icon:createNewShortlistIcon("/img/map/map_"+id+".gif","shortlist"+id+"")});
	GEvent.addListener(shortlist[id], "mouseover", function() {
	    document.getElementById("shortlist"+id).style.backgroundColor='#BDCEE8';
	});
	GEvent.addListener(shortlist[id], "mouseout", function() {
	    document.getElementById("shortlist"+id).style.backgroundColor='#FFFFFF';
	});
}

function initMainMarker(lat,lon,id,typID,tooltip,titel,text,bild,openWin){	
  mainMarker = new Array();
	if(typID.charAt(0)=="E"){
    linktag='<div ><p style="font-weight:bold;padding-bottom:10px;">'+titel+'</p></div>';
    mainMarker['typ']='E';
  }
	else if(typID.charAt(0)=="I"){
	linktag='<div ><p style="font-weight:bold;padding-bottom:10px;">'+titel+'</p></div>';
    mainMarker['typ']='I';
  }
	else if(typID.charAt(0)=="H"){
	  mainMarker['typ']='H';
	  linktag='<div ><p style="font-weight:bold;padding-bottom:10px">'+titel+'</p></div>';
  }
	else if(typID.charAt(0)=="V"){
	 mainMarker['typ']='H';
    linktag='<div style="background-color: RGB(255,88,0); padding:3px;margin-bottom:6px;"><a style="font-weight:bold;color:#FFFFFF;">'+titel+'</a></div>';
  }
	else linktag='<span style="font-weight:bold">'+titel+'</span>';
	
  imagetag = '';
  if(bild !='' && bild !=0){
    if(typID.charAt(0)=="V")imagetag='<img src="'+bild+'" style="float:left;width:60px;height:60px;margin-right:10px;outline:1px solid invert; padding:2px; ">';
    else imagetag='<img src="http://www.ubivo.de/media/rsdb/'+bild+'" style="float:left;width:150px;margin-right:10px;outline:1px solid #DEDEDE ; padding:5px; margin-bottom:10px; ">';
  }
	
	if(typID == 'Emain' || typID == 'Imain' || typID == 'Hmain' || typID == 'Vmain'){
    typID = 'main';
  }

	typID='M'+typID;
	html ='<div style="width:170px;">'+linktag+''+imagetag+'<div style="width:160px;">'+text+'</div></div>';
	mainMarker['html']=html;
	mainMarker['marker']=createMarker(lat,lon,typID,html,openWin);
	mainMarker['openWin']=openWin;
	
	
	
		
}



function DMS(lat,latm,lats,lon,lonm,lons) {
  if (lat<0) {
    latm = -latm;
    lats = -lats;
  }
  if (lon<0) {
    lonm = -lonm;
    lons = -lons;
  }
  lat = lat + latm/60 + lats/3600;
  lon = lon + lonm/60 + lons/3600;
  return new GLatLng(lat,lon);

}

function setMapZoom(z){
  zoom=z;
}

function setMapCenter(lati,lon){
  center = new GLatLng(lati,lon);
}




function createNewIcon(iconName,index){
	var Icon = new GIcon();
   	Icon.image = ""+iconName;
   	Icon.iconSize = new GSize(37, 39);        	
   	Icon.shadowSize = new GSize(20, 20);
   	Icon.iconAnchor = new GPoint(19, 20);
   	Icon.infoWindowAnchor = new GPoint(30, 30);
	icons[index]= Icon;
	return Icon;
}

function createNewShortlistIcon(iconName,index){
	var Icon = new GIcon();
   	Icon.image = ""+iconName;
   	Icon.iconSize = new GSize(27, 18);        	
   	Icon.shadowSize = new GSize(27, 18);
   	Icon.iconAnchor = new GPoint(13, 8);
   	Icon.infoWindowAnchor = new GPoint(20, 15);
	icons[index]= Icon;
	return Icon;
}
