var map;
var areas = [];

var rules= {
	'.wd' : function(el) {
		el.onclick = function() {
			var params = Form.serialize($('navi'));
		//	console.log(params);
			new Ajax.Request('./hawoma.class.php?getallorte=1',{parameters:params,onSuccess:process_it});
		
		}
	
	
	},
	'#dealer' : function(el) {
		el.onclick = function() {
			Effect.BlindDown('hoeker',{duration:.1});
			
			Effect.BlindUp('gps',{duration:1.0});
			
		}
	
	},
	'#dealerwahl' : function(el) {
		el.onchange = function() {
			new Ajax.Request('./dealer.class.php?getdealer=1',{
					parameters:'did='+$F('dealerwahl'), 
					onSuccess:function(a) {
						//console.log(a.responseText);
						var data = eval('(' + '{"dealer": '+ a.responseText +'}' +')');
						//console.log(data.dealer);
						$('motto').value = data.dealer.motto;
						$('kontakt').value =  data.dealer.kontakt;
						$('produkte').value = data.dealer.produkte;
						$('bem').value = data.dealer.bem;
						$('titel').innerHTML = '„' +data.dealer.titel + '“';
						
					}});
		}
	
	},


	'#stadtteile,#map' : function(el) {
		Element.setOpacity(el, 0.8);
	},
	'#zeiten' : function(el) {
		Element.setOpacity(el, 0.8);
	},

	'#map' : function(el) {
		map = new GMap2($("map"));
  		map.setMapType(G_NORMAL_MAP);
	    map.addControl(new GSmallMapControl());
    	map.setCenter(new GLatLng(53.59, 10.0), 11);
 		window.setTimeout(function(){GDownloadUrl("./hawoma.class.php?getallorte=1", process_it);},1);
		window.setTimeout(function(){GDownloadUrl("./hawoma.class.php?getteile=1",fillSelect);},300);
	}
};

var gmarkers = [];
var htmls = [];
var i = 0;

Event.observe(window, 'load', init_app);
Event.observe(window, 'unload', GUnload);

function init_app(event) {
 	Behaviour.register(rules);
 	Behaviour.apply(rules);
  
   
}


process_it = function(a) {
	map.clearOverlays();
	var data = (typeof(a) == 'object')
		? eval('(' + '{"markers": '+a.responseText+'}' + ')')
		: eval('(' + '{"markers": '+a+'}' + ')');
	for (var i=0; i<data.markers.length; i++) {
    	var item = data.markers[i];
    	var pts = [];
 		var total = item.polygon.length;
 		for (var j=0; j<total; j++) {
 		    var vertex = item.polygon[j];
	  		pts[j] = new GLatLng(vertex.lat,vertex.lng);
	  	}
	  	var poly = new GPolygon(pts, "#222222",1, 0.7, "#ff0000", 0.5 );
	  	item.area = Math.round(poly.getArea());
	  	map.addOverlay(poly); 
    	map.addOverlay(createMarker(new GLatLng(item.marker.lat, item.marker.lng),item));
    }
}          

  

fillSelect = function(json) {
	var elem = $('stadtteile');
	elem.size = 9;
	while (elem.length) elem.options[0] = null;
	var teile = eval("(" + json + ")");
//	console.log(teile);
   	elem.options[elem.length] = new Option('alle Stadtteile',i,true);
	for (var i in  teile) {
		elem.options[elem.length] = new Option(teile[i],i);
	}
}



createMarker = function (point,item) {
		var icon =  new GIcon(icon, "http://tools.webmasterei.com/hawoma/wagen.png" , null);
        icon.iconSize = new GSize(24,18);
      //  icon.shadowSize = new GSize(32,32);
        icon.iconAnchor = new GPoint(5,12);
        icon.infoWindowAnchor = new GPoint(16,0);
     //  icon.infoShadowAnchor = new GPoint(6,22);
        var marker = new GMarker(point,{icon:icon,title:item.name});
        var html = '<h1>' + item.name  + ' (' + item.stadtteil + ')</h1><table cellpadding=0 cellspacing=0><tr><th>Markttage</th><th>&nbsp;&nbsp;&nbsp;</th><th>Kontakt</th></tr><tr><td valign="top"><code>';
        for (var i =0 ; i< item.zeiten.length; i++ ) {
        	html += ('<font face="monospace"><br/>'+item.zeiten[i].name+':&nbsp;' +item.zeiten[i].start +'&ndash;'+item.zeiten[i].stop + '&nbsp;Uhr'); 
        }
        html += ('</td><td></td><td><i>'+item.kontakt+'</td></tr></table>'); 
        html += 'Fl&auml;che: ≈ ' + item.area + ' m<sup>2</sup>';
        
        GEvent.addListener(marker, "click", function() {
          	 map.setMapType(G_HYBRID_MAP);

          	 map.setCenter(point, 16);
	
      
      			marker.openInfoWindowHtml(html);
          // console.log(item);
        });
        GEvent.addListener(marker, "infowindowopen", function(m) {
        });
        GEvent.addListener(marker, "infowindowclose", function(m) {
        	 map.setZoom(12);
        	 map.setMapType(G_NORMAL_MAP);
	
        });
        return marker;
  }
      
      

