
var activReloadTrigger = null;
var newRefreshUrl = null;
var beginOfNormalReloadLevel = 120;
var reloadDefaultLevel = 180;
var selectValues = new Array(); // values needs to be ascending!
selectValues[0] = new Array(0, "deaktiviert");
selectValues[1] = new Array(10, "10 sek");
selectValues[2] = new Array(20, "20 sek");
selectValues[3] = new Array(60, "1 min");
selectValues[4] = new Array(180, "3 min");
selectValues[5] = new Array(540, "9 min");
// methods
function reloadPage()	{
	if(newRefreshUrl == null){
		location.href = location.href;
	}else{
		location.href = newRefreshUrl;
	}
}
function setReloadTrigger(value){
	if(activReloadTrigger != null){
		window.clearTimeout(activReloadTrigger);
	}
	if(value >= 1){
		activReloadTrigger = window.setTimeout("reloadPage()", value * 1000);
	}
}
function clearReloadTrigger(){
	if(activReloadTrigger != null){
		window.clearTimeout(activReloadTrigger);
		activReloadTrigger = null;
	}
}
function interruptReload(){
	if(activReloadTrigger != null){
		clearReloadTrigger();
		var selectNode = document.getElementById("shoutbox_reload");
		selectNode.value = 0;
		selectNode.setAttribute("class", getCssClassForValue(0));
	}
}
function applyChanges(){
	if( document.getElementsByName("shoutbox_submit").length == 0){
		window.setTimeout("applyChanges()", 10);
	}else{
		document.getElementById("shoutbox-message").setAttribute("onfocus", "interruptReload();");
		document.getElementsByName("shoutbox_name")[0].setAttribute("onfocus", "interruptReload();");
		document.getElementsByName("shoutbox_submit")[0].setAttribute("value", "Nachricht senden!");
		var theForm = document.getElementById("shoutbox_form");
		var newTag = document.createElement("strong");
		newTag.appendChild(document.createTextNode("Aktualisierungsintervall:"));
		theForm.parentNode.insertBefore(newTag, theForm);
		newTag = document.createElement("br");
		theForm.parentNode.insertBefore(newTag, theForm);
		newTag = document.createElement("select");
		newTag.setAttribute("style", "width:25%;");
		newTag.setAttribute("id", "shoutbox_reload");
		newTag.setAttribute("onchange", "refreshSelect();");
		for(i = 0; i < selectValues.length; i++){
			addOption(newTag, selectValues[i][1], selectValues[i][0]);
		}
		newTag.value = getReloadValue();
		newTag.setAttribute("class", getCssClassForValue(newTag.value));
		theForm.parentNode.insertBefore(newTag, theForm);
		setFormAction(newTag.value);

		// document.getElementsByName("body")[0].setAttribute("onfocus", "alert('page get focus');");
		// document.getElementsByName("body")[0].setAttribute("onblur", "alert('page lost focus');");
	}
}
function addOption(selectNode, text, value){
	var cssClass = getCssClassForValue(value);
	neuOpt = document.createElement("option");
	neuOpt.appendChild(document.createTextNode(text));
	neuOpt.setAttribute("class", cssClass);
	neuOpt.setAttribute("value", value);
	selectNode.appendChild(neuOpt);
}
function getReloadValue(){
		var urlParts = location.href.split("?reload=");
		var result = reloadDefaultLevel;
		if(urlParts.length == 2 && !isNaN(urlParts[1])){
		var inputVal = urlParts[1];
		result = 0;
		for(i = 0; i < selectValues.length; i++){
			result = selectValues[i][0];
			if(selectValues[i][0] >= inputVal){
				break;
			}
		}
		if(result != inputVal){
			 newRefreshUrl = location.href.split("?reload=")[0] + "?reload=" + result;
		}
		}
		return result;
}
function getCssClassForValue(value){
	if(value <= 0)
		return "reload_deactivated";
	if(value < beginOfNormalReloadLevel)
		return "reload_tooFast";
	return "reload_normal";
}
function refreshSelect(){
	var urlParts = location.href.split("?reload=");
	var selectNode = document.getElementById("shoutbox_reload");
		 selectNode.setAttribute("class", getCssClassForValue(selectNode.value));
		 newRefreshUrl = urlParts[0] + "?reload=" + selectNode.value;
		 if(selectNode.value == 0){
				clearReloadTrigger();
		 }else if(selectNode.value > 0){
				setReloadTrigger(selectNode.value);
		 }
		 setFormAction(selectNode.value);
}
function setFormAction(value){
		var theForm = document.getElementById("shoutbox_form");
		 var formActionParts = theForm.action.split("?reload=");
	theForm.action = formActionParts[0] + "?reload=" + value;
}
// function calls
setReloadTrigger(getReloadValue());
window.setTimeout("applyChanges()", 15);


