isIE = false;
if(document.all && navigator.appVersion.indexOf('MSIE') > -1) {
	isIE = true;
}

opacity_level = 0.85;

aLyrics = new Array();
sCurrIdx = null;

function findPos(obj) {
	var curleft = 0, curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function showLyrics(txt) {
	var lc = document.getElementById('lyric-container');
	lc.style.top = (yPos + 86) + "px";
	var lct = document.getElementById('lyric-container-text');
	lct.innerHTML = txt;
	
	if(isIE) {
		lc.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + (opacity_level * 100) + ')';
	}
	else {
		lc.style.opacity = opacity_level;
	}
	lc.style.display = 'block';
	createNewXMLHTTP();
}

yPos = 0;
xmlhttp = null;
function createNewXMLHTTP() {
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
	} else {
		xmlhttp = null;
	}
	
	if(xmlhttp != null) {
		xmlhttp.onreadystatechange = function () {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				// so far so good
				if(xmlhttp.responseXML != null) {
					var lyr = xmlhttp.responseXML.firstChild;
					if(lyr.firstChild && lyr.firstChild.data) {
						aLyrics[sCurrIdx] = lyr.firstChild.data;
						sCurrIdx = null;
						showLyrics(lyr.firstChild.data);
					}
				}
			}
		}
	}
}
createNewXMLHTTP();

function handleLyricClick(srcElement) {
	var aPos = findPos(srcElement);
	yPos = Math.max(0, aPos[1] - 100);
	var lyr_idx = srcElement.innerHTML;
	lyr_idx = lyr_idx.toLowerCase().replace(/ /g, '_').replace(/\./g, '');
	
	if(typeof(aLyrics[lyr_idx]) != 'undefined') {
		showLyrics(aLyrics[lyr_idx]);
		return;
	}
	if (xmlhttp != null) {
		sCurrIdx = lyr_idx;
		xmlhttp.open('GET', 'lyrics.php?' + lyr_idx, true);
		xmlhttp.send('');
	}
}

function closeLyrics() {
	document.getElementById('lyric-container').style.display = 'none';
}
