function Anketa(idobal, id, options)
{
	
	this.Proxy = function(self, pfunc) { return function() { pfunc.apply(self, arguments); } }

	var defaults = {
		url: "/modules/anketa/anketa_data.php",
		cookieName: "Anketa",
		closed: false,
		thanks: "D\u011bkujeme za Vá\u0161 hlas !",
		error404: "Nepoda\u0159ilo se navázat spojení se serverem !",
		cant_vote: "Nem\u016f\u017eete hlasovat !",
		error_vote: "Neplatné hlasování !"
	};
	this.idobal = $("#"+idobal);
	this.id = id;
	this.options = $.extend({}, defaults, options);



	this.vote = function(index) {
		this.removeEvents();
		$.ajax({
			url: (this.options.url+"?id="+this.id+"&moznost="+index),
			success: this.Proxy(this, function(data){
				if(data == "error") {
					alert(this.options.error_vote);
					return;
				}
				if(data == "cant") {
					alert(this.options.cant_vote);
					return;
				}
				this.idobal.html(data);
				this.addToCookie(index);
				this.setVisibleVote(index);
				var popis = $("#"+idobal+" .popis");
				var temp = popis.html();
				popis.html(this.options.thanks);
				setTimeout(this.Proxy(this, function(){
					popis.html(temp);
				}), 5000);
			}),
			error: this.Proxy(this, function(){
				alert(this.options.error404);
				this.addEvents();
			})
		})
	}


	this.getCookieIndex = function() {
		var cookie = $.cookie(this.options.cookieName);
		if(cookie == null)
			return null;
		var splited = cookie.split(",");
		var ids = new Array();
		var values = new Array();

		for(i=0; i<splited.length; i++) {
			var kv = splited[i].split("=");
			ids[i] = kv[0];
			values[i] = kv[1];
		}
		var index = $.inArray(this.id.toString(), ids);
		if(index < 0)
			return null;
		else
			return values[index];
	}

	this.addToCookie = function(index) {
		var cookie = $.cookie(this.options.cookieName);
		cookie = (cookie == null) ? (this.id + "=" + index) : (cookie + "," + this.id + "=" + index);
		$.cookie(this.options.cookieName, cookie, {expires: 365});
	}

	this.addEvents = function() {
		var progresstext = $("#" + idobal + " .progresstext");
		$(progresstext).each(this.Proxy(this, function(index, obj){
			
			var pt = $(obj);
			var id = pt.attr("id").split("_t_")[1];
			var bar = $( "#" + pt.attr("id").replace("_t_", "_b_") );
			var fClick = this.Proxy(this, function(){
				this.vote(id);
			});
			var fOver = this.Proxy(this, function(){
				bar.addClass("progressbar_hover");
				pt.addClass("progresstext_hover");
			});
			var fOut = this.Proxy(this, function(){
				bar.removeClass("progressbar_hover");
				pt.removeClass("progresstext_hover");
			});
			pt.click(fClick);
			bar.click(fClick);
			pt.mouseover(fOver);
			bar.mouseover(fOver);
			pt.mouseout(fOut);
			bar.mouseout(fOut);
		}));
		
	}

	this.removeEvents = function() {
		var progresstext = $("#" + idobal + " .progresstext");
		var progressbar = $("#" + idobal + " .progressbar");
		$(progresstext).each(this.Proxy(this, function(index, obj){
			var pt = $(obj);
			var bar = $(progressbar[index]);
			pt.mouseout();
			pt.unbind("click");
			bar.unbind("click");
			pt.unbind("mouseover");
			bar.unbind("mouseover");
			pt.unbind("mouseout");
			bar.unbind("mouseout");
		}));
	}

	this.setVisibleVote = function(voteIndex) {
		$("#anketa_" + this.id + "_t_" + voteIndex).addClass("progresstext_voted");
		$("#anketa_" + this.id + "_b_" + voteIndex).addClass("progressbar_voted");
	}

	this.init = function() {
		var voteIndex = this.getCookieIndex();
		if(voteIndex == null && !this.options.closed) {
			this.addEvents();
		} else {
			this.setVisibleVote(voteIndex);
		}
	}

	this.init();
}
