// JavaScript Document

function tickInit() {}

$(document).ready(function() {
	pwf.init();
});

var pwf = {};
pwf.init = function() {
	pwf.getPosition();
	window.onresize = function(){
		pwf.getPosition();
	}
	pwf.homeLink();
	pwf.processLevel1Links();
	pwf.processLevel1CurrentLinks();
	pwf.placePoppetjes();
	pwf.procesNewsLetterForm();
	pwf.procesTellAFriendForm();
	pwf.procesDonateForm();
	if (editmode!='content') {
		pwf.makeMovies();
		if ($('#linked .projectviewer').size())pwf.projectViewer();
	}
	
	pwf.newsLetter();
    $('#wrapper').css('minHeight',Math.max($('body').height(),($('#topmenu .menulevel2:visible').height()+400))+'px');           
	$('address').show();	
	
}
pwf.homeLink = function() {
	$('#topmenu .menulevel1').append('<li class="menuitemlevel1 home"><a href="?" class="linklevel1">home</a></li>');
}
pwf.getPosition = function(){
	$('body').removeClass('small').removeClass('xsmall').removeClass('xxsmall').removeClass('xxxsmall');
	var classStr = '';
	if ($('body').width()<1200) classStr+=' small';
	if ($('body').width()<1100) classStr+=' xsmall';
	if ($('body').width()<1060) classStr+=' xxsmall';
	if ($('body').width()<960)  classStr+=' xxxsmall';  
	createCookie('bodyClass',classStr,365);
	$('body').addClass(classStr);
	
   
}
pwf.processLevel1Links = function(){
	var k = 0;
	$('#topmenu li.menuitemlevel1').each(function(){
		k++;
		$(this).addClass('item_'+k);
	});
}
pwf.processLevel1CurrentLinks = function() {
	$('#topmenu .menulevel2').prepend('<li class="currentLevel1Link"><span>' + $('#topmenu .menulevel1 .currenttree span.itemtitlelevel1').html() + '</span></li>');
}
pwf.placePoppetjes = function() {
	$('h1#logo').append('<div id="poppetjes"></div>');
	for (var k=5; k>0; k--) {
		$('h1#logo div#poppetjes').append('<img src="site/img/poppetjes/'+k+'.png"/>');
	}
	for (var k=2; k<6; k++) {
		$('h1#logo div#poppetjes').append('<img src="site/img/poppetjes/'+k+'.png"/>');
	}
	$('h1#logo img').hide();
	function showPop() {
		$('#poppetjes img:hidden:first').show(300,function(){showPop()});
	}
	showPop();

	
}
pwf.procesNewsLetterForm = function() {
	$('#subcontent .newsletter form input').focus(function(){this.select();}).click(function(){this.select();})
	$('#subcontent .newsletter form a.signon').click(function(){
		$('#subcontent input[name="nieuwsbrief"]').val('register');
		if (!jsGvldtEmail($('#subcontent input[name="pwf_input_1"]').val())) {
			alert('Emailadres is ongeldig');
		}
		else $(this).parents('form:first').submit();
		return false;
	});
	$('#subcontent .newsletter form a.signoff').click(function(){
		$('#subcontent input[name="nieuwsbrief"]').val('unregister');
		if (!jsGvldtEmail($('#subcontent input[name="pwf_input_1"]').val())) {
			alert('Emailadres is ongeldig');
		}
		else $(this).parents('form:first').submit();
		return false;
	});
}

pwf.procesTellAFriendForm = function() {
	$('#subcontent .tell-a-friend form input').focus(function(){this.select();}).click(function(){this.select();})
	$('#subcontent .tell-a-friend form a.signon').click(function(){
		
		if (!jsGvldtEmail($('#subcontent input[name="pwf_input_3"]').val())) {
			alert('Emailadres is ongeldig');
		}
		else $(this).parents('form:first').submit();
		return false;
	});
	
}
pwf.procesDonateForm = function() {
	$('#subcontent .donate form a.donate_button').click(function(){
		$(this).parents('form:first').submit();
		return false;
	});
	
pwf.makeMovies = function(){
	$('#content').each(function(){
		var pos = true;
		$('a',this).each(function(){
				var matches = $(this).attr('href').match(/http:\/\/www.youtube.com\/watch\?v=(.*)/);
				if (matches && matches[1]) {
					var width=$(this).parent().width();
					//console.log(matches[1]);
					$(this).replaceWith('<div class="youtubeplayer"><object width="'+width+'" height="'+((width)*9/16)+'"><param name="movie" value="http://www.youtube.com/v/'+matches[1]+'&hl=en_US&fs=1&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+matches[1]+'&hl=en_US&fs=1&autoplay=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+width+'" height="'+((width)*9/16)+'"></embed></object></div>');
			}
			else pos = false;
		});

		if (pos) $(this).addClass('youtube');
	});
	}
}

pwf.newsLetter = function(){
	if (editmode=='start') {
		
		$('#editButtons.balkstart #stats').after('<div style="width: 10px;" class="CTextItem odd">&nbsp;|&nbsp;</div><div style="background-image: url(&quot;site/img/newsletter.gif&quot;);" onmouseout="editOut(this)" onmouseover="editOver(this)" title="newsletter" id="newsletterButton" name="newsletterButton" unselectable="on" class="CJavascriptItem"><span class="noshow">Newsletter</span></div>');
		$('#newsletterButton').click(function(){
			var left = screen.width/2 - 950/2;
			var popupWindow = window.open(document.location.href+'&window=newsletter','popupWindow','width=950, left='+left+', top=20, scrollbars');
			popupWindow.focus();
		});
	}
	
}

pwf.projectViewer = function() {
	
	var projectOver = false;
	
	

	$('#content p:first').after($('.projectviewer'));
	
	$('.projectviewer').show().hover(function(){
		projectOver = true;
	},function(){
		projectOver = false;
	});
	
	
	str = '<ul class="counter">';
	$('.projects li').each(function(i,li){
		$(this).attr('done',parseInt($('strong',this).text()));
		$(this).attr('needed',parseInt($('em',this).text()));
		$(this).hide().attr('id','project_'+i);
		str += '<li><a href="#_project_'+i+'" title="'+$('h3',this).text()+'">'+i+'</a></li>';
	});
	str += '</ul>';
	$('.projects').before(str);
	
	$('.projectviewer .counter a').click(function(){
		
		clearTimeout(projectTimer);
		var link =this;
		$('.activeProject').removeClass('activeProject');
		$(this).addClass('activeProject');
	
	$('.projects li.active').removeClass('active').hide();
	
	$('.projects li#project_'+$(link).text()).addClass('active').show();
	
	var needed = parseInt($('.projectviewer li.active').attr('needed'));
	$('.projectviewer li.active em').text(needed.formatMoney(0,',','.'));
	var done = parseInt($('.projectviewer li.active').attr('done'));
	
	function how_much() {

		
		var howmuch = $('.projectviewer li.active .money strong').width()/450*needed;
		$('.projectviewer li.active .money strong span').html('&euro;&nbsp;'+(howmuch.formatMoney(0,',','.')));
		
	}
	var pricer = setInterval(function(){how_much();},10);
	$('.projectviewer li.active .money strong').css({width: '0px'}).stop(true,true).animate({
		width: 450*(done/needed)+'px'
	},2000,'',function(){
		clearInterval(pricer);
		$('.projectviewer li.active .money strong span').html('&euro;&nbsp;'+(done.formatMoney(0,',','.')));
		projectTimer = setTimeout(function(){
			clearTimeout(projectTimer);
			if (projectOver) {
				$(link).click();
			}
			else {
				if ($(link).parent().next().find('a').size()) $(link).parent().next().find('a').click();
				else $('.projectviewer .counter a:first').click();
			}
		},4000);
	});
	
	return false;
	});
	
	var projectTimer;
	
	$('.projectviewer .counter a:first').click();

$('#content').append($('<div class="news"></div>').html($('.projectviewer').nextUntil('#linked')));
$('#content .news *:not(h3, p, a, strong, img)').remove();
var newsBlock = [];
var numberOfNewsBlocks = $('#content .news h3').size();
$('#content .news h3').each(function(){
var div = document.createElement('div');
div.className = 'newsBlock';
if (numberOfNewsBlocks>1) {
	$(div).css({float: 'left',width: '215px', marginRight: '10px'});
}
$(div).html($(this).nextUntil('h3').andSelf());
newsBlock.push(div);
});
for (k in newsBlock) {
    $('#content .news').append(newsBlock[k]);
}

	
	
}



Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};     

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}



