// ====== Melkweg Dynamic Functions =======
// Lodewijk Evers (c)2005
//
// some parts of the code are from the following sources
// http://www.quirksmode.org
// http://twilightuniverse.com
// http://www.alistapart.com

/* ***** variables configuration here ***** */

// change these values for the clock
var clockDivId = 'headclock';
var clockID = 0;
var oldbrowser=!document.all&&!document.getElementById
// change these values for the header scroller
var newsDivId = 'headscroller';
var newsNumItems = 0;
// a little optimization
var sfHoverReg = new RegExp(" sfhover\\b");
var multiDateReg = new RegExp(" dateon\\b");
// the animations for multidate events
var mds = new Array();


/* ***** basic browser detection ***** */
if (navigator.product == 'Gecko') {
	isGecko = true;
} else {
	isGecko = false;
}

/* ***** functions start here ***** */
// this is an adapted suckerfish like the one from http://www.alistapart.com
function sfHover() {
	var sfEls = document.getElementById("static").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(sfHoverReg, "");
		}
	}
}

function multiDateExpand() {
	var node = document.getElementById('container'); 
	var sfEls = getElementsByClassName(node,'multidate');
	for (var i=0; i<sfEls.length; i++) {
		var els = sfEls[i].getElementsByTagName('span');
		//alert (getItemHeight(sfEls[i])+' '+ sfEls[i].parentNode.parentNode.id);
		expandableNodeParent = sfEls[i].parentNode.parentNode;
		expandableNodeParent.className+=" expander";

	}
}

function classExpander() {
	var node = document.getElementById('container'); 
	var sfEls = getElementsByClassName(node,'ititle');
	for (var i=0; i<sfEls.length; i++) {
//		alert (getItemHeight(sfEls[i]));
		var els = sfEls[i].getElementsByTagName('span');
		for(var k=0,l=els.length; k<l; k++) {
			if (getItemHeight(els[k])>45) {
				expandableNodeParent = sfEls[i].parentNode.parentNode;
				expandableNodeParent.className+=" expander";
//				alert (getItemHeight(els[k])+ ' - '+sfEls[i].parentNode.parentNode.nodeName+ ' - '+sfEls[i].parentNode.parentNode.className+ ' - '+sfEls[i].parentNode.parentNode.getAttribute('id'));
			}
		}
	}
}
	
function getElementsByClassName(node, className)
{
	var a = [];
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++) {
		if(els.item(i).className.indexOf(className)!= -1) {
			a.push(els[i]);
		}
	}
	return a;
}

/* start windows dimension scripts */
// for an explanation of these functions see http://www.quirksmode.org

// check the screen
function getViewPortHeight() {
	var y;
	if (self.innerHeight) {
		// all except Explorer
			y = self.innerHeight;
		}
	else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
			y = document.documentElement.clientHeight;
		}
	else if (document.body) {
		// other Explorers
			y = document.body.clientHeight;
		}
	return y;
	}

function getViewPortWidth() {
	var x;
	if (self.innerWidth) {
		// all except Explorer
			x = self.innerWidth;
		}
	else if (document.documentElement && document.documentElement.clientWidth) {
		// Explorer 6 Strict Mode
			x = document.documentElement.clientWidth;
		}
	else if (document.body) {
		// other Explorers
			x = document.body.clientWidth;
		}
	return x;
	}

function getScrollOffsetHeight () {
	var y;
	if (self.pageYOffset) {
		// all except Explorer
			y = self.pageYOffset;
		}
	else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		y = document.documentElement.scrollTop;
	}
	else if (document.body) {
		// all other Explorers
			y = document.body.scrollTop;
		}
	return y;
	}

function getScrollOffsetWidth () {
	var x;
	if (self.pageXOffset) {
		// all except Explorer
			x = self.pageXOffset;
		}
	else if (document.documentElement && document.documentElement.scrollLeft) {
		// Explorer 6 Strict
			x = document.documentElement.scrollLeft;
		}
	else if (document.body) {
		// all other Explorers
			x = document.body.scrollLeft;
		}
	return x;
	}

function getPageHeight () {
	var y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) {
		// all but Explorer Mac
			y = document.body.scrollHeight;
		}
	else {
		// Explorer Mac;
		// would also work in Explorer 6 Strict, Mozilla and Safari
			y = document.body.offsetHeight;
		}
	return y;
	}

function getPageWidth () {
	var x;
	var test1 = document.body.scrollWidth;
	var test2 = document.body.offsetWidth
	if (test1 > test2) {
		// all but Explorer Mac
			x = document.body.scrollWidth;
		}
	else {
		// Explorer Mac;
		// would also work in Explorer 6 Strict, Mozilla and Safari
			x = document.body.offsetWidth;
		}
	return x;
	}
	
function getItemHeight(Item) {
	var x;
	var test1 = Item.scrollHeight;
	var test2 = Item.offsetHeight;
	if (test1 > test2) {
		// all but Explorer Mac
			x = Item.scrollHeight;
		}
	else {
		// Explorer Mac;
		// would also work in Explorer 6 Strict, Mozilla and Safari
			x = Item.offsetHeight;
		}
	return x;
	}
	
function getItemWidth(Item) {
	var x;
	var test1 = Item.scrollWidth;
	var test2 = Item.offsetWidth;
	if (test1 > test2) {
		// all but Explorer Mac
			x = Item.scrollWidth;
		}
	else {
		// Explorer Mac;
		// would also work in Explorer 6 Strict, Mozilla and Safari
			x = Item.offsetWidth;
		}
	return x;
	}
/* end windows dimension scripts */

/* start manipulation scripts */
// most of these are customized for the melkweg pages
// handle with care
function swapLyrs (lyr1, lyr2) {
	toggleVisibility(lyr1,'block');
	toggleVisibility(lyr2,'none');
	return true;
	}

function toggleVisibility(id, vis) {
	if (document.getElementById) {
			eval("document.getElementById(id).style.display = \"" + vis + "\"");
		}
	else {
		if (document.layers) {
				document.layers[id].display = vis;
			}
		else {
			if (document.all) {
					eval("document.all." + id + ".style.display = \"" + vis + "\"");
				}
			}
		}
	}

function getStyleProperty (id, styleProp) {
	var x = document.getElementById(id);
	if (window.getComputedStyle) {
			var y = window.getComputedStyle(x,null).getPropertyValue(styleProp);
		}
	else if (x.currentStyle) {
			var y = eval('x.currentStyle.' + styleProp + '.value');
		}
	return y;
	}

function setNodeProperty (node, styleProp, value) {
	if (document.getElementById) {
			//node.style.styleProp = value;
			node.styleProp = value;
		}
	else {
		if (document.layers) {
				node.styleProp = value;
			}
		else {
			if (document.all) {
					//eval(node + ".style."+styleProp+" = \"" + value + "\"");
					eval(node.styleProp+" = \"" + value + "\"");
				}
			}
		}
	}
	
function setStyleProperty (id, styleProp, value) {
	if (document.getElementById) {
			if (!eval("document.getElementById(id).style."+styleProp+" = \"" + value + "\"")) {
				document.getElementById(id).style.styleProp = value;
			}
		}
	else {
		if (document.layers) {
				document.layers[id].styleProp = value;
			}
		else {
			if (document.all) {
					eval("document.all." + id + ".style."+styleProp+" = \"" + value + "\"");
				}
			}
		}
	}

function toggleItem (id) {
	if (getStyleProperty(id, "display")=="block") {
			setStyleProperty(id, "display", "none");
		}
	else {
			setStyleProperty(id, "display", "block");
		}
	}

// specific editor functions
function showNotes(id) {
		setStyleProperty (id, "display", "block");
	}

function hideNotes(id) {
		setStyleProperty (id, "display", "none");
	}
	
// specific category functions
function showSubCat(id) {
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") != -1) {
			setStyleProperty (id, "display", "block");
		}
	else {
		// msie does not know "table-row" value
			setStyleProperty (id, "display", "table-row");
		}
	}

function hideSubCat(id) {
		setStyleProperty (id, "display", "none");
	}

function getItemStyleProperty (id, styleProp) {
	var rid = "r"+id;
	var x = document.getElementById(rid);
	
	if (window.getComputedStyle) {
			var y = window.getComputedStyle(x,null).getPropertyValue(styleProp);
		}
	else if (x.currentStyle) {
			var y = eval('x.style.' + styleProp);
		}
	else {
			if (x.style.length>0) { // this one's for KHTML .. and hopefully safari
				var y = x.style.getPropertyValue(styleProp);
			}
		}
	return y;
	}

function setItemStyleProperty (id, style, value) {
	var lid = "l"+id;
	var rid = "r"+id;
	if (document.getElementById) {
			eval("document.getElementById(lid).style."+style+" = \"" + value + "\"");
			eval("document.getElementById(rid).style."+style+" = \"" + value + "\"");
		}
	else {
		if (document.layers) {
				document.layers[lid].style = value;
				document.layers[rid].style = value;
			}
		else {
			if (document.all) {
					eval("document.all." + lid + ".style."+style+" = \"" + value + "\"");
					eval("document.all." + rid + ".style."+style+" = \"" + value + "\"");
				}
			}
		}
	}

function toggleItemExpander (id) {
	if (getItemStyleProperty(id, "display")=="block") {
			setItemStyleProperty(id, "display", "none");
			toggleImage(id+'ex', "images/i/arrz-wi.gif");
		}
	else {
			setItemStyleProperty(id, "display", "block");
			toggleImage(id+'ex', "images/i/arrn-wi.gif");
		}
	}

function toggleImage(id,value) {
	if (document.getElementById) {
			eval('document.getElementById(\''+id+'\').src = \''+value+'\'');
		} // other browsers wont even try
	}
/* end manipulation scripts */
	
/* start SACK */

// ====== Simple Ajax Code Kit =======
// code by Gregory Wild-Smith (c)2005
// http://twilightuniverse.com
// If you use this code please keep this credit intact.
// A link or email would be nice, but is not required.
// v1.01

function sack(file){
	this.AjaxFailedAlert = "Your browser doesn't support the extended functionality of this website, and therefore you have have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };
	
	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			// no XMLHttpRequest support, so no AJAX.
			this.failed = true; 
		}
	};

	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			urlVars[0] = encodeURIComponent(urlVars[0]);
			urlVars[1] = encodeURIComponent(urlVars[1]);
			varArray[i] = urlVars.join("=");
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	this.runAJAX = function(urlstring){
		if (urlstring){ this.URLString = urlstring; }
		if (this.element) { this.element = document.getElementById(this.element); }
		if (this.xmlhttp) {
			var self = this; // wierd fix for odd behavior where "this" wouldn't work in the readystate function.
			this.xmlhttp.open(this.method, this.requestFile ,true);
			if (this.method == "POST") { this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') }
			if (this.encodeURIString){ this.URLString = this.encodeURLString(this.URLString); }
			this.xmlhttp.send(this.URLString);
			this.xmlhttp.onreadystatechange = function() {
				switch (self.xmlhttp.readyState){
					case 1: // Loading.
						self.onLoading();
					break;
					case 2: // Loaded.
						self.onLoaded();
					break;
					case 3: // Interactive - is called every 4096 bytes.. pretty much just tells you it's downloading data.
						self.onInteractive();
					break;
					case 4: // Completed.
						self.response = self.xmlhttp.responseText
						self.xmlresponse = self.xmlhttp.responseXML
						self.onCompletion();
						if(self.execute){ self.runResponse(); }
						if (self.element) { 
							self.element.innerHTML = self.response;
						}
					break;
				}
			};
		}
	};
this.createAJAX();
// hidden for silent operation, we assume a fallback
//if(this.failed && this.AjaxFailedAlert){ alert(this.AjaxFailedAlert); }
}
/* end SACK */


/* start the clock */
function showClock(){
	if (!oldbrowser) {
		var clockobj=document.getElementById? document.getElementById(clockDivId) : eval("document.all."+clockDivId);
	}

	var Days = new Array('Zo','Ma','Di','Wo', 'Do','Vr','Za');
	
	var today = new Date();
	var Year = TakeYear(today);
	var Month = LeadingZero(today.getMonth()+1);
	var DayName = Days[today.getDay()];
	var Day = LeadingZero(today.getDate());
	var Hours = LeadingZero(today.getHours());
	var Minutes = LeadingZero(today.getMinutes());
	var Seconds = LeadingZero(today.getSeconds());
	var clockTime = Hours + ':' + Minutes + ':' + Seconds;
	var clockDate = '<p><span>'+DayName + ' ' + Day + '.' + Month + '.' + Year+'</span> ' + clockTime +'</p>';

	if (!oldbrowser){
		// this is very slow on mozilla, lets see if we can change that someday
		clockobj.innerHTML=clockDate;
		setTimeout("showClock()",1000)
	}
}

function TakeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function LeadingZero(nr) {
	if (nr < 10) nr = "0" + nr;
	return nr;
}
/* end the clock */


// really cool event window loader
// http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
// usage
//addWindowEvent(window, 'load', foo);
//addWindowEvent(window, 'load', bar);
function addWindowEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

// popup function
var popUp = null;

function pop(url)
{
	if (popUp && !popUp.closed)
		popUp.location.href = url;
	else
		popUp = window.open(url,'popUp','height=350,width=400,scrollbars=yes,resizable=yes,toolbar=no,location=no');
	popUp.focus();
	return false;
}

function printpop(url)
{
	if (popUp && !popUp.closed)
		popUp.location.href = url;
	else
		popUp = window.open(url,'popUp','height=550,width=500,scrollbars=yes,resizable=yes,toolbar=yes,location=yes');
	popUp.focus();
	return false;
}

// get popup windows
var x = document.getElementsByTagName('a');
for (var i=0;i<x.length;i++)
{
	if (x[i].getAttribute('type') == 'popup')
	{
		x[i].onclick = function () {
			return pop(this.href)
		}
		x[i].title += ' (Popup)';
	}
}

// load tha shit
if (!isGecko) {
	// gecko can handle the mouseover without suckerfish
	// the rest of the browsers was not harmed in the process
	addWindowEvent(window, 'load', sfHover);

	// the clock is flickering very annoying in gecko
	addWindowEvent(window, 'load', showClock);

}

// changes the visibility properties for double dates
addWindowEvent(window, 'load', multiDateExpand);

// absolutely neccesary for the switching of blocks
addWindowEvent(window, 'load', classExpander);

