﻿// Grab comments from ektron's webservice
// Written By: David Smith
// Date: 20081028 1547

function GetComments(ContentID, writeDiv) {
    if(ContentID != undefined) {
        $j.ajax({
            type: 'POST',
            url: 'http://'+document.domain+'/comments.asmx/GetComments',
            data: 'ContentID='+ContentID,
            dataType: 'xml',
            contentType:  'application/x-www-form-urlencoded',
            success: function(xml) {
                $j(writeDiv).html('<div id="hideComments">Hide Comments</div>');
                var comments = '';
                $j(xml).find('Comment').each(function() {
                    //$j(writeDiv).append('<p>'+$j(this).text()+'</p><br />');
                    comments += '<p>'+$j(this).text()+'</p><br />';
                });
                if(comments!='<p></p><br />') {
                   $j(writeDiv).append(comments);
                } else {
                    $j(writeDiv).append("There are currently no comments.");
                }
                $j('#hideComments').bind('click', function() {
	                RemoveComments('#displayComments');
	            });
            }
        });
    }
}

function RemoveComments(writeDiv) {
    $j(writeDiv).html('<div id="showComments">View Comments</div>');
    $j('#showComments').bind('click', function() {
        GetComments(GetContentID(), "#displayComments");
    });
}

function CommentInit() {
    $j('#showComments').bind('click', function() {
        GetComments(GetContentID(), "#displayComments");
    });
}

function GetContentID() {
    var qs = new Querystring();
    id = qs.get("id");
    if(id==undefined) {
        idNum = $j('#email').html();
        re = new RegExp(/\=([0-9]+)/);
        id = idNum.match(re, "$2");
        return id[1];
    } else {
        return id;
    }
}

