﻿/// <reference path="../../../shared/js/jquery-1.4.2-vsdoc.js" />

/*	=================================================================================================================== */
/* shuffle */
//function shuffle(m, e) {               // pass the divs to the function
//	var replace = $('<div>');
//	var size = e.size();

//	while (size >= 1) {
//		var rand = Math.floor(Math.random() * size);
//		var temp = e.get(rand);      // grab a random div from our set
//		if (size == 1) {             // add an extra class to the last div
//			$(temp).addClass("last");
//		}
//		replace.append(temp);        // add the selected div to our new set
//		e = e.not(temp); // remove our selected div from the main set
//		size--;
//	}
//	m.html(replace.html());     // update our container div with the new, randomized divs
//}

(function ($) {
	$.fn.shuffle = function () {
		return this.each(function () {
			var items = $(this).children();
			return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
		});
	}

	$.shuffle = function (arr) {
		for (
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
		return arr;
	}
})(jQuery);


/*	=================================================================================================================== */
/*	HARMONICA */
function toggleHarmonica(titleId, textId, title, useSlider, url) {
	if ($("#" + textId).is(":hidden")) {

		// Google Analytics code
		registerGA(url + "?" + title);

		document.getElementById(titleId).className = "title open";

		if (useSlider) {
			$("#" + textId).animate({ "height": "toggle" }, "slow");
		} else {
			$("#" + textId).show();
		}
	} else {
		document.getElementById(titleId).className = "title closed";
		if (useSlider) {
			$("#" + textId).animate({ "height": "toggle" }, "slow");
		} else {
			$("#" + textId).hide();
		}
	}
}
/*	=================================================================================================================== */
/*	click with enter */
function doClick(buttonName, e) {
	//the purpose of this function is to allow the enter key to 
	//point to the correct button to click.
	var key;
	if (window.event) {
		key = window.event.keyCode;     //IE
	} else {
		key = e.which;      //firefox
	}
	if (key == 13) {
		//Get the button the user wants to have clicked
		var btn = document.getElementById(buttonName);
		if (btn != null) { //If we find the button click it
			newBtnName = buttonName.replace(/_/g, "$");
			if (DES_BtnClick(buttonName)) {
				__doPostBack(newBtnName, e)
			}
			event.keyCode = 0
		}
	}
}

function doSearch(id) {
	var val = $(id).val();
	if (val == "") {
		alert("Wanneer je niets invult, zal je niets vinden.");
		return false;
	} else {
		top.window.document.location.href = "/nl/zoek.aspx?query=" + encodeString(removeAccents(val));
	}
}

function doEncySearch(id) {
	var val = $(id).val();
	if (val == "") {
		alert("Wanneer je niets invult, zal je niets vinden.");
		return false;
	} else {
		top.window.document.location.href = "/encyclopedie/trefwoord.aspx?zoekwoord=" + val;
	}
}

function doFAQSearch(id) {
    var val = $(id).val();
    if (val == "") {
        alert("Wanneer je niets invult, zal je niets vinden.");
        return false;
    } else {
        top.window.document.location.href = "/nl/service/veelgestelde-vragen/zoekresultaten.aspx?query=" + encodeString(removeAccents(val));
    }
}

function doButtonClick(buttonName, e) {
	//the purpose of this function is to allow the enter key to 
	//point to the correct button to click.
	var key;
	if (window.event) {
		key = window.event.keyCode;     //IE
	} else {
		key = e.which;      //firefox
	}
	if (key == 13) {
		//Get the button the user wants to have clicked
		var btn = document.getElementById(buttonName);
		if (btn != null) { //If we find the button click it
			//btn.click();
			event.keyCode = 0
		}
	}
}

/* GOOGLE ANALYTICS 
==================================================*/
function registerGA(url) {
    try {
        _gaq.push(['_trackPageview', url]);
	} catch (err) {
		setTimeout(function () { registerGA(url); }, 500);
	}
}

function trackEventGA(category, action, label) {
    try {
        _gaq.push(['_trackEvent', category, action, label]);
    } catch (err) {
        setTimeout(function () { trackEventGA(category, action, label); }, 500);
    }
}

/* ADH 
==================================================*/
function toggleGender(genderId, age, pregnant, breastfeeding) {
	var gender = "m";
	tblGender = document.getElementById(genderId);
	rblGender = document.getElementsByTagName('input');

	for (var i = 0; i < rblGender.length; i++) {
		if (rblGender[i].type == "radio") {
			if (rblGender[i].checked) gender = rblGender[i].value;
		}
	}
	if (gender == "m" || document.getElementById(age).value > 50) {
		document.getElementById('trWoman').style.display = 'none';
		document.forms[0].elements[pregnant].checked = false;
		document.forms[0].elements[breastfeeding].checked = false;
	}
	else if (gender == "v" && document.getElementById(age).value < 16) {
		document.getElementById('trWoman').style.display = 'none';
		document.forms[0].elements[pregnant].checked = false;
		document.forms[0].elements[breastfeeding].checked = false;
	}
	else {
		document.getElementById('trWoman').style.display = '';
	}
}

function adh() {
	$('.pAdvice').animate({ height: 130 }, 618);
}

/* GENERAL 
==================================================*/
function getElementPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft, curtop];
}



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 = "";
	//   alert(name + "=" + value + expires + "; path=/; domain=voedingscentrum.nl");
	document.cookie = name + "=" + value + expires + "; path=/;";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

function defaultInputText() {
	// default text 
	$('input[type="text"]').each(function () {
		if ($(this).attr('title')) {
			this.value = $(this).attr('title');
			$(this).addClass('text-label');

			$(this).focus(function () {
				if (this.value == $(this).attr('title')) {
					this.value = '';
					$(this).removeClass('text-label');
				}
			});

			$(this).blur(function () {
				if (this.value == '') {
					this.value = $(this).attr('title');
					$(this).addClass('text-label');
				}
			});
		}
});
$('textarea').each(function () {
	if ($(this).attr('title')) {
		this.value = $(this).attr('title');
		$(this).addClass('text-label');

		$(this).focus(function () {
			if (this.value == $(this).attr('title')) {
				this.value = '';
				$(this).removeClass('text-label');
			}
		});

		$(this).blur(function () {
			if (this.value == '') {
				this.value = $(this).attr('title');
				$(this).addClass('text-label');
			}
		});
	}
});

	$('input:text').focus(function () { if (this.select) this.select(); }); $('textarea').focus(function () { if (this.select) this.select(); }); $('input:password').focus(function () { if (this.select) this.select(); });
}

function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i = 0; i < gy.length; i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

/* SHARE AND PRINT 
==================================================*/
function shareThis() {
	$('.sharepanel').animate({ height: 139 }, 200, function () {
		// animation complete.
		$(".sLoader").hide(function () {
			var u = $(".siframe").attr("rel");
			$(".siframe").attr("src", u);
		});
	});

	return false;
}

function shareClose() {
	$('.sharepanel').hide();
	$('.sharepanel').height(1);
	$('.siframe').height(115);
	$(".sLoader").show();
}

function expandShare() {
	if (parent.$('.sharepanel').height() < 400) {
		parent.$('.sharepanel').animate({ height: 425 }, 200);
		parent.$('.siframe').height(400);
	}
}

/* Site Wide Alert
==================================================*/
function swaClose(swaID) {
	createCookie('vcSWA', swaID, 365);
	$('.swa').hide();
}

/* DEURMAT
==================================================*/
var dmclosetimer = 0;
var dmopentimer = 0;
var dmopen = true;
var dmtimeropenmilli = 200;
var dmcurrentactive;
var dmconst = 400;
var dmtimeropenmilli = dmconst;


function doormat_delayedopen(counter) {
	dmopentimer = window.setTimeout(function () { doormat_open(counter) }, dmtimeropenmilli);
}

function doormat_open(counter) {

	// cancel closing timer and allow opening	
	doormat_canceltimer();
	dmopen = true;

	// define the elements we will be working with 
	var dm = $(".dm" + counter); // get the doormat 

	// do the magic
	$(".deurmat").hide(); // hide all doormats

	dm.show(); // show the doormat
		
	// make everytab inactive
	$('.tab').removeClass('hover');
	// make this one active
	//l.addClass('hover');
	dmtimeropenmilli = 0;
}

function doormat_show() {
	// check if dmopen is still allowed
	if (dmopen == true) {
		$('.sharepanel').hide();
		doormat_canceltimer();
		$('#deurmat').show();
	}
}
function doormat_dmshow() {
	dmopen = true;
	doormat_show();
}

function doormat_close() {
	$('.deurmat').hide();
	//	$('#loader').show();

	// make everytab inactive
	$('.tab').removeClass('active');

	// reset active tab
	$('#' + dmcurrentactive).addClass('active');
	dmtimeropenmilli = dmconst;
}

function doormat_timer() {

	// cancel opening timer 
	if (dmopentimer) {
		window.clearTimeout(dmopentimer);
		dmopentimer = null;
	}

	// check if doormat is open
	if (dmopen == true) {
		dmopen = false;
		// hide doormat in 200
		dmclosetimer = window.setTimeout(doormat_close, dmconst);
	} else {
		doormat_close();
	}
}

function doormat_canceltimer() {
	if (dmclosetimer) {
		window.clearTimeout(dmclosetimer);
		dmclosetimer = null;
	}
}

/*	SEARCH
==================================================*/
var gSearchURL;
function searchDoormat() {
	defaultInputText();
	$(".txt").bind("focus", noCloseDoormat);
	$(".txt").bind("blur", doCloseDoormat);
}
function noCloseDoormat() {
	//doormat_canceltimer();
	$('#deurmat').unbind();
}

function doCloseDoormat() {
	$('#deurmat').bind('mouseover', doormat_dmshow);
	$('#deurmat').bind('mouseout', doormat_timer);

}

function searchClose() {
	doormat_close();
}

/* CAMPAGNEHEADER 
==================================================*/
function WriteCookie(cbID) {
	//gCHurl wordt gezet door Campagne(header)
	var cb = document.getElementById(cbID);
	if (cb.checked) {
		createCookie('CustomHomePage', gCHurl, 365);
	} else {
		eraseCookie('CustomHomePage');
		cb.checked == 0;
	}
}

/* Personas 
==================================================*/
function closePersonas(ckie) {
	if (ckie == true) createCookie('Personas', "off");

	$("#bar").hide();
	$("#deurmat").css("top","122px");
}

/* FLICKR
==================================================*/
function flickrJson(photosetid, divid, gallery) {
	$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=" + photosetid + "&nsid=31619316@N05&lang=nl-NL&format=json&jsoncallback=?", function (data) {
		$.each(data.items, function (i, item) {
			var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");
			var biggie = (item.media.m).replace("_m.jpg", "_b.jpg");

			var id = item.link.replace("http://www.flickr.com/photos/voedingscentrum/", "");
			id = id.substring(0, id.indexOf("/"));
			//$("<img/>").attr("src", sourceSquare).attr("width", 75).attr("height", 75).attr("alt", item.title).appendTo("#" + divid).wrap("<a href='/sites/shared/popups/flickr.aspx?id=" + id + "&gallery=" + gallery + "' title='" + item.title + "' onclick='openShadowBox(this); return false;'></a>");
			var link = "<a href=\"" + biggie + "\" onclick=\"Shadowbox.open({content: '" + biggie + "', player: 'img', title: '"+item.title + "', gallery: '"+photosetid+"'}); return false;\"></a>";
			$("<img/>").attr("src", sourceSquare).attr("width", 75).attr("height", 75).attr("alt", item.title).appendTo("#" + divid).wrap(link);
		});
	});
}

/* SHADOWBOX
==================================================*/
function ShadowBoxCloseReload() { window.parent.location.reload(); var oWin = window.parent.Shadowbox; if (oWin) { oWin.close() } }
function ShadowBoxClose() { var oWin = window.parent.Shadowbox; if (oWin) { oWin.close() } }
function ShadowBoxFitHeight(w) { var oWin = window.parent.Shadowbox; var h = $('#aspnetForm').height(); if (oWin) { oWin.skin.dynamicResize(w, h) } }
function ShadowBoxResize(w, h) { alert(w); var oWin = window.parent.Shadowbox; if (oWin) { oWin.skin.dynamicResize(w, h) } }

function openShadowBox(me) {
	var sUrl = $(me).attr("href");
	var sTitle = $(me).attr("title");
	if (sTitle == "undefined" || sTitle == "") {
		sTitle = "Popup"
	}
	Shadowbox.open({
		content: sUrl,
		player: "iframe",
		animate: true,
		showOverlay: true,
		title: sTitle,
		height: 400,
		width: 500
	});
}

/*	=================================================================================================================== 
SCHIJF VAN VIJF V2 (mek)
=================================================================================================================== */
var imgClient; // id van image
var orgImg; // origineel, als 0 dan niet terugspringen
var curImg = 1; // huidige image
var schijfActive = false;
var schijfNr = 0;

//Rollover Grote Schijf van Vijf
function setSchijf(klasse, nr) {
	if (schijfActive) clearTimeout(schijfActive); // stop reset action
	if (nr) {
		schijfId = $('.' + klasse).attr('id');
		size = klasse == 'schijfvan5' ? 295 : 190;
		document.getElementById(schijfId).src = '/sites/shared/images/2010/schijf' + nr + '_' + size + '.png';
		if (klasse == 'schijfvan5') {
			$('#schijfnummers LI').removeClass('schijfbutton_oranje');
			$('#schijfnummers LI').removeClass('schijfbutton_wit');
			$('#schijfnummers LI:not(#' + nr + ')').addClass('schijfbutton_wit');
			$('#schijfnummers LI#' + nr).addClass('schijfbutton_oranje');
		}
	}
}
function resetSchijf(klasse) {
	if (schijfActive) clearTimeout(schijfActive); // stop reset action
	schijfActive = setTimeout("resetSchijfNu('" + klasse + "');", 100); // (re)start reset action
}
function resetSchijfNu(klasse) {
	if (schijfActive) clearTimeout(schijfActive); // stop reset action
	schijfId = $('.' + klasse).attr('id');
	size = klasse == 'schijfvan5' ? 295 : 190;
	if (klasse == 'schijfvan5') {
		// seek default (open div schijf#)
		var schijfDefault = 0;
		for (var i = 0; i < 6; i++) {
			if (document.getElementById("Schijf" + i).style.display != "none") {
				schijfDefault = i;
				break;
			}
		}
		document.getElementById(schijfId).src = '/sites/shared/images/2010/schijf' + schijfDefault + '_' + size + '.png';
		$('#schijfnummers LI').removeClass('schijfbutton_oranje');
		$('#schijfnummers LI').removeClass('schijfbutton_wit');
		$('#schijfnummers LI:not(#' + schijfDefault + ')').addClass('schijfbutton_wit');
		$('#schijfnummers LI#' + schijfDefault).addClass('schijfbutton_oranje');
	} else {
		document.getElementById(schijfId).src = '/sites/shared/images/2010/schijf0_' + size + '.png';
	}
}

//Functie die aangeroepen wordt voor onclick grote Schijf van Vijf
function selectSchijf(teller) {
	for (var i = -1, n = 6; ++i < n; ) {
		document.getElementById("Schijf" + i).style.display = 'none';
	}
	document.getElementById("Overzicht").style.display = 'none';
	if (teller == 6) {
		document.getElementById("Overzicht").style.display = '';
	} else if (teller != 6) {
		document.getElementById("Schijf" + teller).style.display = '';
	}
}

// select schijf in deurmat
function selectDeurmatSchijf(teller) {
	var link;
	if (teller == 6) {
		link = "/nl/schijf-van-vijf/schijf.aspx?schijf=Overzicht";
	}
	if (teller != 6) {
		link = "/nl/schijf-van-vijf/schijf.aspx?schijf=" + teller;
	}
	window.location = link;

	// google analytics
	if (_gaq) {
		_gaq.push(['_trackPageview', link])
	}
}


function encodeString(str) {
	var encodedStr = escape(str);

	encodedStr = encodedStr.replace("+", "%2B");
	encodedStr = encodedStr.replace("/", "%2F");

	return encodedStr;

}



function removeAccents(s) {

	var r = s.toLowerCase();

	//r = r.replace(new RegExp("\\s", 'g'), "");
	r = r.replace(new RegExp("[àáâãäå]", 'g'), "a");
	r = r.replace(new RegExp("æ", 'g'), "ae");
	r = r.replace(new RegExp("ç", 'g'), "c");
	r = r.replace(new RegExp("[èéêë]", 'g'), "e");
	r = r.replace(new RegExp("[ìíîï]", 'g'), "i");
	r = r.replace(new RegExp("ñ", 'g'), "n");
	r = r.replace(new RegExp("[òóôõö]", 'g'), "o");
	r = r.replace(new RegExp("œ", 'g'), "oe");
	r = r.replace(new RegExp("[ùúûü]", 'g'), "u");
	r = r.replace(new RegExp("[ýÿ]", 'g'), "y");
	//r = r.replace(new RegExp("\\W", 'g'), "");

	return r;
}

/*TOP TABS
=================================================================================================================== */
var ttclosetimer = 0;
var xxopen = false;

function toptap_open() {
	var pt = $("#xx").position().left;
	$("#toptabsxx").css("left", pt + "px").animate({ top: '0px' }, 200);
	$("#xx").removeClass("tt");
	xxopen = true;

	// close again after 7.8 seconds
	setTimeout('toptap_timer', 7000);
}

function toptap_close() {
	if (xxopen == false) {
		$("#xx").addClass("tt");
		$("#toptabsxx").animate({ top: '-380px' }, 200);
	}
}

function toptap_timer() {
	xxopen = false;
	ttclosetimer = window.setTimeout(toptap_close, 800);
}
function toptap_show() {
	toptap_canceltimer();
	if (xxopen == false) {
		toptap_open();
	}
}

function toptap_canceltimer() {
	if (ttclosetimer) {
		window.clearTimeout(ttclosetimer);
		ttclosetimer = null;
	}
}


/*	=================================================================================================================== 
ON LOAD
=================================================================================================================== */
$(document).ready(function () {
	// run all kinds of scripts 

	//$('#deurmatholder').bind('mouseover', doormat_dmshow);
	$('#toptabs a#xx').bind('mouseover', toptap_show);
	$('#toptabs a#xx A').bind('mouseover', toptap_show);
	$('#toptabsxx A').bind('mouseover', toptap_show);
	$('#toptabsxx A').bind('mouseout', toptap_timer);
	// *tabs
	$('A.tt').mouseover(function () {
		$(this).not('#xx').animate({ top: '0px' }, 200);
	});

	$('A.tt').mouseout(function () {
		$(this).not('#xx').animate({ top: '-8px' }, 200);
	});

	// set default texts on textfields
	defaultInputText();

	// webshop table height optimizer
	var th = 0;
	$('TABLE.webshop').each(function () {

		if ($(this).height() > th) {
			th = $(this).height();
		}
	});
	$('TABLE.webshop').each(function () {
		$(this).height(th);
	});

});
