// JavaScript Document

        function escapeQuote(text){
           var result = text.replace(/'/g,"\\'");
           return result;
        }

        function txtshow( txt2show ) { 
 
            if (parseInt(navigator.appVersion) >=5) {DOM=1};           
            // Detect Browser
            if (DOM) {
		            var viewer = document.getElementById("txtmsg");
                viewer.innerHTML=txt2show;
            }
            else if(IE) {
                document.all["txtmsg"].innerHTML=txt2show;
            }
        }

      function keyup(what){
        var str = new String(what.value);
        var len = str.length;
        var showstr = "You have typed "+len + " characters out of " + maxKeys + " allowed.";
        if (len > maxKeys) showstr += '<br>Some information will be lost, please revise your entry';
        txtshow( showstr );
      }
    	
    	function nextComments(vid, beginning){
        var thisVID = vid;
        var thisBeginning = beginning;
    	   var nextJSON = "{                                                     "+                   
                   " 'videoId':               '"+thisVID+"',                  "+
                   " 'begin':                '"+thisBeginning+"'              "+
                   "}                                                         ";
                   
         fetchcomments(nextJSON);
      }
      
       function fetchcomments(json){            
            var data = eval("("+json+")");
            jQuery.post("/community/allVideoComments.html", data, function(data){
            drawPage(data);},"json");
         }
         
         function drawPage(newjson){
            if (newjson.count != undefined){
                 var count = parseInt(newjson.count);
           }else{
                var count = 0;
           }
            if (newjson.begin != undefined){
                 var begin = parseInt(newjson.begin);
            }else{
                 var begin = 0;
            }
            if (begin != 0){
                var end = begin + 9;
            }else{
                var end = 0;
            }
            var prev = parseInt(newjson.begin);
            document.getElementById('commentsCount').innerHTML = "";
            var countText = "";
            document.getElementById('commentsPagination').innerHTML = "";
			if(begin > 1){ 
                var previous = prev-10;               
                jQuery('#commentsPagination').append('<a href="javascript:nextComments(\''+videoID+'\','+previous+')"><img width="5" height="10" src="/community/images/icons/prev_page.png" border="0" alt="Previous Page" title="Previous Page"/></a>&nbsp;&nbsp;');
            }
			else {
				jQuery('#commentsPagination').append('<img width="5" height="10" src="/community/images/icons/prev_page_disabled.png" border="0"/>&nbsp;&nbsp;');
			}
            if (end > count){
              countText = begin.toString()+'-'+count.toString()+' of '+count.toString();
			  jQuery('#commentsPagination').append(countText);
			  jQuery('#commentsPagination').append('&nbsp;&nbsp;<img width="5" height="10" src="/community/images/icons/next_page_disabled.png" border="0"/>');
            }else{
              var next = end+1;
              countText = begin.toString()+'-'+end.toString()+' of '+count.toString(); 
			  jQuery('#commentsPagination').append(countText);
              jQuery('#commentsPagination').append('&nbsp;&nbsp;<a href="javascript:nextComments(\''+videoID+'\','+next+')"><img width="5" height="10" src="/community/images/icons/next_page.png" border="0" alt="Next Page" title="Next Page"/></a>');
            }
            
            //jQuery('#commentsCount').append(countText);
            
            items = newjson.items;
            sfx = eval(items);
            //alert(sfx);
            //alert(items.length);                    
            document.getElementById('communityComments').innerHTML = "";
            z=1;
            for ( var i in items ){
                //alert(z);
                //alert(items[i].comment);
                if (items[i].comment != undefined){
                var scrubbedComment = items[i].comment.replace(/\+/g, '%20');
                scrubbedComment = unescape(scrubbedComment);
                if (items[i].uid != '0' ){
                var commentDiv = '<div class="aComment">                                                                                                                  '+
                             '<div class="aCommentPortraitContainer">                                                                                                     '+
                             '<div class="commentPortrait">                                                                                                               '+
                             '<a href="/community/manage/userProfileHome.html?u='+items[i].uid+'"><image src="/'+items[i].photoURl+'" alt="" border="0" width="65" height="65"/></a>'+
                             '</div>                                                                                                                                      '+
                             '</div>                                                                                                                                      '+
                             '<div class="aCommentFlag"><span id="flag'+items[i].cid+'"><a href="javascript:flagComment('+items[i].cid+')">report</a></span></div>          '+
							 '<div class="aCommentBodyContainer"><div class="commentUsername"><a href="/community/manage/userProfileHome.html?u='+items[i].uid+'">'+items[i].username+'</a></div>            '+
                             '<div class="aCommentBody">'+scrubbedComment+'</div></div><div class="clear"></div>                                                                '+
                             '</div>                                                                                                                                      ';
                  }else{
                     var commentDiv = '<div class="aComment">                                                                                                                  '+
                             '<div class="aCommentPortraitContainer">                                                                                                     '+
                             '<div class="commentPortrait">                                                                                                               '+
                             '<image src="/'+items[i].photoURl+'" alt="" border="0" width="65" height="65"/>'+
                             '</div>                                                                                                                                      '+
                             '</div>                                                                                                                                      '+
                             '<div class="aCommentFlag"><span id="flag'+items[i].cid+'"><a href="javascript:flagComment('+items[i].cid+')">report</a></span></div>          '+
							               '<div class="aCommentBodyContainer"><div class="commentUsername"><span class="anonymousComment">'+items[i].username+'</span></div>            '+
                             '<div class="aCommentBody">'+scrubbedComment+'</div></div><div class="clear"></div>                                                                '+
                             '</div>                                                                                                                                      ';                 
                  }
                jQuery('#communityComments').append(commentDiv);
                z++;
                }
            
            }
            
         }
         
         function addComment(){
                var comment = jQuery('#videoCommentsForm textarea[name=comments]').val();
                var cleanComment = escape(comment);
                var videoTitle = jQuery('#videoCommentsForm input[name=videoTitle]').val();
                var videoUrl = jQuery('#videoCommentsForm input[name=defaultVideoUrl]').val();
                var videoImage = jQuery('#videoCommentsForm input[name=defaultVideoPhotoUrl]').val();
                var showTitle = jQuery('#videoCommentsForm input[name=showTitle]').val();
                
                var cleanComment = escape(jQuery('#videoCommentsForm textarea[name=comments]').val());
                var MyJSONObject = "{                                                                                         "+
                   " 'claz':                  '"+jQuery('#videoCommentsForm input[name=claz]').val()+"',                  "+
                   " 'comments':              '"+cleanComment+"',                                                         "+
                   " 'defaultVideoPhotoUrl':  '"+jQuery('#videoCommentsForm input[name=defaultVideoPhotoUrl]').val()+"',  "+
                   " 'defaultVideoUrl':       '"+jQuery('#videoCommentsForm input[name=defaultVideoUrl]').val()+"',       "+
                   " 'season':                '"+jQuery('#videoCommentsForm input[name=season]').val()+"',                "+
                   " 'episode_number':        '"+jQuery('#videoCommentsForm input[name=episode_number]').val()+"',        "+
                   " 'showTitle':             '"+escapeQuote(jQuery('#videoCommentsForm input[name=showTitle]').val())+"',             "+
                   " 'videoTitle':            '"+escapeQuote(jQuery('#videoCommentsForm input[name=videoTitle]').val())+"',            "+
                   " 'videoId':               '"+jQuery('#videoCommentsForm input[name=videoId]').val()+"',               "+
                   " 'showId':                '"+jQuery('#videoCommentsForm input[name=showId]').val()+"'                 "+
                   "}                                                                                                     ";

                  if (facebook_enabled && facebook_uid != "") {
                      fbPublishComment(comment,videoTitle,videoUrl,videoImage,showTitle);
                  }
                  
                  submitComment(MyJSONObject);
         }

         function fbPublishComment(comment,videoTitle,videoUrl,videoImage,showTitle) {
                var serverHost = window.location.hostname;
                var imageTag = '<img src="' + videoImage + '" height="80"/>';
                var imageAnchor = imageTag.link("http://"+serverHost+videoUrl);
                var videoAnchor = videoTitle.link("http://"+serverHost+videoUrl);
                var comment_data = {
                    "verb":'commented on the video ',
                    "noun":videoAnchor + ' from the show '+ showTitle,
                    "body":truncate(comment,35,' ...'),
                    "fullbody":imageAnchor + '<br/><br/>' + truncate(comment,70,' ...')
                };
                facebookPublish(comment_data);
         }
         
         function truncate (text, maxlen, moreLabel) {
            if (text.length > maxlen) { return text.substring(0,maxlen) + moreLabel; }
            return text;
         }
         
         function submitComment(json){
            var data = eval("("+json+")");
            jQuery.post("/community/writeEntertainmentComment.html", data, function(data){
            postComment(data);},"json");
         }
         
         function postComment(json){
            if (json.message == "Success" || json.message == "Success with Anonymous user!"){
                document.getElementById('comments').value = '';
                var uid = json.uid;
                var username = json.username;
                var userphoto = json.photoURl;
                var cid = json.cid;
                var scrubbedComment = json.comment;
                if (uid != '0'){
                var commentDiv = '<div class="aComment">                                                                                                                  '+
                             '<div class="aCommentPortraitContainer">                                                                                                     '+
                             '<div class="commentPortrait">                                                                                                               '+
                             '<a href="/community/manage/userProfileHome.html?u='+uid+'"><image src="/'+userphoto+'" alt="" border="0" width="65" height="65"/></a>'+
                             '</div>                                                                                                                                      '+
                             '</div>                                                                                                                                      '+
                             '<div class="aCommentFlag"><span id="flag'+cid+'"><a href="javascript:flagComment('+cid+')">report</a></span></div>                            '+
							 '<div class="aCommentBodyContainer"><div class="commentUsername"><a href="/community/manage/userProfileHome.html?u='+uid+'">'+username+'</a></div>                              '+
                             '<div class="aCommentBody">'+scrubbedComment+'</div></div><div class="clear"></div>                                                                '+
                             '</div>                                                                                                                                      ';
                }else{
                var commentDiv = '<div class="aComment">                                                                                                                  '+
                             '<div class="aCommentPortraitContainer">                                                                                                     '+
                             '<div class="commentPortrait">                                                                                                               '+
                             '<image src="/'+userphoto+'" alt="" border="0" width="65" height="65"/>'+
                             '</div>                                                                                                                                      '+
                             '</div>                                                                                                                                      '+
                             '<div class="aCommentFlag"><span id="flag'+cid+'"><a href="javascript:flagComment('+cid+')">report</a></span></div>                            '+
							 '<div class="aCommentBodyContainer"><div class="commentUsername"><span class="anonymousComment">'+username+'</span></div>                              '+
                             '<div class="aCommentBody">'+scrubbedComment+'</div></div><div class="clear"></div>                                                                '+
                             '</div>                                                                                                                                      ';
                }           
                jQuery('#communityComments').prepend(commentDiv); 
            }else{
              alert("Unable to add your comment.");
            }        
         }
         
         function flagComment(cid){
            comment_id = cid;
            flagJSON = "{ 'commentId': '"+comment_id+"' }";
            submitFlag(flagJSON);
         }
         
          function submitFlag(json){
            var data = eval("("+json+")");
            jQuery.post("/community/flagComment.html", data, function(data){
            finishFlag(data);},"json");
         }
         
         function flagPhoto(pid){
            photo_id = pid;
            flagJSON = "{ 'pid': '"+photo_id+"' }";
            submitPhotoFlag(flagJSON);
         }
         
          function submitPhotoFlag(json){
            var data = eval("("+json+")");
            $.post("/community/flagPhoto.html", data, function(data){
            finishPhotoFlag(data);},"json");
         }
         
         function finishPhotoFlag(json){
            var thisFlagId = "flag"+photo_id;
            var thisFlagSpan = document.getElementById(thisFlagId);
            thisFlagSpan.innerHTML = "Reported!";
         }
         
         function finishFlag(json){
            var thisFlagId = "flag"+comment_id;
            var thisFlagSpan = document.getElementById(thisFlagId);
            thisFlagSpan.innerHTML = "Reported!";
         }
         
         function urldecode( str ) {   
            ret = str;       
            ret = ret.replace(/\+/g, '%20');
            ret = decodeURIComponent(ret);
            ret = ret.toString(); 
            return ret;
        } 
