DBM.module.Vote = {
	
	currentCommentId: "",
	
	votingCallback: {  	
		  success: function(o) {
		  	if (o.responseText.length > 0) {
		  		var object = eval ( "(" + o.responseText + ")" );
				var sourceId = object.sourceId;		  		
				var nrVotesTxt = document.getElementById('votenumber'+"_"+sourceId);
				var info = document.getElementById('vote_error_'+sourceId);
				if(object.hasVoted){
					YAHOO.util.Dom.removeClass(info, "collapse");
					//info.innerHTML = "<p>Din stemme er allerede registert!</p>";
				} 
				var nrVotes = object.votesCount;
				nrVotesTxt.innerHTML = nrVotes;
		  	}
		  	
		  	DBM.module.Common.defaultCallback.success(o);
		  },  	
		  failure: function(o) {

		  	DBM.module.Common.defaultCallback.failure(o);
		  	if (DBM.module.Common.currentRequestIsStopped) {
		  		DBM.module.Common.currentRetry = 0;
			  	errorElement = document.getElementById("navigationError");
			  	
				YAHOO.util.Dom.removeClass(errorTxt, "collapse");
				errorTxt.innerHTML = "Det har oppstått en feil. Prøv gjerne på nytt!";
		  	}
		  },  	
		  timeout: DBM.module.Common.timeout  	
		  	
	},
	
	// Inserts comments as a part of the page
	addVote: function (gameid, sourceid) {
		var loadingDiv = 'votenumber_'+gameid;
		DBM.util.createDiv(loadingDiv,  DBM.module.Common.defaultLoadingHTML);
		var callback = this.votingCallback;
		var url = "http://www.123spill.no/api/kommentar/";
		var op = "?op=AddVote";
		var id = "&sourceId="+gameid;
		var source = "&sourceTypeId="+sourceid+"&templateSet=json&voteValue=1";
			
		url = url+op+id+source;
		
		if ( YAHOO.util.Connect.isCallInProgress(DBM.module.Common.currentRequest) ) {
			setTimeout("DBM.module.Vote.addVote(" + gameid + ", " + sourceid+")", 200);
		}
		else {
			DBM.module.Vote.currentCallback = callback;
		 	tId = DBM.module.Vote.currentRequest = YAHOO.util.Connect.asyncRequest('GET', url, this.votingCallback); 
		 	
		 	DBM.module.Common.addRequest(tId, url, this.votingCallback);
		 	
		}
	}, 
	
	
	
	getvoteCallback: {  	
		  success: function(o) {
		  	if (o.responseText.length > 0) {
		  		var object = eval ( "(" + o.responseText + ")" );
		  		if (object){
		  			var sourceId = object.sourceId;
		  			var nrVotesTxt = document.getElementById('votenumber_'+sourceId);
					var nrVotes = object.votesCount;
					nrVotesTxt.innerHTML = nrVotes;	
		  		}
		  	}
		  	
		  	DBM.module.Common.defaultCallback.success(o);
		  },  	
		  failure: function(o) {

		  	DBM.module.Common.defaultCallback.failure(o);
		  	if (DBM.module.Common.currentRequestIsStopped) {
		  		DBM.module.Common.currentRetry = 0;
			  	//alert("Det oppsto en feil i henting av antall stemmer")
		  	}
		  },  	
		  timeout: DBM.module.Common.timeout  	
		  	
	},
	
	
	
	getVoteCount: function(gameid, sourceid) {
		var callback = this.getvoteCallback;
		var url = "http://www.123spill.no/api/kommentar/";
		var op = "?op=GetVotes";
		var id = "&sourceId="+gameid;
		var source = "&sourceTypeId="+sourceid+"&templateSet=json";
		url = url+op+id+source;
		
		if ( YAHOO.util.Connect.isCallInProgress(DBM.module.Common.currentRequest) ) {
			setTimeout("DBM.module.Vote.getVoteCount(" + gameid + ", " + sourceid+")", 200);
		}
		else {
			DBM.module.Vote.currentCallback = callback;
		 	tId = DBM.module.Vote.currentRequest = YAHOO.util.Connect.asyncRequest('GET', url, this.getvoteCallback); 
		 	
		 	DBM.module.Common.addRequest(tId, url, this.getvoteCallback);
		 	
		}
	},
	
	getMostVotedCallback: {  	
		  success: function(o) {
		  	if (o.responseText.length > 0) {
		  		alert(o.responseText);
		  		/*var object = eval ( "(" + o.responseText + ")" );
		  		if (object){
		  			var nrVotesTxt = document.getElementById('votenumber');
					var nrVotes = object.votesCount;
					nrVotesTxt.innerHTML = nrVotes;	
		  		}*/
		  	}
		  	
		  	DBM.module.Common.defaultCallback.success(o);
		  },  	
		  failure: function(o) {

		  	DBM.module.Common.defaultCallback.failure(o);
		  	if (DBM.module.Common.currentRequestIsStopped) {
		  		DBM.module.Common.currentRetry = 0;
			  	alert("Det oppsto en feil i henting av antall stemmer")
		  	}
		  },  	
		  timeout: DBM.module.Common.timeout  	
		  	
	},
	
	
	
	getMostVoted: function(sourceid) {
		var callback = this.getMostVotedCallback;
		var url = "http://www.123spill.no/api/kommentar/";
		var op = "?op=MostVoted";
		//var id = "&sourceId="+gameid;
		var source = "&sourceTypeId="+sourceid+"&templateSet=json";
		url = url+op+source;
		alert(url);
		if ( YAHOO.util.Connect.isCallInProgress(DBM.module.Common.currentRequest) ) {
			setTimeout("DBM.module.Vote.getMostVoted(" + gameid + ", " + sourceid+")", 200);
		}
		else {
			DBM.module.Vote.currentCallback = callback;
		 	tId = DBM.module.Vote.currentRequest = YAHOO.util.Connect.asyncRequest('GET', url, this.getMostVotedCallback); 
		 	
		 	DBM.module.Common.addRequest(tId, url, this.getvoteCallback);
		 	
		}
	}
}	