var one_poll_Master = {
	/**
	 * Holds the JQuery Instance
	 */
	j : null,
	
	/**
	 * Processes the poll vote in a way which will work from an iframe.
	 * 
	 * @param string container The poll container to update
	 * 
	 * @return void
	 */
	sendIframeVote : function (container)
	{
		var voteForm = YAHOO.util.Dom.getElementsByClassName(
			"pollVoteForm",
			"form",
			container
		);
		
		voteForm = voteForm[0];
		
		var params = '&display=1&vote=1';
		
		// Gather up the form arguments.
		for (var i=0; i<voteForm.elements.length; i++) {
			switch (voteForm.elements[i].type) {
				case "hidden":
					break;
				case "text":
					break;
				case "checkbox":
					if (!voteForm.elements[i].checked) {
						continue;
					}
					break;
				case "radio":
					if (!voteForm.elements[i].checked) {
						continue;
					}
					break;
				default:
					continue;
			}
			
			params += "&"+voteForm.elements[i].name + "=";
			params += voteForm.elements[i].value;
		}
		
		YAHOO.util.Connect.asyncRequest(
			'POST',
			'/iframes/poll_iframe.one',
			{
				success  : this.displayResults,
				argument : {"container": container}
			},
			params
		);
	},
	/**
	 * Processes a poll vote form and submits it using AJAX.
	 * 
	 * @param string container  the poll container to update
	 * @param int    width      The width to display the results
	 * @param int    height     The height to display the results
	 * 
	 * @return void
	 */
	sendVote : function(container, width, height)
	{
		var voteForm = ONESITE.core.getElementsByClassName(
			"pollVoteForm",
			document.getElementById(container)
		);
		voteForm = voteForm[0];
		
		var params = 'action=logPollVote&responseFormat=json&requestType=class';
		params += '&handlerName=one_ajax_poll_handler&display=1&heavyAjax=1';
		
		if (width) {
			params += '&width=' + width;
		}
		
		if (height) {
			params += '&height=' + height;
		}
		
		// Gather up the form arguments.
		for (var i=0; i<voteForm.elements.length; i++) {
			switch (voteForm.elements[i].type) {
				case "hidden":
					break;
				case "text":
					break;
				case "checkbox":
					if (!voteForm.elements[i].checked) {
						continue;
					}
					break;
				case "radio":
					if (!voteForm.elements[i].checked) {
						continue;
					}
					break;
				default:
					continue;
			}
			
			params += "&"+voteForm.elements[i].name + "=" + voteForm.elements[i].value;
		}
		
		if (ONESITE.reg.get('widget_url')) {
			params += '&container=' + container;
			one_poll_Master.jsonp(
				'vote',
				'one_poll_Master.displayResults',
				params
			);
			return;
		}
		
		YAHOO.util.Connect.asyncRequest(
			'POST',
			'/resources/ajax/_router.one',
			{
				success  : this.displayResults,
				argument : {"container": container}
			},
			params
		);
	},
	
	/**
	 * Method to handle a successful AJAX request. Replaces poll div's innerHTML
	 * with the request response.
	 * 
	 * @param o  the request response
	 */
	displayResults : function(o)
	{
		if (ONESITE.reg.get('poll_widget_js_callback')) {
			try {
				eval(ONESITE.reg.get('poll_widget_js_callback'))({
					'type'     : 'displayResults',
					'response' : o
				});
			} catch (e) { }
		}
		
		var container = o.argument['container'];
		
		if (o && o.responseText) {
			if (YAHOO) {
				o = YAHOO.lang.JSON.parse(o.responseText);
			} else {
				o = one_poll_Master.j.parseJSON(o.responseText);
			}
		}
		
		document.getElementById(container).innerHTML = o.msg;
		
		if (o.script) {
			eval(o.script);
		}
	},
	
	init : function(poll_id)
	{
		var params = 'action=displayPoll&responseFormat=json&requestType=class';
		params += '&handlerName=one_ajax_poll_handler&display=1&heavyAjax=1';
		params += '&poll_id=' + poll_id;
		
		YAHOO.util.Connect.asyncRequest(
			'POST',
			'/resources/ajax/_router.one',
			{
				success : one_poll_Master.display,
				failure : function(o) {},
				argument : {"poll_id" : poll_id}
			},
			params
		);
	},
	
	display : function (o)
	{
		var results = YAHOO.lang.JSON.parse(o.responseText);
		var dom = YAHOO.util.Dom.get('pollWrapper_' + o.argument['poll_id']);
		
		if (!dom) {
			return;
		}
		
		dom.innerHTML = results.buffer;
		
		if (results.script) {
			eval(results.script);
		}
	},
	
	/**
	 * Performs a JSONP widget query.
	 * 
	 * @param string action   Action to Perform
	 * @param array  callback Callback after the pageload.
	 * @param params params   Parameters to send to the widget.
	 * 
	 * @return void
	 */
	jsonp : function (action, callback, params)
	{
		var url = ONESITE.reg.get('widget_url') + action;
		url += '?devkey=' + ONESITE.reg.get('devkey');
		url += '&one_widget_node=' + ONESITE.reg.get('one_widget_node');
		url += '&callback=' + callback;
		url += '&wsetup=true';
		url += '&' + params.replace('action=', 'pollAction=');
		
		var e = document.createElement("script");
		e.type = "text/javascript";
		e.src = url;
		document.getElementsByTagName("head")[0].appendChild(e);
	}
}

try {
	one_poll_Master.j = jQuery.noConflict();
} catch (e) {}
