﻿var HM_posted = false;
var HM_http_request = false;

function HMMakePOSTRequest(url, parameters) {
  HM_http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     HM_http_request = new XMLHttpRequest();
     if (HM_http_request.overrideMimeType) {
     	// set type accordingly to anticipated content type
        HM_http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        HM_http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           HM_http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!HM_http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  HM_http_request.onreadystatechange = HMGetImageURL;
  HM_http_request.open('POST', url, true);
  HM_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  HM_http_request.setRequestHeader("Content-length", parameters.length);
  HM_http_request.setRequestHeader("Connection", "close");
  HM_http_request.send(parameters);
  return true;
}

function HMGetImageURL() {
  if (HM_http_request.readyState == 4) {
     if (HM_http_request.status == 200) {
        //alert(HM_http_request.responseText);
        result = HM_http_request.responseText;
        document.getElementById('HMImageURL').value = result;            
        HM_posted = true;
     } else {
        alert('There was a problem with the request to render the HeatMapAPI image.');
     }
  }
}
  
function HMGoogleOverlay(url) {
  this.baseurl_ = url;
}

var HM_update_image_fcall;

HMGoogleOverlay.prototype.bounds_;
HMGoogleOverlay.prototype.baseurl_;
HMGoogleOverlay.prototype.url_;

HMGoogleOverlay.prototype = new GOverlay();

// Creates the DIV representing this overlay.
HMGoogleOverlay.prototype.initialize = function(map) {
  this.bounds_ = map.getBounds();
	
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);

  var div = null;
  var ie = false;
  if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
    div = document.createElement("span");
    ie = true;
  } else {
      div = document.createElement("img");
      div.src = "http://www.heatmapapi.com/images/spacer.png";
  }
  div.style.position = "absolute";
  div.onload = function() {
  	div.style.opacity = '1';  
  }
  div.id = '' + Math.floor(Math.random()*100);
  
  if(ie) {
      div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'"+this.url_+"\', sizingMethod=\'scale\')";
  } else {
      div.src = "http://www.heatmapapi.com/images/spacer.png";
  }
  map.getPane(G_MAP_MAP_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;

  GEvent.addListener(this.map_, "moveend", function (){eval(HM_update_image_fcall)});
  //GEvent.addListener(this.map_, "load", function (){eval(HM_update_image_fcall)});
}

function HMUpdateImage(img_id, url) {
    HM_posted = false;
	HMGet(url);
	setTimeout("HMShowImage('" + img_id + "','" + url + "')", 20);
}

function HMShowImage(img_id, url){
    if(!HM_posted)
        setTimeout("HMShowImage('" + img_id + "','" + url + "')", 20);
    else
    {
    	var img = document.getElementById(img_id);
	if(img==null)
		alert(img_id + ' has not been defined for image: ' + url);
        var arVersion = navigator.appVersion.split("MSIE");
        var version = parseFloat(arVersion[1]);
        
        var urlstatic = document.getElementById('HMImageURL').value;
            
        if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
            img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'"+urlstatic+"\', sizingMethod=\'scale\')";
        } else {
            img.src = urlstatic;
        }
    }
}

HMGoogleOverlay.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
}

HMGoogleOverlay.prototype.copy = function() {
  return new HMGoogleOverlay(this.bounds_, this.url_);
}

HMGoogleOverlay.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  this.div_.style.opacity = '0';

  this.bounds_ = this.map_.getBounds();
  var southWest = this.bounds_.getSouthWest();
  var northEast = this.bounds_.getNorthEast();
  this.url_ = this.baseurl_ + "&lat1=" + northEast.lat() + "&lat2=" + southWest.lat() + "&lon1=" + southWest.lng() + "&lon2=" + northEast.lng();
  //this.url_ = this.baseurl_;

  HM_update_image_fcall = "HMUpdateImage('" + this.div_.id + "','" + this.url_ + "')";
 
  if(force) eval(HM_update_image_fcall);

  var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = Math.abs(c2.x - c1.x) + "px";
  this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
  this.div_.style.left = Math.min(c2.x, c1.x) + "px";
  this.div_.style.top = Math.min(c2.y, c1.y) + "px";
}