/**
 * VSQ - Easy Poll v.1.0.1
 * @author mad@Max
 */
YAHOO.util.Event.onDOMReady(function(){
	var Y = YAHOO.util, D = Y.Dom, E = Y.Event;
	var VSQ_EP = function(){
		this.pseudoform = null;
		this.view = true;
		this.req = null;
		this.obj = D.getElementBy(function(el){return el.action.search("poll.php") != -1;}, "form")
		var sbm = D.getElementBy(function(el){return el.type == 'submit';}, "input", this.obj);
		var show = fetch_tags(sbm.parentNode, "a")[0];
		E.on(sbm, "click", this.vote, this, true);
		E.on(show, "click", this.show, this, true);
	}
	VSQ_EP.prototype = {
		callback: function(o){
			if (o.responseXML) 
			{
				var error = o.responseXML.getElementsByTagName('error');
				if (error.length) 
				{
					alert(error[0].firstChild.nodeValue);
				}
				else if (this.req == 'pollvote') 
				{
					alert("Thanks for your vote!");
					this.make("showresults");
					this.view = false;
				}
				else if (this.req == 'showresults') 
				{
					this.obj.style.display = "none";
					var res = document.createElement('div');
					res.innerHTML = o.responseXML.getElementsByTagName('results')[0].firstChild.nodeValue;
					this.obj.parentNode.insertBefore(res, this.obj);
					var a = document.createElement('a');
					if (this.view) 
					{
						a.style.cssText = "position:relative;right:40%;float:right;";
						a.href = "javascript://";
						a.innerHTML = "Go Back";
						E.on(a, "click", function(e){
							do_an_e(e);
							this.obj.style.display = "";
							res.parentNode.removeChild(res);
						}, this, true);
						D.getElementsByClassName("tcat", "td", res)[0].appendChild(a);
					}
				}
			}
		},
		make: function(m){
			if ((this.req = m) == 'showresults') 
			{
				D.getElementBy(function(el){return el.name == 'do';}, "input", this.obj).value = "showresults";
			}
			this.pseudoform = new vB_Hidden_Form('poll.php');
			this.pseudoform.add_variable('ajax', 1);
			this.pseudoform.add_variables_from_object(this.obj);
			Y.Connect.asyncRequest("POST", "poll.php?do=" + m + "&pollid=" + this.pseudoform.fetch_variable("pollid"), {
				success: this.callback,
				failure: function(o){
					vBulletin_AJAX_Error_Handler(o);
					m == 'pollvote' ? this.obj.submit() : '';
				},
				timeout: vB_Default_Timeout,
				scope: this
			}, this.pseudoform.build_query_string() + SESSIONURL);
		},
		vote: function(e){
			do_an_e(e);
			this.make("pollvote");
		},
		show: function(e){
			do_an_e(e);
			this.make("showresults");
		}
	};
	new VSQ_EP();
});
