

$(document).ready(function() {
	


	drawBasket();
	// Right bar navigation
	
	$('#cont').slideUp('fast');
	$('#btn_up').hide();
	
	$('#btn_down').click(function() {
	    $('#cont').slideDown('normal');	
	    $('#btn_up').show();
	})
	$('#btn_up').click(function() {
	    $('#cont').slideUp('normal');	
	    $('#btn_up').hide();
	})

	

	
	// Map open and close the map
	
	$("#map").animate({"height": "-=225px"}, "slow");
	$("#map_expand_btn").toggle( 
		function () { 
		        $("#map").animate({"height": "+=225px"}, "slow");
		}, 
		function () { 
		        $("#map").animate({"height": "-=225px"}, "slow");
		} 
	); 
	

	

	// Left bar navigation
	
	$("#search_location").val(''); 
	
	$.each(locaties,function(i, objValue ){
		$("#locations_list").append($( "<li class=\"" + objValue.properties.replace(/:/g, " ")+ "\"><a href=\"\"  class=\"marker\"  rel=\"" + objValue.id + "\">" + objValue.name.substring(0,18) + "</a></li>" ));
	});
		
	$("#search_location").keyup(function(e){ 
		//e.keyCode;
		
	    var search = $(this).val(); 
	 	$("#locations_list").html('');
	    if (search.length > 0) { 
	    	$.each(locaties,function(i, objValue ){
	    		
				if(objValue.name.toLowerCase().indexOf(search.toLowerCase()) == "0"){
					
					$("#locations_list").append($("<li class=\"marker " + objValue.properties+ "\" ><a href=\"\" class=\"marker\"  rel=\"" + objValue.id + "\">" + objValue.name.substr(0, 1).toUpperCase() + objValue.name.substr(1).toLowerCase() + "</a></li>"));
					gmap.toggelId(objValue.id, 'show');
				}else if(objValue.name.toLowerCase().indexOf(search.toLowerCase()) >= "0"){
					if (objValue.name.toLowerCase().charAt(objValue.name.toLowerCase().indexOf(search.toLowerCase())-1) == ' '){
						$("#locations_list").append($("<li class=\"marker " + objValue.properties+ "\"><a href=\"\" class=\"marker\"  rel=\"" + objValue.id + "\">" + objValue.name.substr(0, 1).toUpperCase() + objValue.name.substr(1).toLowerCase() + "</a></li>"));
						gmap.toggelId(objValue.id, 'show');
					}else{
						gmap.toggelId(objValue.id, 'hide');
					}
				}else{
					gmap.toggelId(objValue.id, 'hide');
				}
				
			});
	    }else{
	    	$.each(locaties,function(i, objValue ){
				$("#locations_list").append($( "<li class=\"" + objValue.properties.replace(/:/g, " ")+ "\"><a href=\"\"  class=\"marker\"  rel=\"" + objValue.id + "\">" + objValue.name.substr(0, 1).toUpperCase() + objValue.name.substr(1).toLowerCase().substring(0,19) + "</a></li>" ));
				gmap.toggelId(objValue.id, 'show');
	    	});

	    }
	});
	
	// if the gmap object exists add the markers.
	
	if (typeof gmap != "undefined" && !$('#map').hasClass('leeg')){
		
		// Add areas
		$.each(areas,function(i, objValue ){
			gmap.addAreas(objValue.id,objValue.bounds,objValue.level, '#000000');	
		});	
		
		//Add the markers
		$.each(locaties,function(i, objValue ){
			html = "<table class=\"info\" width=\"100%\" height=\"100%\" border=\"0\"><tr><td width=\"130\" align=\"center\" valign=\"middle\"><img src=\"/viewimage.php?id="+ objValue.img+"&resize=118x79\" alt=\"\"></td><td valign=\"top\"><b>"+ objValue.name+"</b><br> "+ objValue.street +" "+ objValue.housenumber +"<br>"+ objValue.city +"</td></tr><tr><td colspan=\"2\" height=\"20px\" align=\"right\"><a href=\"/locatie/"+objValue.id+"\" class=\"maplink\">Bekijk details</a> | <a href=\"\" rel=\""+objValue.id+"\" class=\"item maplink\">Voeg aan route toe</a></td></tr></table>"; 
			
			var path = '/layout/images/kaart_icons/';
			var marker = '';
		
			switch(objValue.themaid){
				case '1':
				  marker = 'icon_green.png';
				  break;
				case '2':
				  marker = 'icon_pink.png';
				  break;
				case '3':
				  marker = 'icon_blue.png';
				  break;				
				case '4':
				  marker = 'icon_orange.png';
				  break;				
				case '5':
				  marker = 'icon_brown.png';
				  break;
				default:
				  marker = 'icon_pink.png';
				  break;
			}
		
			gmap.addMarker({"id" : objValue.id,"name" : objValue.name, "latlng" : objValue.latlng, "type" : "marker", "prop" : objValue.properties, "markerHTML" : html, "markerIcone" : path+marker});
		});
		
		// Add parking
		$.each(parking,function(i, objValue){
			html = "<table class=\"info\" width=\"100%\" height=\"100%\" border=\"0\"><tr><td width=\"130\" align=\"center\" valign=\"middle\"><img src=\"/viewimage.php?id="+ objValue.img+"&resize=118x79\" alt=\"\"></td><td valign=\"top\"><b>"+ objValue.name+"</b><br> "+ objValue.street +" "+ objValue.housenumber +"<br>"+ objValue.city +"</td></tr><tr><td colspan=\"2\" height=\"20px\" align=\"right\"> <a href=\"\" rel=\""+objValue.id+"\" class=\"item maplink parking\">Voeg aan route toe</a></td></tr></table>"; 

			gmap.addMarker({"id" : objValue.id,"name" : objValue.name, "latlng" : objValue.latlng, "type" : "marker", "prop" : objValue.properties, "markerHTML" : html, "markerIcone" : '/layout/images/kaart_icons/icon_parking.png'});
		});	
		
	
	}
	
	//show news item on the map
//	if (news_id != undefined && news_title != undefined && news_coords != undefined){
//		gmap.addMarker({"id" : news_id,"name" : news_title, "latlng" : news_coords, "type" : "marker", "prop" : "", "markerHTML" : "", "markerIcone" : "/layout/images/kaart_icons/icon_pink.png"});
//	}

	// Hide and show markers by properties
	
	$("#aside_primary").click(function(e){
		target = e.target;
	
		gmap.hideAllMarkers();
		$('.th input:checked').each(function(){
			gmap.toggelProperties(this.name, 'show');
		});	
		
		
		if ($(target).hasClass("cat") && target.checked){
			gmap.toggelProperties(target.name, 'show');
			$('#locations_list li').each(function(){
				if ($(this).hasClass(target.name)){
					$(this).show();
				}
			});	
		}else if ($(target).hasClass("cat")){
			gmap.toggelProperties(target.name, 'hide');
			$('#locations_list li').each(function(){
				if ($(this).hasClass(target.name)){
					$(this).hide();
				}
			});	
		}
			
		$('.ar input:checked').each(function(){
			gmap.toggelAreas(this.name, 'show');
		});	
		
		if ($(target).hasClass("area") && target.checked){
			gmap.toggelAreas(target.name, 'show');	
		}else if ($(target).hasClass("area") ){
			gmap.toggelAreas(target.name, 'hide');
		}
		
	});	
	
	$("#locations_list").click(function(e) {
		target = e.target;
	
		if ($(target).hasClass("marker")) {
			if (typeof gmap != "undefined"){

				if ($('#map').css('height') == '150px'){
					$("#map").animate({"height": "+=225px"}, "slow");
				}
				
	       		gmap.openInfo(target.rel);
	       		return false;
			}else{
				window.location="/locatie/" + target.rel;
				return false;
			}
		}
	});
	
	var mp = [];
	
	$("#map").click(function(e){
		target = e.target;
		if ($(target).hasClass("item")) {	
			$.getJSON("/Jscontent/getLocation/" + target.rel,function(data){
	       		$.each(data.items, function(i,item){
	       		
	       			$.ajax({
					   type: "POST",
					   url: "/session",
					   data: "id=" + item.id + "&name=" +item.name+ "&latlng=" + item.latlng + "&street=" + item.street + "&housenumber=" + item.housenumber+ "&city=" + item.city + "&zipcode=" + item.zipcode + "&phonenumber=" + item.phonenumber+ "&type=" + item.type,
					   success: function(msg){
							drawBasket();
					   }
					 });
	       		});
	        });
			return false;
		}
		
	});	
	
});

function drawBasket(){
	$.getJSON("/Jscontent/getRoute",function(data){
		$('#mandje').html('');
		$.each(data.items, function(i,item){
			$('#mandje').append('<li><b>'+(i+1)+'.</b> '+item.name+'</li>');
       	});
	
	});
}

$(".newsnumber").click(function(e){
	target = e.target;
	$(".news_header").css("display", "none");
	$(".news" + target.rel).css("display", "block");
});