$('.poi > input').click(function() {
	//console.log($(this).attr("id"));
	
	
	
	boxid = $(this).attr("id");
	boxchecked = $('#'+boxid).attr('checked');
	
	if(boxchecked){
		//console.log("ein");
		addinteractivemarker(boxid);
	} else {
		//console.log("aus");
		deleteinteractivemarker(boxid);
	}
	
});






function addinteractivemarker(boxid) {
	$.ajax({
		type: "POST",
		dataType: 'json',
		url: "/?controller=Maps&action=addinteractivemarker",
		data: "&boxid="+boxid+"&language="+lang+"",
		success: function(transport){
			createpoi(transport,boxid);
			//console.log(transport);
		}
	});
}


function deleteinteractivemarker(boxid) {
	$.ajax({
		type: "POST",
		dataType: 'json',
		url: "/?controller=Maps&action=addinteractivemarker",
		data: "&boxid="+boxid+"&language="+lang+"",
		success: function(transport){
			deletepoi(transport,boxid);
		}
	});
}




function createpoi(transport,boxid){

	//console.log(transport.length);

        markers = [];
        for (var i = 0; i < transport.length; i++) {
          var latLng = new google.maps.LatLng(transport[i].latitude, transport[i].longitude);
          var marker = new google.maps.Marker({
            position: latLng,
			markerid: transport[i].id,
			title: transport[i].webcamkey,
			webcamkey: transport[i].webcamkey,
			cityname: transport[i].cityname,
			copyright: transport[i].copyright,
			icon: '/static/img/markers/' + transport[0].marker + '.png'
          });
		  
		  
		  
			google.maps.event.addListener(marker, 'click', function() {
				//console.log($(this).position());
				
				$(".popupwindow").html(
					'<h3>'+this.cityname+'</h3><a title="'+this.cityname+'" href="/static/webcams/'+this.webcamkey+'_640x480.jpg"><img src="/static/webcams/'+this.webcamkey+'_128x96.jpg" width="128" height="96" alt="'+this.cityname+'" title="'+this.cityname+'" /></a><p>'+this.copyright+'</p><div class="cloesew"></div>'
				);

				$(".popupwindow").css("display","block");
				
				$('.cloesew').click(function() {
					$(".popupwindow").css("display","none");
				});
				
				setTimeout('createfancy()', 500);
			});
		  
		  
          markers.push(marker);
        }

		
		//console.log(markers);
		
        markerCluster = new MarkerClusterer(map, markers);
}

function createfancy(){
		$(".popupwindow>a").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true
		});
}

function deletepoi(transport,boxid){
		if(markerCluster) {
			markerCluster.clearMarkers();
		}
		delete markerCluster;

		/*if (markerCluster != null) {
			markerCluster.clearMarkers();
		}*/

		for(i=0; i<transport.length; i++){
		
			  var latLng = new google.maps.LatLng(transport[i].latitude, transport[i].longitude);
			  var marker = new google.maps.Marker({
				position: latLng,
				markerid: transport[0].id,
				title: transport[0].webcamkey,
				icon: '/static/img/markers/' + transport[0].marker + '.png'
			  });
		

			markers.splice(jQuery.inArray(marker, markers), 1);
		}
		
		markerCluster = new MarkerClusterer(map, markers);
		

}






