///Related js: FavoriteToolBar.js and DClientPager.js
//DropDown
var upPager;
var downPager;
var sectionID;
var postID;

function ShowPost(hiddenCntId,shownCntId)
{
    if(document.getElementById(hiddenCntId))
    {
        document.getElementById(hiddenCntId).style.display='none';
    }
    if(document.getElementById(shownCntId))
    {
        document.getElementById(shownCntId).style.display='';
    }
}

function AjaxJsonRequest(url,data,callback)
{
    $.ajax({ type: "GET", url: url,data:data, dataType: "json", timeout:200000, success: callback,error:function(){}});
}

function AjaxXmlRequest(url,data,callback)
{
    $.ajax({ type: "GET", url: url,data:data, dataType: "xml", timeout:200000, success: callback,error:function(){}});
}

function AjaxXmlPostRequest(url,data,callback)
{
    $.ajax({ type: "POST", url: url,data:data, dataType: "xml", timeout:200000, success: callback,error:function(){}});
}

function ThreadViewDataProcessor(appPath,appType,pageIndex,pageSize,threadID,varName,pageCount)
{
    this._appPath=appPath;
    this._appType=appType;
    this._pageIndex=pageIndex;
    this._pageSize=pageSize;
    this._ajaxUrl=appPath+"/Utility/AjaxPages/ThreadViewAjax.aspx";
    this._threadID=threadID;
    this._name=varName;
    this._pageCount =pageCount;
}

                     
ThreadViewDataProcessor.prototype.GetData=function(firstLoad,hash)
{
    var args = hash?hash:location.hash;
    var varName=this._name;
    var reg = new RegExp('#?PageIndex=([^#]*)', 'gi');
    var chk = args.match(reg);
    var pIndex=1;
    if(chk!=null)
    {
        if(isNaN(parseInt(RegExp.$1)))
            pIndex='1';
        else
            pIndex=RegExp.$1;
    }
    else
    {
        pIndex=this._pageIndex+1;
    }
    if (isNaN(parseInt(pIndex))||pIndex<1) pIndex = 1;
    pIndex=pIndex-1;
    
    $("#postsCnt").html('<div class="loading"><img src="http://im.morningstar.com/im/GlobalHome/LoadingScreenAnimation.gif" /></div>');
    
    
    var appPath=this._appPath;
    var appType=this._appType;
    var pageSize=this._pageSize;
    var pageCount = this._pageCount;
    if(pIndex>=pageCount)
    {
        pIndex = pageCount-1;
    }
    
    var callBack=function(data)
    {
        if($(data).find("ErrorMessage").text())
        {
            return;
        }
        else
        {        
            var html='';
            var $TopPost=$(data).find("TopPost");
            
            var isShowModeration= $TopPost.find("IsShowModeration").text() == "1";
            var currentPageIndex = parseInt($(data).find("CurrentPageIndex").text());
            var checkLoginUrl = $TopPost.find("CheckLoginUrl").text();
            
            if(firstLoad)
            {
                $("#topPostSubject").html($TopPost.find("Subject").text());
                var isAnonymous=eval($TopPost.find("IsAnonymous").text());
                var newPostUrl=$TopPost.find("NewPostUrl").text();
                var regScript=$TopPost.find("RegScript").text();
                var rssLink=$TopPost.find("RssUrl").text();
                var threadID=$TopPost.find("ThreadID").text();
                postID=$TopPost.find("PostID").text();
                var printUrl=$TopPost.find("PrintUrl").text();
                var recommend=$TopPost.find("Recommend").text();
                
                sectionID=$TopPost.find("SectonID").text();
                $("#FavoriteToolBar_favoriteToolBar").show();
                favoriteToolBar=new FavoriteToolBar($TopPost.find("SectonID").text(),appPath,appType,3,isAnonymous,newPostUrl,regScript,rssLink,'favoriteToolBar',recommend,threadID,postID,printUrl);
            }
            
            if(currentPageIndex > 0) //when page is not in the first page, we will ignore the thread-starter post
            {
                firstLoad = false;
            }
            
            if(firstLoad)
            {
                html=html+'<div class="dshadowcontain" >';
                html=html+'<div class="shadow">';
                html=html+'<div class="drowa1">';
                html=html+RenderNode(varName,$TopPost.find("UserID").text(),$TopPost.find("UserName").text(),$TopPost.find("UserUrl").text(),
                                 $TopPost.find("UserSPUrl").text(),$TopPost.find("Subject").text(),
                                 $TopPost.find("FormattedBody").text(),$TopPost.find("PostDate").text(),
                                 $TopPost.find("PostID").text(),$TopPost.find("LinkButtons").text(),
                                 $TopPost.find("IsFavoriteUser").text(),$TopPost.find("IsIgnoredUser").text(),
                                 $TopPost.find("EmailPost").text(), appType, appPath,isShowModeration,checkLoginUrl,true);                
                html=html+'</div>';//<div class="drowa1">
                html=html+'</div>';//<div class="shadow">
                html=html+'</div>';//<div class="dshadowcontain" >
            }
            
            var totalRecords=$(data).find("TotalRecords").text();     
            currentPageIndex = parseInt($(data).find("CurrentPageIndex").text())+1;
//            location.hash = "#PageIndex=" + currentPageIndex;
            upPager=new DClientPager(pageSize,parseInt(totalRecords),'upPager',null,true);
            downPager=new DClientPager(pageSize,parseInt(totalRecords),'downPager',null,true);
            
            $(data).find('Comments').find("Comment").each(function()
            {  
                var $Comment = $(this);         
                html=html+'<div class="drow4a2">';
                html=html+RenderNode(varName,$Comment.find("UserID").text(),$Comment.find("UserName").text(),$Comment.find("UserUrl").text(),
                                     $Comment.find("UserSPUrl").text(),$Comment.find("Subject").text(),
                                     $Comment.find("FormattedBody").text(),$Comment.find("PostDate").text(),
                                     $Comment.find("PostID").text(),$Comment.find("LinkButtons").text(),
                                     $Comment.find("IsFavoriteUser").text(),$Comment.find("IsIgnoredUser").text(),
                                     $Comment.find("EmailPost").text(), appType, appPath,isShowModeration,checkLoginUrl,false);
                html=html+'</div>';  
                html=html+'<div class="drowspace"></div>';          
            })   
        
            $("#postsCnt").html(html);
            
            //topic tags
            TopicTags(data,checkLoginUrl);
        }
    }
    
    var currentUrl = location.href;
    var data="act=1&tid="+this._threadID+"&pinx="+pIndex + "&rtnurl=" + currentUrl;
    AjaxXmlRequest(this._ajaxUrl,data,callBack);
}

    function RenderNode (varName,userID,userName,userUrl,userSPUrl,subject,body,postDate,postID,LinkButtons,isFavorite,isIgnored,emailPost, appType, appPath, isShowModeration,checkLoginUrl,isThreadStarter)
    {                            
        var nodeHTML='';
        if(isIgnored=="1")
        {
            nodeHTML=nodeHTML+'<div id="postCnt'+postID+'" style="display:none">';
        }
        nodeHTML=nodeHTML+'<div class="dcolm_e1" id="postLeftPostID' + postID + '" style="width:159px">';
        nodeHTML=nodeHTML+'<div class="dcolm_e3">';
        nodeHTML=nodeHTML+'<a href="'+userUrl+'" class="dtextc1" id="anchorUserName' + postID + '">'+userName+'</a>';
        if(userSPUrl)
            nodeHTML=nodeHTML+'<a href="'+userSPUrl+'" class="port_icon2"></a>';
        nodeHTML=nodeHTML+'</div>';
        if(checkLoginUrl.length == 0)
        {
            if(isFavorite=="1")
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="javascript:'+varName+'.RemoveFavoriteUser('+userID+','+postID+')" class="text_d2 toolcontainer" id="favoriteUserCnt'+postID+'">';      
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOn" id="favoriteUser'+postID+'"></div>Favorite</a></div>';
            }
            else
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="javascript:'+varName+'.AddFavoriteUser('+userID+','+postID+')" class="text_d2 toolcontainer" id="favoriteUserCnt'+postID+'">';      
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOff" id="favoriteUser'+postID+'"></div>Favorite</a></div>';
            }
            if(isIgnored=="1")
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="javascript:RemoveIgnoreUser(\'' + appPath + '\','+userID+','+postID+')" class="text_d2 toolcontainer" id="ignoreUserCnt'+postID+'">';
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOn u'+userID+'" id="ignoreUser'+postID+'"></div>Ignore</a></div>';
            }
            else
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="javascript:AddIgnoreUser(\'' + appPath + '\',' +userID+','+postID+')" class="text_d2 toolcontainer" id="ignoreUserCnt'+postID+'">';
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOff u'+userID+'" id="ignoreUser'+postID+'"></div>Ignore</a></div>';
            }
        }
        else
        {
            if(isFavorite=="1")
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="' + checkLoginUrl.replace("\\'","'").replace("\\'","'")  + '" class="text_d2 toolcontainer" id="favoriteUserCnt'+postID+'">';      
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOn" id="favoriteUser'+postID+'"></div>Favorite</a></div>';
            }
            else
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="' + checkLoginUrl.replace("\\'","'").replace("\\'","'") + '" class="text_d2 toolcontainer" id="favoriteUserCnt'+postID+'">';      
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOff" id="favoriteUser'+postID+'"></div>Favorite</a></div>';
            }
            if(isIgnored=="1")
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="' + checkLoginUrl.replace("\\'","'").replace("\\'","'")  + '" class="text_d2 toolcontainer" id="ignoreUserCnt'+postID+'">';
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOn u'+userID+'" id="ignoreUser'+postID+'"></div>Ignore</a></div>';
            }
            else
            {
                nodeHTML=nodeHTML+'<div class="dcolm_e3"><a href="' + checkLoginUrl.replace("\\'","'").replace("\\'","'")  + '" class="text_d2 toolcontainer" id="ignoreUserCnt'+postID+'">';
                nodeHTML=nodeHTML+'<div style="cursor: pointer;" class="checkOff u'+userID+'" id="ignoreUser'+postID+'"></div>Ignore</a></div>';
            }
        }
        nodeHTML=nodeHTML+'</div>'; //<div class="dcolm_e1">
        
//        nodeHTML=nodeHTML+'<div class="dcolm_e2" id="postRightPostID' + postID + '">';
//        nodeHTML=nodeHTML+'<div class="dcolm_e3">';
//        nodeHTML=nodeHTML+'<a href="javascript:void(0);" class="dtab" onclick="javascript:'+emailPost+'">E-mail</a>';
//        nodeHTML=nodeHTML+'<div class="flagcontainer" style="width:58px;">';
//	    nodeHTML=nodeHTML+'<li id="menu1" class="drop1 text_b7" onclick="(new PostFlag()).GetStatus(\'' + appType + '\',\'' + appPath + '\',\'' + postID +'\',\'' + userID + '\',\'' + checkLoginUrl.replace("'","\'") +  '\',this,event);" style="width: 58px; " >';
//        nodeHTML=nodeHTML+'<div class="flagbutton dtab" style="z-index:0;">Flag</div>';
//        nodeHTML=nodeHTML+'<ul id="flag' + postID + '" class="thread_dd" style="display: none; top: 20px; width: 180px; height: 85px; z-index: 100; overflow-x: hidden;left:2px;"></ul>';
//        nodeHTML=nodeHTML+'</li>';     
//        nodeHTML=nodeHTML+'</div>';

        nodeHTML=nodeHTML+'<div class="dcolm_e2" id="postRightPostID' + postID + '">';
        nodeHTML=nodeHTML+'<div class="dcolm_e3" style="Line-height:20px">';
        nodeHTML=nodeHTML+'<div class="dtab" onclick="javascript:'+emailPost+'">E-mail</div>';        
        nodeHTML =nodeHTML+'<div class="dtab" onclick="(new PostFlag()).GetStatus(\'' + appType + '\',\'' + appPath + '\',\'' + postID +'\',\'' + userID + '\',\'' + checkLoginUrl.replace("'","\'") +  '\',this,event);">';
        nodeHTML=nodeHTML+'<ul id="flag' + postID + '" style="width: 180px"></ul>';
        nodeHTML =nodeHTML+'Flag<span></span></div>';   

//        if(isShowModeration){
//            nodeHTML=nodeHTML+'<div class="flagcontainer" style="width:90px;">';
//	        nodeHTML=nodeHTML+'<li id="menu1" class="drop1 text_b7" onclick="(new Moderation()).GetStatus(\'' + appPath + '\',\'' + postID  + '\',this,event);" style="width: 90px;border:" >';
//            nodeHTML=nodeHTML+'<div class="moderationbutton dtab" >Moderate</div>';
//            nodeHTML=nodeHTML+'<ul id="moderation' + postID + '" class="thread_dd" style="display: none; top: 20px; width: 180px; height: 85px; z-index: 100; overflow-x: hidden;left:0px;"></ul>';
//            nodeHTML=nodeHTML+'</li>';     
//            nodeHTML=nodeHTML+'</div>';        
//        }
        if(isShowModeration){
          
            nodeHTML=nodeHTML+'<div class="dtab" onclick="(new Moderation()).GetStatus(\'' + appPath + '\',\'' + postID  + '\',this,event);">';
            nodeHTML=nodeHTML+'<ul id="moderation' + postID + '" style="width: 180px"></ul>';                
            nodeHTML=nodeHTML+'Moderate<span></span></div>';        
        }
        
        nodeHTML=nodeHTML+'</div>';
        
        nodeHTML=nodeHTML+'<div class="dthread_header">';
        if(isThreadStarter)
            nodeHTML=nodeHTML+'<div class="dthread_title1" id="title' + postID + '">';
        else
            nodeHTML=nodeHTML+'<div class="dthread_title2" id="title' + postID + '">';
        nodeHTML=nodeHTML+subject+'</div>';
        nodeHTML=nodeHTML+'<div class="dthread_tstamp">';
        nodeHTML=nodeHTML+postDate+' | Post #'+postID+'</div>'
        nodeHTML=nodeHTML+'</div>';    
        nodeHTML=nodeHTML+'<div class="dthread_tcontent" id="content' + postID + '" style="word-wrap: break-word;overflow:hidden;">'; 
        nodeHTML=nodeHTML+body;   
        nodeHTML=nodeHTML+'</div> ';   
        
        nodeHTML=nodeHTML+'<div class="dpost_foot">';
        nodeHTML=nodeHTML+LinkButtons;
        nodeHTML=nodeHTML+'</div>';            
        nodeHTML=nodeHTML+'</div>';//<div class="dcolm_e2">
        if(isIgnored=="1")
        {
            nodeHTML=nodeHTML+'</div>';
            nodeHTML=nodeHTML+'<div id="showCnt'+postID+'" class="dthread_tcontent" style="padding:2px 0px 2px 10px;">';
            nodeHTML=nodeHTML+'Post hidden by ignore feature&nbsp;<a href="javascript:ShowPost(\'#postCnt'+postID+'\',\'#showCnt'+postID+'\');">View Post</a>';
            nodeHTML=nodeHTML+'</div>';
        }
               
        return nodeHTML;
    }
    
    function ShowPost(postCntId,showCntId)
    {
        $(showCntId).hide();
        $(postCntId).show();
    }

    function ShowData (data ,firstLoad,appPath,appType,pageSize)
    {
        if($(data).find("ErrorMessage").text())
        {
            return;
        }
        else
        {        
            var html='';
            var $TopPost=$(data).find("TopPost");
            var currentPageIndex = parseInt($(data).find("CurrentPageIndex").text());
            var isShowModeration= $TopPost.find("IsShowModeration").text() == "1";
            var checkLoginUrl = $TopPost.find("CheckLoginUrl").text();
            
            if(firstLoad)
            {
                $("#topPostSubject").html($TopPost.find("Subject").text());
                var isAnonymous=eval($TopPost.find("IsAnonymous").text());
                var newPostUrl=$TopPost.find("NewPostUrl").text();
                var regScript=$TopPost.find("RegScript").text();
                var rssLink=$TopPost.find("RssUrl").text();
                var threadID=$TopPost.find("ThreadID").text();
                postID=$TopPost.find("PostID").text();
                var printUrl=$TopPost.find("PrintUrl").text();
                var recommend=$TopPost.find("Recommend").text();
                
                sectionID=$TopPost.find("SectonID").text();
                $("#FavoriteToolBar_favoriteToolBar").show();
                favoriteToolBar=new FavoriteToolBar($TopPost.find("SectonID").text(),appPath,appType,3,isAnonymous,newPostUrl,regScript,rssLink,'favoriteToolBar',recommend,threadID,postID,printUrl);
            }
            
            if(currentPageIndex > 0) //when page is not in the first page, we will ignore the thread-starter post
            {
                firstLoad = false;
            }
            
            if(firstLoad)
            {
                html=html+'<div class="dshadowcontain" >';
                html=html+'<div class="shadow">';
                html=html+'<div class="drowa1">';
                html=html+RenderNode(this.name,$TopPost.find("UserID").text(),$TopPost.find("UserName").text(),$TopPost.find("UserUrl").text(),
                                 $TopPost.find("UserSPUrl").text(),$TopPost.find("Subject").text(),
                                 $TopPost.find("FormattedBody").text(),$TopPost.find("PostDate").text(),
                                 $TopPost.find("PostID").text(),$TopPost.find("LinkButtons").text(),
                                 $TopPost.find("IsFavoriteUser").text(),$TopPost.find("IsIgnoredUser").text(),
                                 $TopPost.find("EmailPost").text(), appType, appPath,isShowModeration,checkLoginUrl,true);
                
                html=html+'</div>';//<div class="drowa1">
                html=html+'</div>';//<div class="shadow">
                html=html+'</div>';//<div class="dshadowcontain" >
            }
            
            var totalRecords=$(data).find("TotalRecords").text();     
            currentPageIndex = parseInt($(data).find("CurrentPageIndex").text()) + 1;
            location.hash = "#PageIndex=" + currentPageIndex;
            upPager=new DClientPager(pageSize,parseInt(totalRecords),'upPager',null,true);
            downPager=new DClientPager(pageSize,parseInt(totalRecords),'downPager',null,true);
            
            $(data).find('Comments').find("Comment").each(function()
            {  
                var $Comment = $(this);         
                html=html+'<div class="drow4a2">';
                html=html+RenderNode(this.name,$Comment.find("UserID").text(),$Comment.find("UserName").text(),$Comment.find("UserUrl").text(),
                                     $Comment.find("UserSPUrl").text(),$Comment.find("Subject").text(),
                                     $Comment.find("FormattedBody").text(),$Comment.find("PostDate").text(),
                                     $Comment.find("PostID").text(),$Comment.find("LinkButtons").text(),
                                     $Comment.find("IsFavoriteUser").text(),$Comment.find("IsIgnoredUser").text(),
                                     $Comment.find("EmailPost").text(), appType, appPath,isShowModeration,checkLoginUrl,false);
                html=html+'</div>';  
                html=html+'<div class="drowspace"></div>';          
            })   
        
            $("#postsCnt").html(html);
            //topic tags
            TopicTags(data,checkLoginUrl);
        }
}

ThreadViewDataProcessor.prototype.AddFavoriteUser=function(userID,postID)
{
    var varName=this._name;    
    
    var callback=function(data)
    {
        if(data.error==null)
        {
            var hrefAction="javascript:"+varName+".RemoveFavoriteUser("+userID+","+postID+")";
            $('#favoriteUserCnt'+postID).attr("href", hrefAction);
            $('#favoriteUser'+postID).attr("class","checkOn");
        }
    }
    var ajaxPath=this._appPath+'/Utility/AjaxPages/MSFavoriteAjax.aspx';
    var data="act=2&uid="+userID+"&appType="+this._appType ;
    AjaxRequest(ajaxPath,data,callback);
}

ThreadViewDataProcessor.prototype.RemoveFavoriteUser=function(userID,postID)
{
    var varName=this._name;    
    
    var callback=function(data)
    {
        if(data.error==null)
        {
            var hrefAction="javascript:"+varName+".AddFavoriteUser("+userID+","+postID+")";
            $('#favoriteUserCnt'+postID).attr("href", hrefAction);
            $('#favoriteUser'+postID).attr("class","checkOff");
        }
    }
    var ajaxPath=this._appPath+'/Utility/AjaxPages/MSFavoriteAjax.aspx';
    var data="act=3&uid="+userID;
    AjaxRequest(ajaxPath,data,callback);
}

ThreadViewDataProcessor.prototype.Post=function(userName,userID,subject,body,threadID,postID,isTracked,callback)
{

    var currentUrl = location.href;
    var ajaxUrl=this._ajaxUrl+"?act=2&rtnurl=" + currentUrl;
    var data="userName=" + encodeURIComponent(userName)+"&uid="+userID+"&subject="+encodeURIComponent(subject)+"&body="+encodeURIComponent(body)+ "&threadID=" + threadID + "&parentPostID="+postID + "&isTracked=" + isTracked + "&rnd=" + Math.random();
    
    var appPath=this._appPath;
    var appType=this._appType;
    var pageSize=this._pageSize;
    var cb = function(data)
    {
  
        ShowData(data,true,appPath,appType,pageSize);
        callback($(data).find("newPostID").text());
    }
    AjaxXmlPostRequest(ajaxUrl,data,cb);
}

ThreadViewDataProcessor.prototype.Edit = function(userName,userID,subject,body,threadID,postID,isTracked,callback)
{
    var currentUrl = location.href;
    var ajaxUrl=this._ajaxUrl+"?act=6&rtnurl=" + currentUrl;
    var data="userName=" + encodeURIComponent(userName)+"&uid="+userID+"&subject="+encodeURIComponent(subject)+"&body="+encodeURIComponent(body)+ "&threadID=" + threadID + "&parentPostID="+postID + "&isTracked=" + isTracked + "&rnd=" + Math.random();
    
    var appPath=this._appPath;
    var appType=this._appType;
    var pageSize=this._pageSize;
    var postid = postID;
    var cb = function(data)
    {
        //alert('1');
        callback(data,postid);
        //alert('2');
    }
    //AjaxXmlPostRequest(ajaxUrl,data,cb);
     $.ajax({ type: "POST", url: ajaxUrl,data:data,dataType:"html", timeout:20000, success: cb,error:function(){alert('error');}});
}


ThreadViewDataProcessor.prototype.Preview=function(userName,userID,subject,body,threadID)
{
    var currentUrl = location.href;
    var ajaxUrl=this._ajaxUrl+"?act=5&rtnurl=" + currentUrl;
    var data="userName=" + encodeURIComponent(userName)+"&uid="+userID+"&subject="+encodeURIComponent(subject)+"&body="+encodeURIComponent(body)+ "&threadID=" + threadID + "&rnd=" + Math.random();
    
    var appPath=this._appPath;
    var appType=this._appType;
    var pageSize=this._pageSize;
    var cb = function(data)
    {
    
        $(".dthread_preview .dthread_tcontent").html(data); 
        
    }
        
        $.ajax({ type: "POST", url: ajaxUrl,data:data,dataType:"html", timeout:20000, success: cb,error:function(){alert('error');}});
}

/*********************User*********************/
function AddIgnoreUser(appPath, userID, postID)
{
    var callback=function()
    {
        if(data.error==null)
        {
            var hrefAction="javascript:RemoveIgnoreUser('" + appPath + "',"+userID+","+postID+")";
            $('#ignoreUserCnt'+postID).attr("href", hrefAction);
            $('#ignoreUser'+postID).attr("class","checkOn");
        }
    }
    var ajaxPath= appPath+'/Utility/AjaxPages/MSFlagAjax.aspx';
    var data="act=2&uid="+userID + "&rnd=" + Math.random();
    AjaxRequest(ajaxPath,data,callback);
}
function RemoveIgnoreUser(appPath, userID,postID)
{
    var callback=function()
    {
        if(data.error==null)
        {
            var hrefAction="javascript:AddIgnoreUser('" + appPath + "'," + userID+","+postID+")";
            $('#ignoreUserCnt'+postID).attr("href", hrefAction);
            $('#ignoreUser'+postID).attr("class","checkOff");
        }
    }
    var ajaxPath=appPath+'/Utility/AjaxPages/MSFlagAjax.aspx';
    var data="act=3&uid="+userID + "&rnd=" + Math.random();
    AjaxRequest(ajaxPath,data,callback);
}
/*********************User*********************/

/*********************Flag*********************/
function PostFlag()
{
    this._ajaxUrl='/Utility/AjaxPages/MSFlagAjax.aspx';
}
PostFlag.prototype.GetStatus=function(appType,appPath,postID,postAuthorID,checkLoginUrl,obj,event)
{
    if (checkLoginUrl.length > 0){
        location.href = checkLoginUrl;
        return
    }
    var cntID="#flag"+ postID;
    $(cntID).empty();
    var callBack=function(data)
    {
         if(data.error==null)
         {     
            var html='';        
//            if(data.value.ignored)
//            {
//                html=html+'<li><a href="javascript:(new PostFlag()).ShowUser(\''+appPath + '\',\'' + postAuthorID  +'\')">Show this author</a></li>';
//            }
//            else
//            {
//                html=html+'<li><a href="javascript:(new PostFlag()).IgnoreUser(\''+appPath + '\',\'' + postAuthorID +'\')">Ignore this author</a></li>';
//            }
            
            if(data.value.flagged)
            {
                html=html+'<li><a href="javascript:void(0)">This post has been reported</a></li>';
            }
            else
            {
                html=html+'<li><a href="javascript:(new PostFlag()).Flag(1,\'' + appPath + '\',\'' + postAuthorID + '\',\'' + postID + '\',\'' + appType + '\')">Report as off topic</a></li>';
                html=html+'<li><a href="javascript:(new PostFlag()).Flag(2,\'' + appPath + '\',\'' + postAuthorID + '\',\'' + postID + '\',\'' + appType + '\')">Report as inflammatory</a></li>';
                html=html+'<li><a href="javascript:(new PostFlag()).Flag(3,\'' + appPath + '\',\'' + postAuthorID + '\',\'' + postID + '\',\'' + appType + '\')">Report as advertising</a></li>';
                html=html+'<li><a href="javascript:(new PostFlag()).Flag(4,\'' + appPath + '\',\'' + postAuthorID + '\',\'' + postID + '\',\'' + appType + '\')">Report as copyright content</a></li>';
            }              
            $(cntID).append(html);
         }
    }
    var currentUrl = location.href;
    var data="act=1&pid="+ postID+"&uid="+ postAuthorID + "&rtnurl=" + currentUrl + "&rnd=" + Math.random();
    
    AjaxRequest(appPath + this._ajaxUrl,data,callBack);
    showDropdown(event,obj);
}

PostFlag.prototype.IgnoreUser=function(appPath,postAuthorID)
{
    var data="act=2&uid=" + postAuthorID + "&rnd=" + Math.random();
    AjaxRequest(appPath + this._ajaxUrl,data,function(){ $(".u" + postAuthorID).removeClass("checkOff").addClass("checkOn");});
}

PostFlag.prototype.ShowUser=function(appPath,postAuthorID)
{
    var data="act=3&uid=" + postAuthorID + "&rnd=" + Math.random();
    AjaxRequest(appPath + this._ajaxUrl,data,function(){ $(".u" + postAuthorID).removeClass("checkOn").addClass("checkOff");});
}

PostFlag.prototype.Flag=function(flag,appPath,postAuthorID,postID,appType)
{
    
    var data="act=4&uid="+ postAuthorID+"&pid="+ postID+"&flag="+flag+"&appType="+ appType + "&rnd=" + Math.random();
    AjaxRequest(appPath + this._ajaxUrl,data,null);
}

/*********************Flag*********************/


/*********************Thread Navigator*********************/
function ThreadNavigator(postID, appPath, appType,sectionId,top) {
    this._appPath = appPath;
    this._appType = appType;
    this._postID = postID;
    this._ajaxUrl = "/Utility/AjaxPages/ThreadViewAjax.aspx";
    //this._threadUrl = threadUrl;

    this.Render = function() {
        var cntClass="text_b4t";
        var linkClass="dtext_b5t";
        if(!top)
        {
            cntClass="text_b4";
            linkClass="dtext_b5";
        }
        document.write('<div>');
        document.write('<div class="'+cntClass+'">Thread:</div>');
        document.write('<a href="javascript:PreviousThread(\''+ postID +'\',\'' + appPath + '\',\'' + appType +'\')" class="'+linkClass+'">Previous</a>');
        document.write(' <span class="'+cntClass+'">| </span>');
        document.write('<a href="javascript:NextThread(\''+ postID +'\',\'' + appPath + '\',\'' + appType +'\')" class="'+linkClass+'">Next</a>');
        document.write(' <span class="'+cntClass+'">| </span>');
        document.write('<a href="javascript:CoverPage(\'' + appPath + '\',\'' + appType + '\',\'' + sectionId +'\')" class="'+linkClass+'">'+ (appType == "1" ? "Blog" : "Forum")+'</a>');
        document.write('</div>');
    }
    this.Render();
}
            
function PreviousThread(postID, appPath, appType) {
    var ajaxUrl = "/Utility/AjaxPages/ThreadViewAjax.aspx";
    var url = appPath + ajaxUrl;
    var data = "act=3&direct=0&postID=" + postID + "&rnd=" + Math.random();
    var callback = function(result) {
        if (result) {
            if (result.hasThread) {
                location.href = result.url;
            }
            else {
                alert('This is the first thread already!');
            }
        }
    };
    //AjaxRequest(url, data, callback);
    $.getJSON(url, data, callback);
}
function NextThread(postID, appPath, appType) {

    var ajaxUrl = "/Utility/AjaxPages/ThreadViewAjax.aspx";
    var url = appPath + ajaxUrl;
    var data = "act=3&direct=1&postID=" + postID + "&rnd=" + Math.random();
    var callback = function(result) {
        if (result) {
            if (result.hasThread) {
                location.href = result.url;
            }
            else {
                alert('This is the last thread already!');
            }
        }
    };
    $.getJSON(url, data, callback);
}
function CoverPage(appPath, appType, sectionId){
    var ajaxUrl = "/Utility/AjaxPages/ThreadViewAjax.aspx";
    var url = appPath + ajaxUrl;
    var data = "act=4&apptype=" + appType + "&sectionId=" + sectionId;
    var callback = function(result) {
        if (result) {
            if (result) {
                location.href = result.url;
            }
        }
    };
    $.getJSON(url, data, callback);
}
/*********************Thread Navigator*********************/

/*********************topic tags*********************/
function TopicTags(data, checkLoginUrl){
    var categories = $(data).find("Categorie");
    var html = '';
    html += '<div class="dheadline_a1" id="divtags">';
    html += '<span class="text_b6a">Topics</span>';
    categories.each(function (){
        html += '<div class="drow5">';
        html += '<a href="' + $(this).find("CategorieUrl").text() + '" class="text_b6b">' + $(this).find("CategorieText").text() + '</a>';
        html += '</div>';
    })
    if(categories.length < 20 && checkLoginUrl.length == 0){
        html += '<div class="drow5">';
        html += '<a href="javascript:' + $(data).find("EditTopicsClick").text() + '" class="text_b6b"><div class="topics_icon"></div>Assign more topics... </a>';
        html += '</div>';
    }
    html += '</div>';

    $(".dsection_d1").html(html);
}

function RefreshTopicTags(topicsContainerId,data){
    var html;
    var isMatch = false;
    var mathKey;
    //remove
    $("#" + topicsContainerId).find("a").not(":last").each(function(){
        isMatch = false;
        mathKey = $(this).text();
        var mathedObj = $(data).parent().find("a:contains('"+ mathKey +"')");
        if(mathedObj .length == 0){
            $(this).parent().remove();
        }
        else{
            mathedObj.each(function (){
                if($(this).text() == mathKey){
                    isMatch = true;
                    return false;
                }
            })
            if(!isMatch){
                $(this).parent().remove();
            }
        }
    })
    //add
    $(data).parent().find("a").each(function(){
        isMatch = false;
        mathKey = $(this).text();
        $("#" + topicsContainerId).find("a:contains('" + mathKey + "')").each(function(){
            if ($(this).text() == mathKey){
                isMatch = true;
                return false;
            }
        })
        if(!isMatch){
            html = '';
            html += '<div class="drow5">';
            html += '<a href="' + $(this).attr("href") + '" class="text_b6b">' + $(this).text() + '</a>';
            html += '</div>';
            $("#" + topicsContainerId + " .drow5:last").before(html);
        }
    })
}
/*********************topic tags*********************/


/*********************Moderation*********************/
function Moderation()
{
    this._ajaxUrl='/Utility/AjaxPages/ModerationPopupMenuAjax.aspx';
}

Moderation.prototype.GetStatus=function(appPath,postID,obj,event)
{
    var cntID="#moderation"+ postID;
    $(cntID).empty();
    var callBack=function(data)
    {
         var html='';
         if(data && !data.error && data.length > 0)
         {     
            for (var i = 0 ; i < data.length ; i++){
                html += "<li>";
                html += "<a href='" + data[i].href + "'>" + data[i].menuText + "</a>";
                html += "</li>";
            }
         }
        $(cntID).append(html);              
        
        
        if(html== '')
        {
            $(cntID).hide()
        }
        else
        {
            showDropdown(event,obj);
        }
    }
    var data="postid=" + postID  + "&rnd=" + Math.random(); 
    AjaxRequest(appPath + this._ajaxUrl,data,callBack);
}

function menuClick(url){

    url += "&rnd=" + Math.random();
    $.getJSON(url,function(data){
        if(data && data.script){ eval( data.script ); }
    })

}

//function toggleMenu(obj){
//    showDropdown(event,obj);
//}
/*********************Moderation*********************/
