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

bSetRat = false;
bAlreadySet = false;
bStop = false;
iCurOpen = false;
n = false;
iMenuOpen = 0;
oPrev = "";
iCurSnowMap = 11;

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

function changeWeatherPlace()
{
	if(typeof sCountry == "undefined") sCountry = "nl";

	if(typeof knmiIcons == "undefined")
	{
		setTimeout("changeWeatherPlace();", 2000);
		return;
	}

	if(sCountry == 'nl')
	{
		iNumStations = knmiIcons["data"].length;
		iShowStation = Math.round(Math.random() * (iNumStations - 1));
		aThisStation = knmiIcons["data"][iShowStation];
	}
	else if(sCountry == 'be')
	{
		iNumStations = kmiIcons["data"].length;
		iShowStation = Math.round(Math.random() * (iNumStations - 1));
		aThisStation = kmiIcons["data"][iShowStation];
	}

	aIconPos = aThisStation["spos"].split("x");
	aIconPos[0] *= 2;
	aIconPos[1] *= 2;

	$("#weathericon").css('background-position', '-'+aIconPos[0]+'px -'+aIconPos[1]+'px');
	$("#tempgroot").html(aThisStation["temp"].toString().replace(".", ",")+"&deg;C");

	sHTML  = "<strong>"+aThisStation["city"]+"</strong><br />";
	if(aThisStation["lv"] > 0) sHTML += "# luchtvochtigheid: "+aThisStation["lv"]+"%<br />";
	sHTML += "# wind: "+aThisStation["wind"]+" m/s<br />";
	if(aThisStation["sight"] > 0) sHTML += "# zicht: "+aThisStation["sight"]+" meter<br />";
	if(aThisStation["hpa"] > 0) sHTML += "# luchtdruk: "+aThisStation["hpa"]+" hPa";
	$("#weerdiv").html(sHTML);
	setTimeout("changeWeatherPlace();", 8000);
}

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=.onweer-online.nl";
}

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 > 225)
	{
		$("#weerbericht_meer").click(function()
		{
			$("#weerbericht").css('height', iHeight+'px');
			$("#weerbericht_meer").css('display', 'none');
		});
		oWB.css({'overflow': 'hidden', 'height': '190px'});
		$("#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()
{
	window.location = '/controller/?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;
}


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

	changeWeatherPlace();

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

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

		addGmapCheckboxListeners();

		load();
		window.onunload = GUnload;
	}

	addScrollWheelListener();

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

	if($("#footer").length)
	{
		rePositionFooter();
	}
});




// 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';
	}
}

function rePositionFooter()
{
	iBrowserHeight = $(window).height();
	iDocumentHeight = $("body").height();

	if(!iBrowserHeight || !iDocumentHeight) return;
	if(iDocumentHeight >= iBrowserHeight) return;

	iGrowHeight = (iBrowserHeight - iDocumentHeight) + 10;

	$("#footergrowdiv").css("height", iGrowHeight + "px");
}




