
var iCount=3;
var replyUserId = "";
var replyScreenName = "";
var Discussion = function(parent)
{

    //parent should be discussionFrame
    //connect the reply events
    $('.discussionReply', parent).live('click',
        function()
        {
            replyUserId = $(this).attr('userId');
            replyScreenName = $(this).attr('screenName');
            $('.discussionReply .s2', parent).text(replyScreenName);
            $('.discussionComment', parent).hide();
            $('.discussionReply', parent).show();
            return false;
        }
    );
    
    //connects screen names to their profile page
    $('.discussionProfile', parent).live('click',
        function()
        {
            var hostname = window.location.hostname;
            document.location.href = String.format("http://{1}/User/Profile/{0}", $(this).attr('screenName'), hostname );
            return false;
        }
    );
    
    $('.discussionDelete', parent).live('click',
        function()
        {
            DiscussionDelete(this);
            return false;
        }
    );
};

function DiscussionDelete(deleteElement)
{
    discussionFrame=$(deleteElement).closest(".discussionFrame");
    discussionDeleteId = $(deleteElement).attr('discussionId');
    var hostname = window.location.hostname;
    var url = String.format("http://{1}/User/Discussion.aspx?action=delete&discussionId={0}", 
        discussionDeleteId,
        hostname);
    $.get(url, function(data)
        {
            SearchDiscussion(discussionFrame);
        }
    );

}

function DiscussionAdd(item)
{
    discussionFrame=$(item).closest(".discussionFrame");
    var keyword = oPage.Text().val();
    var text = $('.discussionAdd', discussionFrame).val();
    //alert(discussionFrame+"\n"+$(discussionFrame).attr("id")+$('.discussionAdd').val());
    var hostname = window.location.hostname;
    var url = String.format("http://{3}/User/Discussion.aspx?action=add&replyId={0}&q={1}&text={2}", 
        replyUserId, keyword, text, hostname, iCount)
    $.get(url,function(data) 
        { 
            $('.discussionAdd', discussionFrame).val(""); //clear out the comment just added
            replyUserId="";
            replyScreenName="";
            $('.discussionComment', discussionFrame).show();
            $('.discussionReply', discussionFrame).hide();
            SearchDiscussion(discussionFrame);
        }
    );
}

// userId="" discussion of user id
// allowReply can they reply
// displayKeyword show the keyword
function SearchDiscussion(discussionFrame)
{

    //clear out previous values
    $(discussionFrame).children('.dvDiscussionList').empty();
    var userId = $(discussionFrame).attr('userId');
    var allowReply = $(discussionFrame).attr('allowReply');
    var displayKeyword = $(discussionFrame).attr('displayKeyword');
    
    //figure out what our url should be
    var cls = $('.dvDiscussionList.discussion', discussionFrame).attr('class');
    if (cls==null) return;
    
    var hostname = window.location.hostname;
    var url = "http://{5}/User/Discussion.aspx?action={3}&q={0}&allowReply={1}&displayKeyword={2}&userId={4}&count=3";
    if (cls.indexOf('getByKeyword')>-1)
    {
        var keyword = oPage.Text().val();
        url = String.format(url, keyword, allowReply, displayKeyword, "getByKeyword", userId, hostname);
    }
    else if (cls.indexOf('getByAll')>-1)
        url = String.format(url, "", allowReply, displayKeyword, 'getByAll', userId, hostname);
    else if (cls.indexOf('getByUserId')>-1)
        url = String.format(url, "", allowReply, displayKeyword, 'getByUserId', userId, hostname);
    
    $.get(url, function(data) 
        {
            //var end = new Date();
            //alert( end.getTime() - start.getTime() );
            $(discussionFrame).children('.dvDiscussionList').append(data);
            $(discussionFrame).children('.discussionComment').show();
            $(discussionFrame).children('.discussionReply').hide();
        }
    );
}






//find all services and activate the service for each one of these.
$(document).ready(
    function()
    {
        $('.hide').hide();
        $('.discussionFrame').each( function(){Discussion(this); SearchDiscussion(this);} );
        //$('.photo').each( function(){getPhoto(this)}  );
    }
);




