
function Comments()
{
	this.sCmtPref = 'cmt_';
	this.sSRPref = 'sr_';
	this.sRepToPref = 'repto_';
	this.sRepDivPref = 'rep_sec';
	this.sFirstCmntId = "";
    this.sItemId = null;
    this.sReplyToId = null;
    this.sADD_ACTION = 'add';
    this.sBYITEM_ACTION = 'byitem';
    this.sBYUSER_ACTION = 'byuser';
    this.sINREPLY_ACTION = 'inreply';
    this.sAjaxMyStuffUrl = _GL.getBaseUrl() + 'AjaxServices/MyStuff.asmx/';
    this.oErrLyr = this.oCmntsLyr = this.oDialog = this.sPostCommentHTML = null;
    this.sLoginError = 'You need to <a href="'+_GL.getBaseUrl()+'login.aspx?refer='+encodeURIComponent(location.href)+'">login</a> to post comments.';
	this.sAjaxCmnts = _GL.getBaseUrl() + 'json/jcomments.aspx?action=';
	this.init = function(pCmts)
	{
		this.oErrLyr = new HTMLLayer('sErrorMsg');
		this.oCmntsLyr = new HTMLLayer('ulCmnts');
	}

    this.showMsg = function(pShow, pMsg)
    {
        var b = pShow, o = this.oErrLyr;        
        o.setValue(!b ? "" : pMsg);
        o.show(b);
    }
    
    this.getCommentLayer = function(pCmt)
    {
        var cmt = pCmt, oLI = _GL.createElement('LI');
	    oLI.id = this.sCmtPref + cmt.CommentId;
	    oLI.className = 'comment hentry';
	    
	   /* var s = sCmtSec.replaceTokens(cmt.CommentId,cmt.TotalVotes, cmt.UpVotes, cmt.DownVotes,
	                                  _GL.getBaseUrl() + 'people/'+cmt.GenwiUserId,
	                                  cmt.Screenname,cmt.UserProfileImgSrc,cmt.Text,cmt.ItemId,cmt.TimePosted);
	    */
	    
	    var s = sCmtSec.replaceTokens(_GL.getBaseUrl() + 'people/'+cmt.GenwiUserId,cmt.CommentId,
	                                  cmt.UserProfileImgSrc,cmt.Screenname,cmt.TimePosted,cmt.Text,cmt.ItemId);
	    
	    oLI.innerHTML = s;
	    return oLI;
    }
    
	this.getReplies = function(pId)
	{
		// Here we will make AJAX call.
		var a = aJson, len = a.length, oCmtDiv = _GL.getUIElem('cmt_'+pId);
		for (var i=0; i<len; i++)
		{
		    var oDiv = _GL.createElement('div');
            oDiv.id = a[i].cmt_id;
            oDiv.innerHTML = cmtTemp.replaceTokens(a[i].username,a[i].comment,'sr_'+a[i].cmt_id);
            oDiv.style.marginLeft = '30px';
            oDiv.style.marginTop = '10px';
            oCmtDiv.appendChild(oDiv);
        }
	}
	
	this.replyTo = function(pShow,pId)
	{
	    if (!_GL.isAthenticated())
	    {
	        this.showMsg(true,this.sLoginError);
	        return;
	    }
	    var oLI = _GL.getUIElem(this.sCmtPref+pId), oDiv = _GL.getUIElem(this.sRepDivPref + pId);
	    if (pShow)
	    {
	        if (!oDiv)
	        {
	            oDiv = _GL.createElement('div');
	            oDiv.id = this.sRepDivPref + pId;
	        }
	        
	        oDiv.innerHTML = sReptyToHtml.replaceTokens(pId);
	        oLI.appendChild(oDiv);
	    }
	    else if (oDiv)
	        oLI.removeChild(oDiv);
	}
	
	this.saveComment = function()
	{
	    if (!_GL.isAthenticated())
	    {
	        this.showMsg(true,this.sLoginError);
	        return;
	    }
	    
	    var sUrl = this.sAjaxCmnts + this.sADD_ACTION + '&itemid=' + this.sItemId + '&text=' + encodeURIComponent(_GL.getUIElem('itemComments').value);
	    
	    var oAjax = new Ajax('saveCmntReq', (sUrl + '&rd='+_GL.getRandomNumber()), true, [sPageName.is('channel') ? 'oCmnt.processChannelComment' : 'oCmnt.processSaveComment']);
	 
        oAjax.iResponseType = oAjax.RESPONSE_JSON;
        oAjax.send();   
	}
	
	this.addUserComment = function(pCommentElem)
	{
	    if (!_GL.isAthenticated())
	    {
	        this.showMsg(true,this.sLoginError);
	        return;
	    }
	    
	    if (typeof userCommentsTo == 'undefined')
	        return;
	        
	    var sUrl = this.sAjaxMyStuffUrl + 'AjaxUserCommentsPostComment?UserTo='+userCommentsTo;
        sUrl += '&CommentText=' + _GL.getUIElem(pCommentElem).value;
	    var oAjax = new Ajax('saveCmntReq', (sUrl + '&rd='+_GL.getRandomNumber()), true, ['oCmnt.processReloadPage']);
            oAjax.iResponseType = oAjax.RESPONSE_HTML;
            oAjax.bShowProgress = true;
            oAjax.sShowProgressId = 'btnPostComment';
            this.sPostCommentHTML = _GL.getUIElem('btnPostComment').innerHTML;
            oAjax.send();
	    
	}
	
	this.processSaveComment = function(pResp)
	{
	    var oBtnSpan = _GL.getUIElem('btnPostComment');
	    if (oBtnSpan)
	        oBtnSpan.innerHTML = this.sPostCommentHTML;
	    var oD = pResp.oJSON;
	    if (!oD)
	        return;
	    
	    this.oCmntsLyr.elem.insertBefore(this.getCommentLayer(oD.comment),this.oCmntsLyr.elem.firstChild);
	    var oItemComment = _GL.getUIElem('itemComments');
	    if (oItemComment)
	        oItemComment.value = "";
	}
	
	this.processReloadPage = function()
    {
        _GL.redirect(location.href);
    }
	
    this.processChannelComment = function(pResp)
    {
        _GL.redirect(_GL.getBaseUrl() + 'channel/' + sChannelId + '/comments');
    }
    
	this.saveReply = function(pId)
	{
	    if (!_GL.isAthenticated())
	    {
	        this.showMsg(true,this.sLoginError);
	        return;
	    }
	    this.sReplyToId = pId;
	    var sUrl = this.sAjaxCmnts + this.sADD_ACTION + '&itemid=' + this.sItemId + '&text=' + encodeURIComponent(_GL.getUIElem('reply_text_'+pId).value);
	    sUrl +='&replyto='+pId;   
	    var oAjax = new Ajax('saveCmntReq', (sUrl + '&rd='+_GL.getRandomNumber()), true, ['oCmnt.processReplyComment']);
            oAjax.iResponseType = oAjax.RESPONSE_JSON;
            oAjax.bShowProgress = true;
            oAjax.sShowProgressId = 'replyToBtn';
            oAjax.send();
	}
	
	this.processReplyComment = function(pResp)
	{
	    var oD = pResp.oJSON;
	    if (!oD)
	        return;
	    
	    this.replyTo(false,this.sReplyToId); 
	    var oNewComment = this.getCommentLayer(oD.comment);
	    oNewComment.style.marginLeft = '20px';
	    var oReply = _GL.getUIElem(this.sCmtPref+this.sReplyToId), 
	        oPN = oReply.parentNode ? oReply.parentNode : oReply.parentElement;
	    _GL.insertAfter(oPN, oReply, oNewComment);
	}
	this.expandcommentreplytoggle = function(cId)
	{
    	//Mick: add slide trans? 
	    var c_reply = _GL.getUIElem('c-replies-c'+cId)
	    if((c_reply.style.display == "none") || (c_reply.style.display == ""))
	        c_reply.style.display = "block";
	    else   
	        c_reply.style.display = "none";
	}
	
	this.setStarRating = function()
	{
	    var stars = Math.round(ratingStar/20);
	    for (var i=0;i<stars; i++)
	    {
	        var oRate = _GL.getUIElem('rateItem'+i);
	        if (oRate)
	            oRate.style.backgroundPosition = 'left top';
	    }
	    
	}
}

var oCmnt = new Comments();

//var sCmtSec = '<div class="c-head"><div class="c-votes"><img width="34" height="12" id="c-up-c<#1#>" src="images/c-up.png"/><img width="34" height="12" id="c-down-c<#1#>" src="images/c-down.png"/><a><em><#2#></em></a><a style="display:none;"><em><#3#></em> / <span><#4#></span></a></div><strong><a href="<#5#>" id="c-authc<#1#>"><#6#><img class="profileimg" width="16" height="16" alt="" src="<#7#>"/></a></strong></div><div class="c-body" id="c-text-c<#1#>"><#8#></div><div class="c-box"> <a href="?itemid=<#9#>#c<#1#>"><#10#></a> <a class="c-reply-link" id="repto_<#1#>">Reply</a></div>';

var sCmtSec = '<div class="info"><div class="comment-thumbnail"><a href="<#1#>" id="c-authc<#2#>"><img width="50" class="photo" src="<#3#>" alt="" /></a></div><span class="by"><a class="author fn" href="<#1#>"><#4#></a> said</span><span class="time"> on <abbr class="published"><#5#></abbr></span></div><blockquote class="entry-content"><p><#6#></p></blockquote>';


var sReptyToHtml = '<div style="margin-top:20px;"><textarea id="reply_text_<#1#>" class="commentTextArea" style="width:60%;height:50px;"></textarea><div style="margin-top:5px;" id="replyToBtn"><span class="button"><input type="button" value="Post Reply" onclick="oCmnt.saveReply(<#1#>);" /></span>&nbsp;<a href="#" onclick="oCmnt.replyTo(false,<#1#>);return false;">hide</a></div></div></div>';