
var locks = new Array();

function greenoffice(what) { ; }

function blog(id,action) {
	if(action=='open') {
//		$("#"+id+" a").hide();
//		$("#"+id+" h3").slideUp(500, function() {
			$("#"+id+" p.more").slideDown(750,function() {
				$("#"+id+" a").attr('href','javascript:blog(\''+id+'\',\'close\');').html('Sluiten').show();		
			});
			
	
//		});
	} else if(action=='close') {
//		$("#"+id+" a").hide();
		$("#"+id+" p.more").slideUp(500, function() {
		//	$("#"+id+" h3").slideDown(750,function() {
				$("#"+id+" a").attr('href','javascript:blog(\''+id+'\',\'open\');').html('Lees meer').show();		
		//	});
			
	
		});
	}
}

function refresh_logo() {
	$('#head #animated_logo').css('background-image', 'none');
	window.setTimeout("$('#head #animated_logo').css('background-image', global_logobgimg);", 1);
}

function islocked(what) {
	if(locks['what'] == undefined) locks['what'] = false;
	return locks['what'];
}
function lock(what, how) {
	if(how==undefined) how = true;
	var res = islocked(what);
	locks['what'] = how;
	return res;
}
function unlock(what) {
	return lock(what, false);
}

function home_flash(what) {
	if(what==undefined) what = 1;
	update_template('flashcontainer', { 'template': 'home_panel', 'panel': what });
}

/*
function show_splash() {
	$('.splash_shade').fadeIn('slow', function() {
		$('.splash').slideDown('slow');
	});
}
function hide_splash() {
	$('.splash').slideUp('slow', function() {
		$('.splash_shade').fadeOut('slow', function() {
		//	home_flash(1);
		});
	});
}
*/
function show_splash() {
	window.setTimeout("$('#balke').slideDown();", 2000);
}
function hide_splash() {
	$('#balke').slideUp();
}

function open_overlay(args, flash) {

	var speed = 400;

	if(!lock('overlay')) {
		if(flash == undefined) flash = true;
		if(!flash) home_flash(0);
	
		$.post('/ajax/get_template.php', args, function(data) {
			$('.overlay.box').slideUp(speed, function() { // close box if box is open
				$('.overlay.box').html(data);
				$('.overlay.box').css('margin-left', (0-($('.overlay.box').width())/2)+'px');
				$('.overlay.shade').fadeIn(function() { // do nothing if shade already open
					$('.overlay.box').slideDown(speed, function() {
						unlock('overlay');
					});
				});
			 });
		});
	}
	return false;
}

function close_overlay(flash) {
	
	var speed = 400;
	
	if(!lock('overlay')) {
		if(flash == undefined) flash = true;
		
		$('.overlay.box').slideUp(function() {
			$('.overlay.shade').fadeOut(speed, function() {
				if(!flash) home_flash(1);
				unlock('overlay');
			});
		});
	}
}

function update_template(eltid, args, effect) {

	$.post('/ajax/get_template.php', args, function(data) {
		if(data.indexOf('error') != -1) {
			alert(data);
		} else {
			if(effect=='slideleft') {
				$('#'+eltid).animate({marginLeft: "-1000px"}, function() {
					$('#'+eltid).html(data).animate({marginLeft: "0px"});
				});
			} else if(effect=='slideup') {
				$('#'+eltid).slideUp(function() {
					$('#'+eltid).html(data).slideDown();
				});
			} else { // default: replace
				$('#'+eltid).html(data);
			}
		}
	});	
}

function radiobutton_click(what) {
	$('.radiocontainer').removeClass('active');
	$('.radiocontainer'+what).addClass('active');
}

function update_quickscan(thema, question, required) {

	if(thema == undefined) thema = '';
	if(question == undefined) question = '';
	if(required == undefined) required = false;

	var qid = $('#quickscan_form #question_id').attr('value');
	var tid = $('#quickscan_form #question_thema').attr('value');
	var set = false;
	var val = 0;

	if($('#quickscan_form #question_discrete').attr('value') == 'true') { // discrete question: read radio buttons
		$('#quickscan_form .radiocontainer.active').each(function() {
			val = $('.value', this).val(); // second argument overrides selection context, ie, .value is selected within 'this'
			set = true;
		});
	} else { // read slider
		val = $('#quickscan_form #the_answer').attr('value');
		set = true;
	}

	if((!required) || set) {
		if(!set) {
			update_template('quickscan_container', { 'template': 'quickscan', 'thema': thema, 'question': question }, 'slideleft');
		} else {
			$.post('ajax/update_quickscan.php', { 'thema': tid, 'question': qid, 'val': val }, function(data) {
				if(data.indexOf('error') != -1) { alert(data); }
				update_template('quickscan_container', { 'template': 'quickscan', 'thema': thema, 'question': question }, 'slideleft');

				// if not on homepage, update content of tip detail panel
				if(thema != '0') {
					update_template('tipdetail_container', { 'template': 'tip_detail', 'thema': thema }, 'replace');
				}
			});
		}
	} else {
		alert('Maak a.u.b. eerst een keuze.');
	}
}


function update_poll(pollid, optionid) {

	if(pollid == undefined) pollid = '';
	if(optionid == undefined) optionid = '';

	$.post('ajax/update_poll.php', { 'pollid': pollid, 'optionid': optionid }, function(data) {
		update_template('poll_container', { 'template': 'poll' }, 'slideup');
	});
}

function show_poll() {
	$("#toptip_small").hide();
	$("#toptip_big").hide();
	$("#poll_small").hide();
	$("#poll_big").show();
}

function show_tips() {
	$("#toptip_small").hide();
	$("#toptip_big").show();
	$("#poll_small").hide();
	$("#poll_big").hide();
}

function open_tip_detail(tip) {
	
	pageTracker_greenofficeinitiative_com._trackPageview('view_tip/'+tip);
	
	var speed = 400;
	if($('.tip_detail.active').hasClass('tip_detail_'+tip)) {
		// alert('already open');
	} else {
		if(!lock('tipdetail')) {
			$('.tip_detail.active').slideUp(speed, function() {
				stopthemeflash();
				$('.thema_tip').removeClass('active');
				$('.tip_detail').removeClass('active');
				$('.tip_detail_'+tip).slideDown(speed, function() {
					$(this).addClass('active');
					$('.thema_tip_'+tip).addClass('active');
					if(tip=='0') startthemeflash();
					unlock('tipdetail');
				});
			});
		}
	}
}

function close_tip_detail() {
	open_tip_detail('0');
}

function stopthemeflash() {
	$('#themeflashcontainer').empty();
}

// function startthemeflash() { } // thema_home_panel.tpl.php

function open_article_detail(article, tip) {

	pageTracker_greenofficeinitiative_com._trackPageview('view_article/'+article);

	// set backlink
	$('.article_backlink_'+article).unbind('click');
	$('.article_backlink_'+article).click(function() { open_tip_detail(tip); });
	// show article
	open_tip_detail('article_'+article);
}

function load_toptip(tip) {
	update_template('toptips_container', { 'template': 'top_tips', 'tip': tip }, 'slideup');
}

function mouseover_rating_star(where, what) {
	for(count = 0; count < 6; count += 1) {
		if(count <= what) {
			$('.stars'+where+' .star.star'+count).addClass('active');
		} else {
			$('.stars'+where+' .star.star'+count).removeClass('active');
		}
	}
}
function mouseout_rating_star(where, what) {
	var val = $('#rating_stars_'+where+'_value').val();
	for(count = 0; count < 6; count += 1) {
		if(count <= val) {
			$('.stars'+where+' .star.star'+count).addClass('active');
		} else {
			$('.stars'+where+' .star.star'+count).removeClass('active');
		}
	}
}
function click_rating_star(where, what) {
	$('#rating_stars_'+where+'_value').val(what);
}
function click_rating_submit(what) {
	var val = $('#rating_stars_'+what+'_value').val();
	
	if(val==0) {
		alert('Maak a.u.b. eerst een keuze!');
	} else {
		// update vote
		$.post('ajax/update_tipvote.php', { 'tipid': what, 'val': val }, function(err) {
			if(err.indexOf('error') != -1) { alert(err); }
			update_template('tip_waardering'+what, { 'template': 'tip_eval', 'tipid': what }, 'slideup');
		});
	}
}

function show_rss_detail(channel, rssitem) {

	var speed = 400;
	if(!lock('rsschan'+channel)) {
		$('.rss_channel_'+channel+' .rss_channel_show.active').removeClass('active').slideUp(speed, function() {
			$('.rss_channel_'+channel+' .rss_channel_show_'+rssitem).addClass('active').slideDown(speed);
			unlock('rsschan'+channel);
		});
	}
}

function show_rss_overview(channel) {
	show_rss_detail(channel, 0);
}

function submit_overlay_form(what) {
	
	var keys = new Array();
	var vals = new Array();
	
	$('#contactform .input').each(function() {
		keys.push($(this).attr('name'));
		vals.push($(this).val());
	});
	
	open_overlay({ 'template': what, 'action': 'submit', 'keys[]':keys, 'vals[]':vals });
}

function formatPostcode(s) {
//	a = s.match(/^([1-9][0-9]{3})[\s]?([a-zA-Z]{2})?$/);
	a = s.match(/^([1-9][0-9]{3})[\s]?([a-zA-Z]{2})$/);
	if(a == null) return false; // not a valid postcode
	if((a[2]==undefined) || (a[2]=='')) return a[1]; // 4 numbers, no letters
	return a[1] + ' ' + a[2].toUpperCase(); // 4 numbers, 2 letters
}

function open_solutions(id, theme, reseller) {

	if(reseller == 0) {
		var zip = formatPostcode($('#'+id).val());
		if(zip == false) {
			alert('De ingevoerde postcode is niet juist.');
			return false;
		}
		$('#'+id).attr('value', zip);
	} else {
		var zip = '';
	}
	
	open_overlay({ 'template': 'resellers', 'zipcode': zip, 'theme': theme, 'reseller': reseller });
	
	return false;
}

function get_pressed_key_ascii(e) {
	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e;
		return e.which; //character code is contained in NN4's which property
	}
	e = event;
	return e.keyCode; //character code is contained in IE's keyCode property
}

function keypress_callback(e, char, callback) { // note: enter is char 13
	
	if(get_pressed_key_ascii(e) == char) {
		return call_user_func(callback, Array.prototype.slice.call(arguments, 3));
	}
	return false;
}

function call_user_func (cb, parameters) {

	var func;

	if (typeof cb == 'string') {
		if (typeof this[cb] == 'function') {
			func = this[cb];
		} else {
			func = (new Function(null, 'return ' + cb))();
		}
	} else if (cb instanceof Array) {
		func = eval(cb[0]+"['"+cb[1]+"']");
	}
	
	if (typeof func != 'function') {
		throw new Error(func + ' is not a valid function');
	}
	
	return func.apply(null, parameters); // Array.prototype.slice.call(arguments, 1));
}
	
function site_search(what) {
	
	var val = $('#'+what).val();
	
	if(val=='' || val=='Zoeken') {
		alert('Geef a.u.b. minimaal een zoekterm op.');
	} else {
		open_overlay({ 'template': 'search', 'input': val });
	}
}

