/*
 * Pollection Inc. (c) 2008
 */

theWidget = null;

function PollectionWidgetInit(_polls, _onVotePlatform, _onNextPlatform, _onAdvancePlatform) 
{
	theWidget = new Pollection.Widget(_polls, _onVotePlatform, _onNextPlatform, _onAdvancePlatform);
}

Pollection.Widget = Class.create({
	polls : null, 			// JSON object of all active polls
	onVotePlatform : null,	// platform specific on vote event handler
	onNextPlatform : null,	// platform specific on next event handler
	onAdvancePlatform : null,	// platform specific on round advancement event handler
	
	currentPollIndex : null,
	
	pollTemplate : null,	// the UI template for poll
	choiceTemplate : null,	// the UI template for poll choice
	
	initialize : function(_polls, _onVotePlatform, _onNextPlatform, _onAdvancePlatform)
	{
		this.polls = _polls;
		this.onVotePlatform = _onVotePlatform;
		this.onNextPlatform = _onNextPlatform;
		this.onAdvancePlatform = _onAdvancePlatform;
		
		this.pollTemplate = new Template('<div class="poll" id="poll#{id}"><h3>' + g_appQuestion + '</h3><div class="colLeft">#{ch1}#{ch3}<div class="smartclear">&nbsp;</div></div><div class="colRight">#{ch2}#{ch4}<div class="smartclear">&nbsp;</div></div><div class="colCenter">#{ch5}</div></div>');
		this.choiceTemplate = new Template('<div class="choice" id="ch#{id}" onclick="theWidget.vote(#{pid}, #{id}); return false;"><div class="choiceImg"><img src="#{img}" alt="#{name}"></div>#{name}</div>');
		
		if (polls.length > 0) {
			this.currentPollIndex = 0;
			this.renderPoll(this.currentPollIndex);
		}
	},
	renderPoll : function(pollIndex)
	{
		var pollId = this.polls[pollIndex].id;
		var choiceTemplate = this.choiceTemplate;
		var choiceMarkups = Array(5);
		var index = 0;
		this.polls[pollIndex].choices.each(function(choice) {
			var _name = 'Somebody';
			var _img = 'http://images.hi5.com/images/nophoto_boy_100.gif';
			var person = theGadget.friends.get(choice); 
			if (person) {
				var _name = person.getDisplayName();
				var _img = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			}
			
			choiceMarkups[index] = choiceTemplate.evaluate({
				pid : pollId,  
				id : choice, 
				img : _img,
				name : _name
			});
			index++;
		});
		var pollMarkup = this.pollTemplate.evaluate({
			id : pollId,
			ch1 : choiceMarkups[0],
			ch2 : choiceMarkups[1],
			ch3 : choiceMarkups[2],
			ch4 : choiceMarkups[3],
			ch5 : choiceMarkups[4]
		});
		$('poll').update(pollMarkup);
		$$('div.choice').invoke('observe', 'mouseover', function(){
			this.addClassName('hover');			
		}).invoke('observe', 'mouseout', function(e){
			this.removeClassName('hover');	
		});
	},
	vote : function(pollId, choiceId)
	{
		this.polls[this.currentPollIndex].winner = choiceId;
		this.onVotePlatform(choiceId, this.polls[this.currentPollIndex].choices);
		
		$('poll').update('<div style="text-align: center;"><img src="http://static.ayt.pollection.com/images/working.gif" /></div>');
		
		var params = { };
		var postdata = {
			app_key : 1,
			choice : choiceId,
			timestamp : 1,
			user_guid : 'a',
			api_sig : 'sig',
			owner: theGadget.viewerId			
		}
	    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.HTML;
		params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.NONE;
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
		
		var url = g_callbackUrl + '/api/rest/vote/' + pollId;
		Debug.log('voting: ' + url)
		gadgets.io.makeRequest(url, this.next.bind(this), params);
	},
	next : function()
	{	
		this.onNextPlatform();
		++this.currentPollIndex;
		
		if (this.polls.length == this.currentPollIndex) {
			var _from = (this.polls.length == 1) ? 'continue' : 'advance';
			this.onAdvancePlatform(_from);
			return;
		} 
		
		this.renderPoll(this.currentPollIndex);
	},
	advance : function(_from)
	{
		gadgets.views.requestNavigateTo(new gadgets.views.View('canvas'), {from:_from});
	}
});
