SrnMap = new Object();

/**
 * the google map object
 */
SrnMap.map = undefined;

/**
 * the markers list for reference
 */
SrnMap.markers = [];

/**
 * the markers labels for reference
 */
SrnMap.markersLabels = [];

/**
 * the infobox lists for reference
 */
SrnMap.infoboxes = [];

/*
 * map options
 */
SrnMap.mapOptions = {
  // initial zoom
  zoom: 2,
  // center middle earth
  center: new google.maps.LatLng(55.961329081596647, 7.03125),
  // disable mouse zoom
  scrollwheel: false,
  // disable drag
  draggable: true,
  // disable double click
  disableDefaultUI: true,
  // dont allow mouse zooming
  disableDoubleClickZoom: {},
  // hide navigation control
  navigationControl: false,
  // hide type controle
  mapTypeControl: false,
  // set the map type id
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  // control options (custom id)
  mapTypeControlOptions:{
    mapTypeIds: ['srn']
  }
}

/**
 * create the map in the given selector;
 */
SrnMap.createMap = function($selector){
  // add map
  this.map = new google.maps.Map($selector, this.mapOptions);
  this.map.mapTypes.set('srn', SrnMapType);
  this.map.setMapTypeId('srn');

  // setup bounds
  //this.setupBounds();

  // setup markers
  this.setupMarkers();
}

/**
 * setup all markers
 */
SrnMap.setupMarkers = function(){
  // total markers
  var total_markers = srn_countries_list.length;

  // loop in all markers
  for(var i = 0; i < total_markers; i++){
    // the current country
    var c = srn_countries_list[i];

    // add marker
    this.markers[i] = new google.maps.Marker({
      position: new google.maps.LatLng(c.country_longitude, c.country_latitude),
      map: this.map,
      icon: 'assets/images/map/marker.png',
      title:c.country_name
    });

    this.markersLabels[c.country_name] = this.markers[i];
    // bind marker
    google.maps.event.addListener(this.markers[i], 'click', SrnMapMarker.clickHandler);
  }

  // hide preloader
  $("#loading").hide();

}

/**
 * close all infoboxes
 */
SrnMap.closeInfoBoxes = function(){
  for (var i in this.infoboxes){
    // close the infobox
    if(this.infoboxes[i].close != undefined){
      this.infoboxes[i].close();

      // remove from array
      this.infoboxes.splice(i, 1);
    }
  }
}

/**
 * setup map boundaries
 */
SrnMap.setupBounds = function(){
  // dragg limit
  var allowedBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-50, -50),
    new google.maps.LatLng(70, 100));

  // Listen for the dragend event
  google.maps.event.addListener(this.map, 'dragend', function(target) {
    // map alias
    var map = SrnMap.map;
    if (allowedBounds.contains(map.getCenter())) return;

    // Out of bounds - Move the map back within the bounds

    var c = map.getCenter(),
    x = c.lng(),
    y = c.lat(),
    maxX = allowedBounds.getNorthEast().lng(),
    maxY = allowedBounds.getNorthEast().lat(),
    minX = allowedBounds.getSouthWest().lng(),
    minY = allowedBounds.getSouthWest().lat();

    if (x < minX) x = minX;
    if (x > maxX) x = maxX;
    if (y < minY) y = minY;
    if (y > maxY) y = maxY;

    map.setCenter(new google.maps.LatLng(y, x));
  }); 
}
