var left=0;var maxRegion=299;var district={id:[],cd:[],hb:[],colL:[],colR:[]};var sortArray=new Array(maxRegion);var equalDistBoundaries=new Array();var quantileBoundaries=new Array();var classBoundaries=new Array();var min;var max;var selectedData;var colorNr;var dataCol;var idcd='id';var col='colL';var selectedDataLeft;var selectedDataRight;var mapCloneRoot=null;var draggingCloneElement=null;var histogramLayer=null;var mapRoot=null;var svgMap=new Object;var controls=null;var wkrName=null;var CDUkopf=null;var cduAKTproz=null;var spdAKTproz=null;var grueneAKTproz=null;var linkeAKTproz=null;var fdpAKTproz=null;var cduVORPproz=null;var spdVORPproz=null;var grueneVORPproz=null;var linkeVORPproz=null;var fdpVORPproz=null;function fillItems(intStart){var fTypes=document.getElementById('form1').types;var fItems=document.getElementById('form1').items;var a=arItems;var b,c,d,intItem,intType;if(intStart>0){for(b=0;b<a.length;b++){if(a[b][1]==intStart){intType=a[b][0];}}
for(c=0;c<fTypes.length;c++){if(fTypes.options[c].value==intType){fTypes.selectedIndex=c;}}}
if(intType==null){intType=fTypes.options[fTypes.selectedIndex].value}
fItems.options.length=0;for(d=0;d<a.length;d++){if(a[d][0]==intType){fItems.options[fItems.options.length]=new Option(a[d][2],a[d][1]);}
if(a[d][1]==intStart){fItems.selectedIndex=fItems.options.length-1;}}}
var tmpColor='ffffff';function tellHist(MyHistID){var MyDistID=sortArray[MyHistID][1];tmpColor=district.hb[MyHistID].getAttribute('stroke');district.hb[MyHistID].setAttribute('stroke-width',5);district.hb[MyHistID].setAttribute('stroke',colorScheme[colorNr][6]);district[idcd][MyDistID].setAttribute('fill',colorScheme[colorNr][6]);document.getElementById('wkr-id').firstChild.data=('Aktueller Wert: '+sortArray[MyHistID][0]);var hoverName=btw09data[MyDistID][1];wkrName.firstChild.data=hoverName;updateTable(MyDistID);}
function clearHist(MyHistID){district[idcd][sortArray[MyHistID][1]].setAttribute('fill',district[col][sortArray[MyHistID][1]]);district.hb[MyHistID].setAttribute('stroke-width',1.3);district.hb[MyHistID].setAttribute('stroke',tmpColor);}
function tellMe(MyDistID){district[idcd][MyDistID].setAttribute('fill',colorScheme[colorNr][6]);var hoverName=btw09data[MyDistID][1];wkrName.firstChild.data=hoverName;updateTable(MyDistID);var hoverValue=btw09data[MyDistID][dataCol];document.getElementById('wkr-id').firstChild.data=('Aktueller Wert: '+hoverValue);if(controls.style.display=='none'){window.defaultStatus=hoverName+'   ||   '+document.getElementById('keyHead').firstChild.data+':  '+hoverValue;}}
function clearMe(MyDistID){district[idcd][MyDistID].setAttribute('fill',district[col][MyDistID]);}
function sort2dim(a,b){return((a[0]<b[0])?-1:((a[0]>b[0])?1:0));}
function statistics(selected){if(Math.abs(selected)!=99){selectedData=selected;dataCol=Math.floor(selectedData);document.getElementById('option1').selected=true;if(document.getElementById('2maps').checked==true&&document.getElementById('LayoutControl').position[0].checked==true){var clone='c'
selectedDataRight=selectedData}else{var clone=''
selectedDataLeft=selectedData}
var year='';for(i=0;i<arItems.length;i++){if(arItems[i][1]==selectedData){if(arItems[i][0]==1){year=' 2009'};if(arItems[i][0]==2){year=' 2005'};document.getElementById('keyHead').firstChild.data=(arItems[i][2]+year);document.getElementById('311'+clone).firstChild.data=(arItems[i][2]+year)}}
for(i=0;i<maxRegion;i++){sortArray[i]=new Array(2);sortArray[i][0]=btw09data[i][dataCol];sortArray[i][1]=i;}
sortArray.sort(sort2dim);min=sortArray[0][0];max=sortArray[maxRegion-1][0];var quantIncrement=Math.round(maxRegion/5);for(i=0;i<4;i++){quantileBoundaries[i]=sortArray[(i+1)*quantIncrement][0];}
var increment=(max-min)/5;for(i=0;i<4;i++){if((max-min)<10){equalDistBoundaries[i]=Math.round((min+(i+1)*increment)*10)/10}else{equalDistBoundaries[i]=Math.round(min+(i+1)*increment)}}
var myY1=100*max/(max-min);for(i=0;i<(maxRegion);i++){if(min<0){var myY2=myY1-(sortArray[i][0]*100/(max-min));district.hb[i].setAttribute('y1',myY1+10);district.hb[i].setAttribute('y2',myY2+10);}else{district.hb[i].setAttribute('y1',100);district.hb[i].setAttribute('y2',100-(sortArray[i][0]/max*90));}}
document.getElementById('maxTick').firstChild.data=max;if(min<0){tickMin=min}else{tickMin=0}
document.getElementById('minTick').firstChild.data=tickMin;colorMap('default')}
if(selected==-99){window.open('http://www.bundeswahlleiter.de/de/bundestagswahlen/BTW_BUND_09/strukturdaten/');fillItems(1)}}
function colorMap(classMethod){if(classMethod=='default'){for(i=0;i<arItems.length;i++){if(arItems[i][1]==selectedData){for(n=0;n<4;n++){classBoundaries[n]=arItems[i][3+n]}}}}
if(classMethod=='quantiles'){for(n=0;n<4;n++){classBoundaries[n]=quantileBoundaries[n]}}
if(classMethod=='equaldistance'){for(n=0;n<4;n++){classBoundaries[n]=equalDistBoundaries[n]}}
colorNr=Math.round((selectedData-dataCol)*10);if(document.getElementById('2maps').checked==true&&document.getElementById('LayoutControl').position[0].checked==true){var clone='c';idcd='cd';col='colR'}else{var clone='';idcd='id';col='colL'}
for(i=0;i<5;i++){document.getElementById('color'+i).style.backgroundColor=colorScheme[colorNr][i+1];var key=document.getElementById((i+300)+clone);key.setAttribute('fill',colorScheme[colorNr][i+1]);}
var frequencies=[0,0,0,0,0];var suspendIDmap=mapRoot.suspendRedraw(5000);for(i=0;i<(maxRegion);i++){var mapPerc=btw09data[i][dataCol];if(mapPerc<classBoundaries[0]){district[idcd][i].setAttribute('fill',colorScheme[colorNr][1]);frequencies[0]+=1;}
if(mapPerc>=classBoundaries[0]&&mapPerc<classBoundaries[1]){district[idcd][i].setAttribute('fill',colorScheme[colorNr][2]);frequencies[1]+=1;}
if(mapPerc>=classBoundaries[1]&&mapPerc<classBoundaries[2]){district[idcd][i].setAttribute('fill',colorScheme[colorNr][3]);frequencies[2]+=1;}
if(mapPerc>=classBoundaries[2]&&mapPerc<classBoundaries[3]){district[idcd][i].setAttribute('fill',colorScheme[colorNr][4]);frequencies[3]+=1;}
if(mapPerc>=classBoundaries[3]){district[idcd][i].setAttribute('fill',colorScheme[colorNr][5]);frequencies[4]+=1;}
if(clone=='c'){district.colR[i]=district[idcd][i].getAttribute('fill');}else{district.colL[i]=district[idcd][i].getAttribute('fill');}}
mapRoot.unsuspendRedraw(suspendIDmap);for(i=0;i<5;i++){var tmpFreq='freq'+i;document.getElementById(tmpFreq).firstChild.data=frequencies[i];}
for(i=0;i<4;i++){document.getElementById('percClass'+i).firstChild.data=classBoundaries[i];document.getElementById('limit'+(i+1)+'a').firstChild.data=classBoundaries[i];document.getElementById((i+306)+clone).firstChild.data=classBoundaries[i];}
document.getElementById('max').firstChild.data=max;document.getElementById('min').firstChild.data=min;document.getElementById(305+clone).firstChild.data=min;document.getElementById(310+clone).firstChild.data=max;var suspendIDhist=histogramLayer.suspendRedraw(5000);for(i=0;i<(maxRegion);i++){if(sortArray[i][0]<classBoundaries[0]){district.hb[i].setAttribute('stroke',colorScheme[colorNr][1]);}
if(sortArray[i][0]>=classBoundaries[0]&&sortArray[i][0]<classBoundaries[1]){district.hb[i].setAttribute('stroke',colorScheme[colorNr][2]);}
if(sortArray[i][0]>=classBoundaries[1]&&sortArray[i][0]<classBoundaries[2]){district.hb[i].setAttribute('stroke',colorScheme[colorNr][3]);}
if(sortArray[i][0]>=classBoundaries[2]&&sortArray[i][0]<classBoundaries[3]){district.hb[i].setAttribute('stroke',colorScheme[colorNr][4]);}
if(sortArray[i][0]>=classBoundaries[3]){district.hb[i].setAttribute('stroke',colorScheme[colorNr][5]);}}
histogramLayer.unsuspendRedraw(suspendIDhist);}
function updateTable(wkr){if(btw09data[wkr][0]>212&&btw09data[wkr][0]<258){CDUkopf.firstChild.data='CSU';}else{CDUkopf.firstChild.data='CDU';}
spdAKTproz.firstChild.data=btw09data[wkr][27]+'%';cduAKTproz.firstChild.data=btw09data[wkr][28]+'%';fdpAKTproz.firstChild.data=btw09data[wkr][29]+'%';linkeAKTproz.firstChild.data=btw09data[wkr][30]+'%';grueneAKTproz.firstChild.data=btw09data[wkr][31]+'%';spdVORPproz.firstChild.data=btw09data[wkr][2]+'%';cduVORPproz.firstChild.data=btw09data[wkr][3]+'%';fdpVORPproz.firstChild.data=btw09data[wkr][4]+'%';linkeVORPproz.firstChild.data=btw09data[wkr][5]+'%';grueneVORPproz.firstChild.data=btw09data[wkr][6]+'%';}
function newClassInput(){var l1=document.forms['classification'].elements['limit1'].value;var l2=document.forms['classification'].elements['limit2'].value;var l3=document.forms['classification'].elements['limit3'].value;var l4=document.forms['classification'].elements['limit4'].value;classBoundaries=[l1,l2,l3,l4];for(i=0;i<4;i++){classBoundaries[i]=classBoundaries[i].replace(/,/,'.');classBoundaries[i]=classBoundaries[i].replace(/[^0-9\-.]/g,'');}
colorMap('userinput')
document.getElementById('option1').selected=true;}
function buildHistogramFrag(){histogramLayer=document.getElementById('histogramlines');var frag=document.createDocumentFragment(true);for(i=0;i<(maxRegion);i++){var newLine=document.createElementNS(svgns,'line');newLine.setAttributeNS(null,'x1',i+5);newLine.setAttributeNS(null,'y1',100);newLine.setAttributeNS(null,'x2',i+5);newLine.setAttributeNS(null,'y2',50);newLine.setAttributeNS(null,'stroke-width',1.3);newLine.setAttributeNS(null,'stroke','green');district.hb[i]=newLine;district.hb[i].addEventListener('mouseover',function(){var ii=i;return function(){tellHist(ii)};}(),false);district.hb[i].addEventListener('mouseout',function(){var ii=i;return function(){clearHist(ii)};}(),false);frag.appendChild(newLine);}
histogramLayer.appendChild(frag);}
function resultPage(evt){var tmpWkr='x';var myL='y';var myWkr=evt.target.id;if(myWkr>0&&myWkr<12){myL='01'};if(myWkr>11&&myWkr<19){myL='13'};if(myWkr>18&&myWkr<25){myL='02'};if(myWkr>24&&myWkr<55){myL='03'};if(myWkr>54&&myWkr<57){myL='04'};if(myWkr>56&&myWkr<67){myL='12'};if(myWkr>66&&myWkr<76){myL='15'};if(myWkr>75&&myWkr<88){myL='11'};if(myWkr>87&&myWkr<152){myL='05'};if(myWkr>151&&myWkr<168){myL='14'};if(myWkr>167&&myWkr<189){myL='06'};if(myWkr>188&&myWkr<198){myL='16'};if(myWkr>197&&myWkr<213){myL='07'};if(myWkr>212&&myWkr<258){myL='09'};if(myWkr>257&&myWkr<296){myL='08'};if(myWkr>295&&myWkr<300){myL='10'};if(myWkr<10){tmpWkr='00'+myWkr};if(myWkr>9&&myWkr<100){tmpWkr='0'+myWkr};if(myWkr>99){tmpWkr=myWkr};if(evt.altKey==1){var baseURL='http://www.bundeswahlleiter.de/de/bundestagswahlen/BTW_BUND_09/ergebnisse/wahlkreisergebnisse/l';var myURL=baseURL+myL+'/wk'+tmpWkr+'/';window.open(myURL);}}
var nMouseOffsetX=0;var nMouseOffsetY=0;var dragx=0;var dragy=0;var rootctm;var irootctm;var p;function mouseDown(evt){svgMap.addEventListener("mousemove",mouseMove,false);p=mapRoot.createSVGPoint();p.x=evt.clientX;p.y=evt.clientY;rootctm=mapRoot.getScreenCTM();irootctm=rootctm.inverse();p=p.matrixTransform(irootctm);nMouseOffsetX=p.x-dragx;nMouseOffsetY=p.y-dragy;}
function mouseMove(evt){if(nMouseOffsetX==0)
return;p=mapRoot.createSVGPoint();p.x=evt.clientX;p.y=evt.clientY;p=p.matrixTransform(irootctm);p.x-=nMouseOffsetX;p.y-=nMouseOffsetY;dragx=p.x;dragy=p.y;svgMap.setAttribute('transform','translate('+p.x+','+p.y+')');}
function mouseUp(evt){svgMap.removeEventListener("mousemove",mouseMove,false);nMouseOffsetX=0;nMouseOffsetY=0;}
function zoomHelp(){alert('Vergrößern der Karte wie bei Google Maps:\n\nZOOM mit dem Mausrad\nVERSCHIEBEN durch Ziehen mt der Maus\n\n[Alt]+click in einen Wahlkreis\nfür das Wahlergebnis 2009 beim Bundeswahlleiter');}
function resetZoom(){svgMap.setAttributeNS(null,'transform','translate( "0, 0")');var dragx=0;var dragy=0;mapRoot.setAttributeNS(null,'viewBox','-305335 -6849296 672667 910385');if(mapCloneRoot){mapCloneRoot.setAttributeNS(null,'viewBox','-305335 -6849296 672667 910385')}}
function hookEvent(element,eventName,callback)
{if(typeof(element)=='string')
element=document.getElementById(element);if(element==null)
return;if(element.addEventListener)
{if(eventName=='mousewheel')
{element.addEventListener('DOMMouseScroll',callback,false);}
element.addEventListener(eventName,callback,false);}
else if(element.attachEvent)
element.attachEvent("on"+eventName,callback);}
function MouseWheel(e)
{e=e?e:window.event;var wheelData=e.detail?e.detail*-1:e.wheelDelta;scrollZoom(wheelData)}
function scrollZoom(wheeldata){if(wheeldata>0){var scale=0.7}else{var scale=1.3};var myViewBox=mapRoot.getAttribute('viewBox');var viewboxes=myViewBox.split(' ');var ulXcorner=parseFloat(viewboxes[0]);var ulYcorner=parseFloat(viewboxes[1]);var myWidth=parseFloat(viewboxes[2]);var myHight=parseFloat(viewboxes[3]);var xCenter=ulXcorner+myWidth/2;var yCenter=ulYcorner+myHight/2;myWidth=myWidth/scale;myHight=myHight/scale;if(myWidth>15000&&myWidth<1000000){ulXcorner=xCenter-myWidth/2;ulYcorner=yCenter-myHight/2;myViewBox=ulXcorner+' '+ulYcorner+' '+myWidth+' '+myHight;mapRoot.setAttribute('viewBox',myViewBox)
if(mapCloneRoot){mapCloneRoot.setAttribute('viewBox',myViewBox)}}}
function cloneMap(){mapRoot.setAttribute('viewBox','-346279 -6904710 754556 1021213');mapRoot.setAttribute('width','471px');selectedDataRight=selectedData;selectedDataLeft=selectedData;var body=document.getElementsByTagName('body')[0];var mapContainer=document.getElementById('map');mapContainer.setAttribute('id','mapLeft');var cloneElement=mapContainer.cloneNode(true);cloneElement.setAttribute('id','mapRight');body.insertBefore(cloneElement,mapContainer);for(i=0;i<maxRegion;i++){district.cd[i]=document.getElementById(btw09data[i][0]);district.cd[i].addEventListener('mouseover',function(){var ii=i;return function(){tellMe(ii)};}(),false);district.cd[i].addEventListener('mouseout',function(){var ii=i;return function(){clearMe(ii)};}(),false);}
for(i=maxRegion;i<312;i++){var changeID=document.getElementById(i);changeID.setAttributeNS(null,'id',i+'c');}
var changeCap=document.getElementById('capitals');changeCap.setAttribute('id','capitalsc');var changeLB=document.getElementById('laenderboundaries');changeLB.setAttribute('id','laenderboundariesc');var radiocontrols=document.getElementById('LayoutControl');radiocontrols.style.display='inline';radiocontrols.position[0].checked=true;moveLeft()}
function moveLeft(){controls.style.left='0px';left=1;selectedData=selectedDataRight;statistics(selectedData);}
function moveRight(){controls.style.left='561px';left=0;selectedData=selectedDataLeft;statistics(selectedData);}
function layoutControls(position){if(position=='right'){moveRight()}
if(position=='left'){moveLeft()}
if(position=='hide'){controls.style.display='none';document.getElementById('actionIconG').style.display='inline';if(left==1){document.getElementById('LayoutControl').position[0].checked=true;}else{document.getElementById('LayoutControl').position[1].checked=true;}}}
function unhideControls(){controls.style.display='inline';document.getElementById('actionIconG').style.display='none';}
function uncloneMap(){mapRoot.setAttribute('viewBox','-305335 -6849296 672667 910385');mapRoot.setAttribute('width','556px');var body=document.getElementsByTagName('body')[0];var mapContainer=document.getElementById('mapRight');body.removeChild(mapContainer);var origMap=document.getElementById('mapLeft');origMap.setAttribute('id','map');var radiocontrols=document.getElementById('LayoutControl');radiocontrols.style.display='none';controls.style.left='561px';statistics(selectedDataLeft);fillItems(0)}
function load(){if(top.location!=location){top.location.href=document.location.href;}
mapRoot=document.getElementById('svgroot');svgMap=document.getElementById('Wahlkreiskarte');hookEvent('map','mousewheel',MouseWheel);controls=document.getElementById('controlsContainer');wkrName=document.getElementById('wkr-name');CDUkopf=document.getElementById('CDUkopf');cduAKTproz=document.getElementById('cduAKTproz');spdAKTproz=document.getElementById('spdAKTproz');grueneAKTproz=document.getElementById('grueneAKTproz');linkeAKTproz=document.getElementById('linkeAKTproz');fdpAKTproz=document.getElementById('fdpAKTproz');cduVORPproz=document.getElementById('cduVORPproz');spdVORPproz=document.getElementById('spdVORPproz');grueneVORPproz=document.getElementById('grueneVORPproz');linkeVORPproz=document.getElementById('linkeVORPproz');fdpVORPproz=document.getElementById('fdpVORPproz');for(i=0;i<(maxRegion);i++){district.id[i]=document.getElementById(btw09data[i][0]);district.id[i].addEventListener('mouseover',function(){var ii=i;return function(){tellMe(ii)};}(),false);district.id[i].addEventListener('mouseout',function(){var ii=i;return function(){clearMe(ii)};}(),false);}
svgMap.addEventListener("mousedown",mouseDown,false);svgMap.addEventListener("mouseup",mouseUp,false);svgMap.addEventListener("click",resultPage,false);buildHistogramFrag();var selectData=26.0
fillItems(selectData);statistics(selectData)
var indicator=document.getElementById('wkr-id');indicator.firstChild.data=('Karte ist bereit');indicator.style.color='#313f7d';}