/* can't touch this! */
var working = false;
var i = null;
var at_work = false;
var iterations = 0;
var start = null;
var finish = null;
var divCount = 0;
var at_work = true;

function toggle_working(){
	if(!working){
		$("#toggle_work").attr("value", "pause work");
		i = window.setInterval('checkTime()', interval_length);
		working = true;
		start = new Date();
	}else{
		$("#toggle_work").attr("value", "start work");
		window.clearInterval(i);
		working = false;		
	}
}

function reset(){
	window.clearInterval(i);
	iterations = 0;
	working = false;
	at_work = false;
	start = null;
	finish = null;
	$("#toggle_work").attr("value", "start work");
	$("#timer:first-child").text("Working: 0");
}

function checkTime(){
	if(at_work){
		if(iterations == worktime-1){
			alert('Pause!');
			iterations = 0;
			at_work = false;
			finish = new Date();
			addDiv("work");
			start = new Date();
			finish = null;
		}else{
			iterations++;
			$("#timer:first-child").text("Working: "+iterations);
		}
	}else{
		if(iterations == breaktime-1){
			alert('Back to work!');
			iterations = 0;
			at_work = true;
			finish = new Date();
			addDiv("pause");
			start = new Date();
			finish = null;
		}else{
			iterations++;
			$("#timer:first-child").text("Dicking around:"+iterations);
		}
	}
}

function addDiv(type){
	divCount++;
	$("#protocol").prepend("<div id=\"div_"+divCount+"\" class=\""+type+"Div\" style=\"display: none;\">"+((start.getHours() < 10)?"0"+start.getHours():start.getHours())+":"+((start.getMinutes() < 10)?"0"+start.getMinutes():start.getMinutes())+" &mdash; "+((finish.getHours() < 10)?"0"+finish.getHours():finish.getHours())+":"+((finish.getMinutes() < 10)?"0"+finish.getMinutes():finish.getMinutes())+"</div>")
	$("#div_"+divCount).fadeIn("slow");
}