$(document).ready(function(){

    var id = parseInt($.url.param('id'));
    var name = $.url.param('name');

    var group_more_top_page = 1;
    var group_more_all_page = 1;

    var group_more_compare = 0;

    var group_tab_top_cache = '';
    var group_tab_members_cache = '';

    function applyMembersPagesEvents()
    {
        $("#group_detail_more div.pages a").click(function(event) {

            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentScorePage = parseInt($("#group_detail_more .pages a.active").text());
            if (isNaN(currentScorePage)) currentScorePage = 1;

            if (currentScorePage == page) {
                event.preventDefault();
                return;
            }

            $("#group_detail_more").fadeTo("fast", 0.33, function() {

                if ($("#b_group_top").hasClass("active")) {
                    group_more_top_page = page;

                    $("#b_group_top").append(loadingDiv);
                    $.get('?Controller=country&Action=topmembers&id=' + id + '&name=' + name + '&compare=' + group_more_compare + '&topmemberspage=' + page, {}, applyMembersChange);
                } else if ($("#b_group_all").hasClass("active")) {
                    group_more_all_page = page;

                    $("#b_group_all").append(loadingDiv);
                    $.get('?Controller=country&Action=members&id=' + id + '&name=' + name + '&compare=' + group_more_compare + '&memberspage=' + page, {}, applyMembersChange);
                }

            });

            event.preventDefault();
        });

        $("#group_members_compare").change(function() {
            compare = parseInt($(this).val());
            if (isNaN(compare)) compare = 0;
            group_more_compare = compare;
            group_more_page = 1;

            $("#group_detail_more").fadeTo("fast", 0.33, function() {
                $("#b_group_all").append(loadingDiv);
                $.get('?Controller=country&Action=members&id=' + id + '&name=' + name + '&compare=' + compare, {}, applyMembersChange);
            });
        });
    }
    applyMembersPagesEvents();

    function applyMembersChange(data)
    {
        $("#group_detail_more").slideUp("normal", function() {
            $(this).html(data);
            $(this).slideDown("normal", function() {
                $(this).fadeTo("fast", 1, function() {
                    $(".right .tabbar a .loading").remove();
                    applyMembersPagesEvents();
                });
            });
        });
    }

    function applyWallData(data)
    {
        $("#group_wall_wrap .loading").remove();
        $("#group_wall").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                applyWallPagesEvents();
            });
        });
    }

    function applyWallPagesEvents()
    {
        $("#itemdetail div.wall div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#group_wall .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#group_wall").fadeTo("fast", 0.33, function() {
                $("#group_wall_wrap").append(loadingDiv);
                $.get('?Controller=country&Action=wall&id=' + id + '&name=' + name + '&wallpage=' + page, {}, applyWallData);
            });

            event.preventDefault();
        });
    }
    applyWallPagesEvents();

    function applyNewWall(data)
    {
        $("#group_wall_wrap .loading").remove();
        $("#group_wall").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                if ($("#group_wall_submit").attr('disabled') != '') $("#group_wall_submit").attr('disabled', '');
                applyWallPagesEvents();
                $("#group_wall_wrap .success_message").remove();
                $("#group_wall_wrap").prepend('<p class="success_message"><strong>Thank you for your message!</strong></p>');
                $("#group_wall_text").attr('value', '');
            });
        });
    }

    $("#group_wall_submit").click(function() {
        $(this).attr('disabled', 'disabled');
        return group_wall_form_clicked();
    });

    function group_wall_form_clicked()
    {
        text = $("#group_wall_text").attr('value');
        if (text == '') {
            alert('Message text not supplied!');
            $("#group_wall_text").focus();
            $("#group_wall_submit").attr('disabled', '');
        } else {
            $("#group_wall").fadeTo("fast", 0.33, function() {
                $("#group_wall_wrap").append(loadingDiv);
                $.post('?Controller=country&Action=postwall&id=' + id + '&name=' + name, { 'group_wall_text': text }, applyNewWall);
            });
        }

        return false;
    }

    function applyCommentData(data)
    {
        $("#group_comments_block_wrap .loading").remove();
        $("#group_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($("#group_comments_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#group_comments_block").fadeTo("fast", 0.33, function() {
                $("#group_comments_block_wrap").append(loadingDiv);
                $.get('?Controller=country&Action=comments&id=' + id + '&name=' + name + '&page=' + page, {}, applyCommentData);
            });

            event.preventDefault();
        });
    }
    applyCommentPagesEvents();

    function applyNewComment(data)
    {
        $("#group_comments_block_wrap .loading").remove();
        $("#group_comments_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                if ($("#group_comment_submit").attr('disabled') != '') $("#group_comment_submit").attr('disabled', '');
                applyCommentPagesEvents();
                $("#group_comments_block_wrap .success_message").remove();
                $("#group_comments_block_wrap").prepend('<p class="success_message"><strong>Thank you for your comment!</strong></p>');
                $("#group_comment_text").attr('value', '');
            });
        });
    }

    $("#group_comment_submit").click(function() {
        $(this).attr('disabled', 'disabled');
        return group_comment_form_clicked();
    });

    function group_comment_form_clicked()
    {
        text = $("#group_comment_text").attr('value');
        if (text == '') {
            alert('Comment text not supplied!');
            $("#group_comment_text").focus();
            $("#group_comment_submit").attr('disabled', '');
        } else {
            $("#group_comments_block").fadeTo("fast", 0.33, function() {
                $("#group_comments_block_wrap").append(loadingDiv);
                $.post('?Controller=country&Action=postcomment&id=' + id + '&name=' + name, { 'group_comment_text': text }, applyNewComment);
            });
        }

        return false;
    }

    $("#b_group_top").click(function(event){
        if (!$(this).hasClass("active")) {

            //cacheGameTabs();
            $(this).removeClass("next").removeClass("previous").addClass("active").find("span").addClass("active");
            $("#b_group_all").removeClass("active").addClass("next").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#group_detail_more").fadeTo("fast", 0.33, function() {
                if (group_tab_top_cache != '')
                    applyMembersChange(group_tab_top_cache);
                else
                    $.get('?Controller=country&Action=topmembers&id=' + id + '&name=' + name + '&topmemberspage=' + group_more_top_page, {}, applyMembersChange);
            });

        }

        event.preventDefault();
    });

    $("#b_group_all").click(function(event){
        if (!$(this).hasClass("active")) {

            //cacheGameTabs();
            $(this).removeClass("next").addClass("active").find("span").addClass("active");
            $("#b_group_top").removeClass("active").addClass("previous").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#group_detail_more").fadeTo("fast", 0.33, function() {
                if (group_tab_members_cache != '')
                    applyMembersChange(group_tab_members_cache);
                else
                    $.get('?Controller=country&Action=members&id=' + id + '&name=' + name + '&memberspage=' + group_more_all_page, {}, applyMembersChange);
            });

        }

        event.preventDefault();
    });

});

