//onDomReady(function () {


	// init
//	chat.initialize();

//});




var chat = {

    movieId: null,
    lastCommentId: null,
    updateTime: 8000,
    userTimer:0,


	// init
	reset: function(movieId,lastComment)
    {
       chat.movieId = movieId;
       //debugger;
       chat.lastCommentId = lastComment;
       
       clearTimeout(chat.userTimer);
       chat.updateNewComments();
	},
    stop:function()
    {
        clearTimeout(chat.userTimer);  
    },  

    // AJAX content - refresh the live movie, every 5 seconds
    updateNewComments: function() {

        var updater = new Ajax('/movie/PollForNewComments?movieId='+chat.movieId+"&lastCommentId="+chat.lastCommentId , {method: 'get', onComplete:function(){
            var newComments = this.transport.responseXML.childNodes;
            var commentsArray = new Array();
            for(var lm = 0; lm < newComments.length; lm++ )
            {
                if (newComments[lm].tagName && newComments[lm].tagName.toLowerCase()=='newcomments')
                {
                    for(var m=0;m<newComments[lm].childNodes.length;m++)
                    {
                        if (newComments[lm].childNodes[m].tagName && newComments[lm].childNodes[m].tagName.toLowerCase()=='newcomment')
                        {
                            var commentParams = newComments[lm].childNodes[m].childNodes;
                            for(var p=0;p<commentParams.length;p++)
                            {
                                if (commentParams[p].tagName)
                                    switch(commentParams[p].tagName.toLowerCase())
                                    {
                                        case"id":
                                            commentsArray.id = commentParams[p].firstChild.data;
                                        break;
                                        case"username":
                                            commentsArray.userName = commentParams[p].firstChild.data;
                                        break;
                                        case"text":
                                            commentsArray.text = commentParams[p].firstChild.data;
                                        break;
                                        case"when":
                                            commentsArray.when = commentParams[p].firstChild.data;
                                        break;
                                    }
                            }
                        }
                    }
                }
            }
            if(commentsArray.id)
            {
               chat.lastCommentId = commentsArray.id; 
               //invoke the addnewcomments on the movie object
               movie.addNewComment(commentsArray);
               
            }
            
        }}).request();

        //set off the function every 5 minute
        chat.userTimer = setTimeout("chat.updateNewComments()", chat.updateTime);
    }
    
};





