﻿/**
 * 
 * voting need enter - event
 */

// m-questions
function Voting (container, mag) {
    this.isOpened = false;
    this._container = container;
    this._mag = mag;
    /**
    * селектор галочки "проголосовать"
    */
    this._CHECK = '.label_check';
    this._bind();
}

Voting.prototype = {
    /**
     * @param {Number} page страница опросов 
     */
    show : function () {
        if (!this.isOpened && !account.isLogin) this.showTips();
        this.isOpened = true;
        var that = this;
        $.ajax({
            url : "/ajax/return_all_votings/",
            type : 'get',
            success : function (html) {
                that._container.html(html);

                
                
                that._container.find('.vote_button').click(function (event) {
                    var vote = $(this).closest('.vote_div');
                    var radio = vote.find(':checked');
                    if (radio.length) {
                        $.ajax({
                            type : 'post',
                            dataType : 'html',
                            url : '/ajax/question_vote/',
                            data : {
                                object_id : vote.data('id'),
                                rate : radio.val()
                            },
                            success : function (html) {
                                if (html == 1) {
                                   // mMessageBox.alert('Для участия в голосовании необходимо выполнить <b class="votingNeedEnter messageBoxLink pointer">вход</b> или <b class="votingNeedEnter messageBoxLink toEnter pointer">зарегистрироваться</b>');
                                    account.showCabinet(true);
                                
                                } else if (html == 2) {
                                    mMessageBox.alert('Вы уже принимали участие в этом опросе');                               
                                } else {
                                    vote.children().not('.vote_div_bottom').fadeOut(200);
                                    setTimeout(function() {
                                        vote.children().not('.vote_div_bottom').remove();
                                        vote.prepend($(html).unwrap().children().not('.vote_div_bottom'));
                                        vote.children().not('.vote_div_bottom').fadeIn(200);
                                    }, 220);
                                }
                            }
                        });
                    }
                });
                
                
                that._container.find(that._CHECK).click(function(){
                    $(this).siblings(that._CHECK).removeClass('c_on');
                    $(this).addClass('c_on').find('input:checked');
                });
                
                that._container.find('.vote_div_tools a').click(function() {
                    that._shareVK();
                    return false;
                });
            }
        });
    },
    
    showTips: function() {
        $(this._CHECK).mmTooltip();
    }, 
    
    hideTips: function() {
        $(this._CHECK).mmTooltipDestroy();
    },
    
    _shareVK: function() {
        window.open('http://vkontakte.ru/share.php?url=http://21mm.ru/%23voting&title=Опросы на сайте 21мм&image=http://21mm.ru/media/images/votingVK.png', 'vkontakte', 'width=626, height=436'); 
        
    },

    hide : function () {
        this._module.hide().empty();
        this.isOpened = false;
    },

    _bind : function () {
        var that = this;
        $('.votingNeedEnter').live('click', function () {
            mMessageBox.close();
            coreNotice('voting need enter');
        });
        
        $('.voting_pagination span:not(".current, .vertical")').live('click', function () {
           $('.voting_pagination .current').removeClass('current');
           var page = +$(this).addClass('current').text();
           $('#voting_column_container').css('top', -725 * (page - 1) + 'px');
        });
    },

    bind : function (eventType, handler) {
        $.fn.bind.apply(this._module, arguments);
    }
};
