/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = ">";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 



/*
 * Suffrage (for jQuery)
 * version: 0.5 (10/01/2008) [implemented basic support for multiple polls on one page, all must still be from the same site - 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 () {
		return '<span style="padding-left: 1px; *padding-left: 0px"><img src="./images/PollComp8/ONLinePollComp8_01.jpg"></span>';
	};
	
	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 += '<input type="radio" name="suffrageItem'+data.id+'" value="'+item.id+'"> '+item.title+'';
			}
		}
	
		return ''+items+'<input type="button" value="Vote" onclick="jQuery.fn.suffrage.vote(jQuery(\'input[name=\\\'suffrageItem'+data.id+'\\\']:checked\').val(),'+data.id+');">';
	};
	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>'+item.title+' '+item.value+' ('+item.percent+'%)</li>';
			}
		}
		
		return '<ol>'+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);
 