var mooPoll = new Class({
	Implements : [ Events, Options ],
	options: {
		data: false,	
		votes: false, // Mostrar No. total de votos
		percents: true, // Mostrar Porcentajes
		autocolor: false, // Colores al azar	
		fx: { 'duration' : 1250,'transition' : Fx.Transitions.Back.easeInOut,'opacity': 0 }
	},
	initialize: function(container, options){
		//Setting up
		this.setOptions(options);
		
		if($(container)){
			this.poll = $(container);
			this.poll_id = container.split('_')[1];
		} else return false;
		
		if($(container)){ this.poll = $(container); this.bars_container = this.poll.getElement('.poll_options'); }else return false;
		if(!this.options.data) return false;

		this.w = this.bars_container.getSize().x;
		
		this.links = this.poll.getElements('.poll_option a');
		this.setLayout();
		this.bars = this.poll.getElements('.poll_bar');
		
		this.poll.getElement('.poll_results_button').addEvent('click',function(){ this.seeResults(); }.bind(this));
	},
	setLayout:function(){
		var fxoptions = {
			duration: this.options.fx.duration,
			transition: this.options.fx.transition
		}

		this.links.each(function(el){
			var temp = el.getProperty('id').split('_');
			var opt_id = temp[1].substr(1);
			
			el.addEvent('click',function(e){ e = new Event(e); this.vote(opt_id); }.bindWithEvent(this));
			
			var bar_style = {
				position:'absolute',
				top:0,
				left:0,
				width:5,
				height:el.getParent().getSize().y,
				opacity:this.options.fx.opacity
			};
			bar_style = this.options.autocolor?$merge(bar_style,{ 'background-color':$RGB($random(95,255), $random(95,255), $random(95,255))}):bar_style;
			new Element('div',{ id:'p'+this.poll_id+'_o'+opt_id+'_bar', 'class':'poll_bar', styles:bar_style }).inject(el.getParent(),'top').set('morph',fxoptions);
			
		}.bind(this));
		
	},
	vote:function(opt_id){ 
		this.bars_container.fade('0.3').get('tween').chain(function(){
			new Request({
				url:'/polls/vote/poll_id:'+this.poll_id+'/opt_id:'+opt_id+'/?isAjax=1',
				method:'get', encoding:'iso-8859-1', evalScripts:true, onComplete:function(){ this.bars_container.fade('in').get('tween').chain(function(){ this.seeResults();this.links.removeEvents(); }.bind(this)); }.bind(this)
			}).send();
		}.bind(this));
	},
	seeResults:function(){
		this.bars_container.getElements('span').destroy();
		this.data = $H(this.options.data);
		//valor máximo
		var maxval = this.data.getValues().max();
		var totalvotes = this.data.getValues().sum();
		
		this.data.each(function(votes,popid){
			// Validar si existe la barra de esa opción y en caso que sí, animarla
			if(!$('p'+this.poll_id+'_o'+popid+'_bar')) return true;
			else {
			
				if(this.options.percents) new Element('span',{ html:' '+(totalvotes?(votes/totalvotes*100).toInt():0)+'%',styles:{ opacity:0 }}).inject($('p'+this.poll_id+'_o'+popid),'after').tween('opacity',1);
				if(this.options.votes) new Element('span',{ html:' ('+votes+')',styles:{ opacity:0 }}).inject($('p'+this.poll_id+'_o'+popid),'after').tween('opacity',1);
				$('p'+this.poll_id+'_o'+popid+'_bar').morph({'width':(votes*this.w/maxval).toInt(),'opacity':1});
			}
			
		}.bind(this));
	}
});