includeLibrary("/OpenForum/Extensions/GoogleMap/googleMap.js");
includeLibrary("/OpenForum/Extensions/GoogleMap/locator.js");
includeLibrary("/DataServices/GoogleEarthMaps/page.js");

includeInitFunction("initPage();");
includeUnloadFunction("googleMap.close();");

function initPage()
{
  googleMap.init(53.93,-2.33,6,true);
  //googleMap.addOverlay( 49.495577,-12.01,60.995577,1.99,"http://www.rensmart.com/Weather/BERR/Speed10.PNG" );

    rensmartCopyright = new GCopyrightCollection("&copy; ");
    rensmartCopyright.addCopyright(new GCopyright('Demo',
    new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)),
    0,'&copy; 2009 RenSMART'));

    tilelayer = new GTileLayer(rensmartCopyright);
    tilelayer.getTileUrl = function(point,zoom) {
    return "http://tiles.rensmart.com?x="+point.x+"&y="+point.y+"&zoom="+zoom+"&key=data-set-10"; };

    tilelayer.isPng = function() { return true;};
    tilelayer.getOpacity = function() { return 0.5; }

    var noablTileLayer = new GTileLayerOverlay(tilelayer);
    googleMap.map.addOverlay(noablTileLayer);
  
  var keyScreenXY = new GScreenPoint(0,50);
  var keyOverlayXY = new GScreenPoint(0,0);
  var keySize = new GScreenSize(100, 150); 
  var key = new GScreenOverlay("http://www.rensmart.com/DataServices/key-bordered.png",keyScreenXY,keyOverlayXY,keySize);
  googleMap.map.addOverlay( key );

  googleMap.map.addControl( new GScaleControl() );

  GEvent.addListener(googleMap.map, "click", function(overlat,latlng) {
    if(typeof(latlng)!="undefined") {
      googleMap.moveDragMarker( latlng.lat(),latlng.lng() );
    }
  });

  googleMap.oldInfo = googleMap.getInfo;
  googleMap.getInfo = function(point)
  {
    displayForLocation(point);
    return this.oldInfo(point);
  }
  googleMap.addDragMarker();

  //showGrid();

  initLocation();
}

function displayForLocation(point)
{
berrWindSpeeds = ajax.doCall("/RenSMARTSystem/Servcies?service=/WeatherModel/Service&action=getBERRNOABLWindSpeeds&latitude="+point.lat()+"&longitude="+point.lng(),true);

  var htmlData = "<table class=\"wikiTable\">"+
    "<tr class=\"wikiTableOddRow\"><td class=\"wikiTableCell\"><b>Height Above Ground</b></td><td class=\"wikiTableCell\"><b>Wind Speed</b></td></tr>"+
    "<tr class=\"wikiTableEvenRow\"><td class=\"wikiTableCell\">At 10 meters</td><td class=\"wikiTableCell\">"+berrWindSpeeds.at10Meters+" m/s "+(Math.round(parseFloat(berrWindSpeeds.at10Meters)*22.369)/10)+" mph</td></tr>"+
    "<tr class=\"wikiTableOddRow\"><td class=\"wikiTableCell\">At 25 meters</td><td class=\"wikiTableCell\">"+berrWindSpeeds.at25Meters+" m/s "+(Math.round(parseFloat(berrWindSpeeds.at25Meters)*22.369)/10)+" mph</td></tr>"+
    "<tr class=\"wikiTableEvenRow\"><td class=\"wikiTableCell\">At 45 meters</td><td class=\"wikiTableCell\">"+berrWindSpeeds.at45Meters+" m/s "+(Math.round(parseFloat(berrWindSpeeds.at45Meters)*22.369)/10)+" mph</td></tr>"+
    "<tr class=\"wikiTableOddRow\"><td  class=\"wikiTableCell\" colspan=\"2\"><img src=\""+getGoogleChart( berrWindSpeeds )+"\" width=\"200\" height=\"100\" border=\"0\"></td></tr>"+
    "<tr class=\"wikiTableOddRow\"><td  class=\"wikiTableCell\" colspan=\"2\">"+
    "<center><input type=\"button\" value=\"List installers in this area\" onClick=\"displayInstallers();return false;\"></center><br/>"+
"<img src=\"/OpenForum/Images/pages-small.png\"/><b>Custom Map &pound;2.40</b> <br/>Purchase a printable and shareable<br/> version of this map centred on your location<br/> by clicking <a href=\"/Orders/CustomNOABLWindMap?latitude="+point.lat()+"&longitude="+point.lng()+"\">here</a>"+
"</td></tr>"+
    "</table>";

  return htmlData;
}
function addExtraInfo(point)
{
  return displayForLocation(point);
}

function getGoogleChart( berrWindSpeeds )
{

dataText=berrWindSpeeds.at10Meters+","+berrWindSpeeds.at25Meters+","+berrWindSpeeds.at45Meters;

url = "http://chart.apis.google.com/chart?"+
"cht=lxy"+
"&chxt=x,y,x,y"+
"&chxl="+
  "0:|0|2|4|6|8|10|12|"+
  "1:|0|10|20|30|40|50|"+
  "2:|Wind Speed (m/s)|"+
  "3:||Height (m)|"+
"&chd=t:"+dataText+"|10,25,45"+
"&chm=o,00FF00,0,-1,5"+
"&chds=0,12,0,50"+
"&chg=10,20"+
"&chco=50A364"+
"&chs=200x100";

return url;
}

function showGrid()
{
  var map = googleMap.map;

// 49.495577,-12.01,60.995577,1.99
  for(loop=49;loop<=61;loop+=1)
  {
   var polyline = new GPolyline([
    new GLatLng(loop,-12),
    new GLatLng(loop,2)
  ], "#ff0000", 1);
   map.addOverlay(polyline);
  }

  for(loop=-12;loop<=2;loop+=1)
  {
  var polyline = new GPolyline([
    new GLatLng(49,loop),
    new GLatLng(61,loop)
  ], "#ff0000", 1);
  map.addOverlay(polyline);
  }

}

function openSitePlanner()
{
  window.location = "/Tools/SitePlanner?project=Example Wind Turbine&latitude="+document.getElementById("lat").value+"&longitude="+document.getElementById("lng").value;
}

function displayInstallers() {
  window.open("/Tools/IntallersCallBackRequest?technologies=NOTIFY_WIND&latitude="+document.getElementById("lat").value+"&longitude="+document.getElementById("lng").value,"installers");
}

/*========Add This Functions========*/

function getAddThisParameters()
{
  return "?latitude="+document.getElementById("lat").value+"&longitude="+document.getElementById("lng").value;
} 
function getAddThisTitleSuffix()
{
  return " "+document.getElementById("query").value;
}
