var timeLeft;

$(function() {
	$('.external').attr("target", "_new");
	$('.external').click(function() {
		pageView('/external/' + $(this).attr('href'));
	});

	// this is for buttons
	$('.user-action-link').hover(function() {
		$(this).addClass('ui-state-hover');
	}, function() {
		$(this).removeClass('ui-state-hover');
	});

	$("#calendar").datepicker({
		dateFormat : 'yymmdd',
		defaultDate : $('#calendar-default-date').val(),
		maxDate : $('#calendar-max-date').val(),
		minDate : $('#calendar-min-date').val(),
		onSelect : function(dateText, inst) {
			window.location = '/index/scores/s_code/' + dateText;
		}
	});

	timeLeft = $('#score-time-left').val();
	if (timeLeft > 0) {
		setTimeout('updateClock()', 1000);
	}
	
	$('input.check_and_save').click(function() {
		var value = $(this).attr('checked');
		var name  = $(this).attr('id');

		var data  = { "settings" : [ { "value" : value,  "name" : name} ]};
		$('#mini_messages').html('<img height="32" width="32" src="/images/spinner.gif"/>');
		$.ajax({
			  type: 'POST',
			  url: '/ajax/twitter-settings',
			  data: data,
			  success: function(data) { 
				$('#mini_messages').html(data.message);
			}
			});
	});
});

function updateClock() {
	if (timeLeft > 0) {
		timeLeft = timeLeft - 1;
		if (timeLeft == 1) {
			var end = ' second';
		} else {
			var end = ' seconds';
		}
		$('#score-time-left-display').text(timeLeft + end);
		setTimeout('updateClock()', 1000);
	} else {
		// expired, redirect.
		window.location = '/index/answer/choice/time-out/q_sig/' + $('#question-qsig').val();
	}
}

function pageView(url) {
	// add google analytics code
}