//DropDown
function showDropdown(evt,el)
{
   if(el == null)
       el = document.ulEle
   var display = el.getElementsByTagName("UL")[0].style.display;
   if(display == "none" || display == "")
   {
       el.style.position = 'relative';
       el.getElementsByTagName("UL")[0].style.display="block";
       if(el.setCapture)
          el.setCapture(false);
       else
       {
          document.addEventListener("click",showDropdown,true);
          window.captureEvents(Event.CLICK);
       }
       document.ulEle = el;
   }
   else
   {
       el.style.position = '';
       el.getElementsByTagName("UL")[0].style.display="none";
       if(el.setCapture)
          el.releaseCapture();
       else
       {
          document.removeEventListener("click",showDropdown,true);
          window.releaseEvents(Event.CLICK);
          evt.cancelBubble = true;
       }
   }
}

function ShowPost(hiddenCntId,shownCntId)
{
    if(document.getElementById(hiddenCntId))
    {
        document.getElementById(hiddenCntId).style.display='none';
    }
    if(document.getElementById(shownCntId))
    {
        document.getElementById(shownCntId).style.display='';
    }
}


function AjaxRequest(url,data,callback)
{
    $.ajax({ type: "GET", url: url,data:data, dataType: "json", timeout:10000, success: callback,error:function(){alert('error');}});
}


function PostFlag(appPath,appType,postID,postAuthorID,varName)
{
    this._appPath=appPath;
    this._appType=appType;
    this._postID=postID;
    this._postAuthorID=postAuthorID;
    this._name=varName;
    this._ajaxUrl=appPath+'/Utility/AjaxPages/MSFlagAjax.aspx';
}

PostFlag.prototype.GetStatus=function()
{
    var cntID="#flag"+this._postID;
    $(cntID).empty();
    var objName=this._name;
    var callBack=function(data)
    {
         if(data.error==null)
         {     
            var html='';        
            if(data.value.ignored)
            {
                html=html+'<li><a href="javascript:'+objName+'.ShowUser()">Show this author</a></li>';
            }
            else
            {
                html=html+'<li><a href="javascript:'+objName+'.IgnoreUser()">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:'+objName+'.Flag(1)">Report as off topic</a></li>';
                html=html+'<li><a href="javascript:'+objName+'.Flag(2)">Report as inflammatory</a></li>';
                html=html+'<li><a href="javascript:'+objName+'.Flag(3)">Report as advertising</a></li>';
                html=html+'<li><a href="javascript:'+objName+'.Flag(4)">Report as copyright content</a></li>';
            }              
            $(cntID).append(html);
                       
         }
    }
    var data="act=1&pid="+this._postID+"&uid="+this._postAuthorID;
    AjaxRequest(this._ajaxUrl,data,callBack);
}

PostFlag.prototype.IgnoreUser=function()
{
    var data="act=2&uid="+this._postAuthorID;
    AjaxRequest(this._ajaxUrl,data,null);
}

PostFlag.prototype.ShowUser=function()
{
    var data="act=3&uid="+this._postAuthorID;
    AjaxRequest(this._ajaxUrl,data,null);
}

PostFlag.prototype.Flag=function(flag)
{
    var data="act=4&uid="+this._postAuthorID+"&pid="+this._postID+"&flag="+flag+"&appType="+this._appType;
    AjaxRequest(this._ajaxUrl,data,null);
}


function FavoriteControl(appPath,appType,postID,postAuthorID,sectionID,threadID,blogUrl,rssUrl,varName)
{
     this._appPath=appPath;
     this._appType=appType;
     if(this._appType==1)
     {
        this._appStr='blog';
     }
     else
     {
        this._appStr='forum';
     }
     this._blogUrl=blogUrl;
     this._rssUrl=rssUrl;   
     this._postID=postID;
     this._postAuthorID=postAuthorID;
     this._sectionID=sectionID;
     this._threadID=threadID;
     this._name=varName;
     this._ajaxUrl=appPath+'/Utility/AjaxPages/MSFavoriteAjax.aspx';
}


FavoriteControl.prototype.GetStatus=function()
{
    var cntID="#favorite"+this._postID;
    $(cntID).empty();
    var objName=this._name;
    var appStr=this._appStr;
    var blogUrl=this._blogUrl;
    var rssUrl=this._rssUrl;
    var callBack=function(data)
    {
         if(data.error==null)
         {
             var html='';           
             if(data.value.isFavoriteUser)
             {
                html=html+'<li><a href="javascript:'+objName+'.RemoveFavoriteUser()">Remove author from a favorite</a></li>';
             }
             else
             {                
                html=html+'<li><a href="javascript:'+objName+'.AddFavoriteUser()">Add author as a favorite</a></li>';
             }
             
             if(data.value.isFavoritePost)
             {
                html=html+'<li><a href="javascript:'+objName+'.RemoveFavoritePost()">Remove this post from favorites</a></li>';
             }
             else
             {
                html=html+'<li><a href="javascript:'+objName+'.AddFavoritePost()">Add this post as a favorite</a></li>';
             }
             if(data.value.isFavoriteSection)
             {
                html=html+'<li><a href="javascript:'+objName+'.RemoveFavoriteSection()">Remove this '+appStr+' from favorite</a></li>';  
             }
             else
             {
                html=html+'<li><a href="javascript:'+objName+'.AddFavoriteSection()">Add this '+appStr+' as a favorites</a></li>';  
             }
             
             if(data.value.hasSubscribedSection)
             {
                html=html+'<li><a href="javascript:'+objName+'.UnSubscribeSection()">End an e-mail subscription to this '+appStr+'</a></li>';  
             }
             else
             {
                html=html+'<li><a href="javascript:'+objName+'.SubscribeSection()">Start an e-mail subscription to this '+appStr+'</a></li>';  
             }   
             
             
             if(data.value.hasSubscribedThread)
             {
                html=html+'<li><a href="javascript:'+objName+'.UnSubscribeThread()">End an e-mail subscription to this thread</a></li>';  
             }
             else
             {
                html=html+'<li><a href="javascript:'+objName+'.SubscribeThread()">Start an e-mail subscription to this thread</a></li>';  
             } 
             
             
             if(blogUrl.length>0 && this._appType==0)
             {
                html=html+'<li><a href="'+blogUrl+'">Blog about this post</a></li>';  
             }
             
             if(rssUrl.length>0)
             {
                html=html+'<li><a href="'+rssUrl+'">Get an rss feed</a></li>';  
             }
               
             $(cntID).append(html);
         }
    }
    var data="act=1&pid="+this._postID+"&uid="+this._postAuthorID+"&sid="+this._sectionID+"&tid="+this._threadID;
    AjaxRequest(this._ajaxUrl,data,callBack);
}


FavoriteControl.prototype.AddFavoriteUser=function()
{
    var data="act=2&uid="+this._postAuthorID+"&appType="+this._appType;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.RemoveFavoriteUser=function()
{
    var data="act=3&uid="+this._postAuthorID;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.AddFavoritePost=function()
{
    var data="act=4&pid="+this._postID+"&appType="+this._appType;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.RemoveFavoritePost=function()
{
    var data="act=5&pid="+this._postID;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.AddFavoriteSection=function()
{
    var data="act=6&sid="+this._sectionID+"&appType="+this._appType;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.RemoveFavoriteSection=function()
{
    var data="act=7&sid="+this._sectionID;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.SubscribeSection=function()
{
    var data="act=8&sid="+this._sectionID;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.UnSubscribeSection=function()
{
    var data="act=9&sid="+this._sectionID;
    AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.SubscribeThread=function()
{
     var data="act=10&tid="+this._threadID;
     AjaxRequest(this._ajaxUrl,data,null);
}

FavoriteControl.prototype.UnSubscribeThread=function()
{
     var data="act=11&tid="+this._threadID;
     AjaxRequest(this._ajaxUrl,data,null);
}

