﻿function start_activity_feed() {
    getActivityFeed();
    setInterval("getActivityFeed()", 10000);
}

function getActivityFeed() {
    // activity feed
    //PageMethods.GetActivityFeed('test', 0, 0, 0, OnSucceeded, OnFailed);
    if (typeof (WebService) != "undefined") {

        lastfeed = $('#spanLastFeed').text();
        //alert('will call...');
        WebService.GetActivityFeed(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, lastfeed, null, false, true, OnFeedSucceeded);
    }
}

function OnFeedSucceeded(resultStr) {//alert('activity feed: ' + resultStr);
    jsonResult = eval(resultStr);
    result = jsonResult[0];


    $('#spanLastFeed').text(result.lastfeed);


    $('#divActivityFeed #spanDate').text(result.lastfeed);

    if (result.itemcount > 0) {
        $('#divActivityFeed').fadeOut();
        //alert(result.items[0].photo);
        $('#divActivityFeed #spanName').text(result.items[0].name);
        $('#divActivityFeed #spanActivity').html(result.items[0].activity);
        $('#divActivityFeed #spanDate').text(result.items[0].datetime);
        $('#divActivityFeed #imgPhoto').attr('src', result.items[0].photo);
        $('#divActivityFeed #spanTrackingID').text(result.items[0].trackingid);
        $('#divActivityFeed').fadeIn();
        
        if($('#tableActivityFeed tr').length <= 0)
            GenerateActivityFeedHTML(result.items);
    }
    else {
        //$('#divActivityFeed').hide();

    }

}

function OnFeedFailed(error) {
    alert(error);
}
/*------------------------------------------
activity feed call back
--------------------------------------------*/
function OnSucceeded(result) {
    jsonresult = eval(result);

    alert(jsonresult[0].result);
}

function OnFailed(error) {
    alert(error);
}

/*--------------------- status bar ---------------------------*/
function toggleFavorite(divid) {
    seldiv = '#div' + divid;
    if ($(seldiv).is(':visible'))
        $(seldiv).hide('slide', { direction: 'left' }, 150);
    else
        $(seldiv).show('slide', { direction: 'left' }, 150);
}

function UpdateStatusBar(result) {
    // sp
    //alert(result.sp.length);
    if (result.sp.length > 0) {
        $('#spanSPCount').text(result.sp.length);
        $('#divChat').show();
        $('#tableActiveSPList').empty();
        for (var i = 0; i < result.sp.length; i++) {
            sp = result.sp[i];
            onesp = '<tr><td><img src="' + sp.photo + '" height="20" width="20" /></td><td style="padding-left:3px;">' + sp.name + '</td><td style="padding-left:3px;"><input type="checkbox" class="checkUsername" checked="checked" id="cb_' + sp.username + '" /></td></tr>';
            $('#tableActiveSPList').append(onesp);
        }
    }
    else {
        $('#divChat').hide();
    }

    // pending invitation
    if (result.invitation.length > 0) {//alert(resultStr);
        for (var i = 0; i < result.invitation.length; i++) {
            invitation = result.invitation[i];
            divInvNotice = $('#divInvitationNotice').clone();
            //alert($(divInvNotice).length);
            $('body').append(divInvNotice);

            $(divInvNotice).attr('id', 'divInvitationNotice_' + invitation.guid);
            $(divInvNotice).find('.spanInvitedBy:first').text(invitation.invitedBy);
            $(divInvNotice).find('.aAcceptInvitation:first').attr('href', "javascript:acceptInvitation('" + invitation.guid + "')");
            $(divInvNotice).find('.aRejectInvitation:first').attr('href', "javascript:rejectInvitation('" + invitation.guid + "')");

            showChatInvitation('divInvitationNotice_' + invitation.guid);
            // disappear and put pending invitation list after 10 seconds if user do nothing (either join or refuse)
            setTimeout(function() {
                $(divInvNotice).hide('slide', { direction: 'down' }, 500);
                // purge this div
                // put icon(guid) to pending invitation list

            }, 10000);


        }
    }

    // new message
    //alert(resultStr);
    if (result.newmessage.length > 0) {
        //alert(resultStr);
        addNewMessagesToChatRooms(result.newmessage);
    }
}

function GenerateActivityFeedHTML(items) {
    $('#tableActivityFeed').empty();
    $('#divLoadingActivityFeed').hide();
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        var imglinkheader = '';
        var imglinkfooter = '';
        if (item.photourl.length > 0) {
            imglinkheader = '<a href="' + item.photourl + '">';
            imglinkfooter = "</a>";
        }
        var namelinkheader = '';
        var namelinkfooter = '';
        if (item.nameurl.length > 0) {
            namelinkheader = '<a href="' + item.nameurl + '" class="aActivityFeedNameLink">';
            namelinkfooter = "</a>";
        }

        imgtag = imglinkheader + '<img src="' + item.photo + '" width="42" />' + imglinkfooter;

        if ((item.photo.indexOf('specialpeople_default.jpg') >= 0 || item.photo.indexOf('img_female.gif') >= 0 || item.photo.indexOf('img_male.gif') >= 0) && item.fbuid.length > 0)
            imgtag = '<fb:profile-pic uid="' + item.fbuid + '" width="42" height="42" linked="true"></fb:profile-pic>';

        //alert(imgtag);  
        itemtr = '<tr> \
            <td> \
            <table cellpadding="0" cellspacing="0">\
              <tr> \
                <td style="padding-left:7px; padding-right:7px"><table cellpadding="0" cellspacing="0"> \
                  <tr> \
                    <td valign="top" width="42">' + imgtag +
                    '</td> \
                    <td valign="top" style="padding-left:10px"> \
                    <div align="left"> \
                    <table cellpadding="0" cellspacing="0"> \
                      <tr> \
                        <td class="subtext" style="padding-bottom:3px;">' + namelinkheader + '<span style="color:#779772"><strong>' + item.name + '</strong></span>' + namelinkfooter + ' ' + item.location + '</td> \
                      </tr> \
                      <tr><td style="line-height:10px;">' + item.activity + '</td></tr> \
                      <tr> \
                        <td class="subtext_smallest" style="padding-top:2px;">' + item.datetime + ' &nbsp; <a class="aActivityFeedCommentCount" href="javascript:toggleActivityFeedComment(' + item.trackingid + ')" style="display:none;"><span class="count">1 </span> comments</a></td> \
                      </tr> \
                      <tr><td style="display:none; padding-top:5px;" id="tdActivityFeedComment_trackingid"> \
                      comment list \
                      </td></tr> \
                <tr> \
                <td style="padding-top:5px; display:none;"> \
                <input name="textfield" type="text" class="txtActivityFeedComment" value="Write a comment..." size="30"/> \
                </td> \
              </tr> \
                    </table> \
                    </div> \
                    </td> \
                  </tr> \
                </table></td> \
              </tr> \
              </table></td> \
          </tr> \
          <tr> \
        <td style="height:10px; vertical-align:middle; text-align:center"><img src="images/1x1_grey.gif" width="250" height="1" /></td> \
        </tr>';
        $('#tableActivityFeed').append(itemtr);
    }
}

function OnWriteCommentSuccess(resultStr) {//alert(resultStr);
    var results = eval(resultStr);
    var result = results[0];
	$('#spanProgress_' + result.trackingid).hide();
    if (result.success) {
		
        var tr = '<tr> \
                <td style="padding-right:7px; padding-top:5px"> \
                <table cellpadding="0" cellspacing="0"> \
                  <tr> \
                    <td valign="top"><img src="' + result.photo + '" width="20" /> \
                    </td> \
                    <td valign="top" style="padding-left:10px"> \
                    <table cellpadding="0" cellspacing="0"> \
                        <tr> \
                          <td class="subtext" style="padding-bottom:3px;"> \
                          <span style="color:#779772"><strong>' + result.commentedby + ' </strong></span> \
                          ' + result.comment + ' \
                          </td> \
                        </tr> \
                        <tr> \
                          <td class="subtext_smallest">' + result.commentedon + '</asp:Label></td> \
                        </tr> \
                    </table></td> \
                  </tr> \
                </table></td> \
              </tr>';
        var septr = '<tr> \
                    <td style="border-bottom:dotted 1px #999999; height:5px"></td> \
                  </tr>';

        var commentcount = parseInt($('#tableActivityFeed_' + result.trackingid).find('.count:first').text());
        /* if (commentcount <= 0) */tr = septr + tr;
        $('#tdActivityFeedComment_' + result.trackingid).show();
        $('#tableActivityFeed_' + result.trackingid).find('.count:first').text(commentcount + 1);
        $('#tableActivityFeed_' + result.trackingid).find('.aActivityFeedCommentCount').show();
        //$('#tableComment_' + result.trackingid)
        $('#tableComment_' + result.trackingid).fadeIn("slow", function() {$(this).prepend(tr)});
    }
	else {
		$('#spanerror_' + result.trackingid).text('failed to write comment. Please try again.');
		$('#spanerror_' + result.trackingid).show();
	}
}

function OnWriteCommentFail(error) {
	$('#spanProgress_' + result.trackingid).hide();
	$('#spanerror_' + result.trackingid).text('failed to write comment. Please try again.');
	$('#spanerror_' + result.trackingid).show();
}

/* recommend & activity feed */
function toggleActivityFeedComment(trid) {
    $('#tdActivityFeedComment_' + trid).toggle();

}



function toggleSlide(handleid, id, showDirection, hideDirection) {
    if ($('#' + id).is(':visible')) {
        hideSlide(id, hideDirection);
    }
    else {
        showSlide(handleid, id, showDirection);
    }
}

function showSlide(handleid, id, direction) {
    var p = $('#' + handleid).position();
    $('#' + id).css('top', p.top + $('#' + handleid).height());
    $('#' + id).css('left', p.left + $('#' + handleid).width() - $('#' + id).width());
    $('#' + id).show('slide', { direction: direction }, 150);
}

function hideSlide(id, direction) {
   
    $('#' + id).hide('slide', { direction: direction }, 150);
}

function hideAFFilter() {
    if (checkMouseIn('divAFFilter')) {
        setTimeout('hideAFFilter()', 2000);
       
    }
    else {
        //hideSlide('divAFFilter', 'up');
        $('#divAFFilter').hide('slide', { direction: 'up' }, 150);
    }
}
