﻿
/*
Author: thetoan.cao
Create Date: 2010-08-10
Description: profile public
*/
function getMissteen() {
    
    $.ajax({
        url: HomeEnv.urlHome + 'action/Blast.ashx',
        data: { "id": HomeEnv.readerId, "type": "getMissteen", "top": checkTopMissteen },
        dataType: 'json',
        timeout: "15000",
        error: function (request, error) {
        },
        success: function (data) {
            if (data.Success)
                if (data.MissteenId > 0) {
                    HomeEnv.isMissteen = true;

                    if (data.IsTop20)
                        var html = '<div class="mygo_missteen20_bg"><a href="http://missteen.go.vn/' + data.MissteenId + '/thi-sinh.htm" target="_blank">' + data.MissteenId + '</a></div>';
                    else {
                        if (data.IsTop100)
                            var html = '<div class="mygo_missteen100_bg"><a href="http://missteen.go.vn/' + data.MissteenId + '/thi-sinh.htm" target="_blank">' + data.MissteenId + '</a></div>';
                        else
                            var html = '<div class="mygo_missteen_bg"><a href="http://missteen.go.vn/' + data.MissteenId + '/thi-sinh.htm" target="_blank">' + data.MissteenId + '</a></div>';
                    }
                    $('.pnlMissteenBg').html(html);
                }
        }
    });
}
//getMissteen();
$.getScript(HomeEnv.urlMedia + "avatar/js/swfobject.js?v=" + HomeEnv.version, function () {
    GetGoAvatar("mygo_avatar_flash", HomeEnv.viewedId);
});
//Bạn bè
var GoFriendEnv = {
    urlHandler: HomeEnv.urlHome + "Friend/Handlers/",
    timeout: 5000,
    dataType: "json",
    numOfSuggest: 8,
    urlAllFriend: HomeEnv.urlHome + 'Friends.aspx#AllFriends'
}
function replaceQuote(s) {
    return s.replace("'", "&#8217;").replace('"', "&quot;");
}

function getFriendList(id) {
    $.ajax({
        url: GoFriendEnv.urlHandler + 'GetRandomFriends.ashx',
        data: { "accountid": id, "total": GoFriendEnv.numOfSuggest },
        dataType: 'jsonp',
        timeout: GoFriendEnv.timeout,
        error: function (request, error) {
            $('#mygo_friendWall_text').hide();
        },
        success: function (data) {

            if (data.RecordCount > 0 & data.RecordCount != null) {
                // $("#divFriendTotal").html(data.RecordCount + ' bạn');

                var table = '', showMutual = '', fullname = '', accountId;
                $.each(data.FriendCollections, function (i, result) {
                    fullname = result.FullName;
                    accountId = result.AccountId;
                    if (fullname == null || fullname == '')
                        fullname = accountId;
                    else fullname = replaceQuote(fullname);

                    table += ' <li>'
                                                + '<a href="' + HomeEnv.urlHome + '?id=' + accountId + '">'
                                                + '<img src="' + GetAvatarPath(accountId, 50, 50) + '" class="box_small_avatar" alt="'
                                                + fullname + '" title="' + fullname + '" />'
                                                + ' </a> '
                                                + '  <p>'
                                                 + '<a href="' + HomeEnv.urlHome + '?id=' + accountId + '">'
                                                  + fullname + ' </a> ' + '</p>'
                                                + ' </li>';
                });
                $('#mygo_friendWall_content').html(table);
            }
        }
    });
}

var DATA_CLUB = null, clubId, clubName, clubType, clubAvatar, clubUrl, ClubLength = 0;

var ClubEnv = {
    urlHome: "http://clubs.go.vn/",
    urlHandler: "http://api.clubs.go.vn/MygoServices/ClubServices.ashx",
    urlMyHandler: "/Action/ClubHandler.ashx",
    dataType: "jsonp",
    timeout: 3000,
    sltElm: "#mygo_club_content",
    pageSize: 8,
    maxLenghtName: 100
};
var ClubReq = {
    m: "GETCLUBS",
    oi: HomeEnv.readerId,
    gi: HomeEnv.readerId
};
String.prototype.subWords = function (length) {
    // if(typeof this!== String) return "";
    if (this == "") { return ""; }
    if (this.length <= length) { return this; }
    var trim = length;
    if (this[trim] != undefined) {
        while (trim > 0 & this[trim] != " ") { trim--; }
    }
    return this.substring(0, trim) + "...";
}

function clubProcessData(result) {
    clubStatus = result.OwnerStatus; //Status;  
    var h = '';
    if (clubStatus == "MEMBER" || clubStatus == "MANAGER" || clubStatus == "ADMIN") {
        ClubLength++;
        clubId = result.ID;
        clubType = result.Type;
        clubName = result.Title;
        // clubOwnerID = result.OwnerID;
        clubUrl = result.Url;

        clubAvatar = result.Avatar;
        // custom selection with clubAvatar of club
        h = '<li> <div class="box2" id="boxSelectClub_' + clubId + '">';
        h += '<div class="box_img"><a href="' + clubUrl + '">';
        h += '<img title="' + clubName + '" alt="' + clubName + '" class="club_small_avatar"'
    + ' src="' + clubAvatar + '" />';
        h += '</a></div>';
        h += ' <div class="text_nho">';
        clubName = clubName.subWords(ClubEnv.maxLenghtName);
        h += '<a href="' + clubUrl + '">' + clubName;
        h += '</a></div>';

        h += '</div></li>';
    }
    return h;
}
function getAdminClub() {
    $.ajax({
        url: ClubEnv.urlHandler,
        data: ClubReq,
        type: "get",
        dataType: ClubEnv.dataType,
        cache: false,
        timeout: ClubEnv.timeout,

        success: function (data) {
            if (data)
                bindAdminClub(data);
        }
    });
}

function bindAdminClub(data) {
    var new_content = '';
    if (data.totalrecord == 0) return;
    DATA_CLUB = data.item;
    if (DATA_CLUB.length == 0) return;

    var max_elem = Math.min(ClubEnv.pageSize, DATA_CLUB.length);
    for (var i = 0; i < max_elem; i++) {
        new_content += clubProcessData(DATA_CLUB[i]);
    }
    if (ClubLength > 0)
        $("#mygo_club_text").show();
    $(ClubEnv.sltElm).html(new_content);
}
if (HomeEnv.viewedPubName == "") {
    if (HomeEnv.accountId > 0) $('#lblAnnNotLogin').remove();
    //  $('#mygo_pnlUser').html("Tài khoản này không tồn tại trên Mạng Việt Nam.");
    //  document.title = "Tài khoản không tồn tại | go.vn - Mạng Việt Nam"
}
else {
    getFriendList(HomeEnv.viewedId);
    getAdminClub();
}
