/**
 * Класс для работы с фотоконкурсом
 * @requires jQuery
 * @namespace RadioCon
 */

RadioCon.Contest = function() {

    var $wrap = $();


    function _init() {
        $wrap.find(".p-img a").FBox({
            animation : true,
            showOverlay : true,
            overlayOpacity : .3,
            overlayClickClose : true,
            navigationButtons : true,
            imagesList : false,
            useLoaderAnimation : true,
            loaderParams : {
                animationLength : 60,
                animationStep : 5
            }
        });
    }

    function vote(el, objectId, hash) {
        if (el._voting)
            return;
        $.post(
            RadioCon.AJAX_URI,
            {
                '_do' : 'vote_photo',
                'object_id' : objectId
            },
            function(data) {
                updateVoteElement(el, objectId, hash, data);
            },
            "json"
        )
    }

    function _submitForm(form,el, objectId, hash){
        $.post(
            RadioCon.AJAX_URI,
            {
                '_do' : 'vote_photo',
                'object_id' : objectId,
                'hash'      : hash,
                '__captcha_answer': form.__captcha_answer.value
            },
            function(data) {
                updateVoteElement(el, objectId, hash, data);
            },
            "json"
        )
    }

    function updateVoteElement(el, objectId, hash, data) {
        if(data.updated){
            el._voting = true;
            if (data.updated == objectId) {

                $(el.parentNode).find(".pv-cnt").html(data.votes);

                $(document.createElement("div"))
                    .addClass("voted")
                    .html('Ваш голос принят')
                    .appendTo(el.parentNode);
                $(el).remove();
            }
        }else{
            if(data.error){
                var text = ""
                switch(data.error){
                    case 1:
                        text = "Неверно введен ответ на контрольный пример";
                    break;
                    case 2:
                        text = "Вы уже голосовали за эту фотографию";
                    break;
                }
                var fbox= new FBox({
                                animation: true,
                                showOverlay: true,
                                navigationButtons: false,
                                navigationButtonsHover: false,
                                html: "<div class='voted-error text'>"+text+"</div>",
                                constraints: {
                                    minWidth: 200,
                                    minHeight: 100,
                                    maxWidth: 200,
                                    maxHeight: 100
                                }
                            });
                fbox.show();
            }else{
                if(data.html){
                    var fbox= new FBox({
                                animation: true,
                                showOverlay: true,
                                navigationButtons: false,
                                navigationButtonsHover: false,
                                html: data.html,
                                constraints: {
                                    minWidth: 200,
                                    minHeight: 100,
                                    maxWidth: 200,
                                    maxHeight: 100
                                }
                            });
                    fbox.show();

                    $(".fb-content form").submit(function(){
                                                        fbox.hide();
                                                        _submitForm(this, el,objectId, hash );
                                                        return false;
                                                    });
                }
            }
        }
    }



    return {
        init : function() {
            $wrap = $('#photoes');
            if ($wrap.length)
                _init();
        },
        vote: vote
    }
}()

