/**
  *
  *  HM.GooglemapsMethods
  *
  *  Copyright 2009 Holiday Media
  *  Author: Sales Bansie <dev@holiday.nl>
  *  Version: 0.1
  *
  **/
  
HM.GooglemapsMethods = Class.create({
  
  addMarker: function() {
    var data   = arguments[0],
        clearr = arguments[1] || true;
        
    if(clearr) this.removeMarkers();
        
    this._addMarker(data);
  },
  
  addMarkers: function() {
    var data   = arguments[0] || [],
        clearr = arguments[1] || true;
    
    if(clearr) this.removeMarkers();
    
    $A(data).each(function(el) {
      this._addMarker(el); 
    }.bind(this));
  },
  
  getMap: function() {
    return this.Map;
  },
  
  getProjection: function() {
    return this.projection;
  },
  
  getMarkers: function() {
    return this.markers;
  },
  
  getMarker: function() {
    var id = arguments[0];
    return this.marker[id] && this.marker[id];
  },

  hideMarker: function() {
    var id = arguments[0];
    this.marker[id] && this.marker[id].hide();
  },
  
  hideMarkers: function() {
    this.markers.invoke('hide');
  },
  
  showMarker: function() {
    var id = arguments[0];
    this.marker[id] && this.marker[id].show();
  },
  
  showMarkers: function() {
    this.markers.invoke('show');
  },
  
  removeMarker: function() {
    this.marker[id] && this.marker[id].remove();
  },
  
  removeMarkers: function() {
    this.markers.invoke('remove');
  },
  
  clearOverlays: function() {
    this.markers  = [];
    this.marker   = {};
    this.Gmap.clearOverlays();
  },
  
  retrieveAndAddHotspots: function() {
    var param  = arguments[0] || {};
    var clearr = arguments[1] || false;
    this.retrieveAndAddMarkers(param,clearr);
  },
  
  retrieveAndAddMarkers: function() {
    var param   = arguments[0] || {};
    var clearr  = arguments[1] || false;
    var URI     = this.options.hotspotsURI;
    new Ajax.Request(URI, {  
    	method          : 'post',   
    	parameters      : param,
      requestHeaders  : { Accept: "application/json" },
      evalJSON        : "force",
    	onSuccess       : function(request) {     
    		try{
    			var json = request.responseJSON;  
          if(json.result != 1) {
            console.log(json.error);
          }else {
            this.addMarkers(json.data,clearr);
          }
    		}catch(e){
          console.log(e);
        }
    	}.bind(this)
    }); 
  },
  
  retrieveHotspots: function() {
    var param   = arguments[0] || {};
    var clearr  = arguments[1] || false;
    var URI     = this.options.hotspotsURI;
    new Ajax.Request(URI, {  
    	method          : 'post',   
    	parameters      : param,
      requestHeaders  : { Accept: "application/json" },
      evalJSON        : "force",
    	onSuccess       : function(request) {     
    		try{
    			var json = request.responseJSON;  
          if(json.result != 1) {
            console.log(json.error);
            return json;
          }else {
            return json;
          }
    		}catch(e){
          console.log(e);
          return {};
        }
    	}.bind(this)
    }); 
  },
 
  _addMarker: function() {
    var data    = arguments[0] || {},
        point   = this.getPoint(data),
        options = {};
    
    options.title = data.title  ? data.title : null;
    options.icon  = data.icon   ? this._setIcon(data.icon) : null;
    
    this.marker[data.id] = new GMarker(point,(options));
    
    //this.marker[data.id] = new GMarker(point,(data.icon ? this._setIcon(data.icon) : null));
    //if(data.title) this.marker[data.id].title = data.title; 
    this.Gmap.addOverlay(this.marker[data.id]);
    this.markers.push(this.marker[data.id]);
    
    if(this.options.onClick) {
      GEvent.addListener(this.marker[data.id], "click", function() {
        this.marker[data.id].openInfoWindowHtml(this._myMap_genHTML(data.html));
      }.bind(this));
    }
  },
  
  getPoint: function() {
    var data  = arguments[0] || {};
    return new GLatLng(data.x, data.y);
  },
  
  _myMap_genHTML: function(content) {
    var html = '<div class="GMapLocationPopup">';
        html += content;
        html += '</div>';
    return html;
  },
  
  _setIcon: function() {
    var myIcon  = arguments[0]  || {},
        width   = myIcon.width  || 20,
        height  = myIcon.height || 20;
   
    var icon                  = new GIcon();       
        icon.iconSize         = new GSize(width, height);
        icon.iconAnchor       = new GPoint(10, 10);
        icon.infoWindowAnchor = new GPoint(10, 10);
        icon.image            = myIcon.uri;
        
    return icon;
  },
  
  nop: function() {}
  
});

/**
  *
  *  HM.MyMap
  *
  *  Copyright 2009 Holiday Media
  *  Author: Sales Bansie <dev@holiday.nl>
  *  Version: 0.2
  *
  **/

HM.MyMap = Class.create(HM.GooglemapsMethods,{

  initialize: function(){
    this.Gmap    = arguments[0];
    this.options = Object.extend({
      'name'        : 'Holidaymedia',
      'shortName'   : 'HM',
      'tileURI'     : "tiles/",
      'imgType'     : '.jpg',
      'maxZoom'     : 4,
      'minZoom'     : 1,
      'customUI'    : this._defaultUI(),
      'copyrights'  : '&copy;2009 www.holidaymedia.nl',
      'onClick'     : true,
      'hotspotsURI' : '/hotspots/index.lp'
  
    }, arguments[1] || {});
    
    this.markers = [];
    this.marker  = {};
    
    this.myMap      = this._addMap();
    this.projection = this.myMap.getProjection();
    
    this.Gmap.addMapType(this.myMap );
    this.Gmap.setUI(this.options.customUI);
  },
  
  // This method overrides getPoint in parent class
  // Accept pixel input, while parent accept lat,long
  getPoint: function() {
    var data  = arguments[0] || {};
    return this.getProjection().fromPixelToLatLng({
      x: data.x,
      y: data.y
    }, data.zoom || this.options.maxZoom, true);
  },
  
  _addMap: function() {
    var copyrights = new GCopyrightCollection(this.options.name);
        copyrights.addCopyright(new GCopyright(
          'gmaps-hm',
          new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
          0,
          this.options.copyrights
        ));
    
    var tileset  = new GTileLayer(copyrights, 0, this.options.maxZoom);
        tileset.getTileUrl = function(tile, zoom){
          return this.options.tileURI +zoom+"_"+tile.x+"_"+tile.y+this.options.imgType;
        }.bind(this);
        tileset.isPng = function() { 
          return false; 
        }
        tileset.getOpacity = function() { 
          return 1.0; 
        }

    var maptype = new GMapType(
      [tileset],
      new GMercatorProjection(18),
      this.options.name, {
        shortName     : this.options.shortName,
        maxResolution : this.options.maxZoom,
        minResolution : this.options.minZoom,
        tileSize      : this.options.tileSize || 256
    });

    return maptype; 
  },
  
  _defaultUI: function() {
    var customUI                  = this.Gmap.getDefaultUI();
      customUI.maptypes.normal    = false;
      customUI.maptypes.physical  = false;
      customUI.maptypes.hybrid    = false;
      customUI.maptypes.satellite = false;
      
    return customUI;
  },
  
  nop: function() {}
  
});