
// - ///////////////////////////////////////////////////////////////////////
// - PHONEDOG VOTING MODULE
// - ///////////////////////////////////////////////////////////////////////
// - Looks for all voters (div.voter) and builds the voting elements within it
// - as well as attaches the appropriate event handlers for voting and saving
//fields
pdVotingModule.prototype._transport;
pdVotingModule.prototype._user;
pdVotingModule.prototype._channelId = 'site.voting';

function pdVotingModule() {}

pdVotingModule.prototype.initialize = function(transport, user){
  	//this function is called back during module registration
	this._transport = transport;
	this._user = user;
	
	var me=this; //set reference to current class to pass within constructor
	
	//find all voters
	$$('div.voter').each(function(voter){
		var voterId = voter.identify().split('_')[1];
		var votewrapper = new Element('div', {id:'vote_' + voterId});
		var imgUpDown = new Element('img',{src:'/img/icons/thumbupdown.gif'});
		var voteUp = new Element('a',{href:'#',id:'vote_yes_' + voterId}).update('helpful');
		var voteDown = new Element('a',{href:'#',id:'vote_no_' + voterId}).update('not helpful');
		var spanStart = new Element('span').update(' Was this review ');
		var spanMid = new Element('span').update(' or ');
		var spanEnd = new Element('span').update(' for you?');
		var votemessage = new Element('div',{id:'vote_done_' + voterId}).setStyle({display:'none'}).update('Thanks for your vote!');
		
		//attach event observers to handle the clicks
		voteUp.observe('click',me.saveVote.bindAsEventListener(me));
		voteDown.observe('click',me.saveVote.bindAsEventListener(me));
		
		//build the voter html elements
		votewrapper.appendChild(imgUpDown);
		votewrapper.appendChild(spanStart);
		votewrapper.appendChild(voteUp);
		votewrapper.appendChild(spanMid);
		votewrapper.appendChild(voteDown);
		votewrapper.appendChild(spanEnd);
		voter.appendChild(votewrapper);
		voter.appendChild(votemessage);
	});
}

pdVotingModule.prototype.saveVote = function(event){
	event.stop(); //prevent click behavior
	var element = Event.element(event);
	var yesno = element.identify().split('_')[1];
	var review = element.identify().split('_')[2];
	var postData = {reviewid:review,vote:yesno};
	
	//onfailure is left out on purpose
	var options = {parameters:postData, method:'post', onComplete: function(response){$('vote_' + review).hide(); $('vote_done_' + review).show();}}	
	new Ajax.Request('/ajax.aspx?x=%2f7U%2fTAv2H2I%3d', options);
}