function Numsort (a, b) {
  return a - b;
}

function changeMap () {
	var laender = new Array('82310995',
							'63392140',
							'60798438',
							'59131287',
							'44474631',
							'38125479',
							'21565119',
							'16357992',
							'11170957',
							'10599095',
							'10584534',
							'10287189',
							'10066158',
							'9113257',
							'8298923',
							'7679290',
							'5447084',
							'5393637',
							'5276955',
							'4319425',
							'3384879',
							'2281305',
							'2010377',
							'1342409',
							'778537',
							'476187',
							'406020');
    var summe_bev = 495072299;
	var summe_yes = 0;
	var summe_no = 0;
	var summe_unsure = 0;
	var votes_yes = 0;
	var votes_no = 0;
	var laender_no = new Array();
	var summe_laender_no = 0;
	var sperr_anzahl_summe = 0; 
	for (i = 0; i < laender.length; i++) {
		if ($('europa_input_'+i+'_yes').checked) {
			summe_yes = summe_yes + parseInt(laender[i]);
			votes_yes++; 
			$('europa_img_'+i+'_yes').show();
			$('europa_img_'+i+'_no').hide();
		}
		else if ($('europa_input_'+i+'_no').checked) {
			summe_no = summe_no + parseInt(laender[i]);
			laender_no[votes_no] = laender[i];
			votes_no++;
			$('europa_img_'+i+'_yes').hide();
			$('europa_img_'+i+'_no').show();
		}
		else {
			summe_unsure = summe_unsure + parseInt(laender[i]);
			$('europa_img_'+i+'_yes').hide();
			$('europa_img_'+i+'_no').hide();
		}
	}
	var stimmen_height = 500 - (votes_yes * 18.5);
	var prozent_height = 500 - (summe_yes/(summe_bev) * 500);
	laender_no.sort(Numsort);
	laender_no.reverse();
	var sperr_anzahl_smallest = laender_no[laender_no.length-1];
	laender_no.pop();
	for (i = 0; i < laender_no.length; i++) {
		sperr_anzahl_summe = sperr_anzahl_summe + parseInt(laender_no[i]);
	}
	var sperr_abolut_summe = sperr_anzahl_summe/summe_bev;
	$('stim_anzahl_ja').update(votes_yes);
	$('stim_absolut_ja').update((votes_yes/27*100).toFixed(0)+' %');
	$('bev_anzahl_ja').update((summe_yes/1000000).toFixed(1)+' Millionen');
	$('bev_absolut_ja').update((summe_yes/summe_bev*100).toFixed(1)+' %');
    
    
    /*
	$('stim_anzahl_yes').update(votes_yes);
	$('stim_abolut_yes').update(votes_yes/27*100+' %');
	$('stim_relativ_yes').update(votes_yes/(votes_yes+votes_no)*100+' %');
	$('bev_anzahl_yes').update(summe_yes);
	$('bev_abolut_yes').update(summe_yes/495072299*100+' %');
	$('bev_relativ_yes').update(summe_yes/(summe_yes+summe_no)*100+' %');
	$('stim_anzahl_no').update(votes_no);
	$('stim_abolut_no').update(votes_no/27*100+' %');
	$('stim_relativ_no').update(votes_no/(votes_yes+votes_no)*100+' %');
	$('bev_anzahl_no').update(summe_no);
	$('bev_abolut_no').update(summe_no/495072299*100+' %');
	$('bev_relativ_no').update(summe_no/(summe_yes+summe_no)*100+' %');
	$('stim_anzahl_unsure').update(27-votes_yes-votes_no);
	$('stim_abolut_unsure').update(27-votes_yes-votes_no/27*100+' %');
	$('bev_anzahl_unsure').update(summe_unsure);
	$('bev_abolut_unsure').update(summe_unsure/495072299);
	
	$('sperr_anzahl_all').update(summe_no);
	$('sperr_abolut_all').update(summe_no/495072299*100+' %');
	$('sperr_anzahl_smallest').update(sperr_anzahl_smallest);
	$('sperr_abolut_smallest').update(sperr_anzahl_smallest/495072299*100+' %');
	$('sperr_anzahl_summe').update(sperr_anzahl_summe);
	$('sperr_abolut_summe').update(sperr_abolut_summe*100+' %' );
	*/
	$('bar_stimmen').setStyle({height: stimmen_height+'px'});
	$('bar_prozent').setStyle({height: prozent_height+'px'});
	//alert (votes_yes / (votes_yes + votes_no));
	//alert (summe_yes/(summe_yes + summe_no));
	//alert (summe_laender_no);
	//if ((votes_yes / (votes_yes + votes_no)) > 0.55 && (summe_yes/(summe_yes + summe_no)) > 0.65)
	if (votes_yes > 14) {
      $('einfache_mehrheit').setStyle({background: '#007832'});
      $('einfache_mehrheit').update('erreicht');
      if ((votes_yes < 24 && summe_yes/summe_bev > 0.65) || (votes_yes >= 24)) {
        $('qual_mehrheit').setStyle({background: '#007832'});
        $('qual_mehrheit').update('erreicht');
		$('popup').show();
		window.setTimeout("$('popup').hide()",3000);
        if (votes_yes == 24 && summe_yes/summe_bev <= 0.65)
          $('sperr').show();
        else 
          $('sperr').hide();
      }
      else {
        $('qual_mehrheit').setStyle({background: '#E3182D'});
        $('qual_mehrheit').update('nicht erreicht');
		$('popup').hide();
      }  
    }
    else {
      $('einfache_mehrheit').setStyle({background: '#E3182D'});
      $('einfache_mehrheit').update('nicht erreicht');
	  $('popup').hide();
    }
}

function toggleHelp () {
  $('help').toggle();
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}

