﻿// ELabel.js 
//
//   This Javascript is provided by Mike Williams
//   Blackpool Community Church Javascript Team
//   http://www.commchurch.freeserve.co.uk/   
//   http://econym.googlepages.com/index.htm
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/
//
// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap
// version 1.1      Works with GMarkerManager in v2.67, v2.68, v2.69, v2.70 and v2.71
// version 1.2      Works with GMarkerManager in v2.72, v2.73, v2.74 and v2.75
// version 1.3      add .isHidden()
// version 1.4      permit .hide and .show to be used before addOverlay()
// version 1.5      fix positioning bug while label is hidden
// version 1.6      added .supportsHide()
// version 1.7      fix .supportsHide()


      function ELabel(point, html, classname, pixelOffset, percentOpacity, overlap) {
        // Mandatory parameters
        this.point = point;
        this.html = html;
        
        // Optional parameters
        this.classname = classname||"";
        this.pixelOffset = pixelOffset||new GSize(0,0);
        if (percentOpacity) {
          if(percentOpacity<0){percentOpacity=0;}
          if(percentOpacity>100){percentOpacity=100;}
        }        
        this.percentOpacity = percentOpacity;
        this.overlap=overlap||false;
        this.hidden = false;
      } 
      
      ELabel.prototype = new GOverlay();

      ELabel.prototype.initialize = function(map) {
        var div = document.createElement("div");
        div.style.position = "absolute";
        div.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);
        this.map_ = map;
        this.div_ = div;
        if (this.percentOpacity) {        
          if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
          if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
          if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
          if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
        }
        if (this.overlap) {
          var z = GOverlay.getZIndex(this.point.lat());
          this.div_.style.zIndex = z;
        }
        if (this.hidden) {
          this.hide();
        }
      }

      ELabel.prototype.remove = function() {
        this.div_.parentNode.removeChild(this.div_);
      }

      ELabel.prototype.copy = function() {
        return new ELabel(this.point, this.html, this.classname, this.pixelOffset, this.percentOpacity, this.overlap);
      }

      ELabel.prototype.redraw = function(force) {
        var p = this.map_.fromLatLngToDivPixel(this.point);
        var h = parseInt(this.div_.clientHeight);
        this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
        this.div_.style.top = (p.y +this.pixelOffset.height - h) + "px";
      }

      ELabel.prototype.show = function() {
        if (this.div_) {
          this.div_.style.display="";
          this.redraw();
        }
        this.hidden = false;
      }
      
      ELabel.prototype.hide = function() {
        if (this.div_) {
          this.div_.style.display="none";
        }
        this.hidden = true;
      }
      
      ELabel.prototype.isHidden = function() {
        return this.hidden;
      }
      
      ELabel.prototype.supportsHide = function() {
        return true;
      }

      ELabel.prototype.setContents = function(html) {
        this.html = html;
        this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
        this.redraw(true);
      }
      
      ELabel.prototype.setPoint = function(point) {
        this.point = point;
        if (this.overlap) {
          var z = GOverlay.getZIndex(this.point.lat());
          this.div_.style.zIndex = z;
        }
        this.redraw(true);
      }
      
      ELabel.prototype.setOpacity = function(percentOpacity) {
        if (percentOpacity) {
          if(percentOpacity<0){percentOpacity=0;}
          if(percentOpacity>100){percentOpacity=100;}
        }        
        this.percentOpacity = percentOpacity;
        if (this.percentOpacity) {        
          if(typeof(this.div_.style.filter)=='string'){this.div_.style.filter='alpha(opacity:'+this.percentOpacity+')';}
          if(typeof(this.div_.style.KHTMLOpacity)=='string'){this.div_.style.KHTMLOpacity=this.percentOpacity/100;}
          if(typeof(this.div_.style.MozOpacity)=='string'){this.div_.style.MozOpacity=this.percentOpacity/100;}
          if(typeof(this.div_.style.opacity)=='string'){this.div_.style.opacity=this.percentOpacity/100;}
        }
      }

      ELabel.prototype.getPoint = function() {
        return this.point;
      }
      ELabel.prototype.U = function() {
        return this.point;
      }
      ELabel.prototype.V = function() {
        return this.point;
      }
      ELabel.prototype.W = function() {
        return this.point;
      }
      ELabel.prototype.X = function() {
        return this.point;
      }
      ELabel.prototype.Y = function() {
        return this.point;
      }
      ELabel.prototype.Z = function() {
        return this.point;
      }


/// The rest is Google map code for Travel Destination

function loadMap(points, points_description, googlelarge, alwayspoint) {
  if (GBrowserIsCompatible()) {  
      initMap(points, points_description, googlelarge, alwayspoint, 12);
      window.onunload = GUnload;
  }
}

function loadMapZoom(points, points_description, googlelarge, alwayspoint, minimumzoom) {
  if (GBrowserIsCompatible()) {  
      initMap(points, points_description, googlelarge, alwayspoint, minimumzoom);
      window.onunload = GUnload;
  }
}

function initMap(points, points_description, googlelarge, alwayspoint, minimumzoom)
{
      var bnd = new GLatLngBounds()
      for (var i=0; i < points.length; i++)
      {
        bnd.extend(points[i]);
      }      
      bnd.extend(alwayspoint);
      
      var map = new GMap2(document.getElementById("bookingmap"));
      
      if (googlelarge) {
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      }
      
      var zoom = map.getBoundsZoomLevel(bnd)
      if (zoom > minimumzoom) {
        zoom = minimumzoom;
      }
        
      map.setCenter(bnd.getCenter(), zoom );

      var baseIcon = new GIcon();
      baseIcon.iconSize=new GSize(14,14);
      baseIcon.shadowSize=new GSize(29,16);
      baseIcon.iconAnchor=new GPoint(7,7);
      baseIcon.infoWindowAnchor=new GPoint(16,0);      
      
      var icons = new Array(
        new GIcon(baseIcon, "/Public/scripts/icon0.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon1.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon2.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon3.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon4.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon5.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon6.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon7.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon8.gif", null, "/Public/scripts/shadow.png"),
        new GIcon(baseIcon, "/Public/scripts/icon9.gif", null, "/Public/scripts/shadow.png")
      );
      
      for (var i=0; i < points.length; i++)
      {
        if (points_description != null && typeof points_description == "object")
        {
            if (points_description[i].substring(0,2) == "**")
            {
                  map.addOverlay(createLabel(points[i], points_description[i].substring(2)) );
            } else {
                if (googlelarge) {
                    map.addOverlay(createMarker(points[i],points_description[i], icons[i % icons.length] ));
                } else {
                    map.addOverlay(createMarker(points[i],'', icons[i % icons.length] ));
                }
            }
        } else {
            map.addOverlay(createMarker(points[i],'', icons[i % icons.length] ));
        }
      }
}

  function createLabel(point, description)
  {
     var label = new ELabel(point, description, "labelstyle");
     return label
  }


  function createMarker(point,html,icon) {
    var marker = new GMarker(point,icon);
    if (html.length > 0) {
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    }
    return marker;
  }    
