jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox() 
})

function showProjectForm(show){
	show = typeof(show) != 'undefined' ? show : true;
	if(show){
		$('#projectForm').show();
		$('#projectName').focus();
	}else{
		$('#projectForm').hide();
	}
}
function showTaskForm(show){
	show = typeof(show) != 'undefined' ? show : true;
	if(show){
		$('#taskForm').show();
		$('#taskName').focus();
	}else{
		$('#taskForm').hide();
	}
}


function popup() {
	window.open('http://mittens.mgpepe.com/home/home.php','mittens','width=550,height=400,toolbar=no,resizable=yes,status=no,menubar=no,copyhistory=no,directories=no,location=no,scrollbars=1');
}

function setstyle() {
	if(window.name == 'mittens') {
		document.getElementById('body').className = 'popup';
	}
}

function timeCountProjects(projects){
		for(var i in projects){
			time = projects[i]
			var h= Math.floor(time/3600)
			var m= Math.floor((time%3600)/60)
			var s= Math.floor(time%60)
			div = "project"+i
			document.getElementById(div).innerHTML=h+":"+m+":"+s
			projects[i]++
		}
			t=setTimeout('timeCountProjects(projects)',1000)
}

function timeCountClients(clients){
		for(var i in clients){
			time = clients[i]
			var h= Math.floor(time/3600)
			var m= Math.floor((time%3600)/60)
			var s= Math.floor(time%60)
			div = "client"+i
			document.getElementById(div).innerHTML=h+":"+m+":"+s
			clients[i]++
		}
			t=setTimeout('timeCountClients(clients)',1000)
}
function timeCount(){
	var o = $(".task");
	o.each(function(){
		var time =($(this).attr('currtime'));
		if($(this).hasClass('active'))
			time++;	
		$(this).attr('currtime',time);
		var h= Math.floor(time/3600);
		var m= Math.floor((time%3600)/60);
			if(m<10) m="0"+m;
		var s= Math.floor(time%60);
			if(s<10) s="0"+s;

		$(this).text(h+":"+m+":"+s);
	});
	timerId = setTimeout('timeCount()', 1000)
}

function startTask(task_id){
	var o = $('#click_'+task_id);
	$.post('/starttask', {tid: task_id}, function(data){
		//alert("Data Loaded: " + data);
		$('#time_'+task_id).addClass('active');
		$('#start_'+task_id).hide();
		$('#stop_'+task_id).show();
	});

}	

function stopTask(task_id){
	var o = $('#click_'+task_id);
	$.post('/stoptask', {tid: task_id}, function(data){
		//alert("Data Loaded: " + data);
		$('#time_'+task_id).attr('currtime',data);
		$('#time_'+task_id).removeClass('active');
		$('#start_'+task_id).show();
		$('#stop_'+task_id).hide();
		$('#progress_'+task_id).hide();
	});

}	


function complete(task_id, completedStatus){
	completedStatus = Math.abs(completedStatus-1); // if it's 0, make it 1, if it's 1, make it 0
	var o = $(this);
	$.post('/completetask', {tid: task_id, complete: completedStatus}, function(data){
		//alert("Data Loaded: " + data);
		
		if(completedStatus==1){
			$('#time_'+task_id).removeClass('active');
			$('#start_'+task_id).hide();
			$('#stop_'+task_id).hide();
			$('#name_'+task_id).addClass('completed');
			$('#completed_'+task_id).show();
			$('#uncompleted_'+task_id).hide();
			$('#progress_'+task_id).hide();
		}else{
			$('#start_'+task_id).show();
			$('#stop_'+task_id).hide();
			$('#name_'+task_id).removeClass('completed');
			$('#completed_'+task_id).hide();
			$('#uncompleted_'+task_id).show();
		}
	});
}

function editTimepunch(timepunch_id){
	var h = parseInt($('#hours_'+timepunch_id).val());
	var m = parseInt($('#minutes_'+timepunch_id).val());
	var s = parseInt($('#seconds_'+timepunch_id).val());
	var length = 0;
	length = (s)+(60*m)+(3600*h);
	$.post('/editTimepunch', {tp_id: timepunch_id, len: length}, function(data){
		//alert("Data Loaded: " + data);
	});
}
      

       
       
       
       