var map;
var icon0;
var newpoints = new Array();
 
function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function'){ 
    window.onload = func
  } else { 
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
 
addLoadEvent(loadMap);
addLoadEvent(addPoints);
 
function loadMap() {  
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(53.43386565214872, 14.535727500915527), 14);
  map.setMapType(G_NORMAL_MAP);
 
  icon0 = new GIcon();
  icon0.image = "http://www.google.com/mapfiles/marker.png";
  icon0.shadow = "http://www.google.com/mapfiles/shadow50.png";
  icon0.iconSize = new GSize(20, 34);
  icon0.shadowSize = new GSize(37, 34);
  icon0.iconAnchor = new GPoint(9, 34);
  icon0.infoWindowAnchor = new GPoint(9, 2);
  icon0.infoShadowAnchor = new GPoint(18, 25);
}
 
function addPoints() {
 
  newpoints[0] = new Array(53.43386565214872, 14.535727500915527, icon0); 
 
  for(var i = 0; i < newpoints.length; i++) {
    var point = new GPoint(newpoints[i][1],newpoints[i][0]);
    var popuphtml = newpoints[i][4] ;
    var marker = createMarker(point,newpoints[i][2],popuphtml);
    map.addOverlay(marker);
  }
}
 
function createMarker(point, icon, popuphtml) {
  var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
  var marker = new GMarker(point, icon);
  /*GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(popuphtml);
  });*/
  return marker;
}

$(document).ready(function() {     
  
  //select all the a tag with name equal to modal   
  $('a[name=modal]').click(function(e) {   
      //Cancel the link behavior   
      e.preventDefault();   
      //Get the A tag   
      var id = $(this).attr('rel');   
     
      //Get the screen height and width   
      var maskHeight = $(document).height();   
      var maskWidth = $(window).width();   
     
      //Set height and width to mask to fill up the whole screen   
      $('#mask').css({'width':maskWidth,'height':maskHeight});   
         
      //transition effect        
      $('#mask').fadeIn(1000);       
      $('#mask').fadeTo("slow",0.8);     
     
      //Get the window height and width   
      var winH = $(window).height();   
      var winW = $(window).width();   
               
      //Set the popup window to center   
      $(id).css('top',  winH/2-$(id).height()/2);   
      $(id).css('left', winW/2-$(id).width()/2);   
     
      //transition effect   
      $(id).fadeIn(2000);    
      map.checkResize();
      map.setCenter(new GLatLng(53.43386565214872, 14.535727500915527), 14);
     
  });   
     
  //if close button is clicked   
  $('.window .close').click(function (e) {   
      //Cancel the link behavior   
      e.preventDefault();   
      $('#mask, .window').hide();   
  });        
     
  //if mask is clicked   
  $('#mask').click(function () {   
      $(this).hide();   
      $('.window').hide();   
  });            
     
});   
