document.observe("dom:loaded", init);

var m_lyrics;
var m_count;
var m_printInt;

var m_cueCard;
var m_introCount;
var m_introInt;

var m_endTime;
var m_timerInt;

var m_titles;

function init() {
	new Ajax.Request("titles.txt", {
		method: "get",
		onSuccess: getTitles,
		onFailure: ajaxFailure,
		onException: ajaxFailure
	})
}

function getTitles(ajax) {
	m_titles = ajax.responseText.split("\n");
	reset();
}

function getRes(ajax) {
	m_lyrics = ajax.responseText.split("\n");
	m_printInt = setInterval("printLyric()", 1000);
}

function printLyric() {
	size = m_lyrics.length;
	r = Math.floor(Math.random() * size);
	$("lyrics").innerHTML += m_lyrics[r] + "<br />";
	
	m_count++;
	
	if(m_count % 4 == 0) {
		$("lyrics").innerHTML += "<br />";
	}
	
	if(m_count >= max) {
		clearInterval(m_printInt);
		setTimeout("reset()", 5000);
		setTimeout("countDown(4750)", 250);
	}
}

function reset() {

	var bg = ["fotabip.jpg", "tonyC.jpg", "marquee.jpg", "FromDaTroof.JPG"];
	bgIndex = (Math.floor(Math.random() * bg.length));
	
	//$("main").addStyle({"background-url: img/TonyC.jpg"});

	$("hehBody").setStyle({
		backgroundImage: 'url(img/' + bg[bgIndex] + ')'
	});
	
	m_lyrics = "";
	m_cueCard = "";
	
	m_count = 0;
	m_introCount = 0;
	max = (Math.floor(Math.random() * 3) * 4) + 8;
	$("lyrics").innerHTML = "";
	
	
	$("info").innerHTML = max;
	
	intro();
}

function ajaxFailure(ajax, exception) {
	$("error").innerHTML = "There was an error with the Ajax request.<br />";
}

function getIntro(ajax) {
	m_cueCard = ajax.responseText.split("\n");
	m_introCount = 0;
	m_introInt = setInterval("disTonyC()", 1500);
}

function disTonyC() {
	size = m_cueCard.length;
	r = Math.floor(Math.random() * size);
	$("lyrics").innerHTML += m_cueCard[r] + "<br />";
	
	m_introCount++;
	
	if(m_introCount >= 3) {
		clearInterval(m_introInt);
		setTimeout("startTheShow()", 3000);
		setTimeout("countDown(2750)", 250);
	}
	
}

function getTitle() {
	size = m_titles.length;
	words = Math.floor(Math.random() * 3 + 1);
	
	var title = "";
	
	for(var i=0; i < words; i++) {
		r = Math.floor(Math.random() * size);
		title += m_titles[r] + " ";
	}
	
	return title;
	
}

function startTheShow() {

	$("lyrics").innerHTML = "<span class=\"title\">" + getTitle() + "</span><hr />";

	new Ajax.Request("lyrics.txt", {
		method: "get",
		onSuccess: getRes,
		onFailure: ajaxFailure,
		onException: ajaxFailure
	})

}

function countDown(time) {

	$("timer").innerHTML = time;

	var d = new Date();
	m_endTime = d.getTime() + time;
	m_timerInt = setInterval("animTime()", 100);
}

function animTime() {
	var d = new Date();
	curTime = d.getTime();
	
	diff = m_endTime - curTime;
	
	if(diff >= 0) {
		$("timer").innerHTML = diff;
	} else {
		clearInterval(m_timerInt);
		$("timer").innerHTML = "";
	}
}

function intro() {
	m_introCount = 0;
	$("lyrics").innerHTML = "<span class=\"title\">Intro</span><hr />";
	
	new Ajax.Request("disTonyC.txt", {
		method: "get",
		onSuccess: getIntro,
		onFailure: ajaxFailure,
		onException: ajaxFailure
	})

}