var r;

function createmap() {

//$(function(){
	
	$.ajax({
	  type: "GET",
	  url: "include/bavaria_paths.js",
	  dataType: "script",
	  success: function(html){	
			document.getElementById("bavaria-map").style.visibility = "visible";
			highlightmap();			
		}
	});
			
//});
}

function showData(typ,loc) {
	//alert('index.php?id=auswertung&'+typ+'='+loc);
	window.location = 'index.php?id=auswertung&'+typ+'='+loc;
}

function showRegion(id) {
	$target = $("#o"+id);
	$target.attr({
		//fill: '#1669AD'				
	});
}
function hideRegion(id) {
	$target = $("#o"+id);
	$target.attr({
		//fill: '#fff'
	});
}

function showBezirk(id) {
	$target = $("#o"+id);
	$target.attr({
		//fill: '#1669AD'				
	});
}
function hideBezirk(id) {
	$target = $("#o"+id);
	$target.attr({
		//fill: '#fff'
	});
}

function highlightmap() {

	document.getElementById("loadersmall").style.visibility = "visible";
	$('#legende0').fadeOut('slow');
	$('#legende1').fadeOut('slow');
	$('#legende2').fadeOut('slow');
	
	var anzeige 	= document.getElementById("anzeige").value;
	var vogelart  = document.getElementById("vogelart").value;
	
	/*
	if(vogelart!="0") {
		document.getElementById("selprozent").style.display = "block";
	} else {
		document.getElementById("selprozent").style.display = "none";
		if(anzeige=="1") document.getElementById("seldurchschnitt").selected = "selected";
	}
	*/
	
	r = Raphael('bavaria-map'),
		arr = new Array();	
		
	r.clear();
			
	$.ajax({			
		url: 'include/bavaria_highlight.php?m='+anzeige+'&v='+vogelart,			
		type: 'GET',			
		dataType: 'xml',			
		timeout: 110000,			
		error: function(){		
		},			
		success: function(xml){	
			var anz_items = 0;				
			$(xml).find('region').each(function(){
				var bez = $(this).find('name').text();
				var val = $(this).find('wert').text();	
				
				var b = bez.replace(/-/g, "");
				b = b.replace(/\./g, "");
				b = b.replace(/ /g, "");
				b = b.replace(/\(/g, "");
				b = b.replace(/\)/g, "");				
											
				if(paths[b].link=="st") {
					var bg = r.path(paths[b].path);
					attributes = {
			        fill: '#fff',
			        'fill-opacity': 1, 
			        stroke: '#000000',
			        'stroke-width': 1,
			        'stroke-linejoin': 'round'
			    };	        	
					bg.attr(attributes);
				}
				
				/*var obj 			= r.path(paths[b].path);
				var attr 			= {font: "9px Helvetica"};
				var labeltext = obj.getBBox(0);
				var myx 			= labeltext.x+(labeltext.width/2)+paths[b].xkorr;
				var myy 			= labeltext.y+(labeltext.height/2)+paths[b].ykorr;
				r.text(myx, myy, paths[b].label).attr(attr);		*/
				
				var obj = r.path(paths[b].path);
								
				c = "ffffff";
								
				if(anzeige==0) { // Durchschnitt Vögel pro Garten
					c = "BDBDBD";
					if(parseFloat(val)>=0.1)	c = "868A08"; //"9900cc";
					if(parseFloat(val)>=0.25)	c = "66cc00"; //"01af00";
					if(parseFloat(val)>=0.75)	c = "339900"; //"ffff01";
					if(parseFloat(val)>=1.5) 	c = "336600"; //"ff8001";
					if(parseFloat(val)>=3)   	c = "003300"; //"fe3233";
				}
				if(anzeige==1) { // in % der Gärten
					c = "0101DF";
					if(parseFloat(val)<=90)	c = "2E64FE";
					if(parseFloat(val)<=50)	c = "2E9AFE";
					if(parseFloat(val)<=20)	c = "A9D0F5";
					if(parseFloat(val)<=10)	c = "BDBDBD";
				}
				if(anzeige==2) { // Schwarmgröße
					c = "FE9A2E";
					if(parseFloat(val)<=8)	$c = "FF8000";
					if(parseFloat(val)<=6)	c = "FFBF00";
					if(parseFloat(val)<=4)	c = "F7D358";
					if(parseFloat(val)<=2)	c = "BDBDBD";
				}
				
				attributes = {
		        fill: '#'+c,
		        'fill-opacity': 0.6, 
		        stroke: '#000000',
		        'stroke-width': 1,
		        'stroke-linejoin': 'round',
		        title: paths[b].name,
		        cursor: 'pointer'
		    };	
						
				obj.attr(attributes);	
							
				obj.click(function(){
					showData(paths[b].link,paths[b].name);
				});				
		
			});
			document.getElementById("loadersmall").style.visibility = "hidden";
			$('#legende'+anzeige).fadeIn('slow');
		}
	});		
}
