$(document).ready(function(){

    var id = parseInt($.url.param('id'));
    var name = $.url.param('name');

    var b_user_fade_running = false;

    var user_tab_comments_cache = '';
    var user_tab_blog_cache = '';
    var user_tab_games_cache = '';

    var user_games_page = 1;
    var user_blog_page = 1;
    var user_comments_page = 1;

    function applyFriendsData(data)
    {
        $("#user_friends_block_wrap .loading").remove();
        $("#user_friends_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                applyFriendsPagesEvents();
            });
        });
    }

    function applyFriendsPagesEvents()
    {
        $("#itemdetail div.userfriends div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#user_friends_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#user_friends_block").fadeTo("fast", 0.33, function() {
                $("#user_friends_block_wrap").append(loadingDiv);
                $.get('?Controller=user&Action=friends&id=' + id + '&name=' + name + '&friendspage=' + page, {}, applyFriendsData);
            });

            event.preventDefault();
        });
    }
    applyFriendsPagesEvents();

    function applyGroupsData(data)
    {
        $("#user_groups_block_wrap .loading").remove();
        $("#user_groups_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                applyGroupsPagesEvents();
            });
        });
    }

    function applyGroupsPagesEvents()
    {
        $("#itemdetail div.usergroups div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#user_groups_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#user_groups_block").fadeTo("fast", 0.33, function() {
                $("#user_groups_block_wrap").append(loadingDiv);
                $.get('?Controller=user&Action=groups&id=' + id + '&name=' + name + '&groupspage=' + page, {}, applyGroupsData);
            });

            event.preventDefault();
        });
    }
    applyGroupsPagesEvents();

    function applyMygroupsData(data)
    {
        $("#user_mygroups_block_wrap .loading").remove();
        $("#user_mygroups_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                applyMygroupsPagesEvents();
            });
        });
    }

    function applyMygroupsPagesEvents()
    {
        $("#itemdetail div.ucgroups div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#user_mygroups_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#user_mygroups_block").fadeTo("fast", 0.33, function() {
                $("#user_mygroups_block_wrap").append(loadingDiv);
                $.get('?Controller=user&Action=mygroups&id=' + id + '&name=' + name + '&mygroupspage=' + page, {}, applyMygroupsData);
            });

            event.preventDefault();
        });
    }
    applyMygroupsPagesEvents();

    function applyAwardData(data)
    {
        $("#user_awards_block_wrap .loading").remove();
        $("#user_awards_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                applyAwardPagesEvents();
            });
        });
    }

    function applyAwardPagesEvents()
    {
        $("#itemdetail div.awards div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#user_awards_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#user_awards_block").fadeTo("fast", 0.33, function() {
                $("#user_awards_block_wrap").append(loadingDiv);
                $.get('?Controller=user&Action=awards&id=' + id + '&name=' + name + '&trophypage=' + page, {}, applyAwardData);
            });

            event.preventDefault();
        });
    }
    applyAwardPagesEvents();

    function applyCommentData(data)
    {
        $("#profile_comments_block_wrap .loading").remove();
        $("#profile_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($("#profile_comments_block .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#profile_comments_block").fadeTo("fast", 0.33, function() {
                $("#profile_comments_block_wrap").append(loadingDiv);
                $.get('?Controller=user&Action=comments&id=' + id + '&name=' + name + '&page=' + page, {}, applyCommentData);
            });

            event.preventDefault();
        });
    }
    applyCommentPagesEvents();

    function applyNewComment(data)
    {
        $("#profile_comments_block_wrap .loading").remove();
        $("#profile_comments_block").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                if ($("#profile_comment_submit").attr('disabled') != '') $("#profile_comment_submit").attr('disabled', '');
                applyCommentPagesEvents();
                $("#profile_comments_block_wrap .success_message").remove();
                $("#profile_comments_block_wrap").prepend('<p class="success_message"><strong>Thank you for your comment!</strong></p>');
                $("#profile_comment_text").attr('value', '');
            });
        });
    }

    $("#profile_comment_submit").click(function() {
        $(this).attr('disabled', 'disabled');
        return user_comment_form_clicked();
    });

    function user_comment_form_clicked()
    {
        text = $("#profile_comment_text").attr('value');
        if (text == '') {
            alert('Comment text not supplied!');
            $("#profile_comment_text").focus();
            $("#profile_comment_submit").attr('disabled', '');
        } else {
            $("#profile_comments_block").fadeTo("fast", 0.33, function() {
                $("#profile_comments_block_wrap").append(loadingDiv);
                $.post('?Controller=user&Action=postcomment&id=' + id + '&name=' + name, { 'profile_comment_text': text }, applyNewComment);
            });
        }

        return false;
    }

    function applyMoreData(data)
    {
        $("#user_detail_more").slideUp("normal", function() { $(this).html(data).slideDown("normal", function() {
                $(this).fadeTo("fast", 1);
                $(".right .tabbar a .loading").remove();
                applyMorePagesEvents();
            });
        });
    }

    function applyMorePagesEvents()
    {
        $("#itemdetail #user_detail_more div.pages a").click(function(event) {
            page = parseInt($(this).text());
            if (isNaN(page)) page = parseInt($(this).attr('title'));

            currentPage = parseInt($("#user_detail_more .pages a.active").text());
            if (isNaN(currentPage)) currentPage = 1;

            if (currentPage == page) {
                event.preventDefault();
                return;
            }

            $("#user_detail_more").fadeTo("fast", 0.33, function() {

                if ($("#b_user_games").hasClass("active")) {
                    $("#b_user_games").append(loadingDiv);
                    $.get('?Controller=user&Action=games&id=' + id + '&name=' + name + '&scorepage=' + page, {}, applyMoreData);
                }

            });

            event.preventDefault();
        });
    }
    applyMorePagesEvents();

    $("#b_user_games").click(function(event){
        if (!$(this).hasClass("active") && !b_user_fade_running) {

            //b_user_fade_running = true;

            //cacheGameTabs();
            $(this).removeClass("previous").addClass("active").find("span").addClass("active");
            $("#b_user_blog").removeClass("active").removeClass("previous").addClass("next").find("span").removeClass("active");
            $("#b_user_comments").removeClass("next").removeClass("active").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#user_detail_more").fadeTo("fast", 0.33, function() {
                if (user_tab_games_cache != '')
                    applyMoreData(user_tab_games_cache);
                else
                    $.get('?Controller=user&Action=games&id=' + id + '&scorepage=' + user_games_page, {}, applyMoreData);
            });

        }

        event.preventDefault();
    });

    $("#b_user_blog").click(function(event){
        if (!$(this).hasClass("active") && !b_user_fade_running) {

            //b_user_fade_running = true;

            //cacheGameTabs();
            $(this).removeClass("next").removeClass("previous").addClass("active").find("span").addClass("active");
            $("#b_user_games").removeClass("active").addClass("previous").find("span").removeClass("active");
            $("#b_user_comments").removeClass("active").addClass("next").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#user_detail_more").fadeTo("fast", 0.33, function() {
                if (user_tab_blog_cache != '')
                    applyMoreData(user_tab_blog_cache);
                else
                    $.get('?Controller=user&Action=blogs&id=' + id + '&blogpage=' + user_blog_page, {}, applyMoreData);
            });

        }

        event.preventDefault();
    });

    $("#b_user_comments").click(function(event){
        if (!$(this).hasClass("active") && !b_user_fade_running) {

            //b_user_fade_running = true;

            //cacheGameTabs();
            $(this).removeClass("next").addClass("active").find("span").addClass("active");
            $("#b_user_games").removeClass("active").removeClass("previous").addClass("first").find("span").removeClass("active");
            $("#b_user_blog").removeClass("active").removeClass("next").addClass("previous").find("span").removeClass("active");

            $(this).append(loadingDiv);
            $("#user_detail_more").fadeTo("fast", 0.33, function() {
                if (user_tab_comments_cache != '')
                    applyMoreData(user_tab_comments_cache);
                else
                    $.get('?Controller=user&Action=mycomments&id=' + id + '&mycommentspage=' + user_comments_page, {}, applyMoreData);
            });

        }

        event.preventDefault();
    });

});
