function printObj(el) {
	var ret = '';	for(x in el) { ret+='<b>'+x+'</b>: '+el[x]+'<br/>';	}
	return ret;
}
var news,ssswitch=1,transing=false;

window.addEvent('domready', function(){
	if(err!=false) { alert(err); }
	
	$$('.newsitem').each(function(el) {
		el.fx = new Fx.Tween(el.getElement('.imgcont'), {link: 'cancel', property: 'height', duration:200,fps:30,transition:Fx.Transitions.Sine.easeOut});
		el.addEvent('mousemove',function() {
			if(!transing) {
				$$('.nopen').each(function(op) {
					if(op!=el) {
						op.fx.cancel();
						op.fx.start(0);
						//$('test').innerHTML = printObj(op.fx);
						op.removeClass('nopen');
					}
				});
				el.fx.start(153);
				el.addClass('nopen');
				transing=true;
				setTimeout('transing=false',100);
			}
		});
	});
	$$('.newsitem')[0].fx.set(153);
	$$('.newsitem')[0].addClass('nopen');
	//alert($$('.newsitem').getFirst());
	
	//agenda
	//$('calinfo').fade('hide');
	var pc = '#000';
	var cv = null;
	$('calinfo').set('tween',{duration: 300});
	$$('.calprev').each(function(cp) {
		cp.addEvent('mouseover',function() {
			var txt = cp.getElement('.calhid').innerHTML;
			var col = cp.getStyle('background-color');
			pc = col;
			$('calinfo').innerHTML = '<p class="cinf0">Details</p>'+txt;
			$('calinfo').setStyle('background-color',col);
			cp.setStyle('background-color','#fff');
			//$('calinfo').fade('show');
			$('calinfo').tween('right','0px');
			cv = cp;
		});
		cp.addEvent('click',function() {
			document.location.href = baseUrl+'/kalender';
		});
	});
	$('agenda').addEvent('mouseout',function(ev) {
		if(!$('agenda').hasChild(ev.relatedTarget)) {
			//$('gbanner').innerHTML = printObj(ev);
			$('calinfo').tween('right','-160px');
			//$('calinfo').fade(0);
		}
		if(cv!=null) { cv.setStyle('background-color',pc); }
	});
	
	//slideshow
	var slideshow = function() {
		var randpic = new Request.HTML({url:baseUrl+'/index.php'});
		randpic.addEvent('success', function() { eval(randpic.response.html); });
		randpic.post({'act': 'randpic'});
	};
	//$('mss2').fade(0);
	slideshow();
	slideshow();
	slideshow.periodical(5000);
	
	//albums
	nextAlb();
	nextAlb();
	
	//polls
	getPoll(-1);
});

var currAlb = 0,numAlbs = 0,albums = {};
function nextAlb() {
	if(albums[currAlb+1]) {
		currAlb++;
		$('alb2').clone().replaces($('alb1')).id = 'alb1';
		$('alb2').fade('hide');
		$('alb1').fade('in');
		$('alb2').getElement('img').src = baseUrl+'/foto/'+albums[currAlb]['pic_id']+'/thumb.jpg';
		$('alb2').getElement('span').innerHTML = albums[currAlb]['name'];
		$('alb2').getElement('img').onload = function() { $('alb2').fade('in'); }
		$('alb2').getElements('a').each(function(el) {
			el.href = baseUrl+'/fotoalbums/'+albums[currAlb]['id']+'/'+albums[currAlb]['urlname'];
		});
	}
	$('albprev').getElement('img').fade((currAlb>2)?'in':'out');
	$('albnext').getElement('img').fade((currAlb<numAlbs)?'in':'out');
}

function prevAlb() {
	if(currAlb > 2) {
		currAlb--;
		$('alb1').clone().replaces($('alb2')).id = 'alb2';
		$('alb1').fade('hide');
		$('alb2').fade('in');
		$('alb1').getElement('img').src = baseUrl+'/foto/'+albums[currAlb-1]['pic_id']+'/thumb.jpg';
		$('alb1').getElement('span').innerHTML = albums[currAlb-1]['name'];
		$('alb1').getElement('img').onload = function() { $('alb1').fade('in'); }
		$('alb1').getElements('a').each(function(el) {
			el.href = baseUrl+'/fotoalbums/'+albums[currAlb-1]['id']+'/'+albums[currAlb-1]['urlname'];
		});
	}
	$('albprev').getElement('img').fade((currAlb>2)?'in':'out');
	$('albnext').getElement('img').fade((currAlb<=numAlbs)?'in':'out');
}
	

function vote(pollid) {
	var vote=-1;
	$('poll').getElements('input').each(function(el) { if(el.checked) { vote = el.value; } });
	if(vote==-1) { alert('Maak svp een keuze.'); return false; }
	else {
		var doVote = new Request.HTML({url:baseUrl+'/polls'});
		doVote.addEvent('success', function() { eval(doVote.response.html); });
		doVote.addEvent('failure', function() { alert('Fout: de website kan niet benaderd worden. Controleer je internetverbinding of probeer het later opnieuw.'); });
		doVote.post({'pollid': pollid, 'vote': vote, 'type': 'fp'});
	}
}

var filledIn = false;
function addList(form) {
	if(!echeck(form.email.value)) { alert('Het ingevulde e-mailadres is incorrect. Probeert het opnieuw.'); }
	else {
		var req = new Request.HTML({url:'mailinglist.php',evalScripts:false});
		req.addEvent('success', function() { 
			$('list').email.disabled = true;
			$('list').submit.disabled = true;
			$('list').reset.disabled = true;
			$('list').email.value = 'Bedankt!';
			alert(req.response.html);
		});
		req.addEvent('failure', function() { alert('Kan de website niet benaderen. Is je internetverbinding actief?'); });
		var postV = {'add': form.email.value};
		req.post(postV);
	}
	return false;
}
function delList(form) {
	if(!echeck(form.email.value)) { alert('Het ingevulde e-mailadres is incorrect. Probeert het opnieuw.'); }
	else {
		var req = new Request.HTML({url:'mailinglist.php',evalScripts:false});
		req.addEvent('success', function() { 
			$('list').email.disabled = false;
			$('list').submit.disabled = false;
			$('list').reset.disabled = false;
			$('list').email.value = 'Jouw e-mailadres';
			filledIn = false;
			alert(req.response.html);
		});
		req.addEvent('failure', function() { alert('Kan de website niet benaderen. Is je internetverbinding actief?'); });
		var postV = {'del': form.email.value};
		req.post(postV);
	}
	return false;
}
function minit(el) {
	if(!filledIn) {
		el.value = '';
		filledIn = true;
	}
}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){ return false }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false }
	if (str.indexOf(at,(lat+1))!=-1){ return false }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false }
	if (str.substring(lstr,lstr-1)==dot){ return false }
	if (str.indexOf(dot,(lat+2))==-1){ return false }
	if (str.indexOf(" ")!=-1){ return false }
	return true;
}

function selectRadio(el) {
	var rad = el.getParent().getElement('input[type=radio]');
	$('poll').getElements('input[type=radio]').each(function(r,n) { r.checked = (r==rad); } );
}


//polls
function getPoll(id) {
	var pollX = new Request.HTML({url:baseUrl+'/polls'});
	pollX.addEvent('success', function() { eval(pollX.response.html); });
	pollX.addEvent('failure', function() { alert('Fout: de website kan niet benaderd worden. Controleer je internetverbinding of probeer het later opnieuw.'); });
	pollX.post({'pollid': id, 'act': 'get', 'extra': (arguments[1]?arguments[1]:'-1')});
}


function showPoll(poll) {
	if(!poll.id || !poll.question) { return false; }
	//empty the current poll div except for header
	for(var i=0;i<$('poll').children.length;i++) { if($('poll').children[i].className != 'header') { $('poll').removeChild($('poll').children[i--]); } }
	var pc = new Element('div');
	pc.addClass('dpoll'); pc.addClass('dptrans');
	$('poll').innerHTML+= '<p>'+poll.question+'</p>';
	$('poll').appendChild(pc);
	var n=0;
	var answers = {};
	for(q in poll.answers) {
		answers[n] = new Element('div').addClass('voc');
		var ans = answers[n];
		if(!poll.answered) ans.fade('hide');
		var abar = new Element('div').addClass('vbar').setStyle('width','295px');
		var perc = Math.round((poll.answers[q]/poll.numvotes)*1000)/10;
		var percpx = Math.round(295*(perc/100));
		abar.appendChild(new Element('div').addClass('pbbl'));
		abar.appendChild(new Element('div').addClass('pbbr'));
		if(!poll.answered) { 
			abar.addClass('hand'); 
			abar.onclick = function() { selectRadio(this); }
			var radx = new Element('div').addClass('radx');
			var rad = new Element('input').addClass('radio');
			rad.type = 'radio'; rad.name='p-'+poll.id; rad.value=n; 
			radx.appendChild(rad);
			ans.appendChild(radx);
		}
		ans.appendChild(abar);
		pc.appendChild(ans);
		
		if(poll.answered) {
			//percentage-bar
			var pbar = new Element('div').addClass('vperc').setStyle('width','0px');
			pbar.appendChild(new Element('div').addClass('pbfl'));
			pbar.appendChild(new Element('div').addClass('pbfr'));
			abar.appendChild(pbar);
			pbar.tween('width',percpx+'px');
			//percentage-string
			var ppstr = new Element('div').addClass('vpercn').addClass('vpercfp').set('text',perc+'%');
			ppstr.fade('hide');
			ans.appendChild(ppstr);
			ppstr.fade('in');

			
		}
		
		//answer-string
		var pas = new Element('div').addClass('pbans');
		var pas2 = new Element('div');
		pas2.appendChild(new Element('div').set('text',q));
		pas.appendChild(pas2);
		abar.appendChild(pas);

		ans.appendChild(new Element('div').addClass('clearl'));
		n++;
	}
	//buttons
	var buts = new Element('div').addClass('pbbar');
	var viewall = new Element('a').addClass('pbva').set('html','<img src="'+baseUrl+'/img/bpva.png" width="90" height="26" alt="Alle polls" />'); viewall.href = baseUrl+'/polls';
	buts.appendChild(viewall);
	if(poll.answered) {
		var lb = new Element('div').addClass('pbnp').set('text',' ');
		lb.addEvent('click',function() { getPoll(poll.id,'next'); });
		buts.appendChild(lb);
		var txt = new Element('div').addClass('pbtxt');
		txt.set('text',poll.numvotes+' keer gestemd');
		buts.appendChild(txt);
	}
	else {
		var lb = new Element('div').addClass('pbvote');
		lb.addEvent('click',function() { vote(poll.id); });
		buts.appendChild(lb);
		
	}
	ans.appendChild(buts);
	if(!poll.answered) {
		for(var a in answers) { 
			answers[a].fade('in');
			//setTimeout(function(i){answers[i].fade('in')},a*200,a); 
		}
	}
		
	
	
	
}

function pollVotedAll() {
	var currdiv = $('poll').getElement('.pvotedall');
	if(currdiv == null) {
		if($('poll').getHeight() < 30) { $('poll').setStyle('height','120px'); }
		var padtop = Math.max(0,Math.round(($('poll').getHeight()-22-65)/2));
		var vall = new Element('div').addClass('pvotedall');
		var cnt = new Element('div').addClass('cen');
		cnt.setStyle('padding-top',padtop+'px');
		cnt.set('html','<h3>Je hebt op alle polls gestemd.</h3>');
		var viewall = new Element('a').addClass('pbva2').set('html','<img src="'+baseUrl+'/img/bpva.png" width="90" height="26" alt="Alle polls" />'); viewall.href = baseUrl+'/polls';
		cnt.appendChild(viewall);
		cnt.appendChild(new Element('div').addClass('clearl'));
		vall.appendChild(cnt);
		$('poll').appendChild(vall);
	}
}
