function $(e)
{
	return document.getElementById(e);
}
function cE(name)
{
	return document.createElement(name);
}

function startcapture()
{
	if(document.addEventListener)
		document.addEventListener("mousemove", capturemouse, true);
	else
		document.onmousemove = capturemouse;
}

function initrequest()
{
	var req = false;
	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject('Microsoft.XMLHTTP');
		} catch(e) {}
	}
	if(!req) {
		alert('Ihr Browser unterstützt diese Funktion nicht!');
		return false;
	}
	return req;
}

var req1 = false;
function get7days(days, limit, date)
{
	req1 = initrequest();
	req1.onreadystatechange = function()
	{
		var e = $('sevendays');
		var cap = $('latestcaption');
		var ico = $('loadico1');
		if(req1.readyState == 1) {
			ico.style.display = 'inline';
			cap.innerHTML = 'lädt...';
		} else if (req1.readyState == 4) {
			e.innerHTML = req1.responseText;
			ico.style.display = 'none';
			addcharthandler(e);
		}
	}
	req1.open('GET', '?date=' + date + '&ajax=1&days=' + days + '&limit=' + limit, true);
	req1.send(null);
}

var req2 = false;
function checksong()
{
	var t = new Date();
	var y = t.getYear();
	if(y < 1000) y += 1900;
	var m = t.getMonth() + 1;
	var d = t.getDate();
	if(y + '-' + (m<10?'0'+m:m) + '-' + (d<10?'0'+d:d) != date) {
		location.reload();
		return;
	}
	req2 = initrequest();
	var ico = $('loadico2');
	req2.onreadystatechange = function()
	{
		if(req2.readyState == 1) {
			ico.style.display = 'inline';
		}
		else if(req2.readyState == 4) {
			var xml = req2.responseXML;
			var tracks = xml.getElementsByTagName('track');
			for(var i = 0; i < tracks.length; i++)
			{
				var xid = tracks[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				var xtime = tracks[i].getElementsByTagName('time')[0].firstChild.nodeValue;
				var xartist = tracks[i].getElementsByTagName('artist')[0].firstChild.nodeValue;
				var xtitle = tracks[i].getElementsByTagName('title')[0].firstChild.nodeValue;
				addsong(xid, xtime, xartist, xtitle);
				time = xtime;
			}
			ico.style.display = 'none';
		}
	}
	req2.open('GET', '?date=' + date + '&ajax=1&time=' + time);
	req2.send(null);
}
var rowc = 1;
function addsong(id, time, artist, title)
{
	var tbl = $('songs').getElementsByTagName('tbody')[0];
	var row = cE('tr');
	var trs = tbl.getElementsByTagName('tr');
	var obersteTr = trs[trs.length - 1];
	var h = obersteTr.getElementsByTagName('div')[0].offsetHeight + 2;
	
	row.className = 'l' + (rowc++ % 2);
	
	var td1 = cE('td');
	td1.className = 'time';

	var div1 = cE('div');
	div1.style.height = 0;
	div1.style.overflow = 'hidden';

	var span1 = cE('span');
	span1.className = 'q';
	span1.appendChild(document.createTextNode('»'));
	div1.appendChild(span1);
	
	div1.appendChild(document.createTextNode(time));
	td1.appendChild(div1);
	
	var span2 = cE('span');
	span2.className = 'q';
	span2.appendChild(document.createTextNode('«'));
	div1.appendChild(span2);
	
	var td2 = cE('td');
	var div2 = cE('div');
	div2.style.height = 0;
	div2.style.overflow = 'hidden';

	var div3 = cE('div');
	div3.className = 'plbuttons';
	
	var a3 = cE('a');
	a3.target = '_blank';
	a3.charset = 'UTF-8';
	a3.href = 'http://youtube.com/results?search_query=' + encodeURIComponent(artist) + '+' + encodeURIComponent(title);
	a3.title = 'Suche bei YouTube';
	var img3 = cE('img');
	img3.src = 'youtube.png';
	img3.alt = 'YouTube';
	a3.appendChild(img3);
	div3.appendChild(a3);

	var a = cE('a');
	a.target = '_blank';
	a.charset = 'UTF-8';
	a.href = 'http://www.lastfm.de/explore/search/?m=artists&q=' + artist;
	a.title = 'Suche bei lastfm.de';
	var img = cE('img');
	img.src = 'lastfm.png';
	img.alt = 'last.fm';
	a.appendChild(img);
	div3.appendChild(a);
	
	var a2 = cE('a');
	a2.target = '_blank';
	a2.href = 'http://laut.de/suche/wortlaut/suche_interpret.php?suchbegriff=' + artist;
	a2.title = 'Künstler-Suche bei laut.de';
	var img2 = cE('img');
	img2.src = 'laut.gif';
	img2.alt = 'laut.de';
	
	var img3 = cE('img');
	img3.src = 'chart.png';
	img3.alt = '';
	addpgEvent(img3, 'mouseover', id);
	addpgEvent(img3, 'mouseout', 0);

	a2.appendChild(img2);
	div3.appendChild(a);
	div3.appendChild(a2);
	div3.appendChild(img3);
	div2.appendChild(div3);
	div2.appendChild(document.createTextNode(artist + ' - ' + title));
	td2.appendChild(div2);

	row.appendChild(td1);
	row.appendChild(td2);
	td1.style.opacity = td1.style.MozOpacity = td1.style.KhtmlOpacity = 0;
	td2.style.opacity = td2.style.MozOpacity = td2.style.KhtmlOpacity = 0;
	td1.style.filter = 'alpha(opacity=0)';
	td2.style.filter = 'alpha(opacity=0)';
	
	tbl.insertBefore(row, tbl.childNodes[0]);
	slidedown(row, div1, div2, h);
}

function slidedown(row, div1, div2, h)
{
	if(div1.offsetHeight < h)
	{
		div1.style.height = div1.offsetHeight + 1 + 'px';
		div2.style.height = div2.offsetHeight + 1 + 'px';
		setTimeout(function(){ slidedown(row, div1, div2, h) }, 20);
	} else {
		fadein(row, 0);
	}
}

function fadein(row, o)
{
	if(o < 100)
	{
		o += 4;
		var td = row.getElementsByTagName('td');
		td[0].style.opacity = td[0].style.MozOpacity = td[0].style.KhtmlOpacity = o/100;
		td[1].style.opacity = td[1].style.MozOpacity = td[1].style.KhtmlOpacity = o/100;
		td[0].style.filter = 'alpha(opacity=' + o + ')';
		td[1].style.filter = 'alpha(opacity=' + o + ')';
		setTimeout(function(){ fadein(row, o) }, 20);
	}
}

function showpg(tid)
{
	var e = $('pgpopup');
	if(typeof tid == "undefined" || tid == 0) {
		e.style.display = 'none';
	} else {
		e.getElementsByTagName('img')[0].src = '?pg=' + tid;
		e.style.display = 'block';
	}
}

function capturemouse(e)
{
	{
		var ie5 = (document.getElementById && document.all); 
		var ns6 = (document.getElementById && !document.all); 
		var ua = navigator.userAgent.toLowerCase();
		var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);
		if(document.getElementById)
    {
			var iebody = (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
			pagex = (isapple == 1 ? 0 : (ie5) ? iebody.scrollLeft : window.pageXOffset);
			pagey = (isapple == 1 ? 0 : (ie5) ? iebody.scrollTop : window.pageYOffset);
			mousex = (ie5) ? window.event.clientX : (ns6) ? clientX = e.clientX : false;
			mousey = (ie5) ? window.event.clientY : (ns6) ? clientY = e.clientY : false;
			
			var e = $('pgpopup');
			//e.style.left = (mousex + pagex - 190) + 'px';
			var w = (typeof window.innerWidth != 'undefined') ? window.innerWidth : document.body.offsetWidth;
			e.style.right = (w - mousex - pagex - 10) + 'px';
			e.style.top = (mousey + pagey - 120) + 'px';
    }
	}
}

function addcharthandler(el)
{
	var e = new Array();
	if(typeof el == 'undefined') {
		e = document.getElementsByName('pgicon');
	} else {
		var e1 = el.getElementsByTagName('img');
		for(var i = 0; i < e1.length; i++) {
			if(e1[i].name == 'pgicon')
				e[e.length] = e1[i];
		}
	}
	for(var i = 0; i < e.length; i++)
	{
		var id = e[i].id.split('_');
		addpgEvent(e[i], 'mouseover', id[1]);
		addpgEvent(e[i], 'mouseout', 0);
	}
}

function addpgEvent(obj, eventType, id)
{
	var fn = function() { showpg(id) };
	if (obj.addEventListener) {
		obj.addEventListener(eventType, fn, false);
	} else {
		obj['on' + eventType] = fn;
	}
}

function hidebadbrowser()
{
	$('badbrowser').style.display = 'none';
	var a = new Date();
	a = new Date(a.getTime() + 1000 * 60 * 60 * 24 * 30);
	document.cookie = 'hidebadbrowser=true; expires=' + a.toGMTString() + ';';
}

function log(text)
{
	$('debug').innerHTML += text + '<br>';
}

function toggleExSearch()
{
	var e = $('search');
	var e2 = $('exsearch');
	var ico = $('exsearchupdown');
	if(e.style.display == 'none') {
		e.style.display = '';
		e2.style.display = 'none';
		ico.src = 'down.png';
	} else {
		e.style.display = 'none';
		e2.style.display = '';
		ico.src = 'up.png';
	}
}

function tabswitch(menuid, selnum)
{
	var tabmenu = $(menuid);
	var tab = tabmenu.firstChild;
	var num = 0;
	var remnum = 0;
	while(tab != null)
	{
		if(tab.nodeType != 1)
		{
			tab = tab.nextSibling;
			continue;
		}
		num++;
		if(selnum == num)
		{
			tab.className = 'tabselected';
		}
		else if(tab.className == 'tabselected')
		{
			tab.className = '';
			remnum = num;
		}
		tab = tab.nextSibling;
	}
	$('tab' + remnum).style.display = 'none';
	$('tab' + selnum).style.display = '';
}

function togglemoreplaylists(src) {
	var e = $('moreplaylists');
	e.style.display = e.style.display == 'none' ? '' : 'none';
}