// place the ids of every element...
var ids = new Array('pencil', 'timer', 'stop', 'oops', 'lock', 'thread');

// set timer init value...
var secondsLeft = 60;

// set fadeout value...
var TimeToFade = 500.0;

function switchid(id){	
	hideallids();
	showdiv(id);
}

function hideallids(page){
	// loop through the array and hide each element by id...
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}

function hidediv(id) {
	// safe function to hide an element with a specified id...
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	// safe function to show an element with a specified id...
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}


function checkPencil(userID) {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xhr=new XMLHttpRequest();
		}
	catch (e) {
		// Internet Explorer
		try {
			xhr=new ActiveXObject("Msxml2.XMLHTTP");
			}
	catch (e) {
		try {
			xhr=new ActiveXObject("Microsoft.XMLHTTP");
			}
	catch (e)
		{
			alert("Your browser does not support AJAX!");
			return false;
		}
		}
	}
	
	
	
	xhr.open("GET","pencil.php?uid=" + userID,true);
	
	xhr.send(null);
	
	xhr.onreadystatechange=function() {

		if (xhr.readyState==4) {
			status = parseInt(xhr.responseText);
			// ---- do stuff! ----
			if (status == 0) {
				// ---- user can input! ----
				switchid("thread");
				document.getElementById('text').focus();
				secondsLeft = 60;
				postTime();
			} else {
				// ---- user is blocked ----
				switchid("oops");
			}

		}

	}

}

function characterCount() {
	textObj = document.getElementById("text");
	textLength = textObj.value.length;
	charactersLeft = 140 - textLength;
	displayCharacters = charactersLeft + '';
	document.getElementById('counter').innerHTML = displayCharacters;
}

function doTime() {
	totalSecs = secondsLeft;
	displaySecs = totalSecs + '';
	if (displaySecs < 10) displaySecs = '0' + displaySecs;
	document.getElementById('clock').innerHTML = displaySecs;
	secondsLeft = secondsLeft - 1;
	if (secondsLeft < 0) self.location.href = self.location.protocol + '//' + self.location.host + self.location.pathname + "?action=timeout";
}

function postTime() {
	doTime();
	setTimeout("postTime()", 1000);
}

function fadediv(id) {
	var element = document.getElementById(id);
	if (element == null) return;
  
	if (element.FadeState == null) {
		if(element.style.opacity == null || element.style.opacity == '' || element.style.opacity == '1') {
			element.FadeState = 2;
		} else {
			element.FadeState = -2;
		}
	}

	if (element.FadeState == 1 || element.FadeState == -1) {
		element.FadeState = element.FadeState == 1 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
	} else {
		element.FadeState = element.FadeState == 2 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade;
		setTimeout("animateFade(" + new Date().getTime() + ",'" + id + "')", 33);
	}
}

function animateFade(lastTick, eid) { 
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	var element = document.getElementById(eid);

	if (element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		hidediv(eid);
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;
	
	if (element.FadeState == 1) newOpVal = 1 - newOpVal;
	
	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}