﻿/* News */
var GiantFavoriteAuthors = {
    over: function(divId) {
        $(divId).show();
    },
    out: function(divId) {
        $(divId).hide();
    }
}
function NewsFavoriteAuthorRemove(id) {
    $.ajax('NewsFavoriteAuthorRemove', { postBody: "AuthorID=" + id, onSuccess: function(html, state) {
        if (state) {
            $("ctl00_ctl00_BasicMasterPage_dvFavoriteAuthors").innerHTML = html.getHTML('NewsFavoriteAuthor');
        }
    } 
    });
}
function NewsGiantFavoriteAuthorRemove(id, container) {
    $.ajax('NewsFavoriteAuthorRemove', { postBody: "AuthorID=" + id, onSuccess: function(html, state) {
        if (state) {
            $(container).hide();
        }
    }
    });
}


var NewsEditorPost = {
    charLimit: 130,
    init: function(jsonNewsEditor) {
        Object.extend(this, jsonNewsEditor); // inserted from the server
        $(NewsEditorPost.normalModeContainer).observe('mouseover', function() {
            $(NewsEditorPost.normalModeContainer).addClassName('edit-mode')
        })
        $(NewsEditorPost.normalModeContainer).observe('mouseout', function() {
            $(NewsEditorPost.normalModeContainer).removeClassName('edit-mode')
        })
        $(NewsEditorPost.normalModeContainer).observe('click', function() {
            NewsEditorPost.editMode();
        })
        Event.observe(NewsEditorPost.txtPostId, 'keyup', NewsEditorPost.limitText);
        Event.observe(NewsEditorPost.txtPostId, 'keydown', NewsEditorPost.limitText);

    },
    setPost: function(objectType, objectId, post, postContainer, normalContainer, editContainer) {
        var data = {
            ObjectType: NewsEditorPost.moduleId,
            ObjectID: NewsEditorPost.categoryId,
            Post: $F(NewsEditorPost.txtPostId)
        }
        $.json('SetEditorPost', { url: 'json/jsonNews.aspx?action=', postBody: data, onSuccess: function(json, state) {
            if (state) {
                $(NewsEditorPost.normalModeSpnContainer).update(json.post);
                NewsEditorPost.normalMode();
            }
        }
        })
    },
    editMode: function(normalContainer, editContainer) {
        $(NewsEditorPost.txtPostId).value = $(NewsEditorPost.normalModeSpnContainer).innerHTML.trim();
        $(NewsEditorPost.normalModeContainer).hide();
        $(NewsEditorPost.editModeContainer).show();
        $(NewsEditorPost.txtPostId).focus();
    },
    normalMode: function(normalContainer, editContainer) {
        $(NewsEditorPost.normalModeContainer).show();
        $(NewsEditorPost.editModeContainer).hide();
    },
    limitText: function() {
        if ($F(NewsEditorPost.txtPostId).length > NewsEditorPost.charLimit) {
            $(NewsEditorPost.txtPostId).value = $F(NewsEditorPost.txtPostId).substring(0, NewsEditorPost.charLimit);
        }
    }
}

var MainPoll = {
    Vote: function (pollId, voteBtn, options, answers) {
        selectedAnswerId = this.CheckVoteForPoll(options);

        if (selectedAnswerId) {
            var data = { PollId: pollId, SelectedAnswerId: selectedAnswerId };
            $.json('AnswerMainPoll', { url: 'Json/JsonMain.aspx?action=', postBody: data, onSuccess: function (json, state) {
                if (state) {
                    $(options).hide();
                    $(voteBtn).hide();
                    $(answers).innerHTML = json.MainPollAnswersDisplay;
                    $(answers).show();
                }
            }
            });
        }
    },
CheckVoteForPoll: function (questionsContainerID) {

        var questionID = null;
        $$('#' + questionsContainerID + ' input[type=radio]').each(function (radioBtn, index) {
            if ($(radioBtn).checked) {
                questionID = $(radioBtn).value;
                return false;
            }
        })

        if (questionID == null) {
            $('dvNoAnswer').show();
            Effect.Pulsate('dvNoAnswer', { duration: 1.5, afterFinish: function () { $('dvNoAnswer').hide(); } });
        }
        return questionID;
    }
}
