
var currentNewsTitle;
var currentNewsDate;
var currentShowStatusUpdates;
var currentShowComments;

function SetNewsUpdate(dt)
{
    currentNewsTitle= document.title;
    currentNewsDate= dt;
    setInterval("GetNewsUpdates()",60000); //60 seconds
}

function GetNewsUpdates()
{
    var statuses= (currentShowStatusUpdates) ? "1" : "0";
    
    $.post("news/news_refresh.aspx", {MaxDate: currentNewsDate, ShowStatusUpdates: statuses}, function(response) { 
                
         var arr;                     
         try { arr= eval(response); }
         catch (e) { return; }
          
         var total= arr[0].Total;
      
         if (total > 0)
         {
            var display= (total >= 20) ? "20+" : total;
            var s= (total == 1) ? "" : "s";
         
            $(".refresh-anim").html("Show " + display + " more post" + s).show();
            document.title= "("+ total +") " + currentNewsTitle;
         }
         else
         {
            $(".refresh-anim").hide();
            document.title= currentNewsTitle;
         }    
    });
}

function OnNewsFeedAdd()
{
	var Output = $("#statusupdates");
    
    if (Output.find("tr.row-1").length >= 15)
    {
        Output.find("tr.row-1").eq(14).hide();
    }
}

function SetNewsPreferences(bStatus,bComment)
{
    currentShowStatusUpdates= bStatus;
    currentShowComments= bComment;
    
    var statusLink= $("#toggle-status-updates");
    var commentLink= $("#toggle-comments");
        
    if (statusLink.length > 0)
    {
        var statusDisplay= (bStatus) ? "Hide Status Updates" : "Show Status Updates";
        statusLink.html(statusDisplay);
        statusLink.click(function() { OnToggleStatusUpdates(); return false; });  
    }
    
    if (commentLink.length > 0)
    {
        var commentDisplay= (bComment) ? "Hide Comments" : "Show Comments";
        commentLink.html(commentDisplay);      
        commentLink.click(function() { OnToggleComments(); return false });
    }
        
    $("#new-posts").show();
}

function OnToggleStatusUpdates()
{
    UpdateNewsPreference("statusupdate",!currentShowStatusUpdates, function() { window.location= "news.aspx"; });
}

function OnToggleComments()
{   
    currentShowComments= !currentShowComments;
    var display= (currentShowComments) ? "Hide Comments" : "Show Comments";
 
    $("#toggle-comments").html(display);
    ShowComments(currentShowComments); 
    
    UpdateNewsPreference("comment",currentShowComments);
}

function ShowComments(bShow)
{    
    if (bShow)
    {
        $('.comment-list-active.ls-off').removeClass("ls-off").addClass("ls-on");
    }
    else
    {
        $('.comment-list-active.ls-on').removeClass("ls-on").addClass("ls-off");
        $('.comment-list-inactive.ls-on').removeClass("ls-on").addClass("ls-off");
    }   
}

function UpdateNewsPreference(type,bVal,CallBack)
{    
    var val= (bVal) ? "1" : "0";
    $.post("news/newspreferences_update.aspx", {Type: type, Value: val}, CallBack);
}

function ShowConsecutivePosts(Sender)
{
    var tr = $(Sender).parent().parent().nextAll();
    
    var rowclass = "row-1";
    var onclass = "ls-open";
    var offclass = "ls-off";
    var removeclass = offclass;
    var addclass = onclass;
    var lesstext = "less";
    var moretext = "more";
    var lessclass = "see-less";
    var moreclass = "see-more";
    
    if ($(Sender).is("."+lessclass))
    {
        removeclass = onclass;
        addclass = offclass;
        $(Sender).text($(Sender).text().replace(lesstext, moretext));
        $(Sender).removeClass(lessclass);
        $(Sender).addClass(moreclass);
    }
    else if ($(Sender).is("."+moreclass))
    {
        $(Sender).text($(Sender).text().replace(moretext, lesstext));  
        $(Sender).removeClass(moreclass);
        $(Sender).addClass(lessclass);  
    }
   
    for (i = 0; i < tr.length; i++)
    {
        var c = $(tr[i]).attr('class');
        
        if ($(tr[i]).is("."+removeclass))
        {
            $(tr[i]).removeClass(removeclass).addClass(addclass)
        }
        else
        {
            if (c == rowclass)
                return;
        }    
    }
}