$(document).ready(function(){

    b_game_fade_running = false;

    game_rating_clicked = false;

    var game_more_mode = 0;
    var game_more_page = 1;
    var game_more_display = 0;

    var id = parseInt($.url.param('id'));
    var name = $.url.param('name');

    var game_tab_score_cache = '';
    var game_tab_screens_cache = '';
    var game_tab_video_cache = '';

    function applyScorePagesEvents()
    {
        $("#game_detail_more div.pages a").click(function(event) {

            mode = parseInt($("#game_detail_mode").val());
            if (isNaN(mode)) mode = 0;
            display = parseInt($("#game_detail_display").val());
            if (isNaN(display)) display = 0;
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentScorePage = parseInt($("#game_detail_more .pages a.active").text());
            if (isNaN(currentScorePage)) currentScorePage = 1;

            if (currentScorePage == page) {
                event.preventDefault();
                return;
            }

            game_more_mode = mode;
            game_more_display = display;
            game_more_page = page;

            $("#game_detail_more").fadeTo("fast", 0.33, function() {
                $("#b_game_score").append(loadingDiv);
                $.get('?Controller=game&Action=score&id=' + id + '&mode=' + mode + '&display=' + display + '&scorepage=' + page, {}, applyScoreChange);
            });

            event.preventDefault();
        });

        $("#game_detail_mode").change(function() {
            mode = parseInt($(this).val());
            if (isNaN(mode)) mode = 0;
            game_more_mode = mode;
            game_more_page = 1;

            $("#game_detail_more").fadeTo("fast", 0.33, function() {
                $("#b_game_score").append(loadingDiv);
                $.get('?Controller=game&Action=score&id=' + id + '&mode=' + mode + '&display=' + game_more_display, {}, applyScoreChange);
            });
        });

        $("#game_detail_display").change(function() {
            display = parseInt($(this).val());
            if (isNaN(display)) display = 0;
            game_more_display = display;
            game_more_page = 1;

            $("#game_detail_more").fadeTo("fast", 0.33, function() {
                $("#b_game_score").append(loadingDiv);
                $.get('?Controller=game&Action=score&id=' + id + '&mode=' + game_more_mode + '&display=' + display, {}, applyScoreChange);
            });
        });
    }
    applyScorePagesEvents();

    function applyScoreChange(data)
    {
        $("#game_detail_more").slideUp("normal", function() {
            $(this).html(data);
            $(this).slideDown("normal", function() {
                $(this).fadeTo("fast", 1, function() {
                    $(".right .tabbar a .loading").remove();
                    applyScorePagesEvents();
                });
            });
        });
    }

    function applyNewComment(data)
    {
        $("#game_comments_block_wrap .loading").remove();
        $("#game_comments_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                if ($("#game_comment_submit").attr('disabled') != '') $("#game_comment_submit").attr('disabled', '');
                applyCommentPagesEvents();
                $("#game_comments_block_wrap .rating_message").remove();
                $("#game_comments_block_wrap").prepend('<p class="rating_message"><strong>Thank you for your comment!</strong></p>');
                $("#game_comment_text").attr('value', '');
            });
        });
    }

    function applyCommentData(data)
    {
        $("#game_comments_block_wrap .loading").remove();
        $("#game_comments_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                applyCommentPagesEvents();
            });
        });
    }

    function applyCommentPagesEvents()
    {
        $("#itemdetail div.comments div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#game_comments_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#game_comments_block").fadeTo("fast", 0.33, function() {
                $("#game_comments_block_wrap").append(loadingDiv);
                $.get('?Controller=game&Action=comments&id=' + id + '&name=' + name + '&page=' + page, {}, applyCommentData);
            });

            event.preventDefault();
        });
    }
    applyCommentPagesEvents();

    $("#game_comment_submit").click(function() {
        $(this).attr('disabled', 'disabled');
        return game_comment_form_clicked();
    });

    function game_comment_form_clicked()
    {
        text = $("#game_comment_text").attr('value');
        if (text == '') {
            alert('Comment text not supplied!');
            $("#game_comment_text").focus();
            $("#game_comment_submit").attr('disabled', '');
        } else {
            $("#game_comments_block").fadeTo("fast", 0.33, function() {
                $("#game_comments_block_wrap").append(loadingDiv);
                $.post('?Controller=game&Action=postcomment&id=' + id + '&name=' + name, { 'game_comment_text': text }, applyNewComment);
            });
        }

        return false;
    }

    $("#game_rating_form_submit").click(function() {
        $(this).attr('disabled', 'true');
        return game_rating_form_clicked();
    });

    function game_rating_form_clicked()
    {
        vote = parseInt($("#game_rating_form_value").attr('value'));
        if (vote == 0) { alert('No vote selected!'); return false; }

        iid = parseInt($("#game_rating_form_id").attr('value'));
        $.post('?Controller=game&Action=vote&id=' + iid, { 'game_rating_value': vote }, afterVoted );

        return false;
    }

    function afterVoted(data)
    {
        if (data.indexOf('error') == -1) {
            $("#game_rating_data").html(data);
            $("#game_rating_stars").css('opacity', 0.6);
            $("#game_rating_data").prepend('<p class="rating_message"><strong>Thank you for your vote!</strong></p>');
        } else {
            $("#game_rating_data").prepend('<p class="rating_error"><strong>Invalid vote</strong></p>');
        }
    }

    $("#game_rating_stars").css('opacity', 0.6);
    $("#game_rating_stars").hover(
        function() {
            if (!game_rating_clicked) $(this).css('opacity', 1);
        },
        function() {
            if (game_rating_clicked) return;
            $("#game_rating_stars img").attr('src', themePath + 'star_blank.png');
            $(this).css('opacity', 0.6);
        }
    );

    $("#game_rating_stars img").hover(
        function() {
            if (game_rating_clicked) return;
            index = parseInt($(this).attr('alt'));
            $("#game_rating_stars img:gt(" + (index-1) + ")").attr('src', themePath + 'star_blank.png');
            $("#game_rating_stars img:lt(" + index + ")").attr('src', themePath + 'star.png');
        },
        function() {
            if (game_rating_clicked) return;
        }
    );

    $("#game_rating_stars img").click(function() {
        if (!game_rating_clicked) {
            game_rating_clicked = true;
            index = parseInt($(this).attr('alt'));
            $("#game_rating_form_value").attr('value', index);
        } else {
            game_rating_clicked = false;
            $("#game_rating_form_value").attr('value', 0);
        }
    });

    function applyGameInfoChange(data)
    {
        $("#game_detail_more").slideUp("normal", function() {
            $(this).html(data);
            $(this).slideDown("normal", function() {
                $(this).fadeTo("fast", 1, function() {
                    b_game_fade_running = false;
                    $(".right .tabbar a .loading").remove();
                    applyScorePagesEvents();
                });
            });
        });
    }

    function cacheGameTabs()
    {
        if ($("#b_game_score").hasClass("active"))
            game_tab_score_cache = $("#game_detail_more").html();
        else if ($("#b_game_screens").hasClass("active"))
            game_tab_screens_cache = $("#game_detail_more").html();
        else if ($("#b_game_video").hasClass("active"))
            game_tab_video_cache = $("#game_detail_more").html();
    }

    $("#b_game_score").click(function(event){
        if (!$(this).hasClass("active") && !b_game_fade_running) {

            b_game_fade_running = true;

            //cacheGameTabs();
            $(this).removeClass("previous").addClass("active").find("span").addClass("active");
            $("#b_game_screens").removeClass("active").removeClass("previous").addClass("next").find("span").removeClass("active");
            $("#b_game_video").removeClass("next").removeClass("active").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#game_detail_more").fadeTo("fast", 0.33, function() {
                if (game_tab_score_cache != '')
                    applyGameInfoChange(game_tab_score_cache);
                else
                    $.get('?Controller=game&Action=score&id=' + id + '&mode=' + game_more_mode + '&page=' + game_more_page, {}, applyGameInfoChange);
            });

        }

        event.preventDefault();
    });

    $("#b_game_screens").click(function(event){
        if (!$(this).hasClass("active") && !b_game_fade_running) {

            b_game_fade_running = true;

            //cacheGameTabs();
            $(this).removeClass("next").removeClass("previous").addClass("active").find("span").addClass("active");
            $("#b_game_score").removeClass("active").addClass("previous").find("span").removeClass("active");
            $("#b_game_video").removeClass("active").addClass("next").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#game_detail_more").fadeTo("fast", 0.33, function() {
                if (game_tab_screens_cache != '')
                    applyGameInfoChange(game_tab_screens_cache);
                else
                    $.get('?Controller=game&Action=screenshot&id=' + id, {}, applyGameInfoChange);
            });

        }

        event.preventDefault();
    });

    $("#b_game_video").click(function(event){
        if (!$(this).hasClass("active") && !b_game_fade_running) {

            b_game_fade_running = true;

            //cacheGameTabs();
            $(this).removeClass("next").addClass("active").find("span").addClass("active");
            $("#b_game_score").removeClass("active").removeClass("previous").addClass("first").find("span").removeClass("active");
            $("#b_game_screens").removeClass("active").removeClass("next").addClass("previous").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#game_detail_more").fadeTo("fast", 0.33, function() {
                if (game_tab_video_cache != '')
                    applyGameInfoChange(game_tab_video_cache);
                else
                    $.get('?Controller=game&Action=video&id=' + id, {}, applyGameInfoChange);
            });

        }

        event.preventDefault();
    });
});

