// DISQUS Comments - Theme: CNN

/**
 * Theme methods and globals
 */

function disqus_cookie_msgs() {
	if(Dsq.jsonData.request.is_initial_load) {
		Dsq.$('dsq-cnn-unapproved-box').style.display = 'block';
		location.hash = '#dsq-cnn-unapproved-box';
	}
}

var DsqBIA = new function() {
	this.is_not_authenticated = function() {
		if(Dsq.jsonData.settings.debug) {
			return !Dsq.jsonData.request.is_authenticated;
		} else {
			return (Dsq.jsonData.request.missing_perm == 'cnn-required');
		}
	};

	this.login = function() {
		if(Dsq.jsonData.settings.debug) {
			Dsq.Popup.login();
		} else {
			showLoginOverlay();
		}
	};
	
	this.showPostBox = function() {		
		if(DsqBIA.is_not_authenticated()) {
			DsqBIA.login();
		} else {
			Dsq.$('dsq-post-add').innerHTML = '';
			Dsq.Iframes.showReplyIframeInContainerIfAllowed(Dsq.$('dsq-post-add'));
			Dsq.$('dsq-post-add-wrapper').style.display = 'block';
			location.hash = '#dsq-post-a-comment';			
		}
	};
	
	this.cancelPost = function(postId) {
		if(postId) {
			var link = Dsq.$('dsq-reply-link-' + postId);
			DsqBIA.toggleReply(link, postId);
		} else {
			var container = Dsq.$('dsq-post-add-wrapper');
			container.style.display = 'none';
		}

	};
	
	this.showReplyIframe = function(postId) {
		if(postId) {
			var container = Dsq.$('dsq-reply-' + postId);
		} else {
			var container = Dsq.$('dsq-post-add');
		}

		Dsq.Iframes.showReplyIframeInContainerIfAllowed(container, postId);
		
		if(Dsq.Utils.ie) { Dsq.Utils.fixIframesIE(); }
		container.style.display = 'block';
				
	};
	
	this.rate = function(el, id, vote) {
		Dsq.jsonData.request.is_authenticated = true;
		Dsq.Post.rate(el, id, vote);
    };

	this.avatar_url = function(post_id) {
		var username;
		
		if(post_id) {
			var userKey = Dsq.jsonData['posts'][post_id].user_key;
			var userData = Dsq.jsonData['users'][userKey];
			username = userData['display_name'];
		} else {
			username = Dsq.jsonData.request.display_username;
		}
		
		return 'http://www.ireport.com/people/' + username + '/avatar/48.png';
	};

	this.toggleReply = function(el, id) {
		var _meta = Dsq.jsonData.posts[id];

		if(!Dsq.Post.stateReplyToggled[id]) {
			// TOGGLE ON
			if(DsqBIA.is_not_authenticated()) {
				DsqBIA.login();
				return;
			} else {
				if(Dsq.$('dsq-reply-' + id )){
					Dsq.$('dsq-comment-reply-' + id).style.display = 'block';
				} else {
					Dsq.$('dsq-comment-reply-' + id).innerHTML = (!_meta.has_replies ? '<div class="dsq-replies-header"><div></div></div>' : '')
					+ '<div class="dsq-post-reply-wrapper"> \
							<img class="dsq-post-avatar" src="' + DsqBIA.avatar_url() + '" alt="" /> \
						<div id="dsq-reply-' + id + '" class="dsq-reply"> \
						</div> \
						<a href="#" onclick="DsqBIA.cancelPost(' + id + '); return false" class="dsq-post-cancel"></a> \
					<p class="dsq-cnn-moderated">Comments are moderated by CNN, in accordance with the <a href="#cnnFooter">CNN Comment Policy</a>, and may not appear on this blog until they have been reviewed and deemed appropriate for posting. Also, due to the volume of comments we receive, not all comments will be posted.</p> \
					</div>'
					+ (!_meta.has_replies ? '<div class="dsq-replies-footer"><div></div></div>' : '');
					DsqBIA.showReplyIframe(id);
					if(Dsq.Utils.ie6) {
						// HACK: Fixes IE6 issue with iframe width
						Dsq.$('dsq-comment-reply-' + id).style.display = 'none';
						Dsq.Utils.fixIframesIE(id);
						Dsq.$('dsq-comment-reply-' + id).style.display = 'block';
					}
				}
			}
		} else {
			// TOGGLE OFF
			Dsq.$('dsq-comment-reply-' + id).style.display = 'none';
			
			if(Dsq.Post.stateRecordLink[id]) {
				// HACK: Cancel media before canceling self.
				Dsq.Post.toggleMediaReply(Dsq.$('dsq-media-link-' + id), id);
			}
		}

		Dsq.Post.stateReplyToggled[id] = !Dsq.Post.stateReplyToggled[id];
		Dsq.Post._updateReplyLinks(el, id);

		if(Dsq.Utils.ie) { Dsq.Utils.fixIframesIE(id); }

		Dsq.Events.fire(Dsq.Events.REPLY_IFRAME_TOGGLED, {
			postId: id,
			opened: Dsq.Post.stateReplyToggled[id]
		});
	};

	this.readMore = function(post_id) {
		Dsq.$('dsq-comment-original-' + post_id).style.display = 'block';
		Dsq.$('dsq-comment-stripped-' + post_id).style.display = 'none';
	};
	
	this.readLess = function(post_id) {
		Dsq.$('dsq-comment-original-' + post_id).style.display = 'none';
		Dsq.$('dsq-comment-stripped-' + post_id).style.display = 'block';
	};
	
	this.report = function(id, confirmed) {
		if(confirmed) {
			// Will never get here
		} else {
			var title = 'Report inappropriate comment';
			var message = ' \
			Are you sure you would like to report this comment? It will be flagged for CNN moderators to take action.<br /><br /> \
			<button onclick="Dsq.Popup._closePopup(null, true)"><strong>No</strong>, Never mind</button>&nbsp;&nbsp;&nbsp;<button onclick="Dsq.Post.report(' + id + ', true); Dsq.Popup._closePopup(null, true)"><strong>Yes</strong>, Report this comment</button><br /><br /> \
			';

			Dsq.Popup.popModal(message, title, id);
		}
	};
};

/**
 * Custom templates and filters
 */

var DsqLocal = {
	'Filters': {
		header: function(html) {
			// HACK: Dependent on English language setting
			html = html.replace('Newest first', 'Newest to oldest');
			html = html.replace('Oldest first', 'Oldest to newest');
			html = html.replace('Best Rating', 'Most Likes');
			html = html.replace('Popular now', 'Most Popular');
			html = html.replace('Sort by', '');

			// Replace Dsq.Templates.Filters.commentContent
			if(typeof DsqLocal._filterCommentContent != 'function') {
				DsqLocal._filterCommentContent = Dsq.Templates.Filters.commentContent;
				Dsq.Templates.Filters.commentContent = function(post_id, s) {
					var _meta = Dsq.jsonData.posts[post_id];
					var stripped = s.replace(/<.*?>/g, '');
					var max_length = !_meta.depth ? 1000 : 500;

					s = s.replace(/(<br.*?>){3,}/gi, '<br><br>');
					if(stripped.length > max_length) {
						var original = s;
						s = '<div id="dsq-comment-stripped-' + post_id + '">' + stripped.substring(0, max_length) + ' <a href="#" onclick="DsqBIA.readMore(' + post_id + '); return false;">...more</a></div>';
						s += '<div id="dsq-comment-original-' + post_id + '" style="display:none">' + original + ' <a href="#" onclick="DsqBIA.readLess(' + post_id + '); return false;">less</a></div>';
					}
					return DsqLocal._filterCommentContent(post_id, s);
				}
			}
										
			var new_html = '<div class="cnn_INACBCHead"> \
				All comments \
				<a href="http://' + Dsq.jsonData.forum.url + '.' + Dsq.jsonData.settings.disqus_url.slice(7) + '/' + Dsq.jsonData.thread.slug + '/latest.rss"><img width="58" height="11" border="0" style="margin: 0 0 0 12px" alt="" src="http://i.cdn.turner.com/cnn/SPECIALS/2009/black.in.america/images/community/subscribe.gif"/></a> \
			</div>';

			new_html = html + new_html;
			
			new_html += ' \
			<div class="cnn_INACBCpac dsq-post-a-comment"> \
				<a href="#" id="dsq-post-a-comment" onclick="DsqBIA.showPostBox(); return false">POST A COMMENT</a> \
			</div> \
			';

			new_html += ' \
				<div id="dsq-post-add-wrapper" class="dsq-post-add-wrapper" style="display: none"> \
					<img class="dsq-post-avatar" src="' + DsqBIA.avatar_url() + '" alt="" /> \
					<div id="dsq-post-add"> \
					</div> \
					<a href="#" onclick="DsqBIA.cancelPost(); return false" class="dsq-post-cancel"></a> \
					<p class="dsq-cnn-moderated">Comments are moderated by CNN, in accordance with the <a href="#cnnFooter">CNN Comment Policy</a>, and may not appear on this blog until they have been reviewed and deemed appropriate for posting. Also, due to the volume of comments we receive, not all comments will be posted.</p> \
				</div> \
			';
			
			new_html += ' \
			<div class="cnn_INAPostCBox" id="dsq-cnn-unapproved-box" style="display: none"> \
				<div class="cnnBoxHeader"><div></div></div> \
				<div class="cnnBoxContent"> \
					<div style="font:normal 14px/16px arial;">Thank you for contributing</div> \
					<div class="cnnBoxTxt">Comments are moderated by CNN and will not appear on this story until after they have been reviewed and deemed appropiate for posting. Unfortunately, due to the volume of comments we received, not all comments can be posted. \
					<br><br><a href="#" onclick="DsqBIA.showPostBox(); return false">Post another comment</a></div> \
				</div> \
				<div class="cnnBoxFooter"><div></div></div> \
			</div> \
			';
			
			return new_html;
		},
		postFooter: function(html, post_id) {
			var _meta = Dsq.jsonData.posts[post_id];
			if(_meta.killed) { return ''; }
			var RE = /(<div id="dsq-points.*?<ul class="dsq-comment-options.*?>.*?<ul\s*.*?>.*?<\/ul>)/gim;
			var RE2 = /(<div id="dsq-reply.*?<\/div>)/gim;
			
			var new_html = (_meta.depth == 1
						? (_meta.num_replies 
							? '<div class="dsq-child-replies" title="Show/Hide replies">'+ _meta.num_replies + ' Replies <a href="#" class="dsq-child-replies-show" onclick="LS.showReplies(this, ' + post_id + '); return false"><img src="http://images.lifescript.com/images/icon/plus.gif" alt="" /></a></div>'
							: '')
						: '')
				+ '<ul class="dsq-comment-options dsq-list-style">'
				+ '<li class="dsq-list-first dsq-rate" id="dsq-rate-cont-' + post_id + '">'
					+ (_meta.votable
						? (!_meta.up_voted
							? '<a href="#" onclick="DsqBIA.rate(this, ' + post_id + ', 1); return false;">LIKE' 
								+ (_meta.points
									? ' (' + _meta.points + ')'
									: '')
								+'</a>'
							: 'YOU LIKE THIS'
							+ (_meta.points
								? ' (' + _meta.points + ')'
								: '')
							+'</a>')
						: (_meta.points ? _meta.points + Dsq.Utils.pluralize(_meta.points, ' PERSON', ' PEOPLE') + ' LIKED THIS' : '')) 
				+ '</li>'
				+ '<li class="dsq-report" id="dsq-post-report-' + post_id + '"><a href="#" class="dsq-post-report" onclick="DsqBIA.report(' + post_id + ', false); return false;">REPORT</a></li>'
				+ '</ul>'
				+ '<ul class="dsq-list-style">'
				+ (!_meta.depth
					? '<li class="dsq-list-first"><a href="#" id="dsq-reply-link-' + post_id +'" onclick="DsqBIA.toggleReply(this, ' + post_id +'); return false;">'
						+ (DsqBIA.is_not_authenticated()
							? 'LOGIN TO REPLY'
							: 'REPLY')
						+ '</a>' + (_meta.num_replies ? ' <span class="dsq-reply-child-count">(' + _meta.num_replies + ')</span>' : '') + '</li>'
					: '')
				// + (_meta.can_reply && !_meta.has_replies && _meta.from_request_user
				// 	? '<li id="dsq-edit-el-' + post_id + '"><a id="dsq-edit-link-' + post_id + '" href="#" onclick="Dsq.Post.edit(this, ' + post_id + '); return false;">EDIT</a></li>'
				// 	: '')
				+ (Dsq.jsonData.request.is_moderator ? 
					'<li class="' + (!_meta.can_reply ? 'dsq-list-first' : '') + '" id="dsq-more-el-' + post_id + '"><a id="dsq-more-link-' + post_id + '" href="#" onclick="Dsq.Post.showMenu(this, ' + post_id + ', \'more\'); return false">MORE <small>&#9660;</small></a></li>'
					: '')
				+ (Dsq.jsonData.forum.use_media
					? '<li id="dsq-media-el-' + post_id +'" style="display:none"><a id="dsq-media-link-' + post_id + '" href="#" onclick="Dsq.Post.showMenu(this, ' + post_id + ', \'media\'); return false">USE MEDIA <small>&#9660;</small></a></li>'
					: '')
				+ '</ul>';
			
			html = html.replace(RE, new_html);
			html = html.replace(RE2, ''); // Remove dsq-reply-{id} from footer
			
			return html;
		},
		footer: function(html) {
			html = '<div class="cnn_INACBCpac post-a-comment"><a href="#" onclick="DsqBIA.showPostBox(); return false">POST A COMMENT</a></div>' + html;
			return html;
		}
	}, // Filters
	'Templates': {
		authPost: function() {
			var html = ' ';
			return html;
		},
		prependPost: function(post_id) {
			var _meta = Dsq.jsonData.posts[post_id];
			var html = '<div id="comment-' + post_id + '"></div>';
			return html;
		},
		appendPost: function(post_id) {
			var _meta = Dsq.jsonData.posts[post_id];
			var html = ' ' 

			html += (_meta.has_replies
					? '<div class="dsq-replies-header"><div></div></div>'
					: '');

			html += (_meta.is_last_child
					? '<div class="dsq-replies-footer"><div></div></div>'
					: '');

			html += (!_meta.depth
					? '<div id="dsq-comment-reply-' + post_id + '"></div>'
					: '');

			return html;
		},
		postPrependHeader: function(post_id) {
			// Using CNN avatars
			for(var i in Dsq.jsonData.users) {
				Dsq.jsonData.users[i].avatar = DsqBIA.avatar_url(post_id);
			}
			// Removing dropProfile and popupProfile functionality
			Dsq.Post.dropProfile = function() { return; }
			Dsq.Popup.popProfile = function() { return; }

			return Dsq.Templates.$$_postPrependHeader(post_id);
		},
		preBody: function(post_id) {
			// Using this template as the POST HEADER as this theme 
			// requires the POST HEADER and POST BODY to be within the same container
			var _meta = Dsq.jsonData.posts[post_id];
			var userData = Dsq.jsonData['users'][_meta.user_key];

			var html = '<div class="dsq-comment-body-header"><div></div></div>'
			
			html += '<div class="dsq-comment-body-byline"> \
				<cite>' + userData['display_name'] + '</cite> \
				<span class="dsq-header-meta">updated ' + _meta.date + '</span> \
				</div> \
			';

			return html;
		},
		postBody: function(post_id) {
			// TODO: Deprecate flagging conditional
			var _meta = Dsq.jsonData.posts[post_id];
			var html = (_meta.edited
					? '<p class="dsq-editedtxt">(Edited by a moderator)</p>'
					: '');
				
			html += '<div class="dsq-comment-body-footer"><div></div></div>';
					
			return html;
		},
		postAppendHeader: function(post_id) {
			var _meta = Dsq.jsonData.posts[post_id];
			var html = (_meta.author_is_moderator
					? ' '
					: ' ');

			return html;
		},
		_popupGeneric: function(content) {
			return ' \
			<div class="dsq-popup-content"> \
				<div class="dsq-popup-top"></div> \
				<div class="dsq-popup-body" class="clearfix"> \
					<div class="dsq-popup-body-padding"> \
						<div class="dsq-popup-header"> \
							<a class="dsq-close-link" href="#" onclick="Dsq.Popup._closePopup(null, true); return false"><img src="http://i.cdn.turner.com/cnn/.element/img/2.0/sect/connect/btn_close.gif" alt="" /></a>'
							+ content['header']
						+ '</div>'
						+ content['body']
					+ '</div> <!-- padding --> \
				</div> <!-- body --> \
				<div class="dsq-popup-bottom"></div> \
			</div> \
			';
		},
		voted: function(post_id, points, vote) {
			// Update link text
			if(vote) {
				Dsq.$('dsq-rate-cont-' + post_id).innerHTML = 'YOU LIKE THIS (' + points + ')';
			}
			
			return post_id;
		}
	} // Templates
};
