/*
 * Competition Stats
 */

/*
 * Constructor
 */
function CompetitionStats(){
	
	Box.call(this);
	eventhook.addListener(this,"profileInfoChange");
	eventhook.addListener(this,"loggedIn");
}

/*
 * Members
 */
CompetitionStats.prototype = new Box;
CompetitionStats.prototype.constructor = CompetitionStats;
CompetitionStats.prototype.template = "#template_competitionstats";
CompetitionStats.prototype.targetDate = null;
CompetitionStats.prototype.lastBlinkSec = 80;
CompetitionStats.prototype.competitionId = 0;
CompetitionStats.prototype.icon = "boxicons/competition.png";
CompetitionStats.prototype.completed = false;

/*
 * Setter for completed / competition over status
 */
CompetitionStats.prototype.setCompleted = function(completed){
	
	this.completed = completed;
		
	return this;
}

/*
 * Setter for competitionId
 */
CompetitionStats.prototype.setCompetitionId = function (competitionId){
	
	this.competitionId = competitionId;
	
	return this;
}

/*
 * Changes run when a competition is set to completed
 */
CompetitionStats.prototype.doCompleted = function(){
	
	$(this.containerId + " .competition_eligible").hide();
	$(this.containerId + " .competition_ineligible").hide();
	$(this.containerId + " .competition_entered").hide();
	$(this.containerId + " .country_mismatch").hide();
	$(this.containerId + " .country_login").hide();
	$(this.containerId + " .competition_timer").hide();
	
	$(this.containerId + " .competition_timer_completed").show();
	$(this.containerId + " .competition_completed").show();
}

/*
 * Set the target date, when the competition ends
 */
CompetitionStats.prototype.setTargetDate = function(targetDate){
	
	this.targetDate = new Date(targetDate.valueOf());
		
	return this;
}

/*
 * Standard addcontent - sets this box in motion
 */
CompetitionStats.prototype.addContent = function(){
	
	this.timerCallbackFunc(this)();	
	this.getInfo();
}

/*
 * Retrieve the user's competition information from the server
 */
CompetitionStats.prototype.getInfo = function(){
	
	$.getJSON("interface/json.php",{action: "userCompetitionStats", competitionId: this.competitionId},this.jqueryInfoCallbackFunc(this));	
}

/*
 * Callback when information returns from server
 */
CompetitionStats.prototype.jqueryInfoCallbackFunc = function(obj){
	
	return function(data){
		
		if (obj.completed){
			
			obj.doCompleted();
		} else if (userId == 0) {
		
			$(obj.containerId + " .competition_login").css("display", "");
		} else if (data.numEntries > 0 && data.supported) {
			
			$(obj.containerId + " .competition_entered").css("display","");
			$(obj.containerId + " .number_entries").html(data.numEntries);
		} else if (data.supported) {
			
			$(obj.containerId + " .competition_eligible").css("display","");
		} else {
			
			$(obj.containerId + " .competition_ineligible").css("display","");
		}
		
		if (data.conflict)
			$(obj.containerId + " .country_mismatch").css("display","");
			
		obj.loaded();
	}
}

/*
 * Timer callback - triggered repeatedly to update timer
 */
CompetitionStats.prototype.timerCallbackFunc = function(obj){
	
	return function(){
		
		var currDate = new Date();
		var msecsRemaining = obj.targetDate - currDate;
		secs = Math.floor(msecsRemaining / 10);
		
		if (secs <= 0){
			
			if (!obj.completed){
				
				obj.setCompleted(true);
				obj.doCompleted();		
			}			
		}
		
		var hsecs = secs % 100;
		secs = (secs - hsecs) / 100;
		
		var seconds = secs % 60;
		secs = (secs - seconds) / 60;
		
		var blinking = seconds % 2;	
		
		var mins = secs % 60;
		secs = (secs - mins) / 60;
		
		var hours = secs % 24;
		secs = (secs - hours) / 24;
		
		var days = Math.floor(secs);

		if (hsecs < 10)
			hsecs = '0' + hsecs;
		
		if (seconds < 10)
			seconds = '0' + seconds;
		
		if (mins < 10)
			mins = '0' + mins;
			
		if (hours < 10)
			hours = '0' + hours;		
					
		$(obj.containerId + " .hseconds").html(hsecs);
		$(obj.containerId + " .seconds").html(seconds);
		$(obj.containerId + " .minutes").html(mins);
		$(obj.containerId + " .hours").html(hours);
		active = $(obj.containerId + " .days").html(days).size();					
			
		if (seconds != obj.lastBlinkSec) {
			obj.lastBlinkSec = seconds;
			if (blinking == 1 || blinking == -1) {
			
				//var snd = new Audio("audio/beep.wav");
				//snd.play();
				$(obj.containerId + " .blink").css("visibility", "visible");
				obj.beeped = true;
			}
			else {
				
					$(obj.containerId + " .blink").css("visibility", "hidden");
				}
		}	
			
		if (active > 0)
			window.setTimeout(obj.timerCallbackFunc(obj),10);
	}
}

/*
 * Event handler callback when profile info changes or login status changes - refresh
 */
CompetitionStats.prototype.eventCallback = function(eventType){

	if ((eventType = "profileInfoChange" || eventType == "loggedIn") && !this.completed)
	{
		$(this.containerId + " .competition_login").css("display", "none");
		$(this.containerId + " .competition_entered").css("display", "none");
		$(this.containerId + " .competition_eligible").css("display", "none");
		$(this.containerId + " .competition_ineligible").css("display", "none");
		$(this.containerId + " .country_mismatch").css("display", "none");
		this.getInfo();
	}
}
