	
var map;
var points = Array();
var markers = Array();
var markers_titles = Array();
var markers_img = Array();
var markers_url = Array();

function initialize() { 
	var ob = document.getElementById("map_canvas");
	if(ob==null) return;
	var myOptions = {
  	mapTypeId: google.maps.MapTypeId.ROADMAP,
  	scrollwheel:false
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	fitBounds();
	drawmarkers();
}


function fitBounds() {
	map.fitBounds(getBoundsForLatLngs(points)); 
}

function drawmarkers() {
		$.each(markers, function(key,val) {
		var marker = new google.maps.Marker({
		 		position: val, 
		 		map: map,
		 		title:markers_titles[key],
		 		icon:markers_img[key],
		 		zIndex:1
  	});
  	
  	google.maps.event.addListener(marker, 'click', function() {
  		document.location.href = markers_url[key];
		});	
  	google.maps.event.addListener(marker, 'mouseover', function() {
  		this.setZIndex(2);
		});		
  	google.maps.event.addListener(marker, 'mouseout', function() {
  		this.setZIndex(1);
		});
	})
}

function getBoundsForLatLngs(latLngs) {
  var bounds = new google.maps.LatLngBounds;
  for (var i = 0; i<latLngs.length; i++) bounds.extend(latLngs[i]);
  return bounds;
}

function calculateCenter(lats,longs) {
	var minLat = null;
	var maxLat = null;
	var minLng = null;
	var maxLng = null;	
  $.each(lats, function() {
                if (!minLat) {
                        minLat = this;
                } else if (this < minLat) {
                        minLat = this;
                }
                if (!maxLat) {
                        maxLat = this;
                } else if (this > maxLat) {
                        maxLat = this;
                }
  });
  $.each(longs, function() {
                if (!minLng) {
                        minLng = this;
                } else if (this < minLng) {
                        minLng = this;
                }
                if (!maxLng) {
                        maxLng = this;
                } else if (this > maxLng) {
                        maxLng = this;
                }
  });

  var x = ((minLng - maxLng) / 2);
  if (x < 0) x = -x;
  x = minLng + x;
  var y = ((minLat - maxLat) / 2)
  if (y < 0) y = -y;
  y = minLat + y;
  return new google.maps.LatLng(y, x); 
}

function add_marker(point,mtitle,enctitle,lnk,flg) {
	markers[markers.length] = point;
	markers_titles[markers_titles.length] = mtitle;
	if(flg!=0 && flg!=1) markers_img[markers_img.length] = new google.maps.MarkerImage("../GD_flagimage.php?fl="+flg,new google.maps.Size(20, 14));	
	if(flg==0) markers_img[markers_img.length] = new google.maps.MarkerImage("../GD_imagetxt.php?txt="+enctitle+"&angle=0");	
	if(flg==1) markers_img[markers_img.length] = new google.maps.MarkerImage('../images/hotel.png',new google.maps.Size(32, 37));		
	markers_url[markers_url.length] = lnk;	
}

function reset_map() {          
 	points = Array();        
 	markers = Array();       
 	markers_titles = Array();
 	markers_img = Array();   
 	markers_url = Array();	 
}
	
function add_point(p) {
	points[points.length] = p;
}

function loadCountriesForMap(continent){
	reset_map();
	$.getJSON("../ajax/JSON_countries_data.php?continent="+continent+"&rnd="+Math.floor(Math.random()*9999999), 
		function(data){
     $.each(data.countries, function(i,item){
     	lat = Array();
     	lon = Array();
     	$.each(item.lats, function(x,l){
     		lat[lat.length] = parseFloat(l.value);
     	})
     	$.each(item.longs, function(x,l){
     		lon[lon.length] = parseFloat(l.value);
     	})  
     	add_point(calculateCenter(lat,lon));
     	add_marker(calculateCenter(lat,lon),item.name,item.encname,item.link,item.flag);
    })
    initialize();
  })
  $("#cont_Europe").hide();
  $("#cont_Asia").hide();
  $("#cont_Africa").hide();
  $("#cont_S_America").hide();
  $("#cont_N_America").hide();
  $("#cont_Australia").hide();
  
  $("#cont_"+continent).show();
}


