/*
Vote Verification 
*/

var _ContestEntry = function(votes, lastVoted)
{
	this.votes		= votes;
	this.lastVoted	= lastVoted;
}

var VoteVerification = function ()
{
	return VoteVerification.prototype.__getInstance();
}

VoteVerification.prototype = {		
	instanceOfObj:		null,
	__getInstance: function()
	{
		if(!VoteVerification.prototype.instanceOfObj) 
		{
			VoteVerification.prototype.instanceOfObj = this;
			this.__construct();
		}
		return VoteVerification.prototype.instanceOfObj;			
	},	
			
	__construct: function()
	{
		this.MAX_VOTES		= 8;
		this.voteEntryArray = new Object();
		
		this.getVoteEntryArrayFromCookie();
	},
	
	
	getVoteEntryArrayFromCookie: function(){		
		//console.log("Get Vote Entry: ");
		
		var tempVar = COOKIE_MANAGER.getCookie("voteVerification");
		if(tempVar)
		{
			eval("this.voteEntryArray = "+tempVar);	
		}
	},	
	
	setVoteEntryArrayToCookie:	function()
	{
		var voteEntryArray = this.voteEntryArray;		

		//console.log("Set Vote Entry: "+voteEntryArray);
		//console.log("Set Vote Entry objectified: "+ Object.toJSON(voteEntryArray));
		
		COOKIE_MANAGER.setCookie("voteVerification", Object.toJSON(voteEntryArray)); 
		this.getVoteEntryArrayFromCookie();
	},
	
	processNewVoteEntry:	function(contestName)
	{
		//console.log("process new vote entry "+ contestName);
		var currentContest	= this.voteEntryArray[contestName];

		if(currentContest && this.exceedTotalVotes(contestName))
		{
			return false;			
		}
		else
		{
			this.voteEntryArray[contestName] = this.addTotalVoteCount(this.voteEntryArray[contestName]);
			
			//console.log("Process new Vote Entry got votes now: "+ this.voteEntryArray[contestName]);
			this.setVoteEntryArrayToCookie();
			
			return true;
		}
	},
	
	
	isDayChanged:	function(prevDay, currDay)
	{
		if(prevDay == currDay)
		{
			return false;
		}
		else
		{
			return true;
		}
	},
	
	addTotalVoteCount:	function(currentContest)
	{
		//console.log("add to total vote count "+ currentContest);	
		var nowDate		= new Date();
		var votes		= 1;
		
		if(currentContest) //there exists a contest in cookie
		{		
			if(this.isDayChanged(currentContest.lastVoted, nowDate.getDate())) //new day!
			{
				currentContest.lastVoted	= nowDate.getDate();
				currentContest.votes		= votes;
			}
			else //still same day
			{
				//console.log("addTotalVoteCount: still same day");
				currentContest.votes++;
			}
		}
		else //there doesnt exists a contest in cookie
		{		
			currentContest	= new _ContestEntry(votes, nowDate.getDate());			

		}	
				
		//console.log("ending add to total vote count "+ currentContest.votes + currentContest.lastVoted);
		return currentContest;
	},
	
	exceedTotalVotes:	function(contestName)
	{
		var currentContest	=  this.voteEntryArray[contestName];		
		var nowDate			=  new Date();
		
		if(currentContest)
		{
		 	if(this.isDayChanged(currentContest.lastVoted, nowDate.getDate())) //day changed, 
		 	{
				return false;
			}
			else if(currentContest.votes >= this.MAX_VOTES)
			{
				return true;
			}
		}
		else
		{
			return false;
		}
	}
}

