function get_poll(id, box)
{
	var ajax = new AjaxJSON({
		script: "../poll/poll.php"
	});
	ajax.addEvent("onComplete", function(response){
		if(response.poll){
			build_poll(response, box);
		}
	});
	ajax.make_request({
		action: "get",
		id: id
	}, "../poll/poll.php");
}

function build_poll(data, box)
{
	var box = $(box);
	if(!box){
		return;
	}
	
	new Element("div", {
		"class": "poll-header",
		"html": data.poll["question"]
	}).inject(box);
	var form = new Element("form", {
		"action": "",
		"method": "post"
	}).inject(box);
	var list = new Element("ul").inject(form);
	if(data.variant){
		data.variant.each(function(v){
			var item = new Element("li").inject(list);
			var radio = new Element("input", {
				"type": "radio",
				"value": v["id"],
				"name": "poll-" + data.poll["id"],
				"class": "radio"
			}).inject(item);
			new Element("span", {
				"html": v["variant"]
			}).inject(item);
		});
	}
	//var div = new Element("div").inject(form);
	var vote_btn = new Element("button", {
		"html": "голосовать",
		"class": "vote"
	}).inject(form);
	vote_btn.addEvent("click", function(e){
		e.stop();
		var ajax = new AjaxJSON({
			script: "../poll/poll.php"
		});
		var radio = box.getElement("form")["poll-" + data.poll["id"]];
		var pid = 0;
		if(radio){
			for (var i = 0; i < radio.length; i++) {
				if(radio[i].checked){
					id = radio[i].value;
				}
			}
		}
		ajax.addEvent("onComplete", result);
		ajax.make_request({
			action: "vote",
			pid: data.poll["id"],
			id: id
		});
	});
	
	var res_btn = new Element("button", {
			"html": "результат",
			"class": "vote"
		}).inject(form);
		res_btn.addEvent("click", function(e){
			e.stop();
			var ajax = new AjaxJSON({
				script: "../poll/poll.php"
			});
			ajax.addEvent("onComplete", result);
			ajax.make_request({
				action: "res",
				pid: data.poll["id"]
			});
		});
}

function result(response){
			if(response.variant){
				var chd = [];
				var chdl = [];
				var chl = [];
				var chco = [];
				for(var i = 0; i < response.variant.length; i++){
					response.variant[i]["votes"] = (response.variant[i]["votes"] / response.sum * 100).toFixed(1);
					chd.push(response.variant[i]["votes"]);
					chl.push(response.variant[i]["votes"] + "%");
					chdl.push(response.variant[i]["variant"]);
					chco.push(response.variant[i]["color"].substr(1));
				}
				
				chd = "t:" + chd.join(",");
				chdl = encodeURIComponent(chdl.join("|"));
				//chdl = encodeURIComponent("kzkzkkz kjdssds sdf\n|sdsdfdsf sduhsfd|lsdfldsfjl sldk\nsfsdds|sdfsdfsd");
				chl = chl.join("|");
				chco = chco.join("|");
				
				var uri = "http://chart.apis.google.com/chart?cht=p3&chma=50,50,50,50&chdlp=bv|l&chs=500x450&chd="+chd+"&chdl="+chdl+"&chl="+chl+"&chco="+chco;
				
				var img = new Asset.image(uri);
				//box.empty();
				
				var modal = new ModalBox("popup", "overlay");
				modal.show();
				modal.popup.adopt(img);
				
				var div = new Element("div").inject(modal.popup);
				div.setStyles({
					"width": "100%",
					"margin-top": "20px",
					"text-align": "center"
				});
				var close_btn = new Element("button", {
					"html": "Закрыть",
					"class": "close_btn"
				}).inject(div);
				close_btn.addEvent("click", function(e){
					modal.destroy();
				})
			}
		}

var AjaxJSON = new Class({
	Implements: [Events, Options, Class.Binds],
	Binds: ["make_request"],
	options: {
		script: "",
		onRequest: Class.empty,
		onComplete: Class.empty,
		onFailure: Class.empty
	},
	initialize: function(options){
		this.setOptions(options);
	},
	
	make_request: function(data)
	{
		if(!this.options.script)
		{
			return;
		}
		
		//запрос
		new Request.JSON({
            url: this.options.script,
			onRequest: function(){
				this.fireEvent('onRequest', null);
			}.bind(this),
            onComplete: function(response){
				this.fireEvent('onComplete', [response])
			}.bind(this),
            onFailure: function(){
				this.fireEvent('onFailure', null);
            }.bind(this)
        }).post(data);
	}
});

var ModalBox = new Class({
	Implements: [Options, Events, Class.Binds],
	Binds: ["hide"],
	options: {
		overlay_opacity: 0.7
	},

	initialize: function(popup, overlay, options)
	{
		this.setOptions(options);

		this.popup = this.create_popup(popup);
		this.overlay = this.create_overlay(overlay);
		
		this.overlay.addEvent("click", this.hide);
		
		this.coords = this.popup.getCoordinates();

		this.fx = {
			overlay_fx: new Fx.Tween(this.overlay, {
				property: "opacity"
			}),
			popup_fx: new Fx.Tween(this.popup, {
				property: "opacity"
			})
		}
	},
	
	create_overlay: function(id) {
		return new Element('div', {
			"id": id
		}).inject(document.body, "top");
	},
	
	create_popup: function(id) {
		return new Element("div", {
			"id": id
		}).inject(document.body, "top");
	},	

	show: function()
	{
		this.set_position();
		this.popup.empty();
		this.fx.overlay_fx.start(0, this.options.overlay_opacity);
		this.fx.popup_fx.start(0, 1);
		
		/*var div = new Element('div', {
			'html': content
		}).inject(this.popup);
		*/
	},

	hide: function()
	{
		this.fx.overlay_fx.start(this.options.overlay_opacity, 0);
		this.fx.popup_fx.start(1, 0);
	},

	set_position: function()
	{
		this.popup.setStyles({
			"marginLeft": -(this.coords.width / 2) + "px",
			"marginTop": -(this.coords.height / 2) + "px"
		});
	},
	
	destroy: function(){
		this.hide();
		this.popup.dispose();
		this.overlay.dispose();
	}
});
