var markers = new Array();
infowindows = new Array();

function initialize_map(map_id, zoom) {
	if(!zoom) {
		zoom = 6
	}
	var latlng = new google.maps.LatLng(50.986099,9.008789);
	var myOptions = {
	  zoom: zoom,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP,
	  mapTypeControl: false
	};
        
	var map = new google.maps.Map(document.getElementById(map_id), myOptions);
	return map
}

function close_windows() {
	infowindows.each(function(e) {
		e.close()
	});
}

function add_marker(map, position, tip, content, id, iwindow, redirect) {
	var marker = new google.maps.Marker({
		map: map,
		position: new google.maps.LatLng(position[0], position[1]),
		title: tip
	})
	
	google.maps.event.addListener(marker, 'click', function() {
		if(redirect) {
			window.location.href = redirect
		} else {
			iwindow.setContent(content);
			iwindow.open(map, marker);
                        $('.' + id).each(function(index) {
                            $(this).click()
                        })
		}
	});
	
	$('.' + id).each(function(index) {
		$(this).click(function(e) {
                        setTracking('tourdates::js::' + id.substr(3))
			iwindow.setContent(content);
			iwindow.open(map, marker);
		});
	});
	return marker
}

$(document).ready(function(){
	var map = null
	var redirect = null
	var infoWindowSize = new google.maps.Size(50,50)
	
	if($('#bigmap').length > 0) {
		map = initialize_map('bigmap')
	} else if($('#tourmap').length > 0) {
		map = initialize_map('tourmap', 5)
		redirect = true
	}
        
	if(map) {
		var locations = getContents('locations');
		/*$$('.uoLocation').each(function(el, index) {*/
		var infowindow = new google.maps.InfoWindow({
			content: ''
		});
		
		jQuery.each(locations, function(index, el) {
			var tpl = getTemplate('infoWindowTpl')
			if(el.link) {
                            el.link = '<a href="' + el.link  + '">Mehr Infos</a>'
			} else {
			    el.link = ''
			}
			content = render(tpl, el)
			path = el.path
			
			if(!redirect) path = null
			
			marker = add_marker(map, [el.latitude, el.longitude], el.town, content, 'loc' + el.id, infowindow, path);
			markers['loc' + el.id] = marker
		});
		
		$('#c_left ul.standard li').each(function(index) {
			$(this).click(function(e) {
				if(!$(this).hasClass('active')) {
					$('#c_left ul.standard li.active').each(function(i) {
						$(this).removeClass('active');
					});
				}
				$(this).toggleClass('active');
			});
		});
		
		var activeLocation = getContents('activeLocation');
		
		if(activeLocation) {
			$('.loc' + activeLocation).each(function(i) {
                            $(this).click();
			});
		}
	}
})

