// Init vars
var gamesPlayed = $.cookie('brain_plays', { path: '/' } );
var currentAge = $.cookie('brain_age', { path: '/' } );
var displayAge = currentAge;
var tickDelay = 100;

// Update Brain Age functions
function updateBA() {
	tickDelay = 100; // Reset timer
	if (currentAge < $.cookie('brain_age', { path: '/' } ) ) {
		for (displayAge=currentAge;displayAge<=$.cookie('brain_age', { path: '/' } );displayAge++) {
			tickDelay += 50;
			setTimeout('writeBA('+displayAge+')',tickDelay);
		}
		currentAge = ( displayAge - 1 );
	} else if (currentAge > $.cookie('brain_age', { path: '/' } ) ) {
		for (displayAge=currentAge;displayAge>=$.cookie('brain_age', { path: '/' } );displayAge--) {
			tickDelay += 50;
			setTimeout('writeBA('+displayAge+')',tickDelay);
		}
		currentAge = ( displayAge + 1 );
	}
}

function writeBA(displayAge) {
	$("#tracker h1").html("<p>" + displayAge + "</p>");
}

function updateTicker() {
	if ( $.cookie('brain_age', { path: '/' } ) != null && $.cookie('brain_age', { path: '/' } ) != 0 ) {
		var percentComplete = $.cookie('brain_plays', { path: '/' } ) * 20; // Read cookie because Flash does not update the var
		$("#tracker .dynamic").html("<h1>" + currentAge + "</h1> <p class='yba'>your Brain Age</p> <p class='complete'>" + percentComplete + "% complete</p>");
		updateBA(); // Update current age
	} else {
		$("#tracker .dynamic").html("<h2>Play some games to find out your Brain Age</h2>");
	}
	
	$("#tracker p a").attr("href", "mailto:?subject=Check%20this%20out,%20my%20Brain%20Age%20is "+currentAge+"!&body=http://www.freebrainagegames.com" );
}

function gameFinished(ba, bp) { // Called when game finishes
	$.cookie('brain_age', ba, { path: '/' });
	$.cookie('brain_plays', bp, { path: '/' });
	updateTicker();
}

// Initialise SWFs
if ( currentAge == null ) { currentAge = 0; } // If first time player
if ( gamesPlayed == null ) { gamesPlayed = 0; } // If first time player
var movieName = $("#game").attr("name");
$('#flash').flash({
	'src': 'swfs/'+movieName+'.swf',
	'width': '640',
	'height': '480',
	'flashvars': { 
		'brain_age': currentAge,
		'brain_plays': gamesPlayed
	}
});


// Page loaded
$(function(){
	
	// Update Brain Age
	updateTicker();
	
	// Done
	if ( $("body").attr("page") == "done") { // Only if on done page
		// If no brain age go home 
		if (currentAge == 0) {
			window.location.replace("index.php");
		}
		// Display age and CTA
		$("#donePage h2").html("Congratulations! <b>Your Brain Age is "+currentAge+ "</b>");
		$("#donePage .ctaText").html("Why not challenge your friends? Click <a href='mailto:?subject=Check%20this%20out,%20my%20Brain%20Age%20was "+currentAge+"&body=http://www.freebrainagegames.com'> here </a> to send an email.");
		$("#donePage .ctaButton").attr("href", "mailto:?subject=Check%20this%20out,%20my%20Brain%20Age%20was "+currentAge+"&body=http://www.freebrainagegames.com");
		// Clear Brain Age for next user
		$.cookie('brain_age', null, { path: '/' });
		$.cookie('brain_plays', 0, { path: '/' });		
	}
	
	// Games played can not go over 100%
	if ( gamesPlayed > 4 ) {
		gamesPlayed = 5;
	}
	
	// Reset
	$(".fbagReset").click(function() {
		$.cookie('brain_age', null, { path: '/' });
		$.cookie('brain_plays', 0, { path: '/' });
		updateTicker();
	});
	
	// Record the final BA to a CSV looking cookie then render as bar graph.

});
