var ajax_url = 'ajax.php';
var intervall_time = 30;
var d = document;
var w = window;
var interval = new Array();
var boxes = new Array('cdu', 'spd', 'fdp', 'gruen', 'pirat', 'linke');

function wahl_load(what, count, id)
{
	var params = 'action=box&what=' + what + '&num=' + count;
	if(what != '') {
		wahl_more(what, count);
		
		$(what + '_reload').onclick = function() { wahl_update(what); };
		$(what + '_reload').innerHTML = 'Aktualisieren';

		$(what + '_feed').href = what+'.rss';
		$(what + '_feed').innerHTML = 'RSS-Feed';
		
		new Ajax.Request(ajax_url, {
			method: 'get',
			parameters: params,
			onSuccess: function(transport) {
				$(id + '_content').innerHTML = transport.responseText;
				if(count == 3 || id == what+'_more')
				{
					new Effect.Appear(id + '_content', { duration: 2.0, afterFinish: function() { $(what + '_progress').hide();  } });
				}
			},
			onFailure: function() {
				alert("Fehler! Probier es einfach nochmal!");
			}
		});
	}
	return false;
}

function wahl_update(what)
{
	if(what != '') {
		$(what + '_progress').show();

		first_element_name = $(what+'_list').firstChild.getAttribute('id');
		params = 'action=update&what=' + what + '&start='+first_element_name;

		new Ajax.Request(ajax_url, {
			method: 'get',
			parameters: params,
			onSuccess: function(transport) {
				response = transport.responseText.split("<t>");
				if(response[0] != '' && response[0] != first_element_name)
				{
					first = $(what+'_1');
					second = $(what+'_2');

					last_element_name = $(what+'_list').lastChild.getAttribute('id');
					// new Effect.Fade(last_element_name, { duration: 2.0, afterFinish: function() { $(last_element_name).remove(); } });
					$(last_element_name).remove();

					first.setAttribute('id', what+'_2');
					first.className = 'second';

					second.setAttribute('id', what+'_3');
					second.className = 'third';

					new_element = d.createElement('li');
					new_element.style.display = 'none';
					new_element.setAttribute('id', response[0]);
					new_element.innerHTML = response[1];
					$(what+'_list').insertBefore(new_element, $(what+'_list').firstChild);
					new Effect.Appear(response[0], { duration: 3.0, afterFinish: function() { $(what + '_progress').hide(); } });
					// new Effect.BlindDown(response[0], { duration: 2.0, afterFinish: function() { $(what + '_progress').hide(); } });
					// new Effect.SlideDown(response[0], { duration: 2.0 });
				} else {
					$(what + '_progress').hide();
				}
			},
			onFailure: function() {
				alert("Fehler! Probier es einfach nochmal!");
			}
		});
	}

	return false;
}

function wahl_more(what, count)
{
	window.scrollTo(0, 0);
	
	switch(count)
	{
		default:
		case 3:
		$(what + '_more').onclick = function() { wahl_load(what, 20, what + '_more'); };
		$(what + '_more').innerHTML = 'mehr...';
		$('overlay').hide();
		break;

		case 20:
		$(what + '_more').onclick = function() { wahl_load(what, 3, what + '_more'); };
		$(what + '_more').innerHTML = 'Weniger';
		$('overlay').show();
		break;
	}
}

function wahl_start(what)
{
	interval[what] = w.setInterval("wahl_update('"+what+"')", intervall_time*1000);
	$(what+'_stop').innerHTML = 'Stop!';
	$(what+'_stop').className = 'icon icon_stop';
	$(what+'_stop').onclick = function() { wahl_stop(what); };
}

function wahl_stop(what)
{
	interval[what] = w.clearInterval(interval[what]);
	$(what+'_stop').innerHTML = 'Start';
	$(what+'_stop').className = 'icon icon_play';
	$(what+'_stop').onclick = function() { wahl_start(what); };
}

function wahl_close(what)
{
	$(what + '_more_content').hide();
	wahl_more(what, 3);
}

function wahl()
{
	for(var i = 0; i < boxes.length; i++)
	{
		wahl_load(boxes[i], 3, boxes[i]);
		wahl_start(boxes[i]);
	}
}