clickthroughs=0;

function getquestion(answer,displayid) {
	jQuery.get("/games/celebnamegame.inc.php?answer=" + answer + "&displayid=" + displayid, function(data){
		displaynextquestion(data,displayid);
	})
}

function displaynextquestion(data,displayid) {
	jQuery('#' + displayid).html(data);

	if (clickthroughs > 3) {
		clickthroughs=0;
		refreshAd('ad1');
		refreshAd('ad2');
	};
	clickthroughs++;

	var answerlinks = jQuery('#' + displayid + " > div > a.answerlink");
	answerlinks.each(function(){ jQuery(this).addClass("selectable"); });
	answerlinks.click( function() {
		if (jQuery(this).hasClass('selectable')) {
			answerlinks.each(function(){ jQuery(this).removeClass("selectable"); });
			jQuery(this).addClass('selected');
			var answer=jQuery(this).text();
			getquestion(answer,displayid);
		}
	})

	var difficultylinks = jQuery('#celebnamegame_difficulty > a');
	difficultylinks.click( function() {
		difficultylinks.each(function(){ jQuery(this).removeClass("selected"); });
		jQuery(this).addClass('selected');
		var difficulty=jQuery(this).text();

		if (difficulty == 'Easy') {
			data='(correct: +2, incorrect: -1)';
		} else if (difficulty == 'Medium') {
			data='(correct: +4, incorrect: -2)';
		} else if (difficulty == 'Hard') {
			data='(correct: +6, incorrect: -3)';
		};

		data +=' Your questions will be <b>' + difficulty + '</b> from now on.';
		jQuery('#celebnamegame_points').html(data);

		document.cookie ='celebng_difficulty=' + difficulty + '; domain =.starpulse.com';
	})
}

function refreshStats(display_period,stats_id){
	jQuery.get("/games/celebnamegame.inc.php?display_stats_only=yes&display_period=" + display_period + "&answer=0", function(data){
		jQuery('#' + stats_id).html(data);
	})
	return true;
}

function refreshAd(ad_id){
	var d=document.getElementById(ad_id);
	if(d){
		var s=d.getElementsByTagName('iframe');
		if(s && s.length){
			s[0].src=s[0].src;
		}
	}
	return true;
}
