$(document).ready(function(){

    var t_top_user_content = '';
    var t_top_game_content = '';

    var banner_active_id = 1;
    var banner_data = new Array();

    function switchBanner(data)
    {
        $("#banner_item").fadeTo("slow", 0.33, function() { $(this).html(data); $(this).fadeTo("slow", 1); });
    }

    $("#banner p.links a").click(function(event){

        num = parseInt($(this).text());
        br = false;
        if (num == banner_active_id) {
            br = true;
        }

        if (!br && banner_data[num] != undefined) {
            switchBanner(banner_data[num]);
            br = true;
        }

        $("#banner p.links a:eq("+(banner_active_id-1)+")").removeClass('active');
        $(this).addClass('active');

        if (br) {
            banner_active_id = num;
            event.preventDefault();
            return;
        }

        if (banner_data[banner_active_id] == undefined)
            banner_data[banner_active_id] = $("#banner_item").html();

        bannerid = $(this).attr('id').split('_');
        id = parseInt( bannerid[2] );

        $.get("?Controller=game&Action=banneritem&id=" + id, {}, switchBanner);

        banner_active_id = num;

        event.preventDefault();
    });

    function switchTopGameContent(data)
    {
        //t_top_game_content = $("#t_top_game").html();
        $("#t_top_game").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() { $(this).fadeTo("fast", 1) }) });
        $("#topgame .content .loading").remove();
    }

    function switchTopContent(data)
    {
        //t_top_user_content = $("#t_top_user").html();
        $("#t_top_user").slideUp("normal", function() { $(this).empty().html(data).slideDown("normal", function() { $(this).fadeTo("fast", 1) }) });
        $("#topuser .loading").remove();
    }

    $("#b_top_user").click(function(event){
        if (!$(this).hasClass("active")) {
            $(this).removeClass("previous").addClass("active");
            $("#b_top_group span").removeClass("active");
            $("#b_top_user span").addClass("active");
            $("#b_top_group").removeClass("active").addClass("next");

            $("#t_top_user").fadeTo("fast", 0.33);
            $("#topuser").append(loadingDiv);

            if (t_top_user_content != '') {
                //tmp = $("#t_top_user").html();
                //switchTopContent(t_top_user_content);
                //t_top_user_content = tmp;
            } else {
                $.get("?Controller=user&Action=top", {}, switchTopContent);
            }
        }

        event.preventDefault();
    });

    $("#b_top_group").click(function(event){
        if (!$(this).hasClass("active")) {
            $(this).removeClass("next").addClass("active");
            $("#b_top_user span").removeClass("active");
            $("#b_top_group span").addClass("active");
            $("#b_top_user").removeClass("active").addClass("previous");

            $("#t_top_user").fadeTo("fast", 0.33);
            $("#topuser").append(loadingDiv);

            if (t_top_user_content != '') {
                //tmp = $("#t_top_user").html();
                //switchTopContent(t_top_user_content);
                //t_top_user_content = tmp;
            } else {
                $.get("?Controller=group&Action=top", {}, switchTopContent);
            }
        }

        event.preventDefault();
    });

    $("#b_top_game").click(function(event){
        if (!$(this).hasClass("active")) {
            $(this).removeClass("previous").addClass("active");
            $("#b_top_game span").addClass("active");
            $("#b_top_10").removeClass("active").addClass("next");

            $("#t_top_game").fadeTo("fast", 0.33);
            $("#topgame .content").append(loadingDiv);

            if (t_top_game_content != '') {
                //tmp = $("#t_top_game").html();
                //switchTopGameContent(t_top_game_content);
                //t_top_game_content = tmp;
            } else {
                $.get("?Controller=game&Action=top", {}, switchTopGameContent);
            }
        }

        event.preventDefault();
    });

    $("#b_top_10").click(function(event){
        if (!$(this).hasClass("active")) {
            $(this).removeClass("next").addClass("active");
            $("#b_top_10 span").addClass("active");
            $("#b_top_game").removeClass("active").addClass("previous");

            $("#t_top_game").fadeTo("fast", 0.33);
            $("#topgame .content").append(loadingDiv);

            if (t_top_game_content != '') {
                //tmp = $("#t_top_game").html();
                //switchTopGameContent(t_top_game_content);
                //t_top_game_content = tmp;
            } else {
                $.get("?Controller=game&Action=top10", {}, switchTopGameContent);
            }
        }

        event.preventDefault();
    });
});
