/*
 * Suffrage (for jQuery)
 * version: 0.3 (07/30/2008) [fixed some ie6 bugs - 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).
 * 
 */

(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);
		this.each(function(){
			targets.push(this);
		});
		
		jQuery.fn.suffrage.vote(0);

		return this;
	}

	//private functions
	function updateElements () {
		for (i=0; i < targets.length; i++) {
			var target = targets[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));			
			}
		}
	}

	//public functions
	jQuery.fn.suffrage.markupTitle = function (data) {
		return '<h5>'+data.title+'</h5>';
	};
	
	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><input type="radio" name="suffrageItem" value="'+item.id+'"> '+item.title+'</li>';
			}
		}
	
		return '<ol>'+items+'</ol><input class="qotm_button" type="button" value="submit my vote!" onclick="jQuery.fn.suffrage.vote(jQuery(\'input[name=\\\'suffrageItem\\\']:checked\').val());">';
	};
	jQuery.fn.suffrage.markupResults = function (data) {
		var items = '';
		var sorted={};
		for (i=0; i < data.items.length; i++) {
			var percent = data.items[i].percent;
			sorted[i]=percent;
		}
		data.items.sort(function (a,b) {
			return ((a.value < b.value) ? 1 : ((a.value > b.value) ? -1 : 0));
		});
		for (i=0; i < data.items.length; i++) {
			var item = data.items[i];
			if (item.id) {
				items += '<li><span>'+Math.ceil(item.percent)+'%</span>'+item.title+' </li>';
			}
		}
		
		return '<ol id="qotm_results">'+items+'</ol><p id="qotm_thanks">thanks for voting!</p>';
	};

	jQuery.fn.suffrage.vote = function (voteid) {
		jQuery.ajax({
			dataType: 'jsonp',
			data: 's='+jQuery.fn.suffrage.opts.siteid+'&p='+jQuery.fn.suffrage.opts.pollid+'&v='+voteid,
			url: jQuery.fn.suffrage.opts.url,
			success : function (data) {
				jQuery.fn.suffrage.data = data;
				updateElements();
			}
		});
	};

	//define our settings array	
	jQuery.fn.suffrage.settings = {siteid: 0, pollid: 0, url: 'http://suffrage.nbcuni.com/remote.php'};

})(jQuery);

