var Poll = function() {
	this.getElement = function (selector,parent) {
		if(parent) {
			if(jQuery!=undefined) return this.getFirstElement(jQuery(parent).find(selector)); else return Element.getElementsBySelector(parent,selector);
		} else {
			if(jQuery!=undefined) return this.getFirstElement(jQuery(selector)); else return $$(selector);
		}
	}

	this.updateElement = function (element,html) {
		if(jQuery!=undefined) jQuery(element).html(html); else Element.update(element,html);
	}

	this.removeElement = function(element) {
		if(jQuery!=undefined) jQuery(element).remove(); else Element.remove(element);
	}

	this.setElementStyle = function(element,styles) {
		if(jQuery!=undefined) {
			for(s in styles) {
				jQuery(element).css(s,styles[s]);
			}
		} else Element.setStyle(element,styles);
	}

	this.addEvent = function(element,event,handler) {
		if(jQuery!=undefined) jQuery(element).bind(event+'.poll',handler); else Element.observe(element,event,handler);
	}

	this.setCookie = function(name,value) {
		if(jaaulde!=undefined) jaaulde.utils.cookies.set(name,value);
	}

	this.getCookie = function(name) {
		if(jaaulde!=undefined) return jaaulde.utils.cookies.get(name); else return null;
	}

	this.setElementAttr = function(element,attr,value) {
		if(jQuery) jQuery(element).attr(attr,value); else Element.writeAttribute(element,attr,value);
	}

	this.getElementAttr = function(element,attr) {
		if(jQuery) return jQuery(element).attr(attr); else return Element.readAttribute(element,attr);
	}

	this.getFirstElement = function(element) {
		if(element.length==1) {
			element = element[0];
			if(element.length==1) return element[0]; else return element;
		} else return element;
	}

	this.results = new Array();
	this.total = 0;

	this.calcBars = function(selector) {
		//if poll was submitted insert thank-you message into poll-title
		var thanks = this.getElement('.poll-thanks');
		var h = this.getElement('#poll-results .poll-title');
		if(thanks && (thanks.length!=0 || thanks.length==null || thanks.length==undefined)) {
			this.updateElement(h,h.innerHTML+': '+thanks.innerHTML);
			this.removeElement(thanks);
			this.setElementStyle(this.getElement('.poll-results-link'),{'display':'none'});
		} else { //insert some wording into poll-title
			this.updateElement(h,h.innerHTML+': '+'Ergebnis der aktuellen Umfrage');
			if(this.getElement('.zurabstimmunglink').href) {
				this.getElement('.zurabstimmungziel').href = this.getElement('.zurabstimmunglink').href;
			}
		}

		var bars = this.getElement(selector);
		var a;
		var p;
		var li;

		for(var i=0;i<bars.length;i++) {
			a = (this.results[i].search('#')>-1)?-1:parseInt(this.results[i]);
			li = bars[i].parentNode.parentNode;
			if(a>-1) {
				p = Math.round(((100/this.total)*a));
				this.setElementStyle(bars[i],{'width':p+'%'});
				this.setElementStyle(li,{'display':'block'});
				this.updateElement(this.getElement('.poll-percent',li),p+'%');
			} else {
				this.removeElement(li);
			}
		}
	}

	this.initValidate = function(selectorForm,selectorSubmit,selectorAnswers,selectorError) {
		this.form = this.getElement(selectorForm);
		this.action = this.getElementAttr(this.form,'action');
		this.setElementAttr(this.form,'action','javascript:void(0);');
		this.submit = this.getElement(selectorSubmit);
		this.answers = this.getElement(selectorAnswers);
		this.error = this.getElement(selectorError);
		this.pollId = this.getElementAttr(this.form['tx_myquizpoll_pi1[uid1]'],'value');
		var _this = this;

		if(this.answers && this.answers.length>0) {
			for(var i = 0;i<this.answers.length;i++) {
				this.addEvent(this.answers[i],'click',function(){_this.validate();});
			}
		}
		
		this.validate();
		this.checkCookie();
		this.addEvent(this.submit,'click',function(){if(_this.valid) _this.setCookie('myquizpolljs_'+_this.pollId,'1');});
	}

	this.showError = function() {
		if(this.valid) {
			this.updateElement(this.error,'');
		} else this.updateElement(this.error,'Bitte w&auml;hlen Sie eine Antwort aus.');
	}

	this.checkCookie = function() {
		var cookie = this.getCookie('myquizpolljs_'+this.pollId);
		if(cookie && cookie=='1') {
			this.removeElement(this.submit);
			this.updateElement(this.error,'Sie haben bereits an dieser Umfrage teilgenommen.');
		} else {
			var h = this.getElement('#poll-results .poll-title');
			//add some wording in to poll-title
			this.updateElement(h,h.innerHTML+': '+'Stimmen Sie ab');
		}
	}
	
	this.validate = function() {
		this.valid = false;
		if(this.answers && this.answers.length>0) {
			for(var i = 0;i<this.answers.length;i++) {
				if(this.answers[i].checked) {
					this.valid = true;
					break;
				}
			}
		}
		if(!this.valid) {
			this.setElementAttr(this.form,'action','javascript:void(0);');
		} else this.setElementAttr(this.form,'action',this.action);
	}
}