var start_time;
var loop;

jQuery(document).ready(function(){
	jQuery(document).bind('keypress', { 'combi':'Space', 'disableInInput':true } , function(evt) {
		return false;
	});

	jQuery(document).bind('keyup', { 'combi':'Space', 'disableInInput':true } , function(evt) {
		if (start_time)
			unstart();
		else
			startCounting();
		return false;
	});

	jQuery(document).bind('keyup', { 'combi':'ESC', 'disableInInput':true } , function(evt) {
		clear();
		return false;			
	});
	
	setUpMessageText();
});

function setUpMessageText(){
	jQuery('#message_text').bind('keyup', function(){
		charsLeft = 99 - (jQuery('#message_text').val().length); 
		jQuery('#chars_left').text(charsLeft);
		updateTweetButtonText(jQuery('#message_text').val());
	});
}

// Testing
function startCounting(startTime) {
	start_time = typeof(startTime) == 'undefined' ? new Date() : startTime;
	
	jQuery('#start_button').hide();
	jQuery('#stop_button').show();

	loop = window.setInterval("update()", 1);
	
	cleanScreen(true);
}

// Unavaiable to be tested
function update() {
	printTime(format_seconds(getTime()));
}

// Tested
function printTime(time){
	jQuery('#difference').text(time);
}

// Tested
function getTime(){
	return (new Date() - start_time);
}

// Tested
function format_seconds(seconds) {
	if(isNaN(seconds))
		seconds = 0;

	var diff = new Date(seconds);
	var minutes = diff.getMinutes();
	var seconds = diff.getSeconds();
	var milliseconds = diff.getMilliseconds();

	if (minutes < 10)
		minutes = "0" + minutes;
	if (seconds < 10)
		seconds = "0" + seconds;

	if (milliseconds < 10)
		milliseconds = "00" + milliseconds;
	else if (milliseconds < 100)
		milliseconds = "0" + milliseconds;

	document.title = minutes + ":" + seconds + ":" + milliseconds + " - Cronômetro Online";
	return minutes + ":" + seconds + ":" + milliseconds;
}

// Tested
function unstart() {
	clearInterval(loop);

    start_time = 0;
	
	jQuery('#start_button').show();
	jQuery('#stop_button').hide();
	
	cleanScreen(false);
	fillText();
}

// Tested
function clear() {
	unstart();
	jQuery('#difference').text(format_seconds(0));
}

// Tested
function updateTweetButtonText(message) {
  var tweetButton = jQuery('#tweet_button');
  tweetButton.attr('href', tweetButton.attr('href').replace(/&text=[^&]+/, "&text=" + encodeURIComponent(message)));
}

// Tested
function cleanScreen(clean){
	if(clean){
		jQuery('.primario').addClass('clean');
		jQuery('.primario h1, .primario .flag').fadeOut(400, function(){
			jQuery('#difference').css('margin-top', '73px');
		});
		jQuery('.secundario').fadeOut();
		jQuery('#twitter').fadeOut();
	}else{
		jQuery('.primario').removeClass('clean');
		jQuery('.primario h1, .primario .flag').fadeIn();
		jQuery('#difference').css('margin-top', '0px');
		jQuery('.secundario').fadeIn();
		jQuery('#twitter').fadeIn();
	}
}

//Testing
function fillText(){
	difference = jQuery('#difference').text();
	jQuery('#message_text').val('Eu gastei '+difference+' para ');
	jQuery('#message_text').focus();
	jQuery('#message_text').keyup();
}

function twitterModal(element){
	largura = 500;
	altura = 400;
	var esquerda = ((screen.width - largura) / 2);
	var topo = ((screen.height - altura) / 2);
	window.open(element.href,element.name,"channelmode=0,directories=0,fullscreen=no,location=0,menubar=0,resizable=0,scrollbars=yes,status=0,titlebar=0,toolbar=0,top="+topo+"px,left="+esquerda+"px,width="+largura+"px,height="+altura+"px");
	return false;
}
