var map;
var logo;

function CustomGetTileUrl(a,b) {
 if ((b==6 && a.x>=45 && a.x<=46 && a.y>=26 && a.y<=27) || (b==7 && a.x>=91 && a.x<=93 && a.y>=53 && a.y<=54) || (b==8 && a.x>=182 && a.x<=187 && a.y>=106 && a.y<=108) || (b==9 && a.x>=364 && a.x<=375 && a.y>=212 && a.y<=217) || (b==10 && a.x>=729 && a.x<=751 && a.y>=424 && a.y<=435)) {
  return "tiles/"+b+"_"+a.x+"_"+a.y+".jpg";
 } else {
  return "grfx/blank.jpg";
 }
}

function prettyBox(title, client, year, info) {
 function CE(value,cat){
  if (value =="" || value ==":" || value =="." || value ==", 0") return '';
   else {
    return '<h1>'+cat+'<\/h1><div class="infoVal">'+value+'</\div>';
   }
 }

 return CE(title,"Name") +
	CE(client,"Client") +
	CE(year,"Year") +
	CE(info,"More Info")
 }

function load() {
 if (GBrowserIsCompatible()) {

  var baseIcon = new GIcon();
  baseIcon.image = "grfx/info.png";
  baseIcon.shadow = "grfx/icon.shadow.png";
  baseIcon.iconSize = new GSize(36, 40);
  baseIcon.shadowSize = new GSize(57, 40);
  baseIcon.iconAnchor = new GPoint(18, 40);
  baseIcon.infoWindowAnchor = new GPoint(16, 0);

  function createMarker(point, html, index) {
   var marker = new GMarker(point, {draggable: false});
   GEvent.addListener(marker, 'click', function(){
    marker.openExtInfoWindow(
      map,
      "infoWindow",
//    "<div>" + html + '<br><br>' + marker.getLatLng() + "</div>",
      "<div>" + html + "</div>",
      {beakOffset: 3}
    );
   });
   return marker;
  }

  var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 0, "© <a href='mailto:contact@viainfotech.com'>VIA Infotech</a>");
  var copyrightCollection = new GCopyrightCollection('Portfolio');
  copyrightCollection.addCopyright(copyright);

  var tilelayers = [new GTileLayer(copyrightCollection, 6, 10)];
  tilelayers[0].getTileUrl = CustomGetTileUrl;

  var custommap = new GMapType(tilelayers, new GMercatorProjection(14), "Portfolio", {errorMessage: "Error"});
  var bWidth = document.getElementById("map").offsetWidth
  if (bWidth < 1024) {
   zoomLevel = 7
  } else {
   zoomLevel = 8
  }

  map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng(27.68, 80.15), zoomLevel, custommap);
  map.addControl(new GLargeMapControl3D());
  map.addMapType(custommap);

  logo = new GScreenOverlay("grfx/logo.png", new GScreenPoint(document.getElementById("map").offsetWidth-140, document.getElementById("map").offsetHeight-130), new GScreenPoint(0, 0), new GScreenSize(0, 0));
  map.addOverlay(logo);

  // Read Data
  GDownloadUrl("data.xml", function(data) {
   var xml = GXml.parse(data);
   var markers = xml.documentElement.getElementsByTagName("marker");
   for (var i = 0; i < markers.length; i++) {
    var title = markers[i].getAttribute("title");
    var client = markers[i].getAttribute("client");
    var year = markers[i].getAttribute("year");
    var info = markers[i].getAttribute("info");
    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
    var marker = createMarker(point, prettyBox(title, client, year, info), baseIcon);
    map.addOverlay(marker);
   }
  });

 }
}

function resizeMapDiv() {
 map.removeOverlay(logo);
 logo = new GScreenOverlay("grfx/logo.png", new GScreenPoint(document.getElementById("map").offsetWidth-140, document.getElementById("map").offsetHeight-130), new GScreenPoint(0, 0), new GScreenSize(0, 0));
 map.addOverlay(logo);
 map.checkResize();
}