﻿

var homeIcon = new GIcon();
homeIcon.image = "http://www.jigsawconferences.co.uk/images/venue_icon.png";
homeIcon.iconSize = new GSize(44, 42);
homeIcon.shadow = null;
homeIcon.iconAnchor = new GPoint(22, 21);
homeIcon.infoWindowAnchor = new GPoint(22, 1);

var hotelIcon = new GIcon();
hotelIcon.image = "http://www.jigsawconferences.co.uk/images/bed_icon.png";
hotelIcon.iconSize = new GSize(44, 42);
hotelIcon.shadow = null;
hotelIcon.iconAnchor = new GPoint(22, 21);
hotelIcon.infoWindowAnchor = new GPoint(22, 1);

var points = new Array();  

function initialize() {
    if (GBrowserIsCompatible()) {
    map = new GMap2($("google-map"));
    map.setCenter(new GLatLng(0, 0), 13);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    }
}


var JigsawMarker = new Class({
    options : {
        name: "",
        postcode: "",
        icon: hotelIcon,
        center: false
    },
    
    initialize: function(options){
        this.setOptions(options);
        this.name = options.name;
        this.postcode = options.postcode;
        this.icon = options.icon;
        this.center = options.center;
        this.localSearch = new GlocalSearch();
        this.showAddress();
        
        
    },


    showAddress: function() {
      
      if (geocoder) {
        this.usePointFromPostcode();
        
      }
    },
    
    gotPoint: function(_point) {
        
        if (!_point) {
                $("google-map").setStyle('display', 'none');
            } else {
               if (this.center) {
                    map.setCenter(_point, 13);
              }
              this.point = _point;
              this.addMarkerAtPoint();
            }
    
    
    },
    
    addMarkerAtPoint: function () {
            
            this.marker = new GMarker(this.point, this.icon);
            
            
            map.addOverlay(this.marker);
            if (this.center) {
                this.marker.openInfoWindowHtml(this.name);
            }
            //this.showInfoWindow();
            points.push(this.point);
            if (points.length == 2) {
                setBoundsToMarkers();
            };
            //addNextVenue();
    },
    
    usePointFromPostcode: function () {
      
      this.localSearch.setSearchCompleteCallback(null,
        function() {
          
          if (this.localSearch.results[0]) {    
            var resultLat = this.localSearch.results[0].lat;
            var resultLng = this.localSearch.results[0].lng;
            var point = new GLatLng(resultLat,resultLng);
            
            this.gotPoint(point);
          }else{
            alert("Postcode not found!");
          }
        }.bind(this));  
    
      this.localSearch.execute(this.postcode + ", UK");
    }

});
JigsawMarker.implement(new Options);


function setBoundsToMarkers() {
        var bounds = new GLatLngBounds();
        
        for (i = 0; i < points.length; i++) {
            bounds.extend(points[i]);
        
        
        }
        
        
        
        
        
        var center = bounds.getCenter();
        

        map.setZoom(map.getBoundsZoomLevel(bounds));

        map.setCenter(center); 
       
        
   
   }
        

