DBM.module.Votes = {
	
	sourceTypeId: "",
	
	sourceId: "",
	
	votesTarget: "votes_container",
	
	rootUrl: "",
	
	modulePath: "",
	
		// The callback object for listing comments
	votesCallback: {  	
		  success: function(o) {
			DBM.util.createDiv(DBM.module.Common.currentTarget, o.responseText);
		  	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("Poengsystemet svarer ikke. Vennligst prøv igjen " + o.status +  " " + o.statusText + " tekst: " + o.responseText);
			  	DBM.util.createDiv(DBM.module.Common.currentTarget, "");
		  	}
		  },  	
		  timeout: DBM.module.Common.timeout  		
	},
	
	// Inserts comments as a part of the page
	insertVotes: function (sourceTypeId, sourceId, target, loadingTarget, loadingHTML, callback) {
		
		if (loadingTarget == undefined) {
			loadingTarget = this.votesTarget;	
		}
		
		if (target != undefined) {
			this.votesTarget = target;	
		}

		
		if (loadingHTML == undefined) {
			loadingHTML = DBM.module.Common.defaultLoadingHTML;	
		}

		
		if (callback == undefined) {
			callback = this.votesCallback
		}
				
		if ( YAHOO.util.Connect.isCallInProgress(DBM.module.Common.currentRequest) ) {
			
			setTimeout("DBM.module.Votes.insertVotes('" + sourceTypeId + "', '" + sourceId + "', '" + target + "', '" + loadingTarget + "', '" + loadingHTML + "')", 200);
		}
		else {
			DBM.module.Common.currentTarget = DBM.module.Votes.votesTarget;
			DBM.module.Common.currentLoadingTarget = loadingTarget;
			DBM.module.Common.currentCallback = callback;
			DBM.util.createDiv(DBM.module.Common.currentLoadingTarget, loadingHTML);
			DBM.module.Common.currentRequestUrl = DBM.module.Votes.rootUrl + DBM.module.Votes.modulePath + "?op=ViewVotes&templateSet=blink&sourceTypeId=" + sourceTypeId + "&sourceId=" + sourceId;		
			DBM.module.Common.currentRequest = YAHOO.util.Connect.asyncRequest('GET', DBM.module.Common.currentRequestUrl, DBM.module.Common.currentCallback); 
		}
	
	},
	
	updateVotesCallback: {

		success: function(o) {
			
			DBM.module.Common.defaultCallback.success(o);
			var response = eval ( "(" + o.responseText + ")" );
			if (response.updateVotes == "1") {
				var starRatingHeader = document.getElementById("star-rating-header");
				starRatingHeader.innerHTML = '<strong>Snitt:</strong>  ' + response.votesAverage + ' av 5 (' + response.votesCount + ')';
			}
			
			if (response.hasVoted == "1") {
				var starRatingFooter = document.getElementById("star-rating-footer");
				starRatingFooter.innerHTML = 'Kun én stemme <br/>per bruker';
			}
			else if (response.voteValue) {
				var starRatingFooter = document.getElementById("star-rating-footer");
				if (response.voteValue == 1 ) {
					starRatingFooter.innerHTML = 'Nydelig. Takk for<br/>din stjerne!';
				}
				else if (response.voteValue == 2) {
					starRatingFooter.innerHTML = 'Nydelig. Takk for<br/>dine to stjerner!';
				}
				else if (response.voteValue == 3) {
					starRatingFooter.innerHTML = 'Nydelig. Takk for<br/>dine tre stjerner!';
				}
				else if (response.voteValue == 4) {
					starRatingFooter.innerHTML = 'Nydelig. Takk for<br/>dine fire stjerner!';
				}
				else if (response.voteValue == 5) {
					starRatingFooter.innerHTML = 'Nydelig. Takk for<br/>dine fem stjerner!';
				}
			}
			
			
			
			
			if (response.hasVoted != "1") {
				
				var starRating = document.getElementById("star-rating-stars");
				var starCounter = 1
				for (var i = 0; i < starRating.childNodes.length; i++) {
					if (starRating.childNodes[i].tagName == "LI") {
						if (starCounter == response.votesAverage) {
							starRating.childNodes[i].className = "current" + starCounter;
						}
						else {
							starRating.childNodes[i].className = "";	
						}
						starCounter++;
					}	
				}
				
				
				if (response.voteValue) {
					var element = document.getElementById('star-rating');
					var toColor = '#ffffd7'; //YAHOO.util.Dom.getStyle('star-rating', 'backgroundColor'); 
					var fromColor = '#ffff7a';
					var myAnim = new YAHOO.util.ColorAnim(element, {backgroundColor: { from: fromColor, to: toColor } });
					myAnim.animate(); 
				}
			}
			
		},
		failure: function(o) {
			DBM.module.Common.defaultCallback.failure(o);
		},
		timeout: DBM.module.Common.timeout
	},
	
	updateVotes: function () {
		DBM.module.Common.currentCallback = this.updateVotesCallback;
		DBM.module.Common.currentRequestUrl = DBM.module.Votes.rootUrl + DBM.module.Votes.modulePath + "?op=GetVotes&templateSet=json&sourceTypeId=" + this.sourceTypeId + "&sourceId=" + this.sourceId;
		DBM.module.Common.currentRequest = YAHOO.util.Connect.asyncRequest('GET', DBM.module.Common.currentRequestUrl, DBM.module.Common.currentCallback);
	},
	
	addVote: function (voteValue) {
		//var starRatingHeader = document.getElementById("star-rating-header");//new getObj("star_rating");
		//starRatingHeader.innerHTML = 'Oppdaterer...';
		DBM.module.Common.currentCallback = this.updateVotesCallback;
		DBM.module.Common.currentRequestUrl = DBM.module.Votes.rootUrl + DBM.module.Votes.modulePath + "?op=AddVote&templateSet=json&sourceTypeId=" + this.sourceTypeId + "&sourceId=" + this.sourceId + "&voteValue=" + voteValue;
		DBM.module.Common.currentRequest = YAHOO.util.Connect.asyncRequest('GET', DBM.module.Common.currentRequestUrl, DBM.module.Common.currentCallback);
		return false;
	},

	clickVote: function (e) { 
		YAHOO.util.Event.stopEvent(e);
		var value = this.innerHTML;
	 	DBM.module.Votes.addVote(this.innerHTML);	 
	},

	prepare: function () {
		/*DBM.util.include.includeOnce("http://dev.dagbladet.no/yui/build/yahoo/yahoo-min.js");
		DBM.util.include.includeOnce("http://dev.dagbladet.no/yui/build/connection/connection-min.js");
		DBM.util.include.includeOnce("http://dev.dagbladet.no/yui/build/dom/dom-min.js");
		DBM.util.include.includeOnce("http://dev.dagbladet.no/yui/build/animation/animation-min.js");
		DBM.util.include.includeOnce("http://dev.dagbladet.no/yui/build/event/event-min.js");*/
	},
	
	init: function (in_sourceTypeId, in_sourceId, in_rootUrl, in_modulePath) {
		
		this.sourceTypeId = in_sourceTypeId;
		this.sourceId = in_sourceId;
		
		if (in_rootUrl != undefined) {
			DBM.module.Votes.rootUrl = in_rootUrl;	
		}
		
		if (in_modulePath != undefined) {
			DBM.module.Votes.modulePath = in_modulePath;	
		}
		
		//this.updateVotes();
		

		this.insertVotes(in_sourceTypeId, in_sourceId, this.votesTarget);
		
		// Attach handlers
		//var stars = ["star1", "star2", "star3", "star4", "star5"]; 
		//YAHOO.util.Event.addListener(stars, "click", DBM.module.Votes.clickVote); 
	}

}	

DBM.module.Votes.prepare();