addEvent (window, 'load', initArchive, false);
			
var W3CDOM = (document.getElementById && document.createElement);


// initialization method

function initArchive ()
{
	// if browser does not support the DOM, bail out
	if (!W3CDOM)
	{
		return;
	}
	
	var monthLinks = getElementsByClass ('monthLink');
	
	for (var i = 0; i < monthLinks.length; i++)
	{
		// assign DOM 0 event handlers ( because Safari can't stop the default action of the links )
		monthLinks[i].onclick = function ()
		{
			var month = this.href.match (/nieuws\/archief\/([0-9]+)/);
			performQuery (month[1]);
			return false;
		}
	}
}

// get results from the PHP script

function performQuery (id)
{
	// if browser does not support AJAX, bail out
	if (!zXmlHttp.isSupported())
	{
		return;
	}
	
	var x = zXmlHttp.createRequest();
	
	x.open ('get', '../../inc/php/getArticles.ajax.php?id=' + encodeURIComponent (id), true);
	x.onreadystatechange = function ()
	{
		if (x.readyState == 4)
		{
			if (x.status == 200)
			{
				// if all is well, build HTML list
				buildHTML (x.responseText, id);
			}
			else
			{
				// if all is not well, remove event handlers
				var monthLinks = getElementsByClass ('monthLink');
	
				for (var i = 0; i < monthLinks.length; i++)
				{
					delete monthLinks[i].onclick;
				}
			}
		}
	}
	
	x.send (null);
}

// build list of articles

function buildHTML (str, month)
{
	
	var c = 'alt';
	var div = document.getElementById('recentNews');
	
	var h2 = div.getElementsByTagName ('h2')[0];
	h2.removeChild (h2.firstChild);
	h2.appendChild (getMonthName (month));
	
	var ul = div.getElementsByTagName ('ul')[0];
	ul.innerHTML = '';
	
	ul.style.display = 'none';
	
	// if an error occurred, display an error
	if (/error/gi.test (str) || str == 'Er zijn in deze maand geen artikelen gepubliceerd.')
	{
		var li = document.createElement ('li');
		var span = document.createElement ('span');
		span.className = 'error';
		
		span.appendChild (document.createTextNode(str));
		
		li.appendChild (span);
		ul.appendChild (li);
	}
	else
	{
		// build article list
		var arr = handleResponse (str);
		
		for (var i = 0; i < arr.length; i++)
		{
			c = (c == '') ? 'alt' : '';
			var props = arr[i];
			
			var a = document.createElement ('a');
			a.href = 'http://www.eastsidemusic.nl/nieuws/' + month + '/' + encodeURIComponent(props[0]);
			a.title = 'Lees dit nieuwsartikel';
			
			a.appendChild (document.createTextNode (props[1]));
			
			var li = document.createElement ('li');
			li.className = c;
			
			li.appendChild (a);
			
			ul.appendChild (li);
		}
	}
	// start animation
	animateList ((ul.getElementsByTagName('li').length*17)+50,div);
}

// method for starting the animation

function animateList (height, el)
{
	// attach style sheet for eliminating the min-height declaration of the recentNews div
	attachStyleSheet ();
	var interval = setInterval (function()
										 {
											 stretch (height, el, interval);
										 }, 30);
}

// method for scaling the div 

function stretch (height, el, interval)
{
	
	var a = parseInt(style(el,'height'));
	if (a < height)
	{
		a += Math.ceil((height - a)/10);
		el.style.height = a + 'px';
	}
	else if (a > height)
	{
		a -= Math.ceil((height + a)/10);
		el.style.height = a + 'px';
	}
	else
	{
		clearInterval (interval);
		el.getElementsByTagName('ul')[0].style.display = '';
	}
}

// create array from string

function handleResponse (str)
{
	var outerArray = str.split(',');
		
	for (var i = 0; i < outerArray.length; i++)
	{
		var innerArray = outerArray[i].split('||');
		outerArray[i] = innerArray;
	}
	
	return outerArray;
	
}

// attach stylesheet 

function attachStyleSheet ()
{
	var l = document.createElement('link');
	l.setAttribute('rel','stylesheet');
	l.setAttribute('href','http://www.eastsidemusic.nl/inc/css/noMinHeight.css');
	l.setAttribute('type','text/css');
	l.setAttribute('id','noMinHeight');
	
	var head = document.getElementsByTagName('head')[0];
	head.appendChild (l);
}

// fetch the month's name

function getMonthName (id)
{
	var months = new Array ('Januari','Februari','Maart','April','Mei','Juni',
							'Juli','Augustus','September','Oktober','November','December');
	
	return document.createTextNode(months[id-1]);
}
