


if (typeof(XMLHttpRequest) == "undefined") {
 XMLHttpRequest = function () {
   var msxmls = ["MSXML3", "MSXML2", "Microsoft"];
   for (var i=0; i < msxmls.length; i++) {
 try {
   return new ActiveXObject(msxmls[i]+".XMLHTTP");
 } catch (e) { }
   }
   throw new Error("No XML component installed");
 }
}



function LiveBlog( pThreadId, pNewestAtBottom, pTime, pUserId, pAllowedToEdit, pDisplayLanguage,  pRootElement, pWidth )
{
    if( pNewestAtBottom == null )
    {
        pNewestAtBottom = true;
    }
    
    if( pTime == null ) 
    {
        pTime = ( new Date() ).toUTCString();
    }

    if( pWidth == null )
    {
        this.Width = 600;
    }
    else
    {
        this.Width = pWidth;
    }
    
    
    this.UserId = pUserId;
    this.NewestAtBottom = pNewestAtBottom;
    this.Time = new Date( pTime );
    
    if( ( pThreadId + "").match( /^[0-9]+$/ ) )
    {
        this.ThreadId = pThreadId;
        this.ThreadKey = "";
    }
    else
    {
        this.ThreadId = "";
        this.ThreadKey = pThreadId;
    }
    
    if( pRootElement == null )
    {
        this.PostsList = document.getElementById("Posts");
    }
    else
    {
        this.PostsList = pRootElement;
    }
    
    this.GroupPostsLists = new Array();
    this.GroupPostsLists[0] = this.PostsList;
    
    this.GroupNewestAtBottom = new Array();
    this.GroupNewestAtBottom[0] = this.NewestAtBottom;
    
    this.IsScriptaculousAvailable = false;
    try
    {
        this.IsScriptaculousAvailable = ( Effect != null && Effect.toggle != null );
    }
    catch( Error ) {}
    
    this.IsSoundManagerAvailable = false;
    try
    {
        this.IsSoundManagerAvailable = ( soundManager != null && soundManager.createSound != null );
    }
    catch( Error ) {}
    
    
    this.IsCrossDomain = false;
    if( ! ( document.location + "" ).match( /(http:\/\/[a-z0-9\-]*\.?scribblelive\.com\/|http:\/\/localhost|http:\/\/liveblogs.thescore.com|http:\/\/www.scribblelive.com)/i ) )
    {
        this.IsCrossDomain = true;
    }
    
    this.AllowedToEdit = ( pAllowedToEdit == true );
    
    this.FadeColor_Start = "#0099CC";
    this.FadeColor_End = "#ffffff";
    
    this.NumPolls = 0;
    
    this.IsEmergency = false;
    
    this.CurrentVisitors = 0;
    
    this.DisplayLanguage = pDisplayLanguage;
    
    this.CurrentRequestId = null;
    
    this.HasShownReloadPrompt = false;
    
    if( this.DisplayLanguage )
    {
        this.TranslateAllPosts();
    }
    
    LiveBlog.StoreInstance( this );
    
    this.LastModeratedCommentsPulsate = null;
    
    this.PollForModeratedComments();
    
    this.PollForMetrics();
    
    this.NewPostSound = null;
    
    this.MaxCommentsDisplayed = 10;
    
    this.UseCDN = true;
    
    this.IsModerated = true;
}

LiveBlog.prototype.AddGroupPostsList = function( pGroupId, pElement, pNewestAtBottom )
{
    this.GroupPostsLists[ pGroupId ] = pElement;
    
    if( pNewestAtBottom == null )
    {
        pNewestAtBottom = this.NewestAtBottom;
    }
    
    this.GroupNewestAtBottom[ pGroupId ] = pNewestAtBottom;
}

LiveBlog.prototype.GetGroupNewestAtBottom = function( pGroupId )
{
    if( pGroupId == null )
    {
        pGroupId = 0;
    }

    return this.GroupNewestAtBottom[ pGroupId ];
}

LiveBlog.prototype.GetGroupPostsList = function( pGroupId )
{
    if( pGroupId == null )
    {
        pGroupId = 0;
    }

    return this.GroupPostsLists[ pGroupId ];
}

LiveBlog.prototype.TranslateAllPosts = function( pDisplayLanguage )
{
    if( ! pDisplayLanguage )
    {
        if( ! this.DisplayLanguage )
        {
            return;
        }
        else
        {
            pDisplayLanguage = this.DisplayLanguage;
        }
    }
    else
    {
        this.DisplayLanguage = pDisplayLanguage;
    }
    
    if( !this.IsCrossDomain )
    {
        var h2s = document.getElementsByTagName("h2");
        for( var i = 0; i < h2s.length; i++ )
        {
            this.TranslateContent( h2s[i].innerHTML, h2s[i] );
        }
        
        var h3s = document.getElementsByTagName("h3");
        for( var i = 0; i < h3s.length; i++ )
        {
            this.TranslateContent( h3s[i].innerHTML, h3s[i] );
        }
    

        var ExistingPosts = this.PostsList.getElementsByTagName("div");
        
        for( var i = 0; i < ExistingPosts.length; i++ )
        {
            var CurrentPostContent = ExistingPosts[i];
            
            if( CurrentPostContent.className.match( "Content" ) 
                && ! CurrentPostContent.innerHTML.match( /<(img|embed|object)/i )
                )
            {
                this.TranslateContent( CurrentPostContent.innerHTML, CurrentPostContent );
            }
        }
    }
    else
    {
        var ExistingPosts = this.PostsList.getElementsByTagName("li");
        
        for( var i = 0; i < ExistingPosts.length; i++ )
        {
            var CurrentPostContent = ExistingPosts[i];
            
            if( ! CurrentPostContent.innerHTML.match( /<(embed|object|img)/i ) 
                )
            {
                this.TranslateContent( CurrentPostContent.innerHTML, CurrentPostContent );
            }
        }
    }
}

LiveBlog.StoreInstance = function( pLiveBlog )
{
    if( LiveBlog.__instances == null )
    {
        LiveBlog.__instances = new Array();
    }
    
    var HashKey;
    
    if( pLiveBlog.ThreadId != "" )
    {
        HashKey = "" + pLiveBlog.ThreadId;
    }
    else if( pLiveBlog.ThreadKey != "" )
    {
        HashKey = pLiveBlog.ThreadKey;
    }
    else
    {
        HashKey = "0";
    }   
    
    if( LiveBlog.__instances[HashKey] == null )
    {
        LiveBlog.__instance = pLiveBlog;
        LiveBlog.__instances[HashKey] = pLiveBlog;
        return true;
    }
    else
    {
        return false;
    }
}

LiveBlog.GetInstance = function( pThreadId )
{
    if( LiveBlog.__instances == null )
    {
        return null;
    }
    else if( pThreadId == null )
    {
        return LiveBlog.__instance;
    }
    else
    {
        return LiveBlog.__instances[""+pThreadId];
    }
    
}

LiveBlog.GetInstances = function()
{
    return LiveBlog.__instances;
}


LiveBlog.DestroyElement = function( pElement )
{
    pElement.parentNode.removeChild( pElement );
    
    return false;
}

LiveBlog.UserLanguage = function()
{   
    if( ( document.location + "" ).match( /http:\/\/(www\.scribblelive\.com\/|localhost)/i ) )
    {
        return "English (United States)";
    }
    else if( navigator.userLanguage != null ) 
    {
	    return navigator.userLanguage;
    }
    else if( navigator.language != null ) 
    {
	    return navigator.language;
    }
    else
    {
        return "en";
    }
}

LiveBlog.Ping = function( pThreadId, pResponseObject )
{
    var _LiveBlog = LiveBlog.GetInstance( pThreadId );
    
    if( _LiveBlog == null )
    {
        return;
    }
    
   
    
    var IncludesPosts = false;
    
    if( pResponseObject.Posts != null )
    {
        IncludesPosts = true;
    }
    
    var CurrentUTCServerTime = new Date( pResponseObject.Time );
    
    if( CurrentUTCServerTime >= new Date( _LiveBlog.Time ) )
    {
        _LiveBlog.PollForNew_Handler( pResponseObject );
    }
    
    var Scripts = document.getElementsByTagName("script");
    for( var i = 0; i < Scripts.length; i++ )
    {
        if( ( Scripts[i].src + "" ).match( /(liveupdate1\.scribblelive\.com|cdn\.cloudfront|lastmodified\.aspx)/i ) )
        {
            //LiveBlog.DestroyElement( Scripts[i] );
        }
    }
    
    var PollDelay = 6000;
    
    var RandDelayAddition = Math.round(Math.random()*4000);
    
    if( RandDelayAddition > 2000 )
    {
        PollDelay += RandDelayAddition - 2000;
    }
    else
    {
        PollDelay -= RandDelayAddition;
    }
    
    if( IncludesPosts )
    {
        setTimeout( function() { _LiveBlog.PollForNew( _LiveBlog.Time ); }, PollDelay ); 
    }
    else
    {
        if( new Date( pResponseObject.Time ) > new Date( _LiveBlog.Time ) )
        {
            _LiveBlog.PollForNew( _LiveBlog.Time, true );
        }
        else
        {
            setTimeout( function() { _LiveBlog.PollForNew( _LiveBlog.Time ); }, PollDelay );
        }
    }  
}

LiveBlog.prototype.PollForNew_Handler = function( pResponseObject )
{

    var ResponseObject = pResponseObject;
	
    var CurrentTime = ResponseObject.Time;
    
    var CurrentUTCServerTime = new Date( ResponseObject.Time );
    
    var Posts = ResponseObject.Posts;
    
    var ValidActions = 0;
    
    if( Posts != null )
    {
        
        var AutoScroll = false;
    	
        if( ! this.IsCrossDomain && Posts.length > 0 && this.NewestAtBottom )
        {
            AutoScroll = ( ( LiveBlog.YPosition() + 50 ) >= LiveBlog.PageHeight() )
        }
        
        try
        {
            if( ! this.UserId )
            {
                this.UserId = -1;
            }
        }
        catch( err )
        {
            this.UserId = -1;
        }


        
        
        var PostElement;
        var PostDate;

        for( var i = 0; i < Posts.length; i++ )
        {
            PostDate = new Date( Posts[i].Date );
            
            if( PostDate <= this.Time )
            {
                // DO NOTHING
            }
            else
            {
            
                if( this.GetGroupPostsList( Posts[i].Group ) == null ) 
                {
                    // DO NOTHING
                }
                else if( this.NewestAtBottom && !this.IsCrossDomain && !Posts[i].IsDeleted && ( Posts[i].EditorId == null || Posts[i].EditorId == "" ) && document.getElementById("LiveBlog_Post" + Posts[i].Id) == null && Posts[i].Type == "1" )
                {
                
                    if( !this.HasShownReloadPrompt )
                    {
                        Posts[i].Rank = 0;
                        Posts[i].Content = "<em>This event is now going live! <a href='#' onclick='return LiveBlog.ReloadPage()'>Click here to reload</a> to see all the new posts</em>";
                    
                        this.AttachNewPost( Posts[i] );
                        this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                    
                        AutoScroll = false;
                    
                        LiveBlog.ScrollToBottom();
                        
                        this.HasShownReloadPrompt = true;
                    }
                    
                } // SYSTEM MESSAGE
                else if( 
                    Posts[i].Type == "4"  
                    && ( this.AllowedToEdit || Posts[i].CreatorId == this.Userid || Posts[i].EditorId == this.UserId )
                )
                {
                    
                    // INVITE
                    if( this.EditorId == this.UserId && Posts[i].Content.match( /invited/ ) )
                    {
                        if( confirm( "You have been invited to join this live event as a writer.\nClick OK to load the writer interface immediately, or just reload when you have a chance." ) )
                        {
                            LiveBlog.ReloadPage();
                        }
                        
                    }
                    else
                    {
                        this.AttachNewPost( Posts[i], 5 );
                        
                        this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                    }
                    
                    
                }
                else if( Posts[i].IsDeleted && document.getElementById("LiveBlog_Post" + Posts[i].Id) != null )
                {
                    this.DisconnectPost( document.getElementById("LiveBlog_Post" + Posts[i].Id ) );
                    ValidActions++;
                }
                else if( 
                    ( !Posts[i].IsDeleted && document.getElementById("LiveBlog_Post" + Posts[i].Id) == null )
                    || ( 
                        document.getElementById("LiveBlog_Post" + Posts[i].Id) != null  
                        && document.getElementById("LiveBlog_Post" + Posts[i].Id).parentNode != this.GetGroupPostsList( Posts[i].Group )
                        )
                    )
                {
                    if( document.getElementById("LiveBlog_Post" + Posts[i].Id) != null  )
                    {
                        PostElement = document.getElementById("LiveBlog_Post" + Posts[i].Id);
                        PostElement.id += "DELETING";
                        this.DisconnectPost( PostElement );
                    }   
                
                
                    if( this.IsCrossDomain )
                    {
                        this.AttachNewCrossposting( Posts[i] );
                        
                        this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                    }
                    else if( ! Posts[i].IsComment )
                    {
                
                        this.AttachNewPost( Posts[i] );
                        
                        this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                        
                        if( Posts[i].Source == "" )
                        {
                            this.UpdateWhosBlogging( Posts[i].CreatorName, Posts[i].CreatorThumbnail );
                        }
                        
                    }
                    else
                    {
                        this.AttachNewComment( Posts[i] );
                        
                        this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                    }
                    
                    ValidActions++;
                }
                else if(
                    document.getElementById("LiveBlog_Post" + Posts[i].Id) != null
                    && 
                    (
                        ( Posts[i].EditorId != null && Posts[i].EditorId != "" && Posts[i].EditorId != this.UserId )
                        || ( Posts[i].EditorId == "" && Posts[i].CreatorId != this.UserId )
                     
                    )
                    && !this.IsCrossDomain
                )
                {
                    PostElement = document.getElementById("LiveBlog_Post" + Posts[i].Id );
                
                    if( 
                    PostElement.firstChild.className.match("IsEditing") == null 
                    && PostElement.LastModified != Posts[i].Date
                    ) 
                    {
                        if( !Posts[i].Content.match( /(<img|<embed|<object)/i ) && Posts[i].Type == "1" )
                        {
                            PostElement.firstChild.innerHTML = this.TranslateContent( Posts[i].Content.replace( /&quot;/g, "\"" ), document.getElementById("LiveBlog_Post" + Posts[i].Id ).firstChild );
                        }
                        
                        
                        PostElement.lastChild.innerHTML = "by <em>" 
                            + ( Posts[i].CreatorId == this.UserId ? "you" : Posts[i].CreatorName ) + "</em>"
                            + "</em> edited by <em>" 
                            + ( Posts[i].EditorId == this.UserId ? "you" : ( Posts[i].EditorName != "" ? Posts[i].EditorName : Posts[i].CreatorName ) )
                            + "</em> at " + LiveBlog.ConvertServerTimeToLocalTimeFriendlyString( Posts[i].Date );
                        
                        if( this.AllowedToEdit )
                        {
                            PostElement.lastChild.innerHTML += "  <a href='#' onClick='return Sticky(" + Posts[i].Id + "," + ( Posts[i].Rank == "0" ? "false" : "true" ) + ", this)'>" + ( Posts[i].Rank == "0" ? "unstick" : "stick" ) + "</a> <a href='#' class='Edit' onClick='return Edit(" + Posts[i].Id + ", this)'>edit</a> <a href='#' onClick='return Delete(" + Posts[i].Id + ")'>delete</a>";
                        }
                        
                        
                        
                        this.SetPostRank( PostElement, Posts[i].Rank, Posts[i].Group );
                        
                        
                        this.OnEditPost( PostElement, PostElement.parentNode, Posts[i].Id, Posts[i].Rank, Posts[i].Group );
                        
                        
                        this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                        
                        ValidActions++;
                    }
                }
                else if(
                    document.getElementById("LiveBlog_Post" + Posts[i].Id) != null
                    && 
                    (
                        ( Posts[i].EditorId != null && Posts[i].EditorId != "" && Posts[i].EditorId != this.UserId )
                        || ( Posts[i].EditorId == "" && Posts[i].CreatorId != this.UserId )
                     
                    )
                    && this.IsCrossDomain
                    && !Posts[i].Content.match( /(<img|<object|<embed)/i )
                    && Posts[i].Type == "1"
                )
                {
                    PostElement = document.getElementById("LiveBlog_Post" + Posts[i].Id );
                    
                    PostElement.innerHTML = PostElement.innerHTML.replace( /^(.*?<\/em>).*$/i, "$1 " + Posts[i].Content );
                
                    this.SetPostRank( PostElement, Posts[i].Rank, Posts[i].Group );
                    
                    this.OnEditPost( PostElement, PostElement.parentNode, Posts[i].Id, Posts[i].Rank, Posts[i].Group );
                        
                    this.FadeIn( "LiveBlog_Post" + Posts[i].Id );
                    
                    ValidActions++;
                }
                else if( document.getElementById("LiveBlog_Post" + Posts[i].Id ) != null )
                {
                    this.SetPostRank( document.getElementById("LiveBlog_Post" + Posts[i].Id ), Posts[i].Rank, Posts[i].Group );
                    this.SetGroup( document.getElementById("LiveBlog_Post" + Posts[i].Id ), Posts[i].Group );
                }
            
            }
            
            
        }
        
    }
    
    if( Posts != null )
    {
        this.Time = CurrentUTCServerTime;
    }
   
}

LiveBlog.prototype.SetGroup = function( pElement, pGroupId )
{
    if( pElement.parentNode != this.GetGroupPostsList( pGroupId ) )
    {
        this.ConnectNewPost( pElement, this.GetGroupPostsList( pGroupId ), null, null, null, pGroupId );
    }
}

LiveBlog.prototype.DisconnectPost = function( pItem )
{
    if( pItem == null )
    {
        return;
    }
    
    if( pItem.IsDeleted != null && pItem.IsDeleted )
    {
        return;
    }
    
    if( ( pItem.className + "" ).match( /Comment/ ) ) this.UpdateStats( 0, -1 )
    else this.UpdateStats( -1, 0 )
    
    for( var i = 0; i < pItem.parentNode.childNodes.length; i++ )
    {
        if( pItem.parentNode.childNodes[i].tagName == null || pItem.parentNode.childNodes[i].tagName.toUpperCase() != "LI" )
        {
            pItem.parentNode.removeChild( pItem.parentNode.childNodes[i] );
        }
    }
    
    
    var IsFirstChild = false;
    if( pItem.id == pItem.parentNode.firstChild.id && pItem.parentNode.childNodes.length > 1 )
    {
        IsFirstChild = true;
        pItem.nextSibling.className += " First";
    }
    
    
    if( this.IsScriptaculousAvailable )
    {
        pItem.IsDeleted = true;
        Effect.toggle(pItem, 'blind', { duration: 0.5 });
        setTimeout( function()
        {
            if( pItem != null )
            {
                pItem.parentNode.removeChild( pItem );
            }
        }, 600 );
    }
    else
    {
        pItem.parentNode.removeChild( pItem );
    }
    
}


LiveBlog.prototype.PollForNew = function( Since, pFullPoll )
{
    if( pFullPoll == null )
    {
        pFullPoll = false;
    }


    if( Since == null )
    {
        Since = this.Time;
    }
    
    var _LiveBlog = this;
    
    /*
    if( ! this.NewestAtBottom && LiveBlog.YPosition() > LiveBlog.findPosY( this.PostsList ) + 100 )
    {
	    setTimeout( function() { _LiveBlog.PollForNew( Since, pFullPoll ); }, 2000 );
	    return;
	}
	*/
    
    
    this.NumPolls++;
    
    var PollFilename;
    
    if( pFullPoll )
    {
        PollFilename = "recentposts.js";
    }
    else
    {
        PollFilename = "lastmodified.js";
    }

    var PollUrlDomain;
    var ScriptUrl;
    
    if( ( document.location + "" ).match(/(scribblelive0|localhost|keebler\.net)/i) || this.AllowedToEdit || ! this.UseCDN )
    {
        ScriptUrl = "http://www.scribblelive.com/LastModified.aspx?ThreadId=" + this.ThreadId + "&rand=" + Math.round( 100000000 * Math.random() );
    }
    else if( ( document.location + "" ).match(/embed-stage\.scribblelive\.com/i) )
    {
        PollUrlDomain = "http://liveupdate1-stage.scribblelive.com/";
        ScriptUrl = PollUrlDomain + ( this.ThreadId + "" ).replace( /([0-9])/g, "$1\/" ) +  PollFilename + "?rand=" + Math.round( 100000000 * Math.random() );
    
    }
    else
    {
        PollUrlDomain = "http://liveupdate1.scribblelive.com/";
        
        ScriptUrl = PollUrlDomain + ( this.ThreadId + "" ).replace( /([0-9])/g, "$1\/" ) +  PollFilename + "?rand=" + Math.round( 100000000 * Math.random() );
    }

    var PollScript = document.createElement("script");
    PollScript.src = ScriptUrl;
    PollScript.type = "text/javascript";
    
    var RequestId = Math.round(Math.random()*10000000000);
    
    _LiveBlog.CurrentRequestId = RequestId;
    
	document.getElementsByTagName("head")[0].appendChild( PollScript );
	
	var RequestChecks = 0;
	
	
	var Interval = setInterval( function()
        {
            if( _LiveBlog.CurrentRequestId != RequestId )
            {
                PollScript.parentNode.removeChild( PollScript );
                clearInterval( Interval );
            }
            else if( _LiveBlog.CurrentRequestId == RequestId && RequestChecks > 15 )
            {
                _LiveBlog.PollForNew( Since, pFullPoll );
                clearInterval( Interval );
            }
        
            RequestChecks++;
            
        }, 1000 );

}

LiveBlog.prototype.MetricsPing = function( pCurrentVisitors, pTotalVisitors )
{
    this.CurrentVisitors = pCurrentVisitors;
    
    var PollScript = document.getElementById("MetricsScript");
    
    PollScript.parentNode.removeChild( PollScript );
    
    if( parseInt( this.CurrentVisitors ) > 100 )
    {
        this.IsEmergency = true;
    }
    
    var ems = document.getElementsByTagName( "em" );
    
    for( var i = 0; i < ems.length; i++ )
    {
        if( ems[i].className == "CurrentVisitors" )
        {
            if( this.NewestAtBottom ) 
            {
                ems[i].innerHTML = "";
            }
            else
            {
                ems[i].innerHTML = ( this.CurrentVisitors == "1" ? "" : "(" + this.CurrentVisitors + "&nbsp;watchers)" );
            }
        }
    }
    
    if( document.getElementById("TotalVisitors") != null )
    {
        document.getElementById("TotalVisitors").innerHTML = ( parseInt( pCurrentVisitors ) + parseInt( pTotalVisitors ) );
    }
}

LiveBlog.prototype.UpdateWhosBlogging = function( pCreatorName, pCreatorThumb )
{
    var Sidebar = document.getElementById( "WhosBloggingSidebar" );
    
    if( Sidebar != null )
    {
    
        if( this.Writers == null )
        {
            this.Writers = new Array();
            
            var WritersInSidebar = Sidebar.getElementsByTagName( "li" );
            var WriterName;
            for( var i = 0; i < WritersInSidebar.length; i++ )
            {
                try
                {
                    WriterName = ( WritersInSidebar[i].innerHTML + "" ).match( /([^>]+)\s*$/ )[1];
                    this.Writers[ WriterName ] = 1;
                } catch( err ) {}
            }
        }
        
        if( this.Writers[ pCreatorName ] == null )
        {
            var ThumbImg = "<span class='NoThumb' />";
            if( pCreatorThumb != null && pCreatorThumb != "" )
            {
                ThumbImg = "<img src='" + pCreatorThumb + "' />";
            }
        
            Sidebar.getElementsByTagName("ul")[0].innerHTML += "<li>" + ThumbImg + pCreatorName + "</li>";
            this.Writers[ pCreatorName ] = 1;
        }
    }
}

LiveBlog.prototype.UpdateStats = function( pIncrementNumPosts, pIncrementNumComments )
{
    if( pIncrementNumPosts != null && pIncrementNumPosts != 0 && document.getElementById("TotalPosts") != null )
    {
        document.getElementById("TotalPosts").innerHTML = parseInt( document.getElementById("TotalPosts").innerHTML ) + pIncrementNumPosts;
    }
    
    if( pIncrementNumComments != null && pIncrementNumComments != 0 && document.getElementById("TotalComments") != null )
    {
        document.getElementById("TotalComments").innerHTML = parseInt( document.getElementById("TotalComments").innerHTML ) + pIncrementNumComments;
    }
}

LiveBlog.prototype.PollForMetrics = function()
{
    this.GetMetrics();
    
    var LiveBlogInstance = this;

    var MetricsInterval = setInterval( function()
        {
            LiveBlogInstance.GetMetrics();    
        }, 50000 + Math.round( 10000 * Math.random() ) );
}

LiveBlog.prototype.GetMetrics = function()
{

    var HitUrl = "http://whos.amung.us/api/query/scribblelive/track/id=" + this.ThreadId + "&rand=" + Math.round( 100000000 * Math.random() );
    
    var PollScript = document.createElement("script");
    PollScript.src = HitUrl;
    PollScript.type = "text/javascript";
    PollScript.id = "MetricsScript";
	document.getElementsByTagName("head")[0].appendChild( PollScript );
    
}

LiveBlog.prototype.PollForModeratedComments = function()
{


    if( !this.AllowedToEdit || this.IsCrossDomain || document.getElementById("Moderated") == null )
    {
        return false;
    }   
    
    if( this.ModCommentsSince == null )
    {
        this.ModCommentsSince = "";
    }
    
    var req = new XMLHttpRequest();
    req.open("GET", "/GetModeratedPosts.aspx?threadid=" + this.ThreadId + "&since=" + ( this.ModCommentsSince ), true );
    
    var _LiveBlog = this;
    
    req.onreadystatechange = function()
        {
            if( req.readyState == 4 )
            {
            
                if( req.status == 200 && req.responseText != "" )
                {
                
			        var ResponseObject = eval("(" +  req.responseText + ")" );
			        
			        if( this.ModCommentsSince != "" )
			        {   
			            var ModeratedComments = ResponseObject.ModeratedComments;
                        if( ModeratedComments != null )
                        {
                            for( var i = 0; i < ModeratedComments.length; i++ )
                            {
                                if( ModeratedComments[i].IsDeleted == "0" )
                                {
                                    _LiveBlog.AttachNewModeratedComment( ModeratedComments[i] );
                                }
                                else
                                {
                                    _LiveBlog.DisconnectPost( document.getElementById("LiveBlog_Post" + ModeratedComments[i].Id ) );
                                }
                            }
                        }
                    
                    }    
                        
                    _LiveBlog.ModCommentsSince = ResponseObject.Time;
                    
                    setTimeout( function() { _LiveBlog.PollForModeratedComments( ); }, 5000 );
                }
                else
                {
				    setTimeout( function() { _LiveBlog.PollForModeratedComments( ); }, 10000 );
                }
            }
        };
    
    req.send( null );
    
    
    return true;
}

LiveBlog.prototype.GetStickyPosts = function( pListElement )
{
    var Stickies = new Array();
    
    var AllPosts = pListElement.getElementsByTagName("li");
    
    for( var i = 0; i < AllPosts.length; i++ )
    {
        if( ( AllPosts[i].className + "" ).match( /Sticky/ ) )
        {
            Stickies.push( AllPosts[i] );
        }
    }
    
    return Stickies;
}

LiveBlog.prototype.SetPostRank = function( Item, pRank, pNewestAtBottom )
{
    if( pNewestAtBottom == null )
    {
        pNewestAtBottom = this.NewestAtBottom;
    }

    var Stickies = this.GetStickyPosts( Item.parentNode );
    
    var ListElement = Item.parentNode;
    
    if( pRank == 0 )
    {
        Item.className += " Sticky";
    
        if( pNewestAtBottom )
        {
            if( Stickies.length <= 0 )
            {
                ListElement.appendChild( Item );
            }
            else
            {
                ListElement.insertBefore( Item, Stickies[0] );   
            }
        }
        else
        {
        
            if( Stickies.length <= 0 )
            {
                for( var i = 0; i < ListElement.childNodes.length && i < 3; i++ )
                {
                    if( ListElement.childNodes[i].className )
                    {
                        ListElement.childNodes[i].className = ListElement.childNodes[i].className.replace( "First", "" );
                    }
                }
            
                ListElement.insertBefore( Item, ListElement.firstChild );
                Item.className += " First";
            }
            else
            {
                if( Stickies[ Stickies.length-1 ].nextSibling )
                {
                    ListElement.insertBefore( Item, Stickies[ Stickies.length-1 ].nextSibling );
                }
                else
                {
                    ListElement.appendChild( Item );
                }
            }
        }
    }   
    else
    {
        if( Item.className.match( /Sticky/ ) )
        {
            Item.className = Item.className.replace( "Sticky", "" );
            
            Stickies = this.GetStickyPosts( Item.parentNode );
            
            
            if( Stickies.length > 0 )
            {
                if( pNewestAtBottom )
                {
                    ListElement.insertBefore( Item, Stickies[0] );    
                }
                else
                {
                    if( Item.className.match(/First/) )
                    {
                        Stickies[0].className += " First";
                        Item.className = Item.className.replace( "First", "" );
                    }
                    
                    if( Stickies[ Stickies.length - 1 ].nextSibling != null )
                    {
                        ListElement.insertBefore( Item, Stickies[ Stickies.length - 1 ].nextSibling );
                    }
                    else
                    {
                        ListElement.appendChild( Item );
                    }
                }
            }    
            
        }
    }
}

LiveBlog.prototype.OnEditPost = function( )
{
    return true;
}

LiveBlog.prototype.OnConnectNewPost = function( )
{
    return true;
}

LiveBlog.prototype.ConnectNewPost = function( Item, ListElement, pNewestAtBottom, pMaxListItems, pRank, pGroupId )
{
    
    if( ! this.OnConnectNewPost( Item, ListElement, pNewestAtBottom, pMaxListItems, pRank, pGroupId ) )
    {
        return;
    }
    
    if( pGroupId == null )
    {
        pGroupId = 0;
    }


    if( ListElement == null )
    {
        ListElement = this.GetGroupPostsList( pGroupId );
        
        if( ListElement == null )
        {
            return;
        }
    }
    
    if( ( Item.className + "" ).match( /Comment/ ) ) this.UpdateStats( 0, 1 )
    else this.UpdateStats( 1, 0 )
    
    
    if( this.IsScriptaculousAvailable )
    {
        Item.style.display = "none";
    }
    
    if( pNewestAtBottom == null )
    {
        pNewestAtBottom = this.GetGroupNewestAtBottom( pGroupId );
    }
    
    if( pMaxListItems == null )
    {
        pMaxListItems = 10000;
    }
    
    if( pRank == null || ! this.IsScriptaculousAvailable )
    {
        pRank = 1;
    }
    
    
    if( pRank == 0 )
    {
        Item.className += " Sticky";
    }


    if( pNewestAtBottom || ListElement.childNodes.length <= 0 )
    {
        ListElement.appendChild( Item );
        
        if( ListElement.getElementsByTagName("li").length == 1 )
        {
            Item.className += " First";
        }
    }
    else
    {
        var Stickies = this.GetStickyPosts( ListElement );
        
        if( Stickies.length <= 0 )
        {
            for( var i = 0; i < ListElement.childNodes.length && i < 3; i++ )
            {
                if( ListElement.childNodes[i].className )
                {
                    ListElement.childNodes[i].className = ListElement.childNodes[i].className.replace( "First", "" );
                }
            }
            
            Item.className += " First";
        
            ListElement.insertBefore( Item, ListElement.firstChild );
        }
        else
        {
            if( Stickies[ Stickies.length - 1 ].nextSibling == null )
            {
                ListElement.appendChild( Item );
            }
            else
            {
                ListElement.insertBefore( Item, Stickies[ Stickies.length - 1 ].nextSibling );
            }
        }
    }
    
    
    if( this.IsScriptaculousAvailable )
    {
        Effect.toggle(Item, 'blind', { duration: 0.5 }); 
        
        
    }
    
    try
    {
        if( 
            this.NewPostSound != null 
            && this.IsSoundManagerAvailable 
            && ( this.LastSoundPlay == null || this.LastSoundPlay + "" != new Date() + "" )
            )
        {
        
            if( soundManager.getSoundById("ScribbleLiveSoundEmbed") == null )
            {
                soundManager.createSound({
                  id: 'ScribbleLiveSoundEmbed',
                  url: this.NewPostSound,
                  autoLoad: true,
                  autoPlay: true
                });
            }
            else if( soundManager.getSoundById("ScribbleLiveSoundEmbed").url != this.NewPostSound  )
            {
                soundManager.getSoundById("ScribbleLiveSoundEmbed").destruct();
                soundManager.createSound({
                  id: 'ScribbleLiveSoundEmbed',
                  url: this.NewPostSound,
                  autoLoad: true,
                  autoPlay: true
                });
            }
            else
            {
                soundManager.getSoundById("ScribbleLiveSoundEmbed").play();
            }
            
            this.LastSoundPlay = new Date();
        
        }
    }
    catch( Error )
    {}
    
    
    if( ListElement.childNodes.length > pMaxListItems )
    {
        var OnTheBubble;
        
        if( pNewestAtBottom )
        {
            OnTheBubble = ListElement.getElementsByTagName("li")[0];
        }
        else
        {
            OnTheBubble = ListElement.getElementsByTagName("li")[ ListElement.getElementsByTagName("li").length - 1 ];
        }
        
        
        if( ! ( OnTheBubble.firstChild.className + "" ).match( /IsEditing/ ) )
        {
            ListElement.removeChild( OnTheBubble );
        }
        else if( OnTheBubble.previousSibling != null )
        {
            ListElement.removeChild( OnTheBubble.previousSibling );
        }
    }
    
}

LiveBlog.prototype.AttachNewTweet = function( PostInfo, pHtmlElement )
{
    var ListElement = pHtmlElement;
    
    if( document.getElementById("LiveBlog_Post" + PostInfo.Id) != null )
    {
        return;
    }
    
    var NewItem = document.createElement("li");
	NewItem.id = "LiveBlog_Post" + PostInfo.Id;
    NewItem.className = "Comment";
    
    var ContentElement = document.createElement( "div" );
	ContentElement.className = "Content";
	ContentElement.innerHTML = this.TranslateContent( PostInfo.Content.replace( /&quot;/g, "\"" ), ContentElement );
	    
    var MetaElement = document.createElement("div" );
    MetaElement.className = "Meta";
    
    MetaElement.innerHTML = "<em>" + PostInfo.CreatorName + "</em>";
    
    
    if( this.AllowedToEdit )
	{
	    MetaElement.innerHTML += " <a href='#' onClick='return SaveTweet(\"" + PostInfo.Id + "\")'>approve</a> <a href='#' onclick='return FollowTweeter(\"" + PostInfo.CreatorName + "\")'>follow</a>";
	}
	
	if(  PostInfo.CreatorThumbnail )
    {
        NewItem.className += " Thumbnail";
        NewItem.style.backgroundImage = "url(" + PostInfo.CreatorThumbnail + ")";
    }
    
    
    if( this.AllowedToEdit && !PostInfo.Content.match( /<object/ ) )
    {
        ContentElement.onmousedown = Wiki.ClickRegion;
    }
    
    NewItem.Meta = PostInfo;
    
    NewItem.appendChild( ContentElement );
    NewItem.appendChild( MetaElement );
    
    
    
    //NewItem.innerHTML = NewItem.innerHTML;
    
    this.ConnectNewPost( NewItem, ListElement, false, 10 );
}

LiveBlog.prototype.AttachNewModeratedComment = function( PostInfo, pHtmlElement )
{
    var ListElement;
    
    if( pHtmlElement != null )
    {
        ListElement = pHtmlElement;
    }
    else if( document.getElementById("Moderated") == null )
    {
        return null;
    }
    else
    {
        ListElement = document.getElementById("Moderated");
    }
    
    if( document.getElementById("LiveBlog_Post" + PostInfo.Id) != null )
    {
        return;
    }
    
    var NewItem = document.createElement("li");
	NewItem.id = "LiveBlog_Post" + PostInfo.Id;
    NewItem.className = "Comment";
    
    var ContentElement = this.ConstructContentElement( PostInfo, 300 );
	    
    var MetaElement = document.createElement("div" );
    MetaElement.className = "Meta";
    
    MetaElement.innerHTML = "<em>" + PostInfo.CreatorName + "</em>";
    
    var CanEdit = ( this.AllowedToEdit && !PostInfo.Content.match( /<(object|embed)/ ) && !ContentElement.innerHTML.match( /<img/ ) );
    
    if( this.AllowedToEdit )
	{
	    MetaElement.innerHTML += " <a href='#' onClick='return ApproveComment(\"" + PostInfo.Id + "\")'>approve</a> <a href='#' onClick='return Delete(\"" + PostInfo.Id + "\", true)'>delete</a> <a href='#' onClick='return InviteWriter(" + PostInfo.CreatorId + ")'>invite</a> <a href='#' onClick='return Ban(\"" + PostInfo.CreatorId + "\", this.parentNode.parentNode )'>ban</a> ";
	}
	
	if(  PostInfo.CreatorThumbnail )
    {
        NewItem.className += " Thumbnail";
        NewItem.style.backgroundImage = "url(" + PostInfo.CreatorThumbnail + ")";
    }
    
    
    if( this.AllowedToEdit && CanEdit && !PostInfo.Content.match( /<object/ ) )
    {
        ContentElement.onmousedown = Wiki.ClickRegion;
    }
    
    
    NewItem.appendChild( ContentElement );
    NewItem.appendChild( MetaElement );
    
    var pMaxCommentsDisplayed = this.MaxCommentsDisplayed;
    if( this.GroupPostsLists.length > 1 )
    {
        pMaxCommentsDisplayed = null;
    }
    
    this.ConnectNewPost( NewItem, ListElement, false, pMaxCommentsDisplayed );
    
    if( 
        this.IsScriptaculousAvailable
        && document.getElementById("Moderated") != null
        && document.getElementById("Moderated").parentNode != null
        && document.getElementById("Moderated").parentNode.parentNode != null
        && document.getElementById("Moderated").parentNode.parentNode.parentNode != null
        && document.getElementById("Moderated").parentNode.parentNode.parentNode.getElementsByTagName("dt").length > 0
    )
    {
        if( this.LastModeratedCommentsPulsate == null || this.LastModeratedCommentsPulsate != this.ModCommentsSince )
        {
            Effect.Pulsate( document.getElementById("Moderated").parentNode.parentNode.parentNode.getElementsByTagName("dt")[0], {pulses:2, duration:1} );
            this.LastModeratedCommentsPulsate = this.ModCommentsSince;
        }
    }    
}


LiveBlog.prototype.AttachNewComment = function( PostInfo )
{
    var NewItem = document.createElement("li");
	NewItem.id = "LiveBlog_Post" + PostInfo.Id;
    NewItem.className = "Comment";
    
    var ContentElement = this.ConstructContentElement( PostInfo );
    
    NewItem.LastModified = PostInfo.Date;
	
    var PostSource = PostInfo.Source;
	if( PostSource != null && PostSource != "" )
	{
	    PostSource = " <span class='Source'>via " + PostSource + "</span>";
	}
	else
	{
	    PostSource = "";
	}
	    
    var MetaElement = document.createElement("div" );
    MetaElement.className = "Meta";
    
    MetaElement.innerHTML = "by <em>" 
        + ( this.UserId == PostInfo.CreatorId ? "you" : PostInfo.CreatorName )
        + "</em>"
        + ( PostInfo.EditorId != "" ? " edited by <em>" + ( this.UserId == PostInfo.EditorId ? "you" : PostInfo.EditorName ) + "</em>" : "" )
        + PostSource;
    
    
    var CanEdit = ( this.AllowedToEdit && !PostInfo.Content.match( /<(object|embed)/ ) && !ContentElement.innerHTML.match( /<img/ ) );
    
    if( this.AllowedToEdit )
	{
	    MetaElement.innerHTML += " <a href='#' onClick='return InviteWriter(" + PostInfo.CreatorId + ")'>invite</a> "
	   + ( PostInfo.Group == 0 ? "<a href='#' onClick='return Sticky(" + PostInfo.Id + "," + ( PostInfo.Rank == "0" ? "false" : "true" ) + ", this)'>" + ( PostInfo.Rank == "0" ? "unstick" : "stick" ) + "</a> " : "" )
	   + ( CanEdit ? "<a href='#' onClick='return Edit(" + PostInfo.Id + ", this)' class='Edit'>edit</a> " : "" )
	   + "<a href='#' onClick='return Delete(" + PostInfo.Id + ")'>delete</a> <a href='#' onClick='return Ban(" + PostInfo.CreatorId + ")'>ban</a>";
	}
	
	if(  PostInfo.CreatorThumbnail )
    {
        NewItem.className += " Thumbnail";
        NewItem.style.backgroundImage = "url(" + PostInfo.CreatorThumbnail + ")";
    }
    
    if( this.AllowedToEdit && CanEdit )
    {
        ContentElement.onmousedown = Wiki.ClickRegion;
    }
    
    NewItem.appendChild( ContentElement );
    NewItem.appendChild( MetaElement );
    
    this.ConnectNewPost( NewItem, null, null, null, PostInfo.Rank, PostInfo.Group );

}

LiveBlog.prototype.AttachNewCrossposting = function( PostInfo )
{
    var NewItem = document.createElement("li");
	var PostType;
	try
	{
	    PostType = PostInfo.Type;
	}
	catch( Error )
	{
	    PostType = 1;
	}
	
	
    NewItem.id = "LiveBlog_Post" + PostInfo.Id;
    NewItem.className = "Writer" + PostInfo.CreatorId;
    NewItem.style.backgroundColor = this.FadeColor_Start;
    
    if( PostInfo.CreatorThumbnail )
	{
	    //NewItem.className = "Thumbnail";
	    //NewItem.style.background = "url(" + PostInfo.CreatorThumbnail + ")";
	}
	
    
    if( PostType == 2 )
	{
	    var ImageContent = PostInfo.Content;
	
	    switch( this.Width )
	    {
	        case 300:
	            ImageContent = ImageContent.replace( /\.(jpe?g|png|gif)$/, "_300.$1" );
	            break;
	        case 400:
	            ImageContent = ImageContent.replace( /\.(jpe?g|png|gif)$/, "_400.$1" );
	            break;
	    }
	
	
	
	    NewItem.innerHTML = "<strong>" + LiveBlog.ConvertServerTimeToLocalTimeFriendlyString( PostInfo.Date ) + "<abbr title='Your local time'>*</abbr></strong> <em>" + PostInfo.CreatorName + " -</em><br /><img src='" + ImageContent + "'/>";
	}
	else
	{
	    var ObjectEmbedContent = PostInfo.Content;
	
	    if( ObjectEmbedContent.match( /(<object|<embed)/i ) )
	    {
	        ObjectEmbedContent = ObjectEmbedContent.replace( /&quot;/g, "\"" );
	    
	        switch( this.Width )
	        {
	            case 300:
	                ObjectEmbedContent = LiveBlog.ResizeObjectEmbed( ObjectEmbedContent, 300, 250 );
	                break;
	            case 400:
	                ObjectEmbedContent = LiveBlog.ResizeObjectEmbed( ObjectEmbedContent, 400, 334 );
	                break;
	        }
	        
	        ObjectEmbedContent = "<br />" + ObjectEmbedContent;
	    }
	
	    NewItem.innerHTML = "<strong>" + LiveBlog.ConvertServerTimeToLocalTimeFriendlyString( PostInfo.Date ) + "<abbr title='Your local time'>*</abbr></strong> <em>" + PostInfo.CreatorName + " -</em> " + ObjectEmbedContent;
        
	}
	
    this.ConnectNewPost( NewItem );
}

LiveBlog.prototype.ConstructContentElement = function( PostInfo, pWidth )
{
    var ContentElement = document.createElement("div");
    ContentElement.className = "Content";
    
    if( pWidth == null )
    {
        pWidth = this.Width;
    }
    
    
    var PostType;
	try
	{
	    PostType = PostInfo.Type;
	}
	catch( Error )
	{
	    PostType = 1;
	}
	
	if( PostType == 2 )
	{
	    var ImageContent = PostInfo.Content.replace( /&quot;/g, "\"" );
	    
	    switch( pWidth )
	    {
	        case 300:
	            ImageContent = ImageContent.replace( /\.(jpe?g|png|gif)$/, "_300.$1" );
	            break;
	        case 400:
	            ImageContent = ImageContent.replace( /\.(jpe?g|png|gif)$/, "_400.$1" );
	            break;
	    }
	    
        ContentElement.innerHTML = "<img src='" + ImageContent + "' onload='LiveBlog.NewContentImageLoaded(this)'/>";
	}
	else
	{
	    if( PostInfo.Content.match( /<(object|embed)/ ) )
	    {
	        var ObjectContent = PostInfo.Content;
	        ObjectContent = ObjectContent.replace( /&quot;/g, "\"" );
	    
	        switch( pWidth )
	        {
	            case 300:
	                ContentElement.innerHTML = LiveBlog.ResizeObjectEmbed( ObjectContent, 300, 250 );
	                break;
	            case 400:
	                ContentElement.innerHTML = LiveBlog.ResizeObjectEmbed( ObjectContent, 400, 334 );
	                break;
	            default:
	                ContentElement.innerHTML = ObjectContent;
	                break;
	        }
	    }
	    else
	    {
	        ContentElement.innerHTML = this.TranslateContent( PostInfo.Content.replace( /&quot;/g, "\"" ), ContentElement );
	    }
	}
	
	return ContentElement;
    
}

LiveBlog.prototype.AttachNewPost = function( PostInfo, pHowLong )
{
	var NewItem = document.createElement("li");
	var PostType;
	try
	{
	    PostType = PostInfo.Type;
	}
	catch( Error )
	{
	    PostType = 1;
	}
	
	NewItem.LastModified = PostInfo.Date;
	
	var PostSource = PostInfo.Source;
	if( PostSource != null && PostSource != "" )
	{
	    PostSource = " <span class='Source'>via " + PostSource + "</span>";
	}
	else
	{
	    PostSource = "";
	}  
	
    NewItem.id = "LiveBlog_Post" + PostInfo.Id;
    NewItem.className = "";
    
    if( this.FadeColor_End != null )
    {
        NewItem.style.backgroundColor = this.FadeColor_Start;
    }
    //NewItem.style.display = "none";
    
    NewItem.appendChild( this.ConstructContentElement( PostInfo ) );
    
    if( PostInfo.CreatorThumbnail )
    {
        NewItem.className += " Thumbnail";
        NewItem.style.backgroundImage = "url(" + PostInfo.CreatorThumbnail + ")";
    }
    
    if( PostType == 2 )
	{
	   NewItem.innerHTML += "<div class='Meta'>by <em>" 
            + ( this.UserId == PostInfo.CreatorId ? "you" : PostInfo.CreatorName )
            + "</em>" 
            + ( PostInfo.EditorId != "" ? " edited by <em>" 
                + ( this.UserId == PostInfo.EditorId ? "you" : PostInfo.EditorName )
                + "</em>" : "" )
            + PostSource
            + " at " + LiveBlog.ConvertServerTimeToLocalTimeFriendlyString( PostInfo.Date ) + "</div>";
	}
	else
	{
	    var MetaElement = document.createElement("div" );
	    MetaElement.className = "Meta";
	    
	    MetaElement.innerHTML += "by <em>" + PostInfo.CreatorName + "</em>" + PostSource + " at " + LiveBlog.ConvertServerTimeToLocalTimeFriendlyString( PostInfo.Date ) + "";
	    
	    NewItem.appendChild( MetaElement );
	    
	    if( this.AllowedToEdit && !PostInfo.Content.match( /<(object|embed)/ ) && PostType != 4 )
	    {
	        NewItem.firstChild.onmousedown = Wiki.ClickRegion;
	    }
        
	}
	
	if( this.AllowedToEdit && PostType != 4 )
	{
	    NewItem.lastChild.innerHTML += "  ";
	    
	    if( PostType != 2 && !PostInfo.Content.match( /<(object|embed)/ ) )
	    {
	        NewItem.lastChild.innerHTML += "<a href='#' onClick='return Edit(" + PostInfo.Id + ", this)' class='Edit'>edit</a> ";
	    }
	    
	    NewItem.lastChild.innerHTML += "<a href='#' onClick='return Sticky(" + PostInfo.Id + "," + ( PostInfo.Rank == "0" ? "false" : "true" ) + ", this)'>" + ( PostInfo.Rank == "0" ? "unstick" : "stick" ) + "</a> ";
	    
	    
	    NewItem.lastChild.innerHTML += "<a href='#' onClick='return Delete(" + PostInfo.Id + ")'>delete</a>";
	}
	
    this.ConnectNewPost( NewItem, null, null, null, PostInfo.Rank, PostInfo.Group );
    
    if( pHowLong != null )
    {
        setTimeout( function()
        {
            LiveBlog.GetInstance().DisconnectPost( NewItem );
        }, pHowLong * 1000 );
    }
    
}

LiveBlog.prototype.TranslateContent = function( pContent, pElement )
{
    if( this.DisplayLanguage 
        && ! this.AllowedToEdit 
        && ! pContent.match( "(<embed|<object|<img)" )
        )
    {
        var TranslatedText = "<em>" + pContent + "</em>";
        
        var ResultElement = pElement;
    
        google.language.translate( pContent, "", this.DisplayLanguage, function(result) {
          if (!result.error) {
            ResultElement.innerHTML = result.translation;
          }
          else
          {
            ResultElement.innerHTML = ResultElement.innerHTML.replace( /^<em>/, "" );
            ResultElement.innerHTML = ResultElement.innerHTML.replace( /<\/em>$/, "" );
            
          }
        });
        
        return TranslatedText;
    }
    else
    {
        return pContent;
    }
}


LiveBlog.PageHeight = function()
{
  var y, h;
  if(window.innerHeight && window.scrollMaxY)
    y = window.innerHeight + window.scrollMaxY;
  else if(document.body.scrollHeight)
    y = document.body.scrollHeight;
  else y = document.body.offsetHeight;
  if(window.innerHeight) h = window.innerHeight;
  else if(document.documentElement && document.documentElement.clientHeight)
    h = document.documentElement.clientHeight;
  else if(document.body) h = document.body.clientHeight;
  return ((y > h) ? y + 20 - h : false); 
}


LiveBlog.YPosition = function()
{
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
}

LiveBlog.findPosX = function(obj)
{
    if( LiveBlog.GetInstance().IsScriptaculousAvailable )
    {
        return Element.cumulativeOffset(obj)[0];
    }


    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

LiveBlog.findPosY = function(obj)
{

    if( LiveBlog.GetInstance().IsScriptaculousAvailable )
    {
        return Element.cumulativeOffset(obj)[1];
    }

    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

LiveBlog.NewContentImageLoaded = function( NewImage )
{
    if( ( LiveBlog.YPosition() + 50 + NewImage.clientHeight ) >= LiveBlog.PageHeight() )
    {
        if( LiveBlog.GetInstance().NewestAtBottom )
        {
            LiveBlog.ScrollToBottom();
        }
    }
}

LiveBlog.ScrollToBottom = function()
{
    /*
    if( Effect && Effect.ScrollTo != null && false )
    {
        LiveBlog.LastPosition = 666;
        LiveBlog.Scroll_Handler( "BottomPagination" );
        return;
    }
    */
    
    try
    {
        clearInterval( LiveBlog.ScrollInterval );
    }
    catch( Error ) {}
    
    LiveBlog.LastPosition = LiveBlog.YPosition();
    
    LiveBlog.ScrollInterval = setInterval('LiveBlog.Scroll_Handler(LiveBlog.YPosition()+15)',50);
}

LiveBlog.Scroll_Handler = function( DestinationY )
{
    /*
    if( Effect && Effect.Tween && false )
    {
        var options = arguments[1] || { }, scrollOffsets = document.viewport.getScrollOffsets(), elementOffsets = $(element).cumulativeOffset();
          if (options.offset)
            elementOffsets[1] += options.offset;
          return new Effect.Tween(null, scrollOffsets.top, elementOffsets[1], options, function (p)
          {
            PositionDiffFromLastScroll = p.round() - LiveBlog.LastPosition;
            if( PositionDiffFromLastScroll < 50 || LiveBlog.LastPosition == 666 && LiveBlog.LastPosition != -666 )
            {
                window.scrollTo(0, p.round());
                LiveBlog.LastPosition = p.round();
            }
            else
            {
                LiveBlog.LastPosition = -666;
            }
          });
    }
    */
  



    PositionDiffFromLastScroll = DestinationY - LiveBlog.LastPosition;
    
    if( 
        LiveBlog.YPosition() + 10 >= LiveBlog.PageHeight()
        || PositionDiffFromLastScroll < 15
    )
    {
        clearInterval( LiveBlog.ScrollInterval );
    }
    else
    {
        window.scrollTo( 0, DestinationY );
        LiveBlog.LastPosition = DestinationY;
    }
}

LiveBlog.ConvertServerTimeToLocalTime = function( ServerTime )
{
    var CurrentUTCServerTime = new Date( ServerTime );
    
    return new Date( CurrentUTCServerTime.getTime() - ( new Date() ).getTimezoneOffset() * 60000 );
    
}

LiveBlog.ConvertServerTimeToLocalTimeFriendlyString = function( ServerTime )
{
    var LocalTime = LiveBlog.ConvertServerTimeToLocalTime( ServerTime );
    
    var IsEnglish = ( LiveBlog.UserLanguage().match( /^en/i ) != null );
    
    var MidnightToday = new Date();
    MidnightToday.setHours( 0 );
    MidnightToday.setMinutes( 0 );
    MidnightToday.setSeconds( 0 );
    
    var Minutes = LocalTime.getMinutes();
    Minutes = ( Minutes < 10 ? "0" + Minutes : Minutes );
    
    var Hours = ( LocalTime.getHours() % 12 );
    Hours = ( Hours == 0 ? "12" : Hours );
    
    var MidnightYesterday = MidnightToday.getTime() - 1000 * 60 * 60 * 24;
    MidnightYesterday = ( new Date() ).setTime( MidnightYesterday );
    
    
    if( LocalTime > MidnightToday )
    {
        if( IsEnglish )
        {
            return Hours + ":" + Minutes + " " + ( LocalTime.getHours() < 12 ? "AM" : "PM" );
        }
        else
        {
            return LocalTime.getHours() + ":" + Minutes;
        }
    }
    else if( LocalTime > MidnightYesterday && IsEnglish )
    {
        return Hours + ":" + Minutes + " " + ( LocalTime.getHours() < 12 ? "AM" : "PM" ) + " yesterday";
    }
    else if( !IsEnglish )
    {
        return ( LocalTime.getMonth()+1 ) + "/" + ( LocalTime.getDate() ) + "/" + LocalTime.getFullYear() + " " + LocalTime.getHours() + ":" + Minutes
    }
    else
    {
        return LocalTime.toLocaleString();
    }
}


LiveBlog.ResizeObjectEmbed = function( Html, Width, Height )
{
    var WidthFound = ( new RegExp( /width=['"]?(\d+)['"]?/i ) ).exec( Html )
    var HeightFound = ( new RegExp( /height=['"]?(\d+)['"]?/i ) ).exec( Html );
    
    if( WidthFound && HeightFound )
    {
        WidthFound = parseInt( WidthFound[1] );
        HeightFound = parseInt( HeightFound[1] );
    
        var Ratio = 1.0;
        
        Width = parseInt( Width );
        Height = parseInt( Height );

        if ( WidthFound > Width )
        {
            Ratio = 1.0 * Width / WidthFound;
        }

        if ( HeightFound > Height )
        {
            if ( 1.0 * Height / HeightFound < Ratio )
            {
                Ratio = 1.0 * Height / HeightFound;
            }
        }

        Width = Math.round( WidthFound * Ratio );
        Height = Math.round(  HeightFound * Ratio );
        
    }

    Html = Html.replace( /width=['"]?([0-9]+)['"]?/g, "width='" + Width + "'" );
    Html = Html.replace( /height=['"]?([0-9]+)['"]?/g, "height='" + Height + "'" );

    return Html;
}


LiveBlog.prototype.FadeIn = function( pWho )
{
    if( document.getElementById(pWho) == null ) return;

    if( this.FadeColor_End != null )
    {
        Fat.fade_element( pWho, 15, 1000, this.FadeColor_Start, this.FadeColor_End );
    }
}

LiveBlog.prototype.RemoveTwitterQueryScript = function()
{
    var PollScriptEmbed = document.getElementById("TwitterSearchQuery");
    if( PollScriptEmbed != null )
    {
        PollScriptEmbed.parentNode.removeChild( PollScriptEmbed );
    }
}

LiveBlog.prototype.TwitterPing = function( pTweets, pSearchQuery )
{
    this.RemoveTwitterQueryScript();

    var SearchBox = document.getElementById("TwitterSearch");
    if( SearchBox != null )
    {
        SearchBox.style.fontStyle = "normal";
    }

    var Tweets;
    if( pTweets != null )
    {
        Tweets = pTweets.results;
    }
    else
    {
        Tweets = new Array();
    }
    
    if( pSearchQuery != null )
    {
        this.TwitterSearchQuery = pSearchQuery;
    }
    

    var CurrentTweet;
    var LastId = -1;
    var HtmlContent;
    
    for( var i = Tweets.length - 1; i >= 0; i-- )
    {
        HtmlContent = Tweets[i].text;
        HtmlContent = HtmlContent.replace( /(http:\/\/[\w\.\/\?:\#,~=&%\-\+]+)/gi, "<a href='$1'>$1</a>" );
        HtmlContent = HtmlContent.replace( /@(\w+)/g, "@<a href='http://twitter.com/$1'>$1</a>" );
    
        CurrentTweet = new Object();
        CurrentTweet.Content = HtmlContent; //+ " <em>via</em> <a href='http://twitter.com/" + Tweets[i].from_user + "'>twitter</a>";
        CurrentTweet.CreatorName = Tweets[i].from_user;
        CurrentTweet.Id = "tweet" + Tweets[i].id;
        CurrentTweet.Type = 1;
        CurrentTweet.Date = new Date( ( new Date( Date.parse( Tweets[i].created_at ) ) ).getTime() + ( new Date() ).getTimezoneOffset() * 60000 );
        CurrentTweet.CreatorThumbnail = Tweets[i].profile_image_url;
        CurrentTweet.TwitterUserId = Tweets[i].from_user_id;
        
        LastId = Tweets[i].id;
    
        this.AttachNewTweet( CurrentTweet, document.getElementById("TwitterModerated")  );       
    }
    
    if( LastId == -1 && pTweets != null && pTweets.since_id  )
    {
        LastId = pTweets.since_id;
    }
    
    this.TwitterLastId = LastId;
    
    var TimeoutInterval;
    if( pTweets == null )
    {
        TimeoutInterval = 1000;
    }
    else
    {
        TimeoutInterval = 30000;
    }
    
    this.IsTwitterPolling = true;    
    
    if( this.TwitterSearchTimeout != null )
    {
        clearInterval( this.TwitterSearchTimeout );
    }
    this.TwitterSearchTimeout = setTimeout( function() {
    
        if( LiveBlog.GetInstance().TwitterSearchQuery != null )
        {
            LiveBlog.GetInstance().RemoveTwitterQueryScript();
            var PollScript = document.createElement("script");
            PollScript.src = "http://search.twitter.com/search.json?q=" + escape( LiveBlog.GetInstance().TwitterSearchQuery ) + "&callback=LiveBlog.GetInstance().TwitterPing&rpp=4&since_id=" + LiveBlog.GetInstance().TwitterLastId + "&rand=" + Math.round( 10000000 * Math.random() );
            PollScript.type = "text/javascript";
            PollScript.defer = "defer";
            PollScript.id = "TwitterSearchQuery";
	        document.getElementsByTagName("head")[0].appendChild( PollScript );
	        var SearchBox = document.getElementById("TwitterSearch");
	        if( SearchBox != null )
	        {
	            SearchBox.style.fontStyle = "italic";
	        }
	    }
	    else
	    {
	        LiveBlog.GetInstance().TwitterPing();
	    }
	    
	    }, TimeoutInterval );
}


LiveBlog.ReloadPage = function()
{
    window.location.hash = "#";
    window.location.reload();
    
    return false;
}


var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#FFFF33";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}