﻿$(document).ready(function () {
    var timer = setInterval(bringPlayers, 2000);
  

    if (getQueryStringByName('name') != '') {
        var id = $('#lblPopupFriendIDContainer').val();
        var playerId = $('#' + id).val();
        if (playerId != '') {
            $('#' + id).val('');
            openFriendPopup(playerId);
        }
    }
});

var avatarIndex = 1;
//start from the end of the current players 
var playerIdx = 8;
function bringPlayers() {
    // Since we get all URLs in one file, we simply add all items
    // at once and set the size accordingly.
    //rnd = Math.floor(Math.random() * 99999); consider random avatar place

    playerIdx ++;

    if (playerIdx >= players.length) {
        playerIdx = 0;
    }


    var i = playerIdx;
    var s = '';

    if (null != players && players.length > 0) {
        var name = (players[i].Name.length < 10) ? players[i].Name : players[i].Name.substring(0, 8) + "...";
        var s = '';
        s += '<a href="javascript:openFriendPopup(\'' + players[i].ID + '\');">';
        s += '<img src="' + players[i].Avatar + '" alt="" height="45" width="45"></a><br />';
        s += '<a href="javascript:openFriendPopup(\'' + players[i].ID + '\');"  title = ' + players[i].Name + '>' + name + '</a>';

        AnimateAvatar(s);
    }
};


function AnimateAvatar(s) {

    if (avatarIndex > 8)
        avatarIndex = 1;

    //    var avatarIndex = getRandomIndex();

    var controlToFade = $("#playerBandUL li:nth-child(" + avatarIndex + ")");
    avatarIndex++;

    controlToFade.fadeOut('slow', function () {


        $(this).fadeIn('slow');
        $(this).html(s);
       
        //Dequeue();
    });

}


function getRandomIndex() {

    var index = Math.ceil(Math.random() * 8);

    return index;
};

