//*********************************************
// vars
//*********************************************

// global map and infowindow
var map;
var infowindow;

// holds the markers of each region
var regionen = {
	de: [],
	fr: [],
	it: [],
	rm: []
};

// holds all single markers
var singles = [];

//*********************************************
// functions
//*********************************************

// document ready
$(document).ready(function() {
  
  // korrespondenten
  if($('.img_col .caption').length) {
    $('.img_col .caption').css('width', $('.img_col img').width());
  }
  
  // google maps
	if($('#map_canvas').length) {
		
		// create map
		var swissCenter = new google.maps.LatLng(46.85, 8.2);
		var mapOptions = {
			zoom: 8,
			center: swissCenter,
			mapTypeId: google.maps.MapTypeId.TERRAIN
		};
		map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
		
		// create infowindow
		infowindow = new google.maps.InfoWindow();
		
		// place all single markers on map
		placeSingleMarkers();
		
		// make a combined marker for each region
		createRegionMarkers();
		
		// cluster close single markers
		var markerCluster = new MarkerClusterer(map, singles, {gridSize: 40, styles: [{
			url: '/wp-content/themes/radiotour2011/images/map-cluster-icon.png',
			height: 52,
			width: 53,
			textColor: '#ffffff'
		}]});
	}
	
	// add class with arrows to navigation items with sub lists
	$('#mainNavigation li ul li ul').siblings('a').addClass('sub');
	// add class with arrows to navigation items with sub lists within side_navigation
	$('#sidebar.side_navigation li ul').siblings('a').addClass('sub');
});

function createRegionMarkers() {
	// regions with their centers
	createRegion('de', new google.maps.LatLng(47.42915410641598, 8.183520507812545));
	createRegion('fr', new google.maps.LatLng(46.90901564838898, 6.8655409546875035));
	createRegion('it', new google.maps.LatLng(46.28244516775517, 8.865052673437503));
	createRegion('rm', new google.maps.LatLng(46.63436805682157, 10.007630798437503));
}

function createRegion(reg, center) {
	// only place region marker if there are any sendungen of this region
	if (sen_reg[reg].length > 0) {
		var marker = new google.maps.Marker({
			position: center,
			map: map,
			icon: '/wp-content/themes/radiotour2011/images/map-square-icon.png'
		});
	
		google.maps.event.addListener(marker, 'click', (function(marker) {
			return function() {toggleMarkers(marker.position, reg);}
		})(marker));
	
		// add markers from json-array sen_reg of corresponding language
		var i;
		for (i = 0; i< sen_reg[reg].length; i++) {
			var marker = addMarkerToRegion(sen_reg[reg][i][1], new google.maps.LatLng(0,0), reg);
			
			// add event listener, click to show infowindow
			google.maps.event.addListener(marker, 'click', (function(marker, i) {
				return function() {
					infowindow.setContent(sen_reg[reg][i][0]);
					infowindow.open(map, marker);
				}
			})(marker, i));
		}
	}
}

function addMarkerToRegion(title, loc, reg) {
  marker = new google.maps.Marker({
    position: loc,
	icon: '/wp-content/themes/radiotour2011/images/map-round-icon.png',
	title: title
  });
  regionen[reg].push(marker);
  return marker;
}

function placeSingleMarkers() {
	var i;
	for (i = 0; i< sen_geo.length; i++) {
		var latlng = new google.maps.LatLng(sen_geo[i][2], sen_geo[i][3]);
		var marker = setMarker(sen_geo[i][1], latlng);
		
		singles.push(marker);
		
		// add event listener, click to show infowindow
		google.maps.event.addListener(marker, 'click', (function(marker, i) {
			return function() {
				infowindow.setContent(sen_geo[i][0]);
				infowindow.open(map, marker);
			}
		})(marker, i));
	}
}

function setMarker(title, latlng) {
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map,
		icon: '/wp-content/themes/radiotour2011/images/map-round-icon.png',
		title: title
	});
	return marker;
}

// Shows or hides marker of specified region
function toggleMarkers(pos, reg) {
	// close infowindow
	infowindow.close();
	
	if (regionen[reg]) {
		if (regionen[reg][0].map == null) {
			// arrange and show markers
			arrangeAndShowMarker(pos, regionen[reg]);
			
			//hide all others
			if (reg != 'de')
				hideMarkersOfRegion(regionen['de']);
			if (reg != 'fr')
				hideMarkersOfRegion(regionen['fr']);
			if (reg != 'it')
				hideMarkersOfRegion(regionen['it']);
			if (reg != 'rm')
				hideMarkersOfRegion(regionen['rm']);
				
		} else {
			// hide own markers
			hideMarkersOfRegion(regionen[reg]);
		}
	}
}

// arrange markers on circle around region and show them
function arrangeAndShowMarker(center, arr) {
	// evenly distribute around center
	var rads = 0;
	var rad_diff = (2 * Math.PI / arr.length);
	
	// dist is relative to zoom (21 -> 0.00005 with factor 2)
	var level_diff = 21 - map.getZoom();
	var dist = 0.00003 * Math.pow(2, level_diff);
	
	for (i in arr) {
		var x = Math.sin(rads) * dist;
		var y = Math.cos(rads) * dist;
		
		// offset proportional to y to incorporate map distortion
		var offset = -0.35 * y;
		arr[i].position = new google.maps.LatLng(center.lat() + y + offset, center.lng() + x);
		arr[i].setMap(map);
		rads += rad_diff;
	}
}

// hides the marker of a region
function hideMarkersOfRegion(reg) {
	for (i in reg) {
		reg[i].setMap(null);
	}
}

