onloadHooks.push(function(){
	tx_dsgapetitiontool_pi2_initialize();
	//markerManage();
});

/*function markerManage() {
	var mgr = new MarkerClusterer(map, markers, {
        // maxZoom: zoom,
        // gridSize: size,
        // styles: styles[style]
      });
}*/

var markers = [];
var labels = [];
var infowindows = [];
var data = '';
var urlBase = '';
var scrollElement = 0;
var zeitraum = "";

function injectWaitingLayer(){
	//console.log("refresh");
	$$('#google_map_waiting_layer').setStyles({	'width': '860px', 
												'height': '305px',
												'background-color': '#ffffff',
												'opacity': '0.7',
												'padding-top': '220px',
												'text-align': 'center',
												'overflow': 'hidden',
												'top': '135px'
	});
	//417
}
function hideWaitingLayer(){
	$$('#google_map_waiting_layer').setStyles({	'width': '1px', 
												'height': '1px'
	});
}

function scrollUp() {
	$$('.contentWrap').each(function(item){
		var myFx = new Fx.Scroll(item, {
		    duration: 1000,
		    wait: false
		}).toElement(item.getChildren('.stimmeLayer')[(scrollElement - 1)]);
		scrollElement = scrollElement - 1;
		//item.getChildren()[1].getChildren()[0].getChildren()[(scrollElement + 2)]
	});
}

function scrollDown(eventmap) {
	$$('.contentWrap').each(function(item){

		if(item.getChildren('.stimmeLayer').length <= (scrollElement + 3)){			
			var infocontent = new Array();	
			infocontent = getInfoContent($$('.zipstore')[0].innerHTML,item.getChildren('.stimmeLayer').length+',10','0',eventmap);
			var length = infocontent.length;
			var i = length - 1;
			for(i; i >= 0; i--){				
				item.appendChild(infocontent[i]);
			}	
			$$('.stimmeLayer').each(function(item){
				if(item.getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('height').toInt() > 60)
				{
					item.getChildren('.textScrollerDown').addEvent('mousedown',function(scrollerDown){
						item.interval = (function(event){
							var top = scrollerDown.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('top').toInt()-10;
							if( (top * -1) < item.getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('height').toInt()-50 ) {
								scrollerDown.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].setStyle('top',top);
							}
						}.bind(item).periodical(1));
					}.bind(item));
					  
					//});
					item.getChildren('.textScrollerDown').addEvent('mouseup',function(scrollerDown){
						$clear(item.interval);
					}.bind(item));
					
					item.getChildren('.textScrollerUp').addEvent('mousedown',function(scrollerUp){
						item.interval = (function(event){
							var top = scrollerUp.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('top').toInt()+10;
							if(top <= 0){ 
								scrollerUp.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].setStyle('top',top);
							}
						}.bind(item).periodical(1));
					}.bind(item));
					
					item.getChildren('.textScrollerUp').addEvent('mouseup',function(scrollerDown){
						$clear(item.interval);
					}.bind(item));
				} else {
					item.getChildren('.textScrollerDown').setStyle('display','none');
					item.getChildren('.textScrollerUp').setStyle('display','none');
				} 
			}); 
// EINKOMMENTIEREN WENN MULTIBOX AUF LIVE IST
			
			var multiBoxOnMap = eventmap.get('multiBox');
			if(!multiBoxOnMap){
				var initMultiBox = new multiBox({
					mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
					container: $(document.body),//where to inject multiBox
					descClassName: 'multiBoxDesc',//the class name of the description divs
					useOverlay: true,//use a semi-transparent background. default: false;
					maxSize: {w:600, h:400},//max dimensions (width,height) - set to null to disable resizing
					addDownload: false,//do you want the files to be downloadable?
					addRollover: false,//add rollover fade to each multibox link
					addOverlayIcon: false,//adds overlay icons to images within multibox links
					addChain: false,//cycle through all images fading them out then in
					recalcTop: true,//subtract the height of controls panel from top position
					addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
				});
				eventmap.set('multiBox',initMultiBox);
			} else {
				multiBoxOnMap.reset();
				multiBoxOnMap.initialize();
				eventmap.set('multiBox',multiBoxOnMap);
			}
		}
		var myFx = new Fx.Scroll(item, {
		    duration: 1000,
		    wait: false
		}).toElement(item.getChildren('.stimmeLayer')[(scrollElement + 1)]);
		scrollElement = scrollElement + 1;
		
		  
	});
}

function submitSearch(data,map,markers,labels) {
	var searchstring; //= $$('#suche')[0].get('value');
	$$('#suche').each(function(item){
		item.addEvent('keydown',  function(e) { 
			if(e.key == 'enter') { 
				searchstring = item.get('value');
				map.set('searchstring',searchstring);
				//map.set('scrollElementList',0);
				
				// Trigger für Andys Liste
				getListView(map,'','1');
				
				$$('#suche')[0].setStyle('display','none');
				$$('#submitSearch')[0].setStyle('display','none');
				//$$('#sucheLabel')[0].setStyle('display','none');
				$$('#searchReset')[0].setStyle('display','block');

				//refreshMarkers(data,map,markers,map.zoom,map.getBounds().getNorthEast().b,map.getBounds().getNorthEast().c,map.getBounds().getSouthWest().b,map.getBounds().getSouthWest().c,labels,searchstring); 
				refreshMarkers(data,map,markers,map.zoom,map.getBounds().getNorthEast().Da,map.getBounds().getNorthEast().Fa,map.getBounds().getSouthWest().Da,map.getBounds().getSouthWest().Fa,labels,searchstring); 
			} 
		});
	});  
	$$('#submitSearch').each(function(item){
		//console.log(item);
		item.addEvent('click',function(){
			//console.log(map);
			searchstring = $$('#suche')[0].get('value');
			map.set('searchstring',searchstring);
			//map.set('scrollElementList',0);
			// Trigger für Andys Liste
			getListView(map,'','1');
			
			$$('#suche')[0].setStyle('display','none');
			$$('#submitSearch')[0].setStyle('display','none');
			//$$('#sucheLabel')[0].setStyle('display','none');
			$$('#searchReset')[0].setStyle('display','block');
			//refreshMarkers(data,map,markers,map.zoom,map.getBounds().getNorthEast().b,map.getBounds().getNorthEast().c,map.getBounds().getSouthWest().b,map.getBounds().getSouthWest().c,labels,searchstring);
			refreshMarkers(data,map,markers,map.zoom,map.getBounds().getNorthEast().Da,map.getBounds().getNorthEast().Fa,map.getBounds().getSouthWest().Da,map.getBounds().getSouthWest().Fa,labels,searchstring);
			//console.log($$('#suche')[0].get('value'));
		});
	});
}

function clearMarkers(data,map,markers,labels) {
	//console.log("data.count "+data.count);
	//console.log("markers.count "+markers);
	markers.each(function(marker){
		
		marker.setMap(null);
	});
	
	labels.each(function(label){
		label.unbind(map);
		label.setMap(null);
	});
	//for (var i = 0; i < data.count; i++) {
		
		//markers[i].setMap(null);
		//map.removeOverlay(markers[i]);
	//}
}
function destroyMarkers(markers) {
	//console.log(markers);
	markers = [];
	//console.log(markers);
}

function buildMarkers(data,markers,map,labels) {
	//console.log("data.count "+data.count);
	//console.log("markers "+markers);
	if(markers[0]) {
		clearMarkers(data,map,markers,labels);
		destroyMarkers(markers);
	}
	//
	//console.log(data);
	//console.log(data.count);
	var image = new google.maps.MarkerImage('typo3conf/ext/dsga_petitionstool/res/img/blind.png');
	/*var shape = {
	      coord: [1, 1, 1, 20, 18, 20, 18 , 1],
	      type: 'poly'
	};*/

	
	if(data){
		for (var i = 0; i < data.count; i++) {
			//console.log(i);
			var latLng = new google.maps.LatLng(data.zips[i].latitude, data.zips[i].longitude);
			//console.log(markers);
			markers[i] = new google.maps.Marker({
				position: latLng,
				draggable: false,
				title: data.zips[i].title.toString(),
				flat: true,
				icon: image
				//shape: shape
			});

			//console.log(markers);
			markers[i].setMap(map);
			markers[i].set('zips',data.zips[i].zips);
			labels[i] = new Label({
				map: map
			});
			labels[i].bindTo('position', markers[i], 'position');
			labels[i].set('stimmen', data.zips[i].title); 
			labels[i].bindTo('text', markers[i], 'position');
			
			markers[i].set('markernr', i); 
	
			google.maps.event.addListener(markers[i], 'click', function(e) {
				var oldinfobox = map.get('infobox')
				if(oldinfobox) {
					oldinfobox.close();
				}
				
				var infobox = new SmartInfoWindow({position: this.getPosition(), 
					 map: map, 
					 content: getInfoContent(this.zips,'10','1',map),
					 zIndex: 600,
					 votes: this.title
				});
				map.set('infobox',infobox);
				
				var initMultiBox = new multiBox({
					mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
					container: $(document.body),//where to inject multiBox
					descClassName: 'multiBoxDesc',//the class name of the description divs
					useOverlay: true,//use a semi-transparent background. default: false;
					maxSize: {w:600, h:400},//max dimensions (width,height) - set to null to disable resizing
					addDownload: false,//do you want the files to be downloadable?
					addRollover: false,//add rollover fade to each multibox link
					addOverlayIcon: false,//adds overlay icons to images within multibox links
					addChain: false,//cycle through all images fading them out then in
					recalcTop: true,//subtract the height of controls panel from top position
					addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
				});
	
			});
			if(data.count == '1' && markers[i].title == '1') {
				google.maps.event.trigger(markers[i], 'click');
				if($$('#listViewContent')[0].getStyle("visibility")== "visible"){
					$$('#listViewContent')[0].setStyle("visibility","hidden");
				}
				//markers[i].fireEvent('click');
			}
		}
	}
}

function refreshMarkers(data,map,markers,zoom,maxLat,minLat,maxLng,minLng,labels,searchstring,searchid,videoFilter,bilderFilter,botschaftFilter,wk003,wk013,wk149,wk173,wk274) {

	if (searchstring == '' || !searchstring){
		searchstring = map.get('searchstring');
		if (!searchstring){
			searchstring = '';
		}
	}
	if(!videoFilter) {
		videoFilter = map.get('videoFilter');
	}
	if(!bilderFilter) {
		bilderFilter = map.get('bilderFilter');
	}
	if(!botschaftFilter) {
		botschaftFilter = map.get('botschaftFilter');
	}
	if(!wk003) {
		wk003 = map.get('wk003');
	}
	if(!wk013) {
		wk013 = map.get('wk013');
	}
	if(!wk149) {
		wk149 = map.get('wk149');
	}
	if(!wk173) {
		wk173 = map.get('wk173');
	}
	if(!wk274) {
		wk274 = map.get('wk274');
	}

	if(searchstring == 'none') {
		searchstring = '';
	}
	if (!searchid){
		searchid = map.get('searchid');
		if (!searchid){
			searchid = '';
		}
	}
	
	var myResponse = new String;
	injectWaitingLayer();
	var req = new Request.JSON({
		method: 'get',
		url: urlBase+'index.php?id=38&json=1&range=70',
		encoding: 'utf-8',
		noCache: true,
		onSuccess: function(responseJSON, responseText) {
			
			// JSON-Array auslesen, zurück kommt data
			data = responseJSON; //meval(responseJSON);
			buildMarkers(data,markers,map,labels);
			hideWaitingLayer();
		}
	})

	if(maxLat == ''){																																	// videoFilter,bilderFilter,botschaftFilter
		req.send('zoom='+zoom+'&maxLat='+maxLat+'&maxLng='+maxLng+'&minLat='+minLat+'&minLng='+minLng+'&searchstring='+searchstring+'&searchid='+searchid+'&videoFilter='+videoFilter+'&bilderFilter='+bilderFilter+'&botschaftFilter='+botschaftFilter+'&zeitraum='+zeitraum+'&wk003='+wk003+'&wk013='+wk013+'&wk149='+wk149+'&wk173='+wk173+'&wk274='+wk274);
	}else{
		//req.send('zoom='+zoom+'&maxLat='+map.getBounds().getNorthEast().Ca+'&maxLng='+map.getBounds().getNorthEast().Ea+'&minLat='+map.getBounds().getSouthWest().Ca+'&minLng='+map.getBounds().getSouthWest().Ea+'&searchstring='+searchstring+'&searchid='+searchid+'&videoFilter='+videoFilter+'&bilderFilter='+bilderFilter+'&botschaftFilter='+botschaftFilter+'&zeitraum='+zeitraum+'&wk003='+wk003+'&wk013='+wk013+'&wk149='+wk149+'&wk173='+wk173+'&wk274='+wk274);
		req.send('zoom='+zoom+'&maxLat='+map.getBounds().getNorthEast().Da+'&maxLng='+map.getBounds().getNorthEast().Fa+'&minLat='+map.getBounds().getSouthWest().Da+'&minLng='+map.getBounds().getSouthWest().Fa+'&searchstring='+searchstring+'&searchid='+searchid+'&videoFilter='+videoFilter+'&bilderFilter='+bilderFilter+'&botschaftFilter='+botschaftFilter+'&zeitraum='+zeitraum+'&wk003='+wk003+'&wk013='+wk013+'&wk149='+wk149+'&wk173='+wk173+'&wk274='+wk274);
	}
}

function getInfoContent(zips,fromto,init,map){
	
	var videoFilter = map.get('videoFilter');
	var bilderFilter = map.get('bilderFilter');
	var botschaftFilter = map.get('botschaftFilter');
	var wk003 = map.get('wk003');
	var wk013 = map.get('wk013');
	var wk149 = map.get('wk149');
	var wk173 = map.get('wk173');
	var wk274 = map.get('wk274');
	
	var searchstring = $$('#suche')[0].get('value');
	var myResponse;
	if (init != 1) {
		myResponse = new Array();
	}
	var searchid = map.get('searchid');
	if(!searchid){
		searchid = '';
	}
		
	var data = '';
	injectWaitingLayer()
	var req = new Request.HTML({
		method: 'get',
		//url: urlBase+'index.php?id=38&json=2&fromto='+fromto+'&initContent='+init+'&searchstring='+searchstring,
		url: urlBase+'aktiv-werden.html?tx_dsgapetitionstool_pi1[list]=2&limit='+fromto,
		encoding: 'utf-8',
		noCache: true,
		async: false,
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			if (init == 1) {
				myResponse = responseHTML;
			} else {
				
				myResponse = responseTree;
			}
			hideWaitingLayer()
		}
	})
	req.send('zips='+zips+'&searchid='+searchid+'&searchString='+searchstring+'&videoFilter='+videoFilter+'&bilderFilter='+bilderFilter+'&botschaftFilter='+botschaftFilter+'&zeitraum='+zeitraum+'&wk003='+wk003+'&wk013='+wk013+'&wk149='+wk149+'&wk173='+wk173+'&wk274='+wk274);
	return myResponse;

}

function tx_dsgapetitiontool_pi2_initialize() {

	document.getElementById('check2011').checked = true;
	currentBox = 'check2011';
	zeitraum = '2011';

	var myLatlng = new google.maps.LatLng(geoStartLat,geoStartLng);

	var myOptions = {
	  zoom: 6,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.TERRAIN,
	  mapTypeControl: false
	};
	var map = new google.maps.Map(document.getElementById(prefixId+"_map_canvas"), myOptions);
	
	var idleEvent = google.maps.event.addListener(map, 'idle', function(map) {
		//refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().b,this.getBounds().getNorthEast().c,this.getBounds().getSouthWest().b,this.getBounds().getSouthWest().c,labels,'');
		//refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().Ca,this.getBounds().getNorthEast().Ea,this.getBounds().getSouthWest().Ca,this.getBounds().getSouthWest().Ea,labels,'');
		refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().Da,this.getBounds().getNorthEast().Fa,this.getBounds().getSouthWest().Da,this.getBounds().getSouthWest().Fa,labels,'');
	});

	map.set('idleEvent',idleEvent);
	google.maps.event.addListener(map, 'zoom_changed', function() {
	    if (this.getZoom() < 5) this.setZoom(5);
	});

	initializeMapEvents(map);
	submitSearch(data,map,markers,labels);
}

function onClickMeldenMap(userId){
	
	document.getElementById('hiddenId').value = userId;

	document.getElementById('dsgaMeldenBoxMap').style.display = 'block';
	
	if(document.getElementById('misuseConfirm')){
		document.getElementById('misuseConfirm').style.display = 'none';
	}

	if(document.getElementById('melden').style.display == "none"){
		document.getElementById('melden').style.display = "block";
		document.getElementById('melden').value = "";
	}
	if(document.getElementById('submitMelden').style.display == "none"){
		document.getElementById('submitMelden').style.display = "block";
	}

	if(document.getElementById('desc').style.display == "none"){
		document.getElementById('desc').style.display = "block";
	}
}

function initializeMapEvents(map){

	if(deeplink != '') {
		var searchid = deeplink;
		var searchstring = '';
		map.set('searchid',searchid);
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,searchstring,searchid);
		$$('#suche')[0].setStyle('display','none');
		$$('#submitSearch')[0].setStyle('display','none');
		//$$('#sucheLabel')[0].setStyle('display','none');
		$$('#searchReset')[0].setStyle('display','block');
	}
	
	$$('.list_view').each(function(item){
		//console.log(item);
		item.addEvent('click',function(){
			if($$('#listViewContent')[0].getStyle('visibility') != "visible"){
				if($$('#listViewContent')[0].getStyle("visibility")== "hidden"){
					$$('#listViewContent')[0].setStyle("visibility","visible");
				}				
				if(!$$('#listViewContainer')[0].getChildren()[0]){
					getListView(map,'','1');
				}
			}
			var oldinfobox = map.get('infobox')
			if(oldinfobox) {
				oldinfobox.close();
			}
		});
	});	
	$$('.deineStimme').each(function(item){
		item.addEvent('click',function(){

			var searchid = $$('#hiddenUserId')[0].get('value');
			var searchstring = '';

			map.set('searchid',searchid);
			refreshMarkers(data,map,markers,map.zoom,'','','','',labels,searchstring,searchid);

			$$('#suche')[0].setStyle('display','none');
			$$('#submitSearch')[0].setStyle('display','none');
			$$('#searchReset')[0].setStyle('display','block');
		});
	});
	$$('#searchReset')[0].addEvent('click',function(){
		$$('#suche')[0].set('value','');
		map.set('searchid','');
		map.set('searchstring','');

		// Trigger für Andys Liste
		getListView(map,'','1');

		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','');
		$$('#suche')[0].setStyle('display','block');
		$$('#submitSearch')[0].setStyle('display','block');
		$$('#searchReset')[0].setStyle('display','none');
	});

	$('filter').addEvent('mouseover',function(){
		$('filter_options').setStyles({'display':'block',
				'z-index': '599',
				'position':'absolute',
				'width':'220px'});
	});
	$('filter').addEvent('mouseleave',function(){
		$('filter_options').setStyles({'display':'none'});
	});

	$('videoFilter').addEvent('click',function(){
		map.set('videoFilter','1');
		//	video bilder botschaft
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','1','','','','','','','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
		//map.set('scrollElementList',0);
	});
	$('bilderFilter').addEvent('click',function(){
		map.set('bilderFilter','1');
		//																video bilder botschaft
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','1','','','','','','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
		//map.set('scrollElementList',0);
	});
	$('botschaftFilter').addEvent('click',function(){
		map.set('botschaftFilter','1');
		//																video bilder botschaft
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','1','','','','','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
		//map.set('scrollElementList',0);
	});
	$('wk003').addEvent('click',function(){
		map.set('wk003','1');
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','','1','','','','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
	});
	$('wk013').addEvent('click',function(){
		map.set('wk013','1');
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','','','1','','','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
	});
	$('wk149').addEvent('click',function(){
		map.set('wk149','1');
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','','','','1','','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
	});
	$('wk173').addEvent('click',function(){
		map.set('wk173','1');
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','','','','','1','');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
	});
	$('wk274').addEvent('click',function(){
		map.set('wk274','1');
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','','','','','','1');
		getListView(map,'','1');
		$('filter').setStyles({'display':'none'});
		$('filterReset').setStyles({'display':'block'});
	});
	$('filterReset').addEvent('click',function(){
		$('filter').setStyles({'display':'block'});
		$('filterReset').setStyles({'display':'none'});
		map.set('botschaftFilter','');
		map.set('bilderFilter','');
		map.set('videoFilter','');
		map.set('wk003','');
		map.set('wk013','');
		map.set('wk149','');
		map.set('wk173','');
		map.set('wk274','');
		refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','');
		getListView(map,'','1');
	});

	if($('listViewContent').getStyle('visibility') == 'hidden'){

		$('check2011').addEvent('click',function(){
			refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','');
			//getListView(map,'','1');
		});

		$('check2010').addEvent('click',function(){
			refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','');
			//getListView(map,'','1');
		});

		$('checkAll').addEvent('click',function(){
			refreshMarkers(data,map,markers,map.zoom,'','','','',labels,'','','','','');
			//getListView(map,'','1');
		});
	}

	$$('#listScrollerTop').each(function(item){

		item.addEvent('click',function(){
			var scrollElementList;
			if(map.get('scrollElementList')){
				scrollElementList = map.get('scrollElementList').toInt();
			} else {
				scrollElementList = 0;
			}
			if(!scrollElementList) {
				scrollElementList = 0;
			}

			if(scrollElementList > 0){
				$$('#listViewContainer').each(function(item){
					var myFx = new Fx.Scroll(item, {
					    duration: 1000,
					    wait: false
					}).toElement(item.getChildren()[0].getChildren('.stimmeLayer')[(scrollElementList - 2)]);
					scrollElementList = scrollElementList - 2;
					map.set('scrollElementList',scrollElementList);
				});
			}
		});
	});

	$$('#listScrollerBottom').each(function(item){
		item.addEvent('click',function(){

			var scrollElementList;
			if(map.get('scrollElementList')){
				scrollElementList = map.get('scrollElementList').toInt();
			} else {
				scrollElementList = 0;
			}
			if(!scrollElementList) {
				scrollElementList = 0;
			}
			$$('#listViewContainer').each(function(item){

				if(item.getChildren()[0].getChildren('.stimmeLayer').length <= (scrollElementList + 6) 
				//		 map.get('scrollElementListMax') <= (scrollElementList + 3)
				){
					var infocontent = new Array();	
					infocontent = getListView(map,(item.getChildren()[0].getChildren('.stimmeLayer').length)+',8','');
				} else {
					$$('#listViewContainer').each(function(item){
						var myFx = new Fx.Scroll(item, {
						    duration: 1000,
						    wait: false
						}).toElement(item.getChildren()[0].getChildren('.stimmeLayer')[(scrollElementList + 2)]);
						scrollElementList = scrollElementList + 2;
						map.set('scrollElementList',scrollElementList);
					});
				}
			});
		});
	});
}

// Custom Labels für Marker
//overlay definieren, vererbt von google.maps.OverlayView
function Label(opt_options) {
 // Initialization
 this.setValues(opt_options);

 // Labelspezifisch
 var span = this.span_ = document.createElement('span');
 //span.style.cssText = 'position: relative; left: -50%; top: -16px; ' +
 span.style.cssText = 'position: relative; left: -21px; top: -16px; ' +
                      'white-space: nowrap; border: 2px solid #ffffff; ' +
                      'padding: 2px 2px 2px 21px; background-color: #000000; color: #ffffff;' + 
                      'font-size: 10px; background-image: url(typo3conf/ext/'+extKey+'/res/img/haken_orange.gif);' +
                      'background-repeat: no-repeat;';
 
 var div = this.div_ = document.createElement('div');
 div.appendChild(span);
 
 div.style.cssText = 'position: absolute; display: none';
};
Label.prototype = new google.maps.OverlayView;

// Implement onAdd
Label.prototype.onAdd = function() {
 var pane = this.getPanes().overlayLayer;
 pane.appendChild(this.div_);

 // Ensures the label is redrawn if the text or position is changed.
 var me = this;
 this.listeners_ = [
   google.maps.event.addListener(this, 'position_changed',
       function() { me.draw(); }),
   google.maps.event.addListener(this, 'text_changed',
       function() { me.draw(); })
 ];
};

// Implement onRemove
Label.prototype.onRemove = function() {
 this.div_.parentNode.removeChild(this.div_);

 // Label is removed from the map, stop updating its position/text.
 for (var i = 0, I = this.listeners_.length; i < I; ++i) {
   google.maps.event.removeListener(this.listeners_[i]);
 }
};

// Implement draw
Label.prototype.draw = function() {
 var projection = this.getProjection();
 var position = projection.fromLatLngToDivPixel(this.get('position'));

 var div = this.div_;
 div.style.left = position.x + 'px';
 div.style.top = position.y + 'px';
 div.style.display = 'block';

 this.span_.innerHTML = this.get('stimmen').toString();
 //this.span_.innerHTML = 'test';
};


/* An SmartInfoWindow is like an info window, but it displays
 * under the marker, opens quicker, and has flexible styling.
 * @param {Object} opts Passes configuration options.
 */
function SmartInfoWindow(opts) {
  google.maps.OverlayView.call(this);
  this.latlng_ = opts.position;
  this.content_ = opts.content;
  this.votes_ = opts.votes;
  this.map_ = opts.map;
  if(this.votes_.toInt() > 1) {
	  this.height_ = 400;
	  this.width_ = 430;
  } else {
	  this.height_ = 212;
	  this.width_ = 430;  
  }
  this.size_ = new google.maps.Size(this.height_, this.width_);
  //this.offsetVertical_ = -this.height_;
  this.offsetVertical_ = 0;
  this.offsetHorizontal_ = 0;
  this.panned_ = false;
  this.setMap(this.map_);

  // We need to listen to bounds_changed event so that we can redraw
  // absolute position every time the map moves.
  // This is only needed because we append to body instead of map panes.
  var me = this;
  google.maps.event.addListener(this.map_, 'bounds_changed', function() {
    me.draw();
  });
}

/**
 * SmartInfoWindow extends GOverlay class from the Google Maps API
 */
SmartInfoWindow.prototype = new google.maps.OverlayView();

/**
 * Creates the DIV representing this SmartInfoWindow
 */
SmartInfoWindow.prototype.onRemove = function() {
	
	var map = this.map_;
	var idleEvent = map.get('idleEvent');
	idleEvent = google.maps.event.addListener(map, 'idle', function(map) {
		//							zoom	,maxLat							  ,minLat							,maxLng										  ,minLng
	//refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().b,this.getBounds().getNorthEast().c,this.getBounds().getSouthWest().b,this.getBounds().getSouthWest().c,labels,'');
	refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().Da,this.getBounds().getNorthEast().Fa,this.getBounds().getSouthWest().Da,this.getBounds().getSouthWest().Fa,labels,'');
	});
	map.set('idleEvent',idleEvent)
	
	
	if (this.div_) {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
  }
};

/**
 * Called when the overlay is first added to the map.
 */
SmartInfoWindow.prototype.onAdd = function() {
  // Creates the element if it doesn't exist already.
  this.createElement();
  var map = this.map_;
  var idleEvent = map.get('idleEvent');
  google.maps.event.removeListener(idleEvent);
};

/**
 * Redraw based on the current projection and zoom level.
 */
SmartInfoWindow.prototype.draw = function() {
  // Since we use bounds changed listener, projection is sometimes null
  if (!this.getProjection()) {
    return;
  }
  
  var map = this.map_;
  var idleEvent = map.get('idleEvent');
  google.maps.event.removeListener(idleEvent);
  
  
  // This gives us the position in the tiles div.
  var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
  var centerPosition = this.getProjection().fromLatLngToDivPixel(this.map_.getCenter());
  var centerPositionReal = new google.maps.Point(this.map_.getDiv().offsetWidth/2, this.map_.getDiv().offsetHeight/2);
  // Figure out difference between map div and tiles div, so that we can
  // calculate position in map div
  var centerOffsetX = -centerPosition.x + centerPositionReal.x;
  var centerOffsetY = -centerPosition.y + centerPositionReal.y;

  //var centerOffsetX = 0;
  //var centerOffsetY = 0;
  //
  if (!pixPosition) return;
  var alignment = this.getBestAlignment();
  var paddingTop = 0;
  var paddingLeft = 0;
  var widthLess = 0;
  this.zIndex = 600;
  
  

  //this.offsetX_ = 20;//6;
  //this.offsetY_ = -110;
  if(this.votes_.toInt() > 1) {
	  this.offsetX_ = -220;//6;
	  //this.offsetY_ = -110;
	  this.offsetY_ = -17-192;
  }
  else {
	  this.offsetX_ = -220;//6;
	  //this.offsetY_ = 75;
	  this.offsetY_ = -85;
  }
  //this.offsetY_ = -(this.height_ / 2);
  paddingLeft = 0;
  paddingRight = 0;
  paddingTop = 0;
  paddingBottom = 20;
  widthLess = 11;
  heightLess = 16;
  
  switch (alignment) {
    case SmartInfoWindow.Align.ABOVE:
      //this.width_ = 430;
      //this.height_ = 400;
      image = 'weiss.gif';
      /*this.offsetX_ = 200;//-(this.width_ / 2 - 17);
      this.offsetY_ = 0;//-(this.height_ + 12);
     paddingLeft = 10;
      paddingRight = 10;
      paddingTop = 10;
      paddingBottom = 10;
      widthLess = 20;
      heightLess = 20;*/
      break;
    case SmartInfoWindow.Align.BELOW:
      //this.width_ = 430;
      //this.height_ = 400;
      image = 'weiss.gif';
      /*this.offsetX_ = 200;//-(this.width_ / 2 - 17);
      this.offsetY_ = 0;//-15;
      paddingLeft = 10;
      paddingRight = 10;
      paddingTop = 10;
      paddingBottom = 10;
      widthLess = 20;
      heightLess = 20;*/
      break;
    case SmartInfoWindow.Align.LEFT:
      //this.width_ = 430;
      //this.height_ = 400;
      image = 'weiss.gif';
      /*this.offsetX_ = 200;//-(this.width_) + 10;
      this.offsetY_ = 0;//-(this.height_ / 2 + 33);
      paddingLeft = 10;
      paddingRight = 10;
      paddingTop = 10;
      paddingBottom = 10;
      widthLess = 20;
      heightLess = 20;*/
      break;
    case SmartInfoWindow.Align.RIGHT:
      image = 'weiss.gif';
      //this.width_ = 430;
      //this.height_ = 400;
      /*this.offsetX_ = 200;//6;
      this.offsetY_ = 0;//-(this.height_ / 2 + 33);
      //this.offsetY_ = -(this.height_ / 2);
      paddingLeft = 10;
      paddingRight = 10;
      paddingTop = 10;
      paddingBottom = 10;
      widthLess = 20;
      heightLess = 20;*/
      break;
   }
  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.width_ + 'px';
  this.div_.style.left = (pixPosition.x + this.offsetX_) + centerOffsetX + 'px';
  this.div_.style.height = this.height_ + 'px';
  this.div_.style.top = (pixPosition.y + this.offsetY_) + centerOffsetY + 'px';
  //this.div_.style.paddingTop = paddingTop + 'px';
  //this.div_.style.paddingLeft = paddingLeft + 'px';
  this.div_.style.background = 'url("typo3conf/ext/'+extKey+'/res/img/box_sprechblase_unten.png")';
  this.div_.style.backgroundPosition = 'bottom left';
  this.div_.style.backgroundRepeat = 'no-repeat';
  //this.div_.style.border = 'none';

  this.div_.style.display = 'block';
  //this.div_.style.z-index = '1000';
  
  this.wrapperDiv_.style.width = (this.width_- widthLess) + 'px';
  this.wrapperDiv_.style.height = (this.height_- heightLess) + 'px';
  this.wrapperDiv_.style.marginTop = paddingTop + 'px';
  this.wrapperDiv_.style.marginLeft = paddingLeft + 'px';
  this.wrapperDiv_.style.overflow = 'hidden';
  this.wrapperDiv_.style.background = 'url("typo3conf/ext/'+extKey+'/res/img/weiss.gif")';
  this.wrapperDiv_.style.borderTop = '1px solid #808080';
  this.wrapperDiv_.style.borderRight = '1px solid #808080';
  this.wrapperDiv_.style.borderLeft = '1px solid #808080';
  this.wrapperDiv_.style.paddingLeft = '9px';
  if (!this.panned_) {
    this.panned_ = true;
    this.maybePanMap();
  }
  
  $$('.stimmeLayer').each(function(item){
		if(item.getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('height').toInt() > 60)
		{
			item.getChildren('.textScrollerDown').addEvent('mousedown',function(scrollerDown){
				item.interval = (function(event){
					var top = scrollerDown.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('top').toInt()-10;
					if( (top * -1) < item.getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('height').toInt()-50 ) {
						scrollerDown.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].setStyle('top',top);
					}
				}.bind(item).periodical(1));
			}.bind(item));
			  
			//});
			item.getChildren('.textScrollerDown').addEvent('mouseup',function(scrollerDown){
				$clear(item.interval);
			}.bind(item));
			
			item.getChildren('.textScrollerUp').addEvent('mousedown',function(scrollerUp){
				item.interval = (function(event){
					var top = scrollerUp.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].getStyle('top').toInt()+10;
					if(top <= 0){ 
						scrollerUp.target.getParent().getChildren('.botschaftContainer')[0].getChildren('.botschaftBox')[0].setStyle('top',top);
					}
				}.bind(item).periodical(1));
			}.bind(item));
			
			item.getChildren('.textScrollerUp').addEvent('mouseup',function(scrollerDown){
				$clear(item.interval);
			}.bind(item));
		} else {
			item.getChildren('.textScrollerDown').setStyle('display','none');
			item.getChildren('.textScrollerUp').setStyle('display','none');
		} 
	}); 
  	
//EINKOMMENTIEREN WENN MULTIBOX AUF LIVE IST
	
	var multiBoxOnMap = map.get('multiBox');
	if(!multiBoxOnMap){
		var initMultiBox = new multiBox({
			mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
			container: $(document.body),//where to inject multiBox
			descClassName: 'multiBoxDesc',//the class name of the description divs
			useOverlay: true,//use a semi-transparent background. default: false;
			maxSize: {w:600, h:400},//max dimensions (width,height) - set to null to disable resizing
			addDownload: false,//do you want the files to be downloadable?
			addRollover: false,//add rollover fade to each multibox link
			addOverlayIcon: false,//adds overlay icons to images within multibox links
			addChain: false,//cycle through all images fading them out then in
			recalcTop: true,//subtract the height of controls panel from top position
			addTips: false//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
		});
		map.set('multiBox',initMultiBox);
	} else {
		multiBoxOnMap.reset();
		multiBoxOnMap.initialize();
		map.set('multiBox',multiBoxOnMap);
	}
  
};

/**
 * Creates the DIV representing this SmartInfoWindow in the floatPane.  If the panes
 * object, retrieved by calling getPanes, is null, remove the element from the
 * DOM.  If the div exists, but its parent is not the floatPane, move the div
 * to the new pane.
 * Called from within draw.  Alternatively, this can be called specifically on
 * a panes_changed event.
 */
SmartInfoWindow.prototype.createElement = function() {
  var panes = this.getPanes();
  var div = this.div_;
  if (!div) {
    // This does not handle changing panes.  You can set the map to be null and
    // then reset the map to move the div.
    div = this.div_ = document.createElement('div');
    //div.style.border = '1px solid #808080';
    //div.style.background-color: = '#ffffff';
    div.style.position = 'absolute';
    //div.style.overflow = 'hidden';
    div.style.zIndex = '600';
    var wrapperDiv = this.wrapperDiv_ = document.createElement('div');
    var contentDiv = document.createElement('div');
    //contentDiv.addClass('contentWrap');
    contentDiv.className = 'contentWrap';
    contentDiv.style.overflow = 'hidden';
    contentDiv.style.height = '320px';
    var votesDiv = document.createElement('div');
    if(this.votes_.toInt() > 1) {
    	votesDiv.innerHTML = this.votes_+' Stimmen aus dieser Region';
    	votesDiv.style.position = 'absolute';
    	votesDiv.style.top = '10px';
    	votesDiv.style.fontWeight = 'bold';
    
    	
    } else {
    	div.style.height = '250px';
    }
    
    if (typeof this.content_ == 'string') {
      contentDiv.innerHTML = this.content_;
    } else {
      contentDiv.appendChild(this.content_);
    }

    var topDiv = document.createElement('div');
    topDiv.style.textAlign = 'right';
    topDiv.style.width = '398px';
    topDiv.style.paddingLeft = '10px';
    topDiv.style.paddingTop = '10px';
    //topDiv.style.paddingTop = '10px';
    var closeImg = document.createElement('img');
    closeImg.src = 'typo3conf/ext/'+extKey+'/res/img/iconClose.gif';
    closeImg.style.width = '9px';
    closeImg.style.height = '9px';
    closeImg.style.cursor = 'pointer';
    topDiv.appendChild(closeImg);

    function removeSmartInfoWindow(ib) {
      return function() {
        ib.setMap(null);
        scrollElement = 0;
      };
    }

    google.maps.event.addDomListener(closeImg, 'click', removeSmartInfoWindow(this));

    wrapperDiv.appendChild(topDiv);
    wrapperDiv.appendChild(votesDiv);
    if(this.votes_.toInt() > 1) {
    	var scrollerTop = document.createElement('a');
    	scrollerTop.className = 'scrollUp';
    	//scrollerTop.setAttribute('href','javascript:scrollUp(map);');
    	wrapperDiv.appendChild(scrollerTop);
    }
    wrapperDiv.appendChild(contentDiv);
    if(this.votes_.toInt() > 1) {
    	var scrollerBottom = document.createElement('a');
    	scrollerBottom.className = 'scrollDown';
    	//scrollerBottom.setAttribute('href','javascript:scrollDown(map);');
    	wrapperDiv.appendChild(scrollerBottom);
    }
    div.appendChild(wrapperDiv);
    div.style.display = 'none';
    // Append to body, to avoid bug with Webkit browsers
    // attempting CSS transforms on IFRAME or SWF objects
    // and rendering badly.
    //document.body.appendChild(div);
    $$('.tx-dsgapetitionstool-pi1')[0].appendChild(div);
    if(this.votes_.toInt() > 1) {
    	var eventmap = this.map_;
	    $$('.scrollDown').each(function(item){
			
			item.addEvent('click',function(e){
	
				scrollDown(eventmap);
			});
		});
		
		$$('.scrollUp').each(function(item,eventmap){
			
			item.addEvent('click',function(e){
	
				scrollUp(eventmap);
			});
		});
  }
    
  } else if (div.parentNode != panes.floatPane) {
    // The panes have changed.  Move the div.
    div.parentNode.removeChild(div);
    panes.floatPane.appendChild(div);
  } else {
    // The panes have not changed, so no need to create or move the div.
  }
  
};

SmartInfoWindow.mouseFilter = function(e) {
  e.returnValue = 'true';
  e['handled'] = true;
}

/**
 * Closes infowindow
 */
SmartInfoWindow.prototype.close = function() {
	/*var map = this.map_;
	var idleEvent = map.get('idleEvent');
	idleEvent = google.maps.event.addListener(map, 'idle', function(map) {
		//							zoom	,maxLat							  ,minLat							,maxLng										  ,minLng
	refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().b,this.getBounds().getNorthEast().c,this.getBounds().getSouthWest().b,this.getBounds().getSouthWest().c,labels,'');
	});
	map.set('idleEvent',idleEvent);
	*/
	this.setMap(null);
	scrollElement = 0;
  
  //console.log(scrollElement);
};

/**
 * Pan the map to fit the SmartInfoWindow,
 * if its top or bottom corners aren't visible.
 */
SmartInfoWindow.prototype.maybePanMap = function() {
  // if we go beyond map, pan map
  var map = this.map_;
  var projection = this.getProjection();
  var bounds = map.getBounds();
  if (!bounds) return;

  // The dimension of the infowindow
  var iwWidth = this.width_;
  var iwHeight = this.height_;

  // The offset position of the infowindow
  var iwOffsetX = this.offsetX_;
  var iwOffsetY = this.offsetY_;

  var anchorPixel = projection.fromLatLngToDivPixel(this.latlng_);
  /* 
  var bl = new google.maps.Point(anchorPixel.x + iwOffsetX + 20,
      anchorPixel.y + iwOffsetY + iwHeight);
  var tr = new google.maps.Point(anchorPixel.x + iwOffsetX + iwWidth,
      anchorPixel.y + iwOffsetY);
  */
  var bl = new google.maps.Point(anchorPixel.x - iwOffsetX - 200,
	      anchorPixel.y - 30);
  var tr = new google.maps.Point(anchorPixel.x + iwOffsetX + iwWidth,
	      anchorPixel.y - 430);
/*
  var bl = new google.maps.Point(anchorPixel.x,
	      anchorPixel.y);
  var tr = new google.maps.Point(anchorPixel.x,
	      anchorPixel.y);
  */
  var sw = projection.fromDivPixelToLatLng(bl);
  var ne = projection.fromDivPixelToLatLng(tr);

  // The bounds of the infowindow
  //if (!map.getBounds().contains(ne) || !map.getBounds().contains(sw)) {

    map.panToBounds(new google.maps.LatLngBounds(sw, ne));
    //idleEvent = google.maps.event.addListener(map, 'idle', function(map) {
		//							zoom	,maxLat							  ,minLat							,maxLng										  ,minLng
	//	refreshMarkers(data,this,markers,this.zoom,this.getBounds().getNorthEast().b,this.getBounds().getNorthEast().c,this.getBounds().getSouthWest().b,this.getBounds().getSouthWest().c,labels,'');
    //});
    //map.set('idleEvent',idleEvent);
  //map.panTo(new google.maps.LatLngBounds(middle));
  //}
};

/**
 * @enum {number}
 */
SmartInfoWindow.Align = {
  ABOVE: 0,
  LEFT: 1,
  RIGHT: 2,
  BELOW: 3
};

/**
 * Finds best alignment for infowindow.
 * @return {number} Alignment.
 */
SmartInfoWindow.prototype.getBestAlignment = function() {
  var bestAlignment = SmartInfoWindow.Align.LEFT;
  var minPan = 0;

  for (var alignment in SmartInfoWindow.Align) {
    var alignment = SmartInfoWindow.Align[alignment];
    var panValue = this.getPanValue(alignment);
    if (panValue > minPan) {
      minPan = panValue;
      bestAlignment = alignment;
    }
  }

  return bestAlignment;
};

/**
 * Calculates distance of corner for each alignment.
 * @param {number} alignment An alignment constant.
 * @return {number} Distance for that alignment.
 */
SmartInfoWindow.prototype.getPanValue = function(alignment) {
  var mapSize = new google.maps.Size(this.map_.getDiv().offsetWidth,
      this.map_.getDiv().offsetHeight);
  var bounds = this.map_.getBounds();
  var sideLatLng;
  switch (alignment) {
    case SmartInfoWindow.Align.ABOVE:
      sideLatLng = new google.maps.LatLng(bounds.getNorthEast().lat(),
          this.latlng_.lng());
      break;
    case SmartInfoWindow.Align.BELOW:
      sideLatLng = new google.maps.LatLng(bounds.getSouthWest().lat(),
          this.latlng_.lng());
      break;
    case SmartInfoWindow.Align.RIGHT:
      sideLatLng = new google.maps.LatLng(this.latlng_.lat(),
          bounds.getNorthEast().lng());
      break;
    case SmartInfoWindow.Align.LEFT:
      sideLatLng = new google.maps.LatLng(this.latlng_.lat(),
          bounds.getSouthWest().lng());
      break;
  }
  var dist = SmartInfoWindow.distHaversine(this.latlng_.lat(), this.latlng_.lng(),
      sideLatLng.lat(), sideLatLng.lng());
  return dist;
};


/**
 * Converts degrees to radians.
 * @param {number} num Angle in degrees.
 * @return {number} Angle in radians.
 */
SmartInfoWindow.toRad = function(num) {
    return num * Math.PI / 180;
}

/**
 * Calculates distance between two coordinates.
 * @param {number} lat1 Latitude of first coord.
 * @param {number} lon1 Longitude of second coord.
 * @param {number} lat2 Latitude of second coord.
 * @param {number} lon2 Longitude of second coord.
 * @return {number} The distance.
 */
SmartInfoWindow.distHaversine = function(lat1, lon1, lat2, lon2) {
  var R = 6371; // earth's mean radius in km
  var dLat = SmartInfoWindow.toRad(lat2 - lat1);
  var dLon = SmartInfoWindow.toRad(lon2 - lon1);
  lat1 = SmartInfoWindow.toRad(lat1), lat2 = SmartInfoWindow.toRad(lat2);

  var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
          Math.cos(lat1) * Math.cos(lat2) *
          Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return d;
}

