/*
 * Suffrage (for jQuery)
 * version: 0.6 (11/17/2009) [fixed some cross-browser bugs in Chrome, added some default css classes cn]
 * @requires jQuery v1.2 or later
 *
 * Property of NBC Universal - Questions: chris.z.nelson@nbcuni.com
 *
 * This is a proof-of-concept implementation of the Suffrage polling front-end using Javascript/JQuery
 * 
 * The plugin will write the contents of a poll into the specified container.  It should be called like this:
 * 
 * $.fn.suffrage.settings.siteid = 1;
 * 
 * $(document).ready(function() {
 * 	$('#test').suffrage({pollid: 916});
 * });
 * 
 * This would make an AJAX call to Suffrage, and when the data is returned, write the HTML for pollid 916 into the element with an id of 'test'
 * 
 * The following options / settings are available 
 * 
 * 	$.fn.suffrage.settings.siteid;  Set this to the numeric ID of the Suffrage site you are accessing.
 * 	$.fn.suffrage.settings.pollid;  Set this to the numeric ID of the poll you are accessing.
 * 	$.fn.suffrage.settings.url; Set this to the URL for the Suffrage remote.php you are accessing.
 * 
 * All of the global parameters can be passed to the plugin call as well, the settings object is just provided as a convinent way to set page or site wide defaults.  
 * In most cases the siteid and URL parameters will not change between polls, just the poll id.
 * 
 * An example of passing all options to the suffrage function:
 * $(document).ready(function() {
 *         $('#test').suffrage({siteid: 1, pollid: 916, url: 'http://example.com/remote.php'});
 * });
 * 
 * You can override the following functions to control the HTML output that is generated. See the code below for example functions
 *   
 * 	jQuery.fn.suffrage.markupTitle; Controls the markup for the title of the poll.
 * 	jQuery.fn.suffrage.markupQuestion; Controls the markup for the possible answers to the poll.
 * 	jQuery.fn.suffrage.markupResults; Controls the markup for the results of the poll (after a user votes).
 * 
 * The function jQuery.fn.suffrage.callback will be executed once the content has been rendered.  By default nothing is done, but you can override this function if you need to trigger some code once the poll has been displayed
 *
 */

(function(jQuery) {
	//private variables
	var targets = Array();

	//our plugin object
	jQuery.fn.suffrage = function (options) {
		jQuery.fn.suffrage.opts = jQuery.extend({},jQuery.fn.suffrage.settings,options);
		targets[jQuery.fn.suffrage.opts.pollid] = Array();
		this.each(function(){
			targets[jQuery.fn.suffrage.opts.pollid].push(this);
		});
		
		jQuery.fn.suffrage.vote(0);

		return this;
	}

	//private functions
	function updateElements () {
		for (i=0; i < targets[jQuery.fn.suffrage.data.id].length; i++) {
			var target = targets[jQuery.fn.suffrage.data.id][i];
			jQuery(target).html(jQuery.fn.suffrage.markupTitle(jQuery.fn.suffrage.data));
			if (jQuery.fn.suffrage.data.voted) {
				jQuery(target).append(jQuery.fn.suffrage.markupResults(jQuery.fn.suffrage.data));
			} else {
				jQuery(target).append(jQuery.fn.suffrage.markupAnswers(jQuery.fn.suffrage.data));			
			}
		}

		jQuery.fn.suffrage.callback(jQuery.fn.suffrage.data.voted);
		
	}

	//public functions
	jQuery.fn.suffrage.markupTitle = function (data) {
		return '<h1 class="suffrageTitle">'+data.title+'</h1>';
	};
	
	jQuery.fn.suffrage.markupAnswers = function (data) {
		var items = '';
		for (i=0; i < data.items.length; i++) {
			var item = data.items[i];
			if (item.id) {
				items += '<li class="suffrageListItem suffrageQuestionListItem"><input type="radio" class="suffrageItem" name="suffrageItem'+data.id+'" value="'+item.id+'"> '+item.title+'</li>';
			}
		}
	
		return '<ol class="suffrageList suffrageQuestionList">'+items+'</ol><button class="suffrageButton" onclick="jQuery.fn.suffrage.vote(jQuery(\'input[name=\\\'suffrageItem'+data.id+'\\\']:checked\').val(),'+data.id+');">Vote</button>';
	};
	jQuery.fn.suffrage.markupResults = function (data) {
		var items = '';
		for (i=0; i < data.items.length; i++) {
			var item = data.items[i];
			if (item.id) {
				items += '<li class="suffrageListItem suffrageResultListItem">'+item.title+' '+item.value+' ('+item.percent+'%)</li>';
			}
		}
		
		return '<ol class="suffrageList suffrageResultList">'+items+'</ol>';
	};

	jQuery.fn.suffrage.vote = function (voteid,pollid) {
		if (!pollid) {
			pollid = jQuery.fn.suffrage.opts.pollid;
		}
		jQuery.ajax({
			dataType: 'jsonp',
			data: 's='+jQuery.fn.suffrage.opts.siteid+'&p='+pollid+'&v='+voteid,
			url: jQuery.fn.suffrage.opts.url,
			success : function (data) {
				jQuery.fn.suffrage.data = data;
				updateElements();
			}
		});
	};

	jQuery.fn.suffrage.callback = function () {
		return;
	}	

	//define our settings array	
	jQuery.fn.suffrage.settings = {siteid: 0, pollid: 0, url: 'http://suffrage.nbcuni.com/remote.php'};

})(jQuery);

