var Vevo=window.Vevo||{};Vevo.Comments=function(container,type){if(!container){return}this.type=type||"video";this.pageIndex=0;this.isrc=null;this.playlistID=null;
this.sort="desc";this.$container=$(container);this.isPlaylistMode=this.$container.hasClass("isPlaylistMode");this.CONSTANTS={REPLY_CLASS:"replyTo",REPORT_CLASS:"reportAbuse",POST_CLASS:"postComment",COMMENT_CLASS:"comment",CLOSE_CLASS:"closeComment",DIRECTION_CLASS:"postDirection",SUBMIT_CLASS:"submitComment",THANKS_CLASS:"thanksComment",REPLY_SELECTOR:"input[name=parent]",BTN_SORT_NEWEST:"sortNewest",BTN_SORT_OLDEST:"sortOldest",CREATE_VIDEO_COMMENT_RESOURCE:"/Proxy/Video/Comment/Create.ashx",REPLY_VIDEO_COMMENT_RESOURCE:"/Proxy/Video/Comment/Reply.ashx",CREATE_PLAYLIST_COMMENT_RESOURCE:"/Proxy/Playlist/Comment/Create.ashx",REPLY_PLAYLIST_COMMENT_RESOURCE:"/Proxy/Playlist/Comment/Reply.ashx"};
this.MEMBERS={};this.c=this.CONSTANTS;this.m=this.MEMBERS;this.getMembers();this.setHandlers()};Vevo.Comments.initWithVideoComments=function(container){return new Vevo.Comments(container,"video")
};Vevo.Comments.initWithPlaylistComments=function(container){return new Vevo.Comments(container,"playlist")};Vevo.Comments.prototype={getMembers:function(){this.m.commentBox=$("."+this.c.COMMENT_CLASS,this.$container);
this.m.direction=$("."+this.c.DIRECTION_CLASS,this.$container);this.m.thanksBox=$("."+this.c.THANKS_CLASS,this.$container);this.m.replyID=$(this.c.REPLY_SELECTOR,this.$container);
this.m.form=$("form",this.$container);this.m.btnSortNewest=$("."+this.c.BTN_SORT_NEWEST,this.$container);this.m.btnSortOldest=$("."+this.c.BTN_SORT_OLDEST,this.$container)
},setHandlers:function(){var that=this;$(Vevo).bind("commentDataReady",function(e,data){that.commentDataHandler(e,data)}).bind("commentDataError",function(e,data){that.commentDataError(e,data)
}).bind("paginateComments",function(e,data){that.initPagination(e,data)}).bind("userInfoAvailable",function(e,data){that.userInfoAvailableHandler(e,data)});$(Vevo.VideoPlayer).bind("videoLoaded",function(e,data){that.videoLoadedHandler(e,data)
});$("."+this.c.CLOSE_CLASS,this.$container).bind("click",function(e){that.hideCommentBox(e)});$("form",this.$container).bind("submit",function(e){that.submitComment(e)
});this.m.btnSortNewest.bind("click",function(e){that.handleSortSelection(e)});this.m.btnSortOldest.bind("click",function(e){that.handleSortSelection(e)})},showCommentBox:function(e){if(e){e.preventDefault()
}var offset=this.$container.offset();this.hideThanks();$(window).scrollTop(offset.top);$(".submitComment").attr("disabled","");$(".postComment").css("visibility","hidden");
if(this.isPlaylistMode&&Vevo.PlaylistWatch){Vevo.PlaylistWatch.disableAutoAdvance();this.showPlaylistAutoAdvanceWarning()}var that=this;this.m.commentBox.slideDown(function(){that.m.commentBox.find("textarea").focus()
})},hideCommentBox:function(e){if(e){e.preventDefault()}this.m.commentBox.slideUp();$(".postComment").css("visibility","visible");if(this.isPlaylistMode&&Vevo.PlaylistWatch){Vevo.PlaylistWatch.enableAutoAdvance(true);
this.hidePlaylistAutoAdvanceWarning()}},addComment:function(e){if(e){e.preventDefault()}this.showCommentBox(e);this.m.direction.text("post your comments");this.m.replyID.val("")
},replyToComment:function(e){if(e){e.preventDefault()}this.showCommentBox(e);var replyingTo=$(e.target).parent("div.commentActions").siblings("a.commenter");this.m.direction.html("post your comments in reply to <strong>"+replyingTo.text()+"</strong>");
this.m.replyID.val($(e.target).attr("rel"))},reportComment:function(e){if(e){e.preventDefault()}var CommentID=$(e.target).attr("rel");var popin=Vevo.dialogHelpers.helper("#report-comment-dialog-wrapper");
popin.dialog("option","bgiframe",true).dialog("option","modal",false);popin.dialog("option","buttons",{Submit:function(){$.post("/Proxy/Video/Comment/Flag.ashx",{CommentID:CommentID,Reason:$(this).find(".ReportReason").val()},function(data){$("a.reportAbuse[rel="+CommentID+"]").replaceWith('<span class="reportedAbuse"> Reported</span>')
},"json");$(this).dialog("close")},Cancel:function(){$(this).dialog("close")}});popin.dialog("open")},hideThanks:function(e){if(e){e.preventDefault()}this.m.thanksBox.hide()
},showThanks:function(e){if(e){e.preventDefault()}this.m.thanksBox.slideDown("slow");$(".postComment").css("visibility","visible");if(this.isPlaylistMode&&Vevo.PlaylistWatch){Vevo.PlaylistWatch.enableAutoAdvance(false);
this.hidePlaylistAutoAdvanceWarning()}},showPlaylistAutoAdvanceWarning:function(){var that=this;this.m.commentBox.find("p.autoAdvanceWarning").remove();$(".autoAdvanceWarning",this.m.commentBox).one("click",function(e){e.preventDefault();
that.hideCommentBox()})},hidePlaylistAutoAdvanceWarning:function(){$(".autoAdvanceWarning",this.m.commentBox).remove()},commentDataError:function(e,data){$(".list",this.$container).empty().append('<li class="last">'+data.error_str+data.error_detail+"</li>")
},commentDataHandler:function(e,data){if(data.type!=this.type){return}this.initPagination(e,data);switch(data.type_name){case"Video":this.isrc=data.item_id;break;
case"Playlist":this.playlistID=data.item_id;break;case"Artist":break}$("input[name=isrc]",this.m.form).val(this.isrc);var $commentList=$(".comments",this.$container);
var commentHTML="";var commentRef={};var commentStack=[];if(data.comments.length>0){$.each(data.comments,function(i){var rootCommentID,isRoot=true;if(!isNaN(parseInt(this.root_comment))){rootCommentID=parseInt(this.parent_id);
isRoot=false}else{if(!isNaN(parseInt(this.root_comment))){rootCommentID=parseInt(this.parent_id);isRoot=false}else{rootCommentID=parseInt(this.id)}}if(commentRef[rootCommentID]==null){commentRef[rootCommentID]={children:[],root:null,id:rootCommentID};
commentStack.push(commentRef[rootCommentID])}var user_type=this.user.user_type||this.user.display_name;if(typeof(user_type)==="string"){user_type=user_type.toLowerCase()
}else{user_type="user"}var user_profile_url="/user/profile/"+this.user.user_name;user_profile_photo=this.user.profile_photo_url;var jEle=$('<li class="'+user_type+' clearfix" id="comment_'+this.id+'"><a href="'+user_profile_url+'" class="ui-f-left image" title="see profile"><img src="'+user_profile_photo+'" alt="'+this.user.user_name+'" /></a><div class="comment-wrapper ui-f-left"><div class="commentActions clearfix"><a href="#" class="reportAbuse" rel="'+this.id+'">Spam</a><a href="#" class="replyTo" rel="'+this.id+'">Reply</a><p class="commentTime ui-f-left">'+Vevo.Util.formatDate(this.created)+'</p></div><a class="commenter" href="'+user_profile_url+'">'+this.user.user_name+'</a> <span class="type">'+user_type+'</span><p class="commentBody"></p></div></li>');
$(".commentBody",jEle).text(Vevo.Util.decodeHTMLEntity(this.body));if(isRoot){commentRef[rootCommentID].hasRoot=true;commentRef[rootCommentID].root=jEle}else{jEle.addClass("reply");
commentRef[rootCommentID].children.push(jEle)}});$commentList.empty();var that=this;$.each(commentStack,function(i){if(this.root){$commentList.append(this.root)}else{$commentList.append('<li class="user clearfix" id="comment_'+this.id+'"><p style="font-style: italic">The following posts are replies to a comment on the '+(that.sort=="desc"?"next":"previous")+" page.</p></li>")
}$.each(this.children,function(){$commentList.append(this)})});$commentList.find(".reply a.replyTo").remove()}else{$commentList.html("<li>No comments</li>")}},pageSelectCallback:function(pageIndex,jq){this.pageIndex=pageIndex*10;
$(".comments",this.$container).html("<li>Loading comments...</li>");switch(this.type){case"video":var data={isrc:this.isrc,commentIndex:this.pageIndex,pageSize:10,sort:this.sort};
$(Vevo).trigger("getVideoComments",[data]);break;case"playlist":var data={playlistID:this.playlistID,commentIndex:this.pageIndex,pageSize:10,sort:this.sort};$(Vevo).trigger("getPlaylistComments",[data]);
break}},refresh:function(cacheBust){var cache=cacheBust||false;switch(this.type){case"video":var data={isrc:this.isrc,commentIndex:this.pageIndex,pageSize:10,sort:this.sort};
$(Vevo).trigger("getVideoComments",[data,cache]);break;case"playlist":var data={playlistID:this.playlistID,commentIndex:this.pageIndex,pageSize:10,sort:this.sort};
$(Vevo).trigger("getPlaylistComments",[data,cache]);break}if(this.sort=="asc"){this.m.btnSortNewest.removeClass("on");this.m.btnSortOldest.addClass("on")}else{this.m.btnSortNewest.addClass("on");
this.m.btnSortOldest.removeClass("on")}},initPagination:function(e,data){if(data.type!=this.type){return}this.comments_count=data.comments_count;var current_page=data.cursor?Math.floor(data.cursor.offset/10):0;
var that=this;$(".pagination",this.$container).pagination(data.comments_count,{prev_show_always:false,next_show_always:false,num_display_entries:5,num_edge_entries:1,current_page:current_page,callback:function(e,data){that.pageSelectCallback(e,data)
}});this.isPaginated=true},submitComment:function(e){if(e){e.preventDefault()}if($.trim($("textarea",this.$container).val()).replace(/[<>]+/g,"").length==0){$(".commentError",this.$container).fadeIn();
return}$(".submitComment").attr("disabled","disabled");var resource={};switch(this.type){case"video":resource.reply=this.c.REPLY_VIDEO_COMMENT_RESOURCE;resource.create=this.c.CREATE_VIDEO_COMMENT_RESOURCE;
break;case"playlist":resource.reply=this.c.REPLY_PLAYLIST_COMMENT_RESOURCE;resource.create=this.c.CREATE_PLAYLIST_COMMENT_RESOURCE;break}var parentId=$("input[name=parent]",this.m.form).val().replace(/[<>]+/g,"");
var postData=$(this.m.form).serialize();postData.body=$.trim(postData.body);var postToURI=(parentId!="")?resource.reply:resource.create;var that=this;$.ajax({type:"POST",url:postToURI,data:postData,dataType:"json",success:function(response,textstatus){if(response.success||(typeof response.error_code==="undefined"||response.error_code===0)){$("form",that.m.commentBox).get(0).reset();
$(".charsLeft",that.$container).html(that.m.commentBox.find("textarea").attr("maxlength"));$(".commentError",that.$container).hide();that.comments_count++;if(parentId==""){that.m.btnSortNewest.click()
}else{that.refresh(true)}that.m.commentBox.slideUp(function(){that.showThanks();$("ul.activityNav li").one("click",function(ev){$("div.thanksComment").hide()})})
}else{$(".commentError",that.$container).text(response.error_detail).fadeIn()}}})},videoLoadedHandler:function(e,data){if(this.type=="video"){var data={isrc:data,pageSize:"10"};
$(".thanksComment").hide();$(Vevo).trigger("getVideoComments",[data])}},userInfoAvailableHandler:function(e,data){if(!data.login){$(".signedInComment",this.$container).hide();
$(".signedOutComment",this.$container).show();var LogInFunc=function(e){if(e){e.preventDefault()}Vevo.messageButNotLoggedIn.show("Contributing Comments","You will need to be signed in to post comments.")
};$("."+this.c.REPLY_CLASS,this.$container).live("click",LogInFunc);$("."+this.c.POST_CLASS,this.$container).bind("click",LogInFunc);var ReportLogInFunc=function(e){if(e){e.preventDefault()
}Vevo.messageButNotLoggedIn.show("Report Abuses","You will need to be signed in to report abuses.")};$("."+this.c.REPORT_CLASS,this.$container).live("click",ReportLogInFunc)
}else{var that=this;$("."+this.c.REPLY_CLASS,this.$container).live("click",function(e){that.replyToComment(e)});$("."+this.c.POST_CLASS,this.$container).bind("click",function(e){that.addComment(e)
});$("."+this.c.REPORT_CLASS,this.$container).live("click",function(e){that.reportComment(e)})}},handleSortSelection:function(e){e.preventDefault();var data={pageSize:"10"},eventType="",cache=false,target=$(e.target);
this.m.btnSortNewest.removeClass("on");this.m.btnSortOldest.removeClass("on");target.addClass("on");data.sort=(target.hasClass("."+this.c.BTN_SORT_NEWEST))?"desc":"asc";
this.sort=data.sort;switch(this.type){case"video":data.isrc=this.isrc;eventType="getVideoComments";break;case"playlist":data.playlistID=this.playlistID;eventType="getPlaylistComments";
break}$(".comments",this.$container).html("<li>Loading comments...</li>");$(Vevo).trigger(eventType,[data,cache])}};$(Vevo).bind("watchPlaylist",function(){if(parseInt(Vevo.pageData.playlistID)>0){Vevo.Comments.initWithPlaylistComments($("#tabPlayListComments"))
}Vevo.Comments.initWithVideoComments($("#tabVideoComments"))}).bind("watchVideo",function(){Vevo.Comments.initWithVideoComments($("#tabVideoComments"))});