/**
// Copyright 2009 Q42 http://www.q42.nl 
// All rights reserved. Redistribution and use in source and binary forms, 
// with or without modification, are permitted.
// Version UtwenteMap 0.2, 27th Aug 2009, Q42
**/

var UTWENTE_LOCATIONS_URL = '/locationsfeed'; // can be absolute path too for easy cross-domain usage
var UTWENTE_CENTER = new GLatLng(52.238917284314844,6.853194236755371);
var UTWENTE_ZOOMLEVEL = 15;
var UTWENTE_MAP_VERSION = '6th Oct 2009, (c)q42';

var utwenteMapSettings = 
{
  'default':
  {
    linecolor:'#991111',
    linesize: 2,
    fillcolor:'#880000',
    lineopacity: 1,
    opacity: 0.6,

    mouseover:
    {
      color:'#ff3333',
      opacity: '1'
    }
  },
  
  'campus':
  {
    linecolor:'yellow',
    linesize: '3',
    fillcolor:null,
    lineopacity: 1,
    opacity: 0

  },  
  'dienst':
  {
    linecolor:'blue',
    linesize: 2,
    fillcolor:'blue',
    lineopacity: 1,
    opacity: 0.6,

    mouseover:
    {
      color:'#8888FF',
      opacity: 1
    }
  },
  'recreatie':
  {
    linecolor:'yellow',
    linesize: 2,
    fillcolor:'yellow',
    lineopacity: null,
    opacity: 0.6,

    mouseover:
    {
      color:'#8888FF',
      opacity:1
    }
  },
  'onderwijs':
  {
    linecolor:'red',
    linesize: 2,
    fillcolor:'#FF8888',
    lineopacity: 1,
    opacity: 0.6,

    mouseover:
    {
      color:'#FF8888',
      opacity:1
    }
  }  
}


var utwenteMap =
{
  elementId: null,
  gmap: null,
  settings: null,
  
  label1:null,
  label2:null,
  
  initialize : function(elementId, settings)
  {
  
    if (!GBrowserIsCompatible()) return;
    if (!document.getElementById(elementId)) { return; }

    // store local props
    this.elementId = elementId;
    this.settings = settings;

    // init map first
    var map = this.gmap = new GMap2(document.getElementById(elementId));
    map.setCenter(UTWENTE_CENTER);
    map.setZoom(UTWENTE_ZOOMLEVEL);
    map.setUIToDefault();
    
    map.setMapType(G_SATELLITE_MAP);
    
    map.disableContinuousZoom();
    map.disableScrollWheelZoom();

    // now, load locations using JSON
    var s = document.createElement("script");
    s.src = UTWENTE_LOCATIONS_URL;
    document.body.appendChild(s);
    
    // let json location array finalize the init-process on the map (showing the locs)
    
    var c = new GLatLng(0,0);
    // prepare on-mouseover label
    this.label1 = new ELabel(c, '', "utwente-map-label", new GSize(-100,0));
    this.label2 = new ELabel(c, '', "utwente-map-label utwente-map-label2", new GSize(-99,1));
    
    map.addOverlay(this.label2);
    map.addOverlay(this.label1);
    
  },
  
  showLabel : function (txt, p)
  {
    this.label1.setContents(txt);
    this.label1.setPoint(p);
    this.label2.setContents(txt);
    this.label2.setPoint(p);
    
  },
  
  hideLabel : function()
  {
    this.label1.setContents('');
    this.label2.setContents('');
  },
  
  showLocations: function(locations)
  {
    // the json script will start the ShowLocations method
    this.locations=locations;
    window.setTimeout('utwenteMap.drawLocations()', 0);// avoid chrome crash
  },
  
  drawLocations: function()
  {
    for (i=0;i<this.locations.length;i++)
    {
      var loc = this.locations[i];
      if (this.settings.show && !arr_contains(this.settings.show,loc.id)) continue;

      var style = loc.style?loc.style:'default';
      if (!utwenteMapSettings[style]) style='default';

      var baseOverOptions = utwenteMapSettings[style];

      // create polygon
      var poly = new GPolygon(loc.polygon,
        utwenteMapSettings[style].linecolor, 
        utwenteMapSettings[style].linesize, 
        baseOverOptions.lineopacity, 
        baseOverOptions.fillcolor, 
        utwenteMapSettings[style].opacity, {}
      );
      poly.overOptions = utwenteMapSettings[style].mouseover; // store for mouseover values
      poly.baseOptions = baseOverOptions; // store for mouseout values

      poly.locref = loc;

      // not used: click on building
      if (loc.link)
      {
        GEvent.addListener(poly, "click", function() {
          if (this.locref.link) document.location=this.locref.link;
        });
      }        

      if (baseOverOptions.mouseover)
      {
        GEvent.addListener(poly, "mouseover", function() {
          //if (this.overOptions) this.setFillStyle(this.overOptions); // disabled for client-perf 
          utwenteMap.showLabel(this.locref.name, this.getBounds().getCenter());
        });    

        GEvent.addListener(poly, "mouseout", function() {
          utwenteMap.hideLabel();
          //if (this.baseOptions && this.baseOptions!=this.overOptions)  // disabled for client-perf 
          //{
          //  this.setFillStyle(this.baseOptions);
          //  
          //}
        });    
      }


      this.gmap.addOverlay(poly);
    }
      
  }
  
}

function arr_contains(arr, needle) 
{
  if (arr==null) return false;
  for (var i=0; i<arr.length; i++) if (arr[i]==needle) return true;
  return false;
}


/* ELabel included for easy deployment */
// ELabel.js 
//
//   This Javascript is provided by Mike Williams
//   Community Church Javascript Team
//   http://www.bisphamchurch.org.uk/   
//   http://econym.org.uk/gmap/
//
//   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()
// version 1.8      remove the old GMarkerManager support due to clashes with v2.143


      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;
      }

