﻿// JScript File

var ShowCommentsTable = {};
var HideCommentsTable = {};
var MyComments = new Array();

function AddComment()
{
    PlayerPause();
}

function GetCommentOfVideo(vID)
{
    var paramArray = new Array();
    paramArray["VideoID"] = vID;
    paramArray["FilterType"] = "All";
    paramArray["SearchType"] = "Current";
    paramArray["UserID"] = username;
    paramArray["Key"] = "";
    AJAXFetch("Comment", paramArray, LoadComment, true);
}

function LoadComment(xmldoc)
{
    ClearClientComments();
    var comments = xmldoc.getElementsByTagName("Comment");
    for(var ci = 0;  ci < comments.length; ci++ )
    {
         var CData = 
          {
	           commentId      : parseFloat( comments[ci].getAttribute("CommentID")),
	           timeStamp      : comments[ci].getAttribute("TimeStamp") ,
	           time           : parseFloat( comments[ci].getAttribute("StartTime") ),
	           left           : parseFloat( comments[ci].getAttribute("Left") ),
	           top            : parseFloat( comments[ci].getAttribute("Top") ),
	           right          : parseFloat( comments[ci].getAttribute("Right") ),
	           bottom         : parseFloat( comments[ci].getAttribute("Bottom") ),
	           duration       : parseFloat( comments[ci].getAttribute("Duration")),
	           username       : comments[ci].getAttribute("WirtePassportName") ,
	           Comment        : comments[ci].getAttribute("Content"),
	           FriendsLevel   : comments[ci].getAttribute("FriendsLevel")
          };
          var CObj = new CommentObj(CData);
          CObj.Creat();
    }
    UpdataBookMark();
    window.setTimeout("SearchCommentsToShow()",200);
}

function CommentObj(data)
{
    this.time = data.time;
    this.posX = data.left;
    this.posY = data.top;
    this.height = data.bottom - data.top;
    this.width = data.right - data.left;
    this.comment = data.Comment;
    this.durTime = data.duration;
    this.id = data.commentId;
    this.writer = data.username;
    this.frameDom =null;
    this.commentDom = null;
    this.gleam = null;
    this.friends = data.FriendsLevel;
    
    this.ShowDom = function()
    {
              document.getElementById(this.id + "text").style.display = "";
              document.getElementById(this.id + "frame").style.display = "";
              document.getElementById(this.id).style.display = "";
              if(showLevel==1)
              {
                  document.getElementById(this.id + "text").style.display = "none";
              }
              else if(showLevel == 0)
              {
                  var text = document.getElementById(this.id + "text");
                  text.style.display = "";
                  if( text.style.posTop + text.clientHeight > 220)
                  {
                       text.style.posTop = text.style.posTop - text.clientHeight;
                  }
              }
              else if(showLevel == 3)
              {
                  document.getElementById(this.id + "text").style.display = "none";
                  document.getElementById(this.id + "frame").style.display = "none";
              }
    }
}

CP = CommentObj.prototype;

CP.Creat = function()
{
    var newTable = document.createElement('table');
    var newRow = newTable.insertRow();
    var newCell = newRow.insertCell();;
    var newSpan = document.createElement("span");
    newSpan.style.border = "solid 1px Red";
    newSpan.style.width = this.width;
    newSpan.style.height = this.height;
    newSpan.style.posLeft = this.posX;
    newSpan.style.posTop = this.posY - moPosY;
    newSpan.style.zIndex = "998";
    newSpan.style.position = "absolute";
    newSpan.id = this.id + "frame";
    newCell.appendChild(newSpan);
    newDiv = document.createElement("div");
    newDiv.style.width = "100%";
    var h = this.height - 17;
    if(h<0)
    {
         newDiv.style.height = 0;
    }
    else
    {
        newDiv.style.height = h;
    }
    newDiv.style.zIndex="40";
    newDiv.style.backgroundColor = "#FFFFFF";
    newDiv.style.filter = "alpha(Opacity=0)";
    this.AddControl(newSpan,newDiv);
    this.frameDom = newSpan;
    
    newRow2 = newTable.insertRow();
    newCell2 = newRow.insertCell();
    newSpan2 = document.createElement("div");
    newSpan2.innerText = this.comment;
    newCell2.appendChild(newSpan2);
    newSpan2.style.posTop = this.posY + this.height - moPosY + 1;
    if((this.posY + this.height) > 245)
    {
        //newSpan2.style.posTop = this.posY + this.height - moPosY - 16; 
    }
    newSpan2.style.posLeft = this.posX;
    newSpan2.style.width = this.width;
    newSpan2.style.overflow = "hidden";
    newSpan2.style.position = "absolute";
    newSpan2.style.backgroundColor = "#FFFFFF";
    newSpan2.style.filter = "alpha(Opacity=60)";
    newSpan2.style.fontFamily = "Arial";
    newSpan2.style.fontSize = "10";
    newSpan2.id = this.id + "text";
     
    this.gleam = document.createElement("img");
    this.gleam.src = "images/gleam.gif";
    this.gleam.style.position = "absolute";
    this.gleam.style.posTop = this.posY - moPosY;
    this.gleam.style.posLeft = this.posX;
    this.gleam.style.zIndex = "997";
    this.gleam.onmouseover = new Function("document.getElementById('" + this.id + "text').style.display = '';document.getElementById('" + this.id + "frame').style.display = '';");
                  
    newTable.insertRow().insertCell().appendChild(this.gleam);
    
    newTable.id = this.id ;
    newTable.style.display = "none";
    newTable.style.position = "absolute";
    newTable.style.zIndex = "998";
    document.getElementById("Player").appendChild(newTable);

    if(!ShowCommentsTable[this.time])
    {
        ShowCommentsTable[this.time] = {};
    }
    var objArray = ShowCommentsTable[this.time];
    objArray[this.id] = this;
    
    
    if(!HideCommentsTable[this.time + this.durTime])
    {
        HideCommentsTable[this.time + this.durTime] = {};
    }
    var objArray = HideCommentsTable[this.time + this.durTime];
    objArray[this.id] = this;
    
    MyComments.push(this);
    
}

var timer = null;
var idNo = 0;

CP.IsInfriends = function()
{
    switch(this.friends)
    {
        case "Own"      : if(friendsLeve != "none") 
                          {
                              return true;
                          }
                          else
                          {
                              return false;
                          }
        case "Friends"  : if(friendsLeve == "own") 
                          {
                              return false;
                          }
                          else
                          {
                              return true;
                          }
        case "Other"    : if(friendsLeve == "all") 
                          {
                              return true;
                          }
                          else
                          {
                              return false;
                          }
    }
}


CP.HideDom = function()
{
    document.getElementById(this.id).style.display="none";
}

CP.Show = function()
{
    var tagsInComment = this.GetTags();
    if(FriendsFilterOpen && !this.IsInfriends())
    {
        this.HideDom();
                    for(var l = 0; l < tagsInComment.length; l++)
                    {
                        var tag = tagsInComment[l].toLowerCase();
                        unhighlightTag(tag);
                    }
    }
    else
    {
         var commentBox = document.getElementById(this.id);
         if(TagFilterOn)
         {
                var inlist = false;
                for(var l = 0; l < tagsInComment.length; l++)
                {
                    tag = tagsInComment[l].toLowerCase();
                    if(tagFilter[tag])
                    {
                        inlist = true;
                        break;
                    }
                }
                
                if( showLevel != 2 && inlist)
                {
                       this.ShowDom();          
                       for(var l = 0; l < tagsInComment.length; l++)
                       {
                            var tag = tagsInComment[l].toLowerCase();
                            myAdQuery += tag + " ";
                            highlightTag(tag);
                       }
                 }
                 else
                 {
                       if(document.getElementById(this.id).style.display=="")
                       {
                           this.HideDom();
                           for(var l = 0; l < tagsInComment.length; l++)
                           {
                               var tag = tagsInComment[l].toLowerCase();
                               unhighlightTag(tag);
                           }
                       }
                 }
            
         }
         else
         {
             if(showLevel != 2)
             {
                   this.ShowDom();     
                   for(var l = 0; l < tagsInComment.length; l++)
                   {
                        var tag = tagsInComment[l].toLowerCase();
                        myAdQuery += tag + " ";
                        highlightTag(tag);
                   }
              }
              else
              {
                   if(document.getElementById(this.id).style.display=="")
                   {
                       this.HideDom();
                       for(var l = 0; l < tagsInComment.length; l++)
                       {
                           var tag = tagsInComment[l].toLowerCase();
                           unhighlightTag(tag);
                       }
                   }
              }
         }
    }
}

CP.Hide = function()
{
     var tagsInComment = this.GetTags();
     document.getElementById(this.id).style.display = "none";
     for(var l = 0; l < tagsInComment.length; l++)
     {
          var tag = tagsInComment[l].toLowerCase();
          unhighlightTag(tag);
     }        
}

CP.AddControl = function(frame,obj)
{
    var reply = document.createElement("a");
    reply.name = "reply";
    reply.href = "#";
    var replyImg = document.createElement("img");
    replyImg.src = "Images/reply.gif";
    replyImg.border = "0";
    reply.appendChild(replyImg);
    reply.style.width="16px";
    reply.style.height="16px";
    reply.style.display="none";
    reply.style.cursor="pointer";
    reply.onclick = new Function("AddReply("+this.id+","+this.posX+","+(this.posY+this.height)+");");
    reply.onmouseover = new Function("ShowObj();ShowTooltip('"+this.id+"','reply');");
    frame.appendChild(reply);
    
    var mail = document.createElement("a");
        mail.name = "mail";
        var location = window.location.href;
    mail.href = "mailto:?subject=Check out this cool video&body="+location;
    var mailImg = document.createElement("img");
    mailImg.src = "Images/email.gif";
    mailImg.border = "0";
    mail.appendChild(mailImg);
    mail.style.width ="16px";
    mail.style.height = "16px";
    mail.style.display="none";
    mail.style.cursor="pointer";
    //mail.onclick = new Function("MailToFriend();");
    mail.onmouseover = new Function("ShowObj();ShowTooltip('"+this.id+"','mail');");
    frame.appendChild(mail);
    
    var impeach = document.createElement("a");
        impeach.name = "report";
        var content = this.writer+" Report Abusive Comment:\n" + this.comment;
    impeach.href = "mailto:vadabuse@microsoft.com?subject=Abusive Comment!&body="+content;
    var impeachImg = document.createElement("img");
    impeachImg.src = "Images/impeach.gif";
    impeachImg.border = "0";
    impeach.appendChild(impeachImg);
    impeach.style.width ="16px";
    impeach.style.height = "16px";
    impeach.style.display="none";
    impeach.style.cursor="pointer";
    //mail.onclick = new Function("MailToFriend();");
    impeach.onmouseover = new Function("ShowObj();ShowTooltip('"+this.id+"','report');");
    frame.appendChild(impeach);
    
    if(this.friends == "Own")
    {
        var edit = document.createElement("a");
        edit.herf = "#";
        edit.name = "edit";
        var editImg = document.createElement("img");
        editImg.src = "Images/edit.gif";
        editImg.border = "0";
        edit.appendChild(editImg);
        edit.style.width ="16px";
        edit.style.height = "16px";
        edit.style.display="none";
        edit.style.cursor="pointer";
        edit.onclick = new Function("EditComment("+ this.posX +","+ this.posY +","+ this.height +","+this.width+","+this.id+");VideoSetPosition("+this.time+");");
        edit.onmouseover = new Function("ShowObj();ShowTooltip('"+this.id+"','edit');");
        frame.appendChild(edit);
        
        var deleteComment = document.createElement("a");
        deleteComment.herf = "#";
        deleteComment.name = "delete";
        var deleteImg = document.createElement("img");
        deleteImg.src = "Images/delete.gif";
        deleteImg.border = "0";
        deleteComment.appendChild(deleteImg);
        deleteComment.style.width ="16px";
        deleteComment.style.height = "16px";
        deleteComment.style.display="none";
        deleteComment.style.cursor="pointer";
        deleteComment.onclick = new Function("DeleteComment("+this.id+");HideComments();");
        deleteComment.onmouseover = new Function("ShowObj();ShowTooltip('"+this.id+"','delete');");
        frame.appendChild(deleteComment);
    }
    
    frame.appendChild(obj);
    
    var tooltip = document.createElement("div");
    tooltip.style.backgroundColor = "yellow";
    tooltip.style.filter = "alpha(Opacity=60)";
    tooltip.style.position = "absolute";
    tooltip.style.posTop =  - 20;
    tooltip.style.height = "16px";
    tooltip.style.posLeft = 1;
    tooltip.style.zIndex = "2000";
    tooltip.style.fontSize = "smaller";
    tooltip.style.display = "none";
    tooltip.id = "tip"+this.id;
    frame.appendChild(tooltip);
    
    obj.onmouseover = new Function("ShowObj();");
    obj.onmouseout = new Function("HideObj('"+this.id+"');");
}

CP.GetTags = function()
{
        var t = 0;
        var comment = this.comment;
        while( t != -1)
        {
            t = comment.indexOf("\n");
            comment = comment.replace("\n"," ");
        }
        t = 0;
        while( t != -1)
        {
            t = comment.indexOf("\r");
            comment = comment.replace("\r","");
        }
        var charArray = comment.split(" ");
        return charArray;
}

var OnEditing = false;
function EditComment(x,y,h,w,id)
{
    document.getElementById("CommentId").value = id;
    OnEditing = true;
    HideReply();
    VideoPause();
    ShowFrame();
    SetFrame(x,y,h,w);
    EndCapture();
    for(var index = 0; index < MyComments.length; index ++)
    {
        if(MyComments[index].id == id)
        {
               document.getElementById("commentArea").value = MyComments[index].comment;
               break;
        }
    }
}

function DeleteComment(id)
{
    HideReply();
    CancelComment();
    var confirmBox = confirm("Do You Want Delete Your Comment!");
    if(confirmBox)
    {
            
        for(var index = 0; index < MyComments.length ; index ++)
        {
            if(MyComments[index].id == id)
            {
                var ShowComments = ShowCommentsTable[MyComments[index].time];
                var HideComments = HideCommentsTable[MyComments[index].time + MyComments[index].durTime];
                ShowComments[id].Hide();
                delete ShowComments[id];
                delete HideComments[id];
                MyComments.splice(index,1);
                break;
            }
        }
        var paramArray = new Array();
        paramArray["CommentID"] = id;
        paramArray["UserID"] = username;
        AJAXSubmit("delcomment",paramArray,DeleteSuccess,true);
    }
    else
    {
        return false;
    }
}

function DeleteSuccess()
{
    GetCommentOfVideo(CurrentVideoID);
    GetTagOfVideo(CurrentVideoID);
    ShowComments();
}

var myAdQuery = "";
var preTime = 0;
function ShowComments()
{
    if(VideoCurrentStatus()=="playing")
    {
        var currentTime = VideoCurrentPosition();
        currentTime = GetInt(currentTime);
        if(preTime == currentTime)
        {
            timer = window.setTimeout("ShowComments()",500);
            return;
        }
        else
        {
            preTime = currentTime;
        }
        commentsToShow = ShowCommentsTable[currentTime];
        commentsToHide = HideCommentsTable[currentTime];
        myAdQuery = "";
        for(var id in commentsToHide)
        {
            commentsToHide[id].Hide();
        }
        for(var id in commentsToShow)
        {
            commentsToShow[id].Show();
        }
        serveAds(myAdQuery);
        if(ticker)
        {
            updateTickerPosition(currentTime);
        }
    }
    else if(VideoCurrentStatus() == "stopped")
    {
        HideComments();
    }
    else if(VideoCurrentStatus()=="paused")
    {
        //SearchCommentsToShow();
    }
    timer = window.setTimeout("ShowComments()",500);
}

function HideComments()
{
    for(var index = 0; index < MyComments.length ; index ++)
    {
        MyComments[index].Hide();
    }
    if(timer)
    {
        window.clearTimeout(timer);
    }
}

function SearchCommentsToShow()
{
    HideComments();
    for(var index = 0; index < MyComments.length ; index ++)
    {
        var comment = MyComments[index];
        var start = comment.time;
        var end = comment.time + comment.durTime
        var time = VideoCurrentPosition();
        myAdQuery = "";
        if(start < time && end > time)
        {
            comment.Show();
        }
        serveAds(myAdQuery);
        if(ticker)
        {
            updateTickerPosition(time);
        }
    }
    window.setTimeout("ShowComments()",200);
}

function UpdateComment()
{
      var time = VideoCurrentPosition();
      var posX = document.getElementById("barTop1").style.posLeft;
      var posY = document.getElementById("barLeft1").style.posTop;
      var h = document.getElementById("barLeft1").height;
      var w = document.getElementById("barTop1").width;
      var commentcontent = document.getElementById("commentArea").value;
      var duration = document.getElementById("commentDuration").value;
      var commentId = document.getElementById("CommentId").value;
                var durationNum = Number(duration);
                if( !durationNum || durationNum < 1 || durationNum > 20)
                {
                    alert("Invalid Duration!");
                    return false;
                }
                if(commentcontent.indexOf("&") != -1 || commentcontent.indexOf("+") != -1)
                {
                    alert("Invalid Charaters !");
                    return false;
                }
        var temp = commentcontent.replace(' ','');
        while(temp.indexOf(' ') != -1) { temp = temp.replace(' ',''); }
        if(temp == '')
        {
            var confirmBox = confirm("You Should Input your comment!");
            if(confirmBox)
            {
                return false;
            }
            else
            {
                HideFrame();
                OnEditing = false;
                document.getElementById("commentArea").value = "";
                document.getElementById("CommentSubmit").style.display="none";
                VideoPlay();
                return false;
            }
        }
      ///
      var paramArray = new Array();
      paramArray["StartTime"] = time;
      paramArray["Top"] = posY;
      paramArray["Left"] = posX;
      paramArray["Bottom"] = posY + h;
      paramArray["Right"] = posX + w;
      paramArray["Duration"] = duration;
      paramArray["Value"] = ClearChars(commentcontent);
      paramArray["WriterPassprotName"] = username;
      paramArray["UserPassprotName"] = username;
      paramArray["VideoID"] = CurrentVideoID;
      paramArray["CommentID"] = commentId;
      AJAXSubmit("updatecomment",paramArray,SubmitSuccess,true);
      
             document.getElementById(commentId+"text").innerText = commentcontent;
             document.getElementById(commentId+"text").style.posLeft = posX;
             document.getElementById(commentId+"text").style.posTop = posY + h - moPosY + 1;
             if((posY + h) > 245)
             {
               // document.getElementById(commentId+"text").style.posTop = posY + h - moPosY -16;
             }
             document.getElementById(commentId+"frame").style.posLeft = posX ;
             document.getElementById(commentId+"frame").style.posTop = posY - moPosY;
             document.getElementById(commentId+"frame").style.width = w;
             document.getElementById(commentId+"frame").style.height = h;
             for(var index = 0; index < MyComments.length; index ++)
             {
                if(MyComments[index].id == commentId)
                {
                    MyComments[index].comment = commentcontent;
                    break;
                }
             }
             UpdataBookMark();
             return;
}

function AddReply(id,x,y)
{
    GetReplyOfComment(id);
    ShowReply(x,y);
    VideoPause();
}

function ShowObj()
{
    var children = event.srcElement;
    while(children.tagName.toLowerCase() != "span") children = children.parentNode;
    children = children.childNodes;
    for(var index= 0;index < children.length;index++)
    {
        if(children[index].tagName.toLowerCase()=="a")
        {
            children[index].style.display="";
        }
    }
}

function HideObj(id)
{
    var children = event.srcElement;
    while(children.tagName.toLowerCase() != "span") children = children.parentNode;
    children = children.childNodes;
    for(var index= 0;index < children.length;index++)
    {
        if(children[index].tagName.toLowerCase()=="a")
        {
            children[index].style.display="none";
        }
    }
    id = "tip"+id;
    var tooltip = document.getElementById(id);
    tooltip.style.display = "none";
}

function ShowTooltip(id,name)
{
    id = "tip"+id;
    var tooltip = document.getElementById(id);
    tooltip.style.display = "";
    tooltip.innerText = name;
}

function GetInt(number)
{
    var  tempnum;  
    tempnum = number;  
    tempnum = Math.round(tempnum);  
    return tempnum; 
}

function ClearClientComments()
{
    HideComments();
    ShowCommentsTable = {};
    HideCommentsTable = {};
    MyComments = new Array();
}

function ClearChars(inStr)
{
    var outStr = inStr.replace("\n"," ");
    if(outStr.indexOf("\n") != -1) outStr = outStr.replace("\n"," ");
    if(outStr.indexOf("\r") != -1) outStr = outStr.replace("\r"," ");
    return outStr;
}