Enjin_Module_Poll = function(options) {
	this.init(options);
}

Enjin_Module_Poll.__instances = {};
Enjin_Module_Poll.getInstance = function(preset_id) {
	return Enjin_Module_Poll.__instances[preset_id];
}

Enjin_Module_Poll.prototype = {
	options: null,
	preset_id: null,
	
	el_main: null,
	el_title: null,
	el_more_info: null,
	el_view: null,
	el_view_items: null,
	el_voted: null,
	el_voted_items: null,
	
	el_btn_results: null,
	el_btn_back: null,
	view_rendered: false,
	votes_rendered: false,
	vote_view: false,
	
	getFirstPoll: function() {
		return 0;
	/*
		var fkey = null;
		var i=0;
		for (i=0; i<this.options.polls.length; i++) {
			if (!fkey)
				fkey = i;
			
			if (this.options.polls[i].can_vote)
				return i;
		}
		
		return 0;*/
	},	
	
	init: function(options) {
		options = $.extend({
			preset_id: 0,
			polls: [],
			current_index: null,
			data: {}
		}, options);
		this.options = options;
			
		if (!this.options.current_index)
			this.options.current_index = this.getFirstPoll();
		
		this.preset_id = options.preset_id;
		this.params = options;
		
		this.params.data.option_results = parseInt(this.params.data.option_results);
		this.params.data.option_results_vote = parseInt(this.params.data.option_results_vote);		
		Enjin_Module_Poll.__instances[this.preset_id]= this;
		
		//prepare titles
		this.el_main = $('.m_poll-'+this.preset_id);
		this.el_title = this.el_main.find('.panel-title .title');
		this.el_more_info = this.el_main.find('.panel-title .more-info');
		
		this.el_view = this.el_main.find('.panel-data .panel-view');
		this.el_view_items = this.el_main.find('.panel-data .panel-view .panel-items');

		this.el_voted = this.el_main.find('.panel-data .panel-voted');
		this.el_voted_items = this.el_main.find('.panel-data .panel-voted .panel-items');
		
		this.el_btn_results = this.el_main.find('.panel-view .panel-info .button-results');		
		this.el_btn_back = this.el_main.find('.panel-voted .panel-info .button-back');		
		
		this.el_nav = this.el_main.find('.panel-nav, .nav-separator');
		this.el_nav_next = this.el_main.find('.panel-nav .right');
		this.el_nav_previous = this.el_main.find('.panel-nav .left');
		
		if (this.params.data.option_results)
			this.vote_view = true;
		
		this.el_more_info.hide();
		if (this.params.polls.length > 0) {
			this.render(true);
		}
	},
	
	getPoll: function() {
		var poll = this.options.polls[this.options.current_index];
		poll.option_results_vote = parseInt(poll.option_results_vote);
		poll.option_multiple = parseInt(poll.option_multiple);
		poll.option_close = parseInt(poll.option_close);
		poll.option_close_days = parseInt(poll.option_close_days);
		poll.number_votes = parseInt(poll.number_votes);
		
		if (this.options.data.option_results_vote) //force
			poll.option_results_vote = 1;
		
		return poll;
	},
	
	render: function(keep_same_screen) {		
		var poll = this.getPoll();
		var mode_view = true;
		
		//generate options
		this.el_title.text(poll.question);
		this.el_more_info.hide();
		
		if (keep_same_screen) {
			mode_view = !this.vote_view;
		}
		
		this.renderView();
		
		if (poll.option_close_time != '') {
			this.el_more_info.text("Poll closes in "+poll.option_close_time);
			this.el_more_info.show();
		}
		if (mode_view && poll.can_vote) {	
			this.el_view.show();
			this.el_voted.hide();
			
						
		} else {
			//render votes
			this.renderVotes();
			
			this.el_view.hide();
			this.el_voted.show();
		}
		
		this.updateNav();
	},
	
	renderView: function() {
		if (this.view_rendered)
			return;
		
		var poll = this.getPoll();
		
		//check if can see the results first
		if (poll.option_results_vote)
			this.el_btn_results.hide();
		else
			this.el_btn_results.show();
		
		
		//display item options
		var html = '';
		var multiple = poll.option_multiple;
		var el_items = this.el_view_items;
		
		el_items.html('');
		jQuery.each(poll.answers, function(key, value) {
			var template = $('#m-poll-template-view-item').clone().removeAttr('id');
			var opt = '';
			
			if (multiple)
				opt = '<input optkey="'+key+'" type="checkbox" name="opt-'+key+'" />';
			else
				opt = '<input type="radio" name="opts" value="'+key+'" />';
			
			template.find('.input').html(opt);
			template.find('.msg').text(value.option_value);
			template.appendTo(el_items);
		});
		
		this.view_rendered = true;
	},
	
	renderVotes: function() {
		if (this.votes_rendered)
			return; //save mem/cpu
		
		
		var i;
		var poll = this.getPoll();
		var answers = poll.answers;
		var self = this;
		
		this.el_voted_items.html(''); //clear
		jQuery.each(answers, function(key, value) {
			self.renderVote(value.option_value, value.percent);
		});	
		
		//set the number of votes
		this.el_voted.find('.panel-info .count-users').hide();
		
		if (poll.number_votes > 0) {
			this.el_voted.find('.panel-info .count-users').show();		
			this.el_voted.find('.panel-info .count-users .number').html(poll.number_votes);
			
			this.el_voted.find('.panel-info .count-users .plural').toggle(poll.number_votes > 1);
		}
		
		this.el_voted.find('.panel-info .button-back').toggle(poll.can_vote);
		this.votes_rendered = true;
	},
	
	results: function() {
		this.vote_view = true;
		this.renderVotes();
		
		this.el_view.hide();
		this.el_voted.show();
	},
	
	voteBack: function() {
		this.vote_view = false;
		
		//just show the view panel
		this.el_view.show();
		this.el_voted.hide();
	},
	
	renderVote: function(title, percent) {
		var template = $('#m-poll-template-voted-item').clone().removeAttr('id');
		
		template.find('.label').html(title);
		template.find('.percent-number').html(percent+'%');
		
		if (percent > 0) {
			template.find('.pl').addClass('selected');
			
			if (percent < 100) {
				//add also separator
				
				template.find('.pc').append('<div class="pseparator"><!--  --></div>');
			}
		}
		
		//set percent
		template.find('.pc .selected').css('width', percent+'%');
		
		if (percent == 100) {
			//enable the other style
			template.find('.pr').addClass('selected');
		}
		
		template.appendTo(this.el_voted_items);
	},
	
	updatePoll: function(poll, index) {
		if (!index)
			index = this.options.current_index;
		
		this.options.polls[index] = poll;
		
		if (index == this.options.current_index) {
			this.votes_rendered = false;
			this.view_rendered = false;
		}
		
		//"refresh"
		this.options.current_index = index;		
		this.render();
	},
	
	vote: function() {
		var poll = this.getPoll();
		if (!poll.can_vote) {
			Enjin_Core.alert('You cannot vote')
			return;
		} 
		
		var voted = false;
		var data = {
			op: 'vote',
			preset_id: this.preset_id,
			poll_id: poll.poll_id
		};
		
		if (poll.option_multiple) {
			var index = 0;
			this.el_view_items.find(':checked').each(function() {
				data['opt['+index+']'] = $(this).attr('optkey');
				voted = true;
				index++;
			});
		} else {
			data.opid = this.el_view_items.find('input[name=opts]:checked').val();
			if (data.opid)
				voted = true;
		}
		
		if (!voted) {
			Enjin_Core.alert('You must select an option to vote');
		} else {
			var self = this;
			$.post('/ajax.php?s=poll', data, function(response) {
				if (response.error) {
					Enjin_Core.alert(response.message);
				} else {
					self.updatePoll(response.poll);
				}
			}, 'json');
		}
	},
	
	//update navigation through current index
	updateNav: function() {
		var index = this.options.current_index;
		
		//disable by default
		this.el_nav.hide();
		this.el_nav_next.hide();
		this.el_nav_previous.hide();
		
		if (index > 0) {
			this.el_nav_previous.show();
			this.el_nav.show();
		}
		
		if (index < this.options.polls.length - 1) {
			this.el_nav_next.show();
			this.el_nav.show();
		}
		
	},
	
	linkNext: function() {
		var index = this.options.current_index + 1;
		if (index < this.options.polls.length) {
			this.options.current_index = index;
			this.votes_rendered = false;
			this.view_rendered = false;
			this.render(true);
		}
	},
	
	linkPrevious: function() {
		var index = this.options.current_index - 1;
		if (index >= 0) {
			this.options.current_index = index;
			this.votes_rendered = false;
			this.view_rendered = false;
			this.render(true);
		}
	}
}
