/********************************************************
*
* onweer-online.nl javascript library - nu met jQuerysaus
* (c) 2012 onweer-online.nl
*
********************************************************/

bSetRat = false;
bAlreadySet = false;
bStop = false;
n = false;
oPrev = "";
iCurSnowMap = 11;

/* JWS @ 26-12-2015 - Frontpage onclick meer recente topics tonen */

$('div.recenttopicrow:gt(17)').hide();
$('.toggle-more-topics').on('click', function() {
    var visible = $('div.recenttopicrow:visible').length -1;
    $('div.recenttopicrow:gt('+visible+')').slice(0,10).show();

    if($('div.recenttopicrow:visible').length == $('div.recenttopicrow').length) {
	    $('.topics-more').hide();
    }
})

/* JWS @ 24 11 2015 - abbr aanklikbaar maken */

function makeAbbrevsClickable()
{
	$("abbr").click(function()
	{
		sUrl = $(this).attr("data-source");
		window.location = sUrl;
	});
}

/* BRS @ 26 08 2014 - Radarfix */

function animateWeatherRadar(prefix, size, target) {
	itr = 0;
	objDate = new Date();
    _timeCode = objDate.getTime();

    setInterval(function() {
        _timeCode = objDate.getTime();
    }, (((1000) * 60) * 5));


	setInterval(function() {
		itr++;
		$(target).attr('src', 'https://www.onweer-online.nl/tools/cache/wpbr'+size+prefix+itr+'.gif?'+_timeCode);
		if(itr == 11) {
			itr = 0;
		}

	}, 800);
}

$(document).ready(function() {

    //NL
	if($('#block_86187a7c8c2d1f252ba0068f20b615bb').length && sCountry == 'nl') {
		animateWeatherRadar('', 'small', '#block_86187a7c8c2d1f252ba0068f20b615bb .block_content_nopadding a img');
		$('#block_86187a7c8c2d1f252ba0068f20b615bb .block_content_nopadding a').css({'background':'url(https://www.onweer-online.nl/tools/mapbgsmall.gif)','display':'block','height':'251px'});
	}

	// BE
	if($('#block_86187a7c8c2d1f252ba0068f20b615bb').length && sCountry == 'be') {
		animateWeatherRadar('be', 'small', '#block_86187a7c8c2d1f252ba0068f20b615bb .block_content_nopadding a img');
		$('#block_86187a7c8c2d1f252ba0068f20b615bb .block_content_nopadding a').css({'background':'url(https://www.onweer-online.nl/tools/mapbgsmallbe.gif)','display':'block','height':'154px'});
	}

	//Large, nl
	if($('#block_ed797bd5614234c2cfa8bfd3668d4485').length && sCountry == 'nl') {
		animateWeatherRadar('', '', '#block_ed797bd5614234c2cfa8bfd3668d4485 .block_content img');
		$('#block_ed797bd5614234c2cfa8bfd3668d4485 .block_content img').css({'background':'url(https://www.onweer-online.nl/tools/mapbg.gif)'});
	}

	//Large, be
	if($('#block_d9065e3804b861194c3480d60bca2435').length && sCountry == 'be') {
		animateWeatherRadar('', '', '#block_d9065e3804b861194c3480d60bca2435 .block_content img');
		$('#block_d9065e3804b861194c3480d60bca2435 .block_content img').css({'background':'url(https://www.onweer-online.nl/tools/mapbg.gif)'});
	}

	/* BRS @ 15 06 2015 - Guidance */
	$('.toggle').click(function(e) {
		e.preventDefault();
		$('.toggle').toggleClass('selected');
		$('.weerbericht').fadeToggle();

		$('#weerbericht_meer').toggleClass('hide')

	});

	$('.alineakop:eq(0), .alineakop:eq(3)').remove();


	makeAbbrevsClickable();

});

/* End BRS @ 26 08 2014 - Radarfix */

function urlencode(str)
{
	ret = encodeURIComponent(str.toString()).replace(/%20/g,'+');
	return ret;
}

function minimizeBlock(sBlockID)
{
	oBlock = document.getElementById("block_"+sBlockID);
	oBlockHeader = oBlock.getElementsByTagName("h1");

	oLangBlock = false;

	oBlockMin = oBlock.getElementsByTagName("div");
	for(i = 0; i < oBlockMin.length; i++)
	{
		if(oBlockMin[i].className == 'minimize') oMinBlock = oBlockMin[i];
		if(oBlockMin[i].className == 'block_content' || oBlockMin[i].className == 'block_content_nopadding') oBlockContent = oBlockMin[i];
		if(oBlockMin[i].className == 'lang_switch') oLangBlock = oBlockMin[i];
	}

	oMinKnop = oMinBlock.getElementsByTagName("div");

	if(oBlock.offsetHeight == 25)
	{
		oBlock.style.height = 'auto';
		oBlockContent.style.display = 'block';
		oBlockHeader[0].style.height = '50px';
		oMinBlock.style.height = '50px';
		oMinKnop[0].style.backgroundPosition = '0px -59px'
		eraseCookie("hideblock_"+sBlockID);
		oLangBlock.style.display = 'block';
	}
	else
	{
		oBlock.style.height = '25px';
		oBlockContent.style.display = 'none';
		oBlockHeader[0].style.height = '25px';
		oMinBlock.style.height = '25px';
		oMinKnop[0].style.backgroundPosition = '-11px -59px'
		createCookie("hideblock_"+sBlockID,"true", 30);
		oLangBlock.style.display = 'none';
	}
}

function createCookie(name, value, days)
{
	if(days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 86400000));
		var expires = "; expires="+date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/; domain=.localhost";
	//document.cookie = "frmNaam=Justinos;expires="+new Date().toGMTString()+"; path=/; domain=.localhost";
}

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);
}

var caps_lock_on = false;

function show_hide_caps_warn(bShow)
{
	if(bShow)
		document.getElementById("gb_warning").innerHTML = 'Je caps-lock staat aan!';
	else
		document.getElementById("gb_warning").innerHTML = '';
}

function caps_lock(e)
{
	var key=null;
	var ShiftPressed=false;

	if(!e) e=window.event;
	if(!e) return;

	if(e.which)
		key=e.which;
	else if(e.keyCode)
		key=e.keyCode;
	else if(e.charCode)
		key=e.charCode;

	if(e.shiftKey)
		ShiftPressed=e.shiftKey;
	else if(e.modifiers)
	{
		if(e.modifiers&4) ShiftPressed=true;
	}

	key=String.fromCharCode(key);

	if((ShiftPressed==true && key.toUpperCase()!=key) || (ShiftPressed==false && key.toLowerCase()!=key))
	{
		show_hide_caps_warn(true);
		caps_lock_on=true;
	}
	else
	{
		show_hide_caps_warn(false);
		caps_lock_on=false;
	}
}


function activateIcon(oTD)
{
	oTD.className = "gb_weericon_active";
	if(oPrev) oPrev.className = "gb_weericon";
	oPrev = oTD;
	document.forms.guestbookForm.frmIcoon.value = oTD.id.replace("id", "");
}

function loadTab(sTabname, oTab)
{
	oTabsBar = document.getElementById("tabs");
	oLis = oTabsBar.getElementsByTagName("li");
	for(i = 0; i < oLis.length; i++)
	{
		oLis[i].className = '';
	}

	oTab.className = 'active-tab';

	oGuestbook = document.getElementById("guestbook");
	oPages = oGuestbook.getElementsByTagName("div");
	for(i = 0; i < oPages.length; i++)
	{
		if(oPages[i].className == 'tabs_panel') oPages[i].style.display = 'none';
	}

	oTabContents = document.getElementById(sTabname);
	oTabContents.style.display = 'block';
}


function viewPhoto(sFile)
{
	window.open('viewgbphoto.php?file='+sFile,'myGBfotoWindow','width=800,height=600,scroll=no,status=no,menu=no,toolbar=no,help=no');
}

function loadPage(iPage)
{
	$("#overzicht").html("<img src=\"/images/loading2.gif\" class=\"gb_loading\" alt=\"loading\" />");
	$("#overzicht").load("/controller/guestbook/?getpage="+iPage);
}

function openWebcam(sURL)
{
	window.open(sURL,"ooWebCamWindow");
}

function showHideNews(iID)
{
	oTD=document.getElementById("newshide"+iID);
	if(oTD.style.display=="none")
		oTD.style.display="block";
	else
		oTD.style.display="none";
}

function setRating(iID)
{
	if(bAlreadySet) return;
	if(n) clearTimeout(n);

	aWaarderingen = new Array();
	aWaarderingen[1] = "Slecht";
	aWaarderingen[2] = "Matig";
	aWaarderingen[3] = "Redelijk";
	aWaarderingen[4] = "Goed";
	aWaarderingen[5] = "Geweldig!";

	oRatingDiv = document.getElementById("rating");
	oSpan = oRatingDiv.getElementsByTagName("span");

	oSpan[0].innerHTML = aWaarderingen[iID];

	bSetRat=true;
	iNumSters=0;
	for(i=1;i<=iID;i++)
	{
		thisid = document.getElementById('rid'+i);
		thisid.src = "/images/sterbv.png";
		iNumSters++;
	}
	for(i=(iNumSters+1);i<6;i++)
	{
		thisid = document.getElementById('rid'+i);
		thisid.src = "/images/sterbl.png";
	}
}

function cancelRating()
{
	if(bAlreadySet) return;
	if(!bSetRat)
	{
		$("#rating").html(sCurRating);
	}
}

function loadRating()
{
	sCurRating = $("#rating").html();
}

function defRating(iScore, iVid)
{
	if(bAlreadySet) return;

	$("#rating span").first().load("/controller/?setvideorating="+iVid+"&score="+iScore);

	bAlreadySet = true;
}

function disableButton(oButton)
{
	oButton.disabled = true;
	return true;
}

function rateComment(iID, sHow)
{
	$("#comment_rating_"+iID).load("/controller/?videocommentrating="+sHow+"&cid="+iID);
}

function adjustWeatherInfoHeight()
{
	oWB = $("#weerbericht");

	iHeight = oWB.height();

	if(iHeight > 460)
	{
		$("#weerbericht_meer").click(function()
		{
			$("#weerbericht").css('height', iHeight+'px');
			$("#weerbericht_meer").css('display', 'none');
		});
		oWB.css({'overflow': 'hidden', 'height': '460px'});
		$("#weerbericht_meer").css('display', 'block');

	}
}

function submitShout()
{
	sShoutInput = $("#frmShout").val();
	$("#frmShout").attr("disabled", "disabled");
	$("#shoutboxrows").load("/controller/?shout="+urlencode(sShoutInput), function()
	{
		$("#frmShout").val("Tik hier je shout...").addClass("shoutinput_italic").attr("disabled", "");
	});
}

function loadNextSnowMap()
{
	if(iCurSnowMap == 1)
		iCurSnowMap = 11;
	else
		iCurSnowMap--;

	oSneeuwKaart = document.getElementById("sneeuwkaartimage");
	oSneeuwKaart.src = "/images/maps/sneeuw"+iCurSnowMap+".gif";

	setTimeout("loadNextSnowMap()", 1000);
}


function scrollIt(sObject, sDirection)
{
	switch(sObject)
	{
		case "meldboek_selected_icon":
			iNumIcons = document.getElementById('num_icons').value;
			moveObject(sObject, document.getElementById('meldboek_icons_scroller'), 114, iNumIcons, sDirection);
			break;
	}
}

function moveObject(sObject, oObject, iAmount, iNumIcons, sDirection)
{
	oInputBox = document.getElementById(sObject+"_input");
	iIconPosition = oInputBox.value;

	if(sDirection == "forward")
		iIconPosition--;
	else
		iIconPosition++;

	if(iIconPosition == iNumIcons) iIconPosition = 0;
	if(iIconPosition == -1) iIconPosition = (iNumIcons - 1);

	oInputBox.value = iIconPosition;

	oObject.style.top = (0 - (iIconPosition * iAmount)) + 'px';
}


function catchScrollwheel(event)
{
	var delta = 0;

	if (!event) event = window.event;

	if(event.wheelDelta)
	{
		// IE & Opera
		delta = event.wheelDelta / 60;
	}
	else if(event.detail)
	{
		// W3C
		delta = -event.detail / 2;
	}

	if(delta < 0)
		scrollIt(this.id, "back");
	else
		scrollIt(this.id, "forward");

	if(event.stopPropagation) event.stopPropagation();
	if(event.preventDefault) event.preventDefault();
	event.cancelBubble = true;
	event.cancel = true;
	event.returnValue = false;
	return false;
}

// voor meldboek_simple (todo: ombouwen naar jQuery)
function addScrollWheelListener()
{
	oScrollable = document.getElementById("meldboek_selected_icon");
	if(!oScrollable) return;

	if(window.addEventListener) oScrollable.addEventListener('DOMMouseScroll', catchScrollwheel, false);
	oScrollable.onmousewheel = catchScrollwheel;
}

function changeVisitorName()
{
	oNameDiv = document.getElementById("gbname");

	sValue = '';
	if(oNameDiv.innerHTML.indexOf("klik hier") == -1) sValue = oNameDiv.innerHTML;

	oNameDiv.innerHTML = '<input type="text" id="gbnewname" name="gbnewname" value="'+sValue+'" onblur="setNewVisitorName(this.value);" />';
	oNameDiv.onclick = null;

	oNewInput = document.getElementById("gbnewname");
	oNewInput.focus();
}

function setNewVisitorName(sValue)
{
	if(sValue == "")
	{
		sValue = "&lt;klik hier&gt;"
	}
	else
	{
		createCookie("frmNaam", sValue, 31);
	}

	$("#gbname").html(sValue).click(function()
	{
		changeVisitorName();
	});
}

function changeVisitorCity()
{
	oPlaceDiv = document.getElementById("gbplace");

	sValue = '';
	if(oPlaceDiv.innerHTML.indexOf("klik hier") == -1) sValue = oPlaceDiv.innerHTML;

	oPlaceDiv.innerHTML = '<input type="text" id="gbnewplace" name="gbnewplace" value="'+sValue+'" onblur="setNewVisitorCity(this.value);" />';
	oPlaceDiv.onclick = null;

	oNewInput = document.getElementById("gbnewplace");
	oNewInput.focus();
}

function setNewVisitorCity(sValue)
{
	if(sValue == "")
	{
		sValue = "&lt;klik hier&gt;"
	}
	else
	{
		createCookie("plaats", sValue, 31);
	}

	$("#gbplace").html(sValue).click(function()
	{
		changeVisitorCity();
	});
}

function submitMeldboekSimple()
{
	sName = $("#gbname").text();
	sPlace = $("#gbplace").text();

	if(!sName || sName.indexOf("klik hier") > 0)
	{
		alert("Vul eerst je naam in.");
		return;
	}

	if(!sPlace || sPlace.indexOf("klik hier") > 0)
	{
		alert("Vul eerst de plaatsnaam in van waar je nu bent.");
		return;
	}

	window.location = '/controller/?gbname='+escape(sName)+'&gbplace='+escape(sPlace)+'&new_meldboek_icon=' + $("#meldboek_selected_icon_input").val();
}

function makeRating(iID)
{
	oBlink = document.getElementById('boz'+iID);
	oBlink.innerHTML = '';
	oRating = document.getElementById('rating'+iID);
	sIH = "Slecht&nbsp;";
	for(i=1; i<6; i++)
	{
		sIH += "<input type='radio' name='makeRating' value='"+i+"' onclick='window.location=\"/controller/?set_link_rating="+i+"&srid="+iID+"\";'>";
	}
	sIH += "&nbsp;Goed";
	oRating.innerHTML = sIH;
}

function getWindBft(iSpeed)
{
	if(iSpeed > 32.6) return 12;
	if(iSpeed > 28.4) return 11;
	if(iSpeed > 24.4) return 10;
	if(iSpeed > 20.7) return 9;
	if(iSpeed > 17.1) return 8;
	if(iSpeed > 13.8) return 7;
	if(iSpeed > 10.7) return 6;
	if(iSpeed > 7.9) return 5;
	if(iSpeed > 5.4) return 4;
	if(iSpeed > 3.3) return 3;
	if(iSpeed > 1.5) return 2;
	if(iSpeed > 0.2) return 1;
	return 0;
}

iMenuY = 0;
iMenuHeight = 0;
bMenuFixed = false;

$(document).ready(function()
{
	sQS = document.location.search;

	if(sQS.indexOf("displayvideo") > 0) loadRating();

	// beetje smerige check om te zien of dit de homepizzo is
	if(typeof(boltekNames) != 'undefined')
	{
		adjustWeatherInfoHeight();
		addGmapCheckboxListeners();
		load();
	}

	addScrollWheelListener();

	setWeatherInfoTabs();

	// nieuwe kak voor o.a. het forum

	$("#headerloginbutton").toggle(
		function()
		{
			$("#headerlogin .formcontents").animate({left: 0}, 800);
			$("#headerloginbutton").addClass("hlbback");
		},
		function()
		{
			$("#headerlogin .formcontents").animate({left: 296}, 800);
			$("#headerloginbutton").removeClass("hlbback");
		}
	);

	oMenuPos = $("#header_menu").offset();
	iMenuY = oMenuPos.top;
	iMenuHeight = $("#header_menu").height();

	$(document).bind("scroll", function()
	{
		checkMenuFixation();
	});

	checkMenuFixation()
});

function setWeatherInfoTabs()
{
	$("#weatherscroll .tabm,#weatherscroll .tab1,#weatherscroll .tab2").click(function()
	{
		$("#weatherscroll .info").hide();
		$("#weatherscroll .activetab").removeClass("activetab");
		$(this).addClass("activetab");

		sDay = $(this).html();
		if(sDay == "Vandaag")
		{
			$("#weatherscroll .vandaag").show();
		}
		if(sDay == "Morgen")
		{
			$("#weatherscroll .morgen").show();
		}
		if(sDay == "Overmrgn")
		{
			$("#weatherscroll .overmorgen").show();
		}
	});
}

function checkMenuFixation()
{
	iScrollTop = $(document).scrollTop();

	if(bMenuFixed == true)
	{
		if(iScrollTop <= iMenuY)
		{
			bMenuFixed = false;
			$("#header_menu").removeClass("fixedmenu");
			//$("#content").css("margin-top", "0px");
		}
	}
	else
	{
		if(iScrollTop >= iMenuY)
		{
			bMenuFixed = true;
			$("#header_menu").addClass("fixedmenu");
			//$("#content").css("margin-top", iMenuHeight+"px");
		}
	}
}


// nieuw toegevoegde meuk voor o.a. het forum
// P.S. Haal ff een biertje voor me - kep dorst

function getFFFocus(oInput, sDefaultValue)
{
	if(oInput.value == sDefaultValue)
	{
		oInput.value = '';
		oInput.className = '';
		if(oInput.name == "password") oInput.type = 'password';
	}
}

function blurFF(oInput, sDefaultValue)
{
	if(oInput.value == '')
	{
		oInput.value = sDefaultValue;
		oInput.className = 'nodata';
		if(oInput.name == "password") oInput.type = 'text';
	}
}
