;
$(document).ready(function(){



    $('#nyjrCommunicator')[0] = new Object();
    
    $('#nyjr_communicator').flash({
        src: '/web/nyjrCommV1.swf',
        width: 2,
        height: 2,
        id: 'nyjrCommunicator',
        name: 'nyjrCommunicator',
        wmode: 'transparent',
        autoplay: 1,
        flashvars: {
            autoplay: ['1'],
            wmode: ['transparent'],
            allowScriptAccess: ['always'],
            //            movie: ['http://d3tulsn8dn9o5l.cloudfront.net/rollinPlayer.swf'],
            //            movie: ['/web/rollinPlayer.swf'],
            quality: ['high'],
            name: ['nyjrCommunicator'],
            id: ['nyjrCommunicator'],
            bgcolor: ['#000000']
        }
    });
    
    $('#nyjrCommunicator')[0].playerComm = function(val){
        //alert("inside the playerComm caller");
        //	newVal = $.toJSON(val);
        string1 = "<invoke name=\"playerComm\" returntype=\"javascript\"><arguments><string>" + val + "</string></arguments></invoke>";
        $('#nyjrCommunicator')[0].CallFunction(string1);
        
    };
    
});






var count = 1;
var lastChatTime = '';
var lastDataTime = '';

function resetChatScroll(){

    //	$('#daddy-shoutbox-list').scrollTop($('#daddy-shoutbox-list').height());
    $('#daddy-shoutbox-list').scrollTop(20000);
    
};


function prepareDataObject(item, videoObj, count){

    //		console.log('item: ' + item);	
    //		console.log('newJSON: ' + $.toJSON(item));	
    //		console.log('item.json: ' + item.json);	
    if (count > 2) {
    
        $('#daddy-shoutbox-list .shoutbox-list:last').remove();
        
    }
    $('#daddy-shoutbox-list').append($('<div class="shoutbox-list" id="list-' + count + '">').tplAppend(item, tplVideoEntryNoLazy));
    
    
    $('#daddy-shoutbox-list #list-' + count + ' .rdc_video_obj .rdc_video_object_mainbox_chat').append('<span class="shoutbox-list-time">' + videoObj.time + '</span>' +
    '<span class="shoutbox-list-nick">' +
    videoObj.artist +
    ':</span>' +
    '<span class="shoutbox-list-message">' +
    videoObj.title +
    '</span>');
    
    $('#list-' + count).fadeIn('slow');
    
    if (lastChatTime < videoObj.time) {
    
        lastChatTime = videoObj.time;
        
    }
    
};





function prepareData(videoObj){

    //console.log('videoObj: ' + videoObj);		
    //console.log(videoObj);		
    //console.debug(videoObj);		
    
    prepareDataObject(videoObj, videoObj, count);
    
    
    
};



function checkNamesToken(data){

    if ((data > lastChatTime) && (data > lastDataTime)) {
        lastDataTime = data;
        
        //console.log('data: ' + data);		
        //console.log('lastChatTime: ' + lastChatTime);		
        $.getJSON("/songchange.php?action=refresh&time=" + lastChatTime, function(json){
            lastChatTime = data;
            //            console.log('chat JSON length: ' + json.length);
            
            count++;
            
            for (ii = 0; ii < json.length; ii++) {
                count = count + ii;
                prepareData(json[ii]);
                $('#daddy-shoutbox-list').scrollTop(2000);
            }
        });
        
        
        setTimeout('resetChatScroll()', 1000);
        setTimeout('resetChatScroll()', 2000);
        setTimeout('resetChatScroll()', 3000);
        setTimeout('resetChatScroll()', 4000);
        setTimeout('resetChatScroll()', 5000);
        
        
    }
    
    
    
    resetChatScroll();
    
    
}

