﻿$.fn.pluginPolls = function(options) {
    var opts = $.extend({}, $.fn.pluginPolls.defaults, options);
    return this.each(function() {
        var $this = $(this);
        $.fn.pluginPolls.init($this, opts);
    });
};

$.fn.pluginPolls.defaults = {
	postHref: HTTP_ROOT + '_plugins/polls/',
	bar_max_width: 200,
	msg_hide: 2000
};

$.fn.pluginPolls.init = function($this, opts) {
	// Vote
	$this.find('.btns input').click(function(e) {
    		$btn = $(this);
    		e.preventDefault();
    		var $answer = $this.find('li input:checked');
    		if ($answer.size() > 0) {
    			var answer = $answer.val();
    			$btn.val('Uzgaidiet...');
    			$btn.attr('disabled', 'disabled');
    			$.fn.pluginPolls.vote($this, opts, answer);
    		}else {
    			$.fn.pluginPolls.showMsg($this, opts, 'Nav izvēlēta atbilde!');
    		}
    	});
    // Change View
    $this.find('.btns .view').click(function(e) {
    		e.preventDefault();
    		if ($(this).attr('href') == '#results') {
    			$.fn.pluginPolls.chView($this, opts, 'results');
    		}else {
    			$.fn.pluginPolls.chView($this, opts, 'vote');
    		}
    	});
}

$.fn.pluginPolls.showMsg = function($this, opts, msg) {
	$this.find('.msg').fadeTo(100, 1, function() {
			$(this).css('display', 'block');
			$(this).html(msg);
			opts.t = setTimeout(function() {
					$.fn.pluginPolls.hideMsg($this, opts);
				}, opts.msg_hide);
		});
}

$.fn.pluginPolls.hideMsg = function($this, opts) {
	$this.find('.msg').fadeTo(1000, 0.1, function() {
			clearTimeout(opts.t);
			$(this).css('display', 'none');
		});
}

$.fn.pluginPolls.vote = function($this, opts, answer) {
    $answ_block = $this.find('.plugin_poll_cont ul');
    $answ_block.fadeTo(600, 0.3, function() {
        $.ajax({
            url: opts.postHref + 'vote.ajax.php',
            type: 'POST',
            data: 'answer=' + answer + '&bar_max_width=' + opts.bar_max_width,
            complete: function(req) {
                if (req.status == 200) { //success
                	var txt = req.responseText.split('|');
                	if (txt[0] != 'err') {
                		$this.find('.plugin_poll_cont').html(req.responseText);
                		$.fn.pluginPolls.init($this, opts);
	                }else {
	                	$.fn.pluginPolls.showMsg($this, opts, txt[1]);
	                	$answ_block.fadeTo(600, 1);
	                	$btn = $this.find('.btns input');
	                	$btn.val('Balsot');
	                	$btn.removeAttr('disabled');
	                }
                } else { //failure
                    $.fn.pluginPolls.showMsg($this, opts, req.responseText);
                	$answ_block.fadeTo(600, 1);
                	$btn = $this.find('.btns input');
                	$btn.val('Balsot');
                	$btn.removeAttr('disabled');
                }
            }
        });
    });
};

$.fn.pluginPolls.chView = function($this, opts, view) {
	var poll_id = $this.find('input').val();
	$cont = $this.find('.plugin_poll_cont');
	$cont.fadeTo(600, 0.4, function() {
        $.ajax({
            url: opts.postHref + 'cont.ajax.php',
            type: 'POST',
            data: 'poll_id=' + poll_id + '&view=' + view + '&bar_max_width=' + opts.bar_max_width,
            complete: function(req) {
                if (req.status == 200) { //success
                	$cont.html(req.responseText);
                	$cont.fadeTo(600, 1);
                	$.fn.pluginPolls.init($this, opts);
                } else { //failure
                    $.fn.pluginPolls.showMsg($this, opts, req.responseText);
                	$cont.fadeTo(600, 1);
                }
            }
        });
    });
}