//*******************************************************************************
// opsi.js
//*******************************************************************************

   var zoom = 4;
   var zoomgrenze = 7;  // ab diesem Zoomlevel große Icons anzeigen
   var fonts = "arial, verdana, helvetica, sans-serif";
   var stdstyle = 'font-family:'+fonts+';font-weight:bold;color:#086b29';

   var anz_all = myDB.data.length;
   var myData  = new Array;    // array von Location-Objekten, erzeugt aus myDB.data
   var addNewPresent = false;  // NewEntry vorhanden? (Wichtig für Zoomwechsel)
   
   if (GBrowserIsCompatible()) {

      var opsiIcon = new GIcon();
      opsiIcon.image = 'opsi_trans.gif';
      opsiIcon.iconSize = new GSize(17,17);
      opsiIcon.iconAnchor = new GPoint(8,8);
      opsiIcon.infoWindowAnchor = new GPoint(8,8);

      var opsiLargeIcon = new GIcon();
      opsiLargeIcon.image = 'opsi_large.gif';
      opsiLargeIcon.iconSize = new GSize(33,34);
      opsiLargeIcon.iconAnchor = new GPoint(17,17);
      opsiLargeIcon.infoWindowAnchor = new GPoint(25,5);

      var opsiNewIcon = new GIcon();
      opsiNewIcon.image = 'opsi_new.gif';
      opsiNewIcon.iconSize = new GSize(33,34);
      opsiNewIcon.iconAnchor = new GPoint(17,17);
      opsiNewIcon.infoWindowAnchor = new GPoint(25,5);
   }
   var map;
   var myMapClickEventListener = null;
   var newLocation =  null;
   
   
   //****************************************************************************
   // Konstruktor fuer neues Location-Objekt 
   //****************************************************************************
   function Location (index, name, desc, link, lat, lng, show, status) {
      
      this.index  = index;
      this.name   = name;
      this.desc   = desc;      
      this.link   = link;      
      this.lat    = lat;
      this.lng    = lng;
      this.show   = show;
      this.status = status; // 0=unverändert, 1=changed, 2=neu
      this.info   = '';

      var dragFlag = false;
      var showIcon = opsiIcon;
      if (status == 2) {
         dragFlag = true;
         showIcon = opsiNewIcon;
      }
      
      this.GLatLng = new GLatLng (lat,lng);
      this.GMarker = new GMarker (this.GLatLng, {
            icon:      showIcon,  // Icon 
            draggable: dragFlag,  // Marker verschiebbar?
            bouncy:    false,     // hüpft beim loslassen? 
            title:     name       // Hint bei Mausover
      });
      showIcon = opsiLargeIcon;
      this.GMarker2 = new GMarker (this.GLatLng, {
            icon:      showIcon,  // Icon 
            draggable: dragFlag,  // Marker verschiebbar?
            bouncy:    false,     // hüpft beim loslassen? 
            title:     name       // Hint bei Mausover
      });

      this.setInfo = function () {
         var lat = Math.round(this.lat*100000)/100000;
         var lng = Math.round(this.lng*100000)/100000;
         this.info = 'new opsi location at:<br>'
            + 'lat='+ lat +'&deg;<br>'
            + 'lng='+ lng +'&deg<br><br>'
            + '<b>zoom in</b> to street level <br>and <b>move position</b> (drag&amp;drop)<br><br>then '
            + '<a href="mai'+'lto'+':opsimap'+'@'+'uib.de?'+'subject=Add new opsi location&amp;body='
            + '- Please fill in your company information to be shown on the map info -%0A'
            + '%0A'
            + 'name of company :%0A'
            + 'address (street,ZIP,town) :%0A'
            + 'website :%0A%0A'
            + 'This entry is to be added to the opsi locations%0A'
            + 'lat = '+ lat +'%0A'
            + 'lng = '+ lng +'%0A%0A'
            + '"><u>send email</u></a> and fill in form<br><br>';
      }
      
      this.clickListener = GEvent.addListener (this.GMarker, 'click', function() {
         this.openInfoWindowHtml ('<br><font style="'+stdstyle+'">'+ desc + '<br>' 
            + (link != '' ? ('<br><a href="'+link+'" target="_blank">'+link+'</a>') : '')
            + '</font><br><br>');
      });
      this.clickListener = GEvent.addListener (this.GMarker2, 'click', function() {
         this.openInfoWindowHtml ('<br><font style="'+stdstyle+'">'+ desc + '<br>' 
            + (link != '' ? ('<br><a href="'+link+'" target="_blank">'+link+'</a>') : '')
            + '</font><br><br>');
      });
            
      if (dragFlag) {
         this.dragstartListener = GEvent.addListener (this.GMarker, 'dragstart', function() {
            this.closeInfoWindow();
         });
         this.dragendListener = GEvent.addListener (this.GMarker, 'dragend', function() {
            newLocation.GLatLng = this.getLatLng();
            newLocation.lat     = this.getLatLng().lat();
            newLocation.lng     = this.getLatLng().lng();
            newLocation.setInfo();
            this.openInfoWindowHtml(newLocation.info);      
         });
      }
   }
   //****************************************************************************
   
   //*****************************************************************
   function load () {      
      if (GBrowserIsCompatible()) {
         
         map = new GMap2(document.getElementById("map"));

         // Locations-Array aus myDB.data erzeugen
         for (var i=0; i<anz_all; i++) {
            myData[i] = new Location(i, myDB.data[i].name, myDB.data[i].desc, myDB.data[i].link, 
                              myDB.data[i].lat, myDB.data[i].lng, myDB.data[i].show, 0);
         }
         // map.setCenter(myData[0].GLatLng, zoom);
         moveto_to ("Europe");
         map.addControl (new GLargeMapControl());
         map.addControl (new GScaleControl());
         map.addControl (new GMapTypeControl());
         map.enableScrollWheelZoom();
         
         // Location-Marker anzeigen
         for (var i=0; i<anz_all; i++) {
            if (myData[i].show)
               map.addOverlay (myData[i].GMarker);
         }
         // alert ('Anzahl = '+anz_all);
         
         // Event-Listener für Zoom-Änderung installieren
         GEvent.addListener(map, "zoomend", function(oldzoom,newzoom) {
            // alert ("zoom="+newzoom);
            adaptIconSizes(oldzoom,newzoom);
         });
      }
   }
   //*****************************************************************
   
   function adaptIconSizes(oldzoom,newzoom) {
      
      if (   (oldzoom <  zoomgrenze && newzoom >= zoomgrenze)
          || (oldzoom >= zoomgrenze && newzoom <  zoomgrenze)
      ) 
      {
         map.clearOverlays(); 
                  
         if (oldzoom < zoomgrenze && newzoom >= zoomgrenze) {
            // große Location-Marker anzeigen
            for (var i=0; i<anz_all; i++) {
               if (myData[i].show)
                  map.addOverlay (myData[i].GMarker2);
            }
         } else {
            // kleine Location-Marker anzeigen
            for (var i=0; i<anz_all; i++) {
               if (myData[i].show)
                  map.addOverlay (myData[i].GMarker);
            }
         }
         
         // Marker Neueintrag restaurieren
         if (addNewPresent) {
            map.addOverlay (newLocation.GMarker);
            newLocation.GMarker.openInfoWindowHtml (newLocation.info);
         }
         
      }
   }
   
   
   //*****************************************************************
   function add_entry() {
      if (newLocation == null) {
         newLocation = new Location (-1, 'new opsi location', 'location address', 
            'http://new.location.de', 
            map.getCenter().lat(), map.getCenter().lng(), 
            1, 2);
         map.addOverlay (newLocation.GMarker);
      }
      else {
         newLocation.GMarker.setLatLng(map.getCenter());
      }
   
      newLocation.setInfo();
      newLocation.clickListener = GEvent.addListener (newLocation.GMarker, 'click', 
         function() {
            newLocation.GMarker.openInfoWindowHtml (newLocation.info);
         }
      );
      newLocation.GMarker.openInfoWindowHtml (newLocation.info);
      
      addNewPresent = true;
   }
   //*****************************************************************

   // Karte positionieren
   function moveto_to (whereto) {
      switch (whereto) {
         case "Europe":
            // map.setCenter (myData[0].GLatLng, zoom);
            map.setCenter (new GLatLng (51, 12), zoom);
            break;
         case "America":
            map.setCenter (new GLatLng (36, -93), 3);
            break;
         case "Asia":
            map.setCenter (new GLatLng (25, 80), 3);
            break;
         case "Africa":
            map.setCenter (new GLatLng (16, 20), 3);
            break;
      }
   }

//*******************************************************************************
//*******************************************************************************
