var showTimer = false;

var timerStarted = false;
var timeIndex = false;
var maxTimeInMilliseconds = (1000 * 60) * 30;	// 30 mins
var timerObject = false;
var pauseTimer = false;
var finalTime = false;



function checkTimer()
{
	var timerHolder = contentFrame.document.getElementById("timerHolderID")
	if ((timerHolder) && (parent.timerStarted))
	{
		//timerHolder.style.display = "block";
		contentFrame.document.getElementById("timeID").innerHTML = getTimePassed()

		if (contentFrame.document.getElementById("timeID").innerHTML == convertToHumanTime(maxTimeInMilliseconds))
		{
			//alert("Starting blink timer, document.getElementById('timeID').innerHTML: " + document.getElementById("timeID").innerHTML)
			window.clearInterval(timerObject)
			//contentFrame.document.getElementById("timerHolderID").style.visibility = "visible";
			timerObject = window.setInterval("toggleTimer()", 800);
			return;
		}
		//contentFrame.document.getElementById("timerHolderID").style.visibility = "visible";
		if ((!timerObject) && (!pauseTimer))
			timerObject = window.setInterval("checkTimer()", 100);
	}
}


function pauseTimerFunc()
{
	finalTime = getTimePassed()
	pauseTimer = true;
}

var flashState = true;
function toggleTimer()
{
	if (flashState)
	{
		//alert("Red")
		if (contentFrame.document.getElementById("timeID"))
			contentFrame.document.getElementById("timeID").style.color = "cc0033"
		flashState = false
	}
	else
	{
		//alert("White")
		if (contentFrame.document.getElementById("timeID"))
			contentFrame.document.getElementById("timeID").style.color = "ffffff"
		flashState = true
	}
}


function getTimePassed()
{
	if (!pauseTimer)
	{
		var dateObj = new Date()
		var currTime = dateObj.getTime();
		var millisecondsSinceStart = currTime - timeIndex;
		if (millisecondsSinceStart > maxTimeInMilliseconds)
		{
			millisecondsSinceStart = maxTimeInMilliseconds;
		}
		var humanTimeExpired = convertToHumanTime(millisecondsSinceStart)
		//alert(humanTimeExpired)

		return humanTimeExpired;
	}
	else
	{
		return finalTime
	}
}

function convertToHumanTime(millisecondsSinceStart)
{
	var minutes = Math.floor((millisecondsSinceStart / 1000) / 60)
	millisecondsSinceStart = millisecondsSinceStart - ((minutes * 60) * 1000)
	if (minutes.toString().length < 2)
		minutes = "0" + minutes
	var seconds = Math.floor((millisecondsSinceStart / 1000))
	if (seconds.toString().length < 2)
		seconds = "0" + seconds

	return "00:" + minutes + ":" + seconds
}
