function redirect(href) {
	window.location = href;	
}

function count_down(number) {
	if(number > 0) {
		$('#working').html(number);
		window.setTimeout(count_down, 1100, number-1);
	} else {
		redirect(app_dir+'/results/');
	}
}

function check_tos() {
	if($('#tos_agree').is(':checked')) {
		$('#tos_error').hide();	
		return true;
	} else {
		$('#tos_error').show();			
		return false;
	}
}	

function checkname() {
	var name = $('#full_name').val();
	if(name == '' || name.length < 6 || name.indexOf(' ') < 0) {
		$('#name_error').show();	
		return false;	
	} else {
		$('#name_error').hide();
		return true;
	}
}

function callback(post_id, exception) { 
	if(post_id) { 
		
	} 
}

function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash == 'full') {		
		$('#top_panel').hide();
		$('#not_impressed').hide();
		$('.toggle').hide();
		$('#full_list').show();
	} else {
		$('#top_panel').show();
		$('#not_impressed').show();
		$('.toggle').show();
		$('#full_list').hide();
	}
}




/**
 * document ready function
 */
$(document).ready(function() {
	
	if($('#working').length > 0) {
		count_down(10);
	}
	
	$('.hint').hint();
	
	$('#start').submit(function() {
		var t = check_tos();
		var n = checkname();
		return t && n;
	});
	
	
	$('.toggle, .reveal').click(function(e) {
		if($(this).hasClass('cancel')) {
			$('#not_impressed').show();
			$('#full_list').hide();
			window.location.hash = '#impr';
		} else {
			$.historyLoad('full');
		}
		
		return false;
	});
	
	$('.refresh').click(function(e) {
		document.location.reload();		
		return false;
	});
	
	$('.breakup').click(function(e) {
		window.location = $(this).find('a').attr('href');
		return true;
	});
	
	$('.fb_button').click(function(e) {
		var actionLinks = [{"text": "The Psychic", "href": 'http://psychic.lapadoo.com/'}];
		var attachment = {'media':[{'type':'image','src':'http://psychic.lapadoo.com/images/psychic_small.jpg','href':'http://psychic.lapadoo.com/'}]}; 
		FB.Connect.streamPublish($('.message').text() + "\n"+"http://psychic.lapadoo.com/", attachment, actionLinks, null, 'What do you think?', callback);
		return false;
	});
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
		
});


