function showWerver()
{
	
	document.getElementById('werver').style.display = 'block';
}


function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}
function getCookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ';', len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}


function removeAllChildNodes(elm)
{
	if ( elm.hasChildNodes() )
	{
	    while ( elm.childNodes.length >= 1 )
	    {
	        elm.removeChild( elm.firstChild );       
	    } 
	}
}

function setDivVisibility(obj, visible){
    if (obj){
	    if(visible){
	        obj.style.visibility = "visible";
	    }else{
	        obj.style.visibility = "hidden";
	        // trying to hide a video? remove the div: flashvideowrapper
	        if ($('flashvideowrapper')){removeAllChildNodes($('flashvideowrapper'))};
	    }
	}
}


// helper method to get the value from a url-querystring variable
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	//alert('Query Variable ' + variable + ' not found');
	return "";
}

function toonStraat(id, lat, lon)
{
		setDivVisibility($('intropopup'), false);
		setDivVisibility($('img_intropopup'), false);
        ajax.get("index.php?go=home.showKaartStraat&straat_id="+id, function(content){ $('straatpopup').innerHTML=content;
        setDivVisibility($('straatpopup'), true);
		setDivVisibility($('img_straatpopup'), true);
        if($('streetmap')){
        	showMap(lat, lon);
        }
    });
		
}
function toonAlleStraten(gemeente,type)
{
		setDivVisibility($('intropopup'), false);
		setDivVisibility($('img_intropopup'), false);
        ajax.get("index.php?go=home.alleGemeenteStraten&gemeente="+gemeente+"&type=" + type, function(content){ $('straatpopup').innerHTML=content;
        setDivVisibility($('straatpopup'), true);
		setDivVisibility($('img_straatpopup'), true);
    });
		
}

function toonSter(id){
		setDivVisibility($('intropopup'), false);
		setDivVisibility($('img_intropopup'), false);
        ajax.get("index.php?go=home.showKaartSter&ster_id="+id, function(content){ $('straatpopup').innerHTML=content;
        setDivVisibility($('straatpopup'), true);
		setDivVisibility($('img_straatpopup'), true);
    });
}

function toonPiet(id){
		setDivVisibility($('intropopup'), false);
		setDivVisibility($('img_intropopup'), false);
        ajax.get("index.php?go=home.showKaartPiet&id="+id, function(content){ $('straatpopup').innerHTML=content;
        setDivVisibility($('straatpopup'), true);
		setDivVisibility($('img_straatpopup'), true);
    });
}

function toonWethouder(id){
	ajax.get("index.php?go=home.getWethouder&id="+id, function(content)
		{ 	$('wethouderpopup').innerHTML=content;
			setDivVisibility($('wethouderpopup'), true);
    	});
}



function submitStraatForm(){
    // kan zowel in de hoofdpagina (met 'aanmeldwrapper') als vanuit gadget worden aangeroepen
    if(""!=($('q').value)){
    	if ($('aanmeldwrapper')){
        	ajax.submit('index.php?go=home.geocodeStraat', 'aanmeldwrapper', $('straatform'));
        }
        else{
            // dit is de gemeentewrapper: open klimaatstraatfeest.nl in top-window
        	// document.location.href='index.php?' + ajax.serialize($('straatform'));
            // onderstaande geeft security problemen:
            // top.document.location.href='index.php?' + ajax.serialize($('straatform'));
			//alert('index.php?' + ajax.serialize($('straatform')));
            feest = window.open('index.php?' + ajax.serialize($('straatform')), 'feestwindow');
        }
    }
}


function showAanmeldForm(straat, woonplaats, gemeente, lat, lon, provincie){
	// als aangeklikt in banner: er is geen div 'aanmeldwrapper'
	if(!$('aanmeldwrapper')){
		document.location.href="index.php";
	}
	else
	{
		// als ie er wel is, geef 'm dan de goede z-index
		document.getElementById('aanmeldwrapper').style.zIndex = 2000;
	}
	
	
    if(straat || woonplaats || gemeente || lat || lon || provincie)
    {
        ajax.update("index.php?go=home.showStraatForm&straat="+straat+"&woonplaats="+woonplaats+"&gemeente="+gemeente+"&lat="+lat+"&lon="+lon+"&provincie="+provincie, $('aanmeldwrapper'));
    }
    else // empty form
    {
        ajax.update("index.php?go=home.showStraatForm", $('aanmeldwrapper'));
    }
}

function startLogin(){
     document.location.href="index.php?email=email";
}


function submitAanmeldForm(){
	if(!document.getElementById('voorwaarden').checked){
		alert('Je bent nog niet akkoord gegaan met de Algemene Voorwaarden');
		return false;
	}
	else{
		ajax.submit('index.php?go=home.meldaan', 'aanmeldwrapper', $('aanmeldform'));
	}
}


function submitLogin(){
    if($('password2')){
        if($('password').value != $('password2').value){
            alert("Je hebt niet twee keer precies hetzelfde wachtwoord gegeven. \nProbeer opnieuw.");
            return;
        }
        if($('password').value.length<6){
            alert("Je hebt een te kort wachtwoord gegeven (minder dan 6 karakters). \nProbeer opnieuw.");
            return;
        }
    }
    emailstr = $('email').value;
	//alert(emailstr.length + "-" + $('email').value + "-" + emailstr.indexOf('@'));
    if (emailstr.length<7 || emailstr.indexOf('@')<0){
        alert("Dit is geen geldig email adres. \nProbeer opnieuw.");
        return;
    }
    ajax.post('index.php?go=home.login', handleLogin, ajax.serialize($('loginform')));
}
function handleLogin(response){
    if (response.indexOf("INLOGOK")>-1){
			respar=response.split("|");
        document.location.href="index.php?" + respar[1];
    }
    else if(response.indexOf("ACTIVATIONFAILED")>=0){
    	//alert(response);
    	alert("Aanmelding met nieuwe wachtwoord is mislukt,\n\n gebruikte je de volledige link uit je aanmeldings-email?");
    }
    else if(response.indexOf("NOACTIVATION")>=0){
    	alert("Geen wachtwoord opgegeven.");
    }
    else{
        document.location.href="index.php?email=email-adres";
    }
}

function mailPassword(){
    ajax.submit('index.php?go=home.mailPass', 'loginpopup', $('loginform'));
}

function submitMailpassForm(){
    ajax.submit('index.php?go=home.mailPass', 'loginpopup', $('mailpassform'));
}

function startPage(){
	// pagina aanroepen met ?q=dezestraat+dezestad: geen inlog of intro popup en direkt invoeren in invoerschermp
	if (""!=getQueryVariable('q')){
		setDivVisibility($('intropopup'), false);
		setDivVisibility($('img_intropopup'), false);
		// evt direct submitten van straat+stad:
		//submitStraatForm();	
	}
	// pagina aanroep met ?email=xxx: geen intro
	else if(""!=getQueryVariable('email')){
		setDivVisibility($('loginpopup'), true);
		setDivVisibility($('img_loginpopup'), true);
        // if there is a cookie with login gegevens don't touch the fields:
        if (getCookie('loginklimaatfeest')==null){
            if($('password')){$('password').value=""};
            if($('password2')){$('password2').value=""};
            $('email').value=getQueryVariable('email');
        }
	}
	else{
		setDivVisibility($('intropopup'), true);
		setDivVisibility($('img_intropopup'), true);
	}
	setTimeout('fade('+i+', '+id+', \'in\')', 10);
	
	setTimeout('fadeWervers('+werverI+', '+werverId+', \'in\')', 10);
	
	freeBalloons();
}

function startupStraatKaart(){
    if (GBrowserIsCompatible()) {
    
      // Hier Map
      map = new GMap2(document.getElementById("streetmap"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(52.119999, 5.600), 7);
      map.setMapType(G_SATELLITE_MAP); // G_NORMAL_MAP G_HYBRID_TYPE G_SATELLITE_MAP
    }
    else{
        alert("U heeft geen of een verkeerde javascript-versie. U ziet geen Google-kaart");
    }
}

function terug(){
    if(document.location.href.indexOf('?')>0){
        if(document.location.href.indexOf('q=')>0){
            document.location.href=document.location.href;
        }else{
            document.location.href=document.location.href+"&q=Klimaatstraat Plaats";
        }
    }
    else{
        document.location.href=document.location.href+"?q=Klimaatstraat Plaats";
    }
}

//fade functie voor nieuws
function fade(i, id, type){
		var div = new Array();
		if(i>0.95){i=1;}
		
		for(t=id;t<id+1;t++){
			div[t] = document.getElementById('nieuws_'+t);
            if(div[t]){
    			div[t].style.display = 'block';
	    		div[t].style.opacity = i;
		    	div[t].style.filter = 'alpha(opacity = ' +100*i+')';
            }
		}
		
		if(type=='in'){
			i=i+0.05;
		}else{
			i=i-0.05;
		}	
		
		if(i>0 && i<1){
			if(type=='in'){
				setTimeout('fade('+i+', '+id+', \'in\')', 100);
			}else{
				setTimeout('fade('+i+', '+id+',  \'out\')', 100);
			}	
		}else if(i<0){
			for(t=id;t<=id+1;t++){
				div[t] = document.getElementById('nieuws_'+t);
                if (div[t]){
    				div[t].style.display = 'none';
                }
			}
			i = 0.01;
			id=id+1;
			if(id>6){
				id=1;
			}
			setTimeout('fade('+i+', '+id+', \'in\')', 100);

		}else if(i>1){
			i = 0.9;
			setTimeout('fade('+i+', '+id+', \'out\')', 6000);
		}	
}

var id=1;
var div = new Array();
var i = 0.01;


//fade functie voor werver
function fadeWervers(i, id, type){
		var div = new Array();
		if(i>0.95){i=1;}
		
		for(t=id;t<id+1;t++){
			div[t] = document.getElementById('werver_'+t);
            if(div[t]){
    			div[t].style.display = 'block';
	    		div[t].style.opacity = i;
		    	div[t].style.filter = 'alpha(opacity = ' +100*i+')';
            }
		}
		
		if(type=='in'){
			i=i+0.05;
		}else{
			i=i-0.05;
		}	
		
		if(i>0 && i<1){
			if(type=='in'){
				setTimeout('fadeWervers('+i+', '+id+', \'in\')', 100);
			}else{
				setTimeout('fadeWervers('+i+', '+id+',  \'out\')', 100);
			}	
		}else if(i<0){
			for(t=id;t<=id+1;t++){
				div[t] = document.getElementById('werver_'+t);
                if (div[t]){
    				div[t].style.display = 'none';
                }
			}
			i = 0.01;
			id=id+1;
			if(id > maxWerverDivs){
				id=1;
			}
			setTimeout('fadeWervers('+i+', '+id+', \'in\')', 100);

		}else if(i>1){
			i = 0.9;
			setTimeout('fadeWervers('+i+', '+id+', \'out\')', 3000);
		}	
}

var werverId		= 1;
var werverdiv 		= new Array();
var maxWerverDivs	= 3;
var werverI 		= 0.01;


	
//stemmen tips
var energieGestemd = false;
function klikSter(tipId, aantalSterren, tipNaam){
	if(!energieGestemd){
		var intro = document.getElementById('energieIntro');
		var introG = document.getElementById('energieGestemd');
		intro.style.display = "none";
		
		introG.innerHTML = "Bedankt voor je stem! Jij vindt de tip '"+tipNaam+"' "+aantalSterren+" sterren waard.<br /><u><a onclick=\"document.location.href='index.php?q=Klimaatstraat%20Plaats&ran='+Math.floor(Math.random()*1000)+'#stem'\" style=\"cursor:pointer;\">Stem op nog een energietip</a></u>";
		introG.style.display = "";
		
		
		var url='index.php?go=home.verwerkenergieSterren&tipId='+tipId+'&aantalSterren='+aantalSterren;
		
		var http = getHTTPObject();
		http.open("GET", url, true);
		http.send(null);
		energieGestemd = true;		
	}
}
