function disqus_cookie_msgs(message, title) {
	var title = '';
	var message = '';
	var numAlerts = 0;

	Dsq.Utils.forEachIn(Dsq.jsonData.cookie_messages, function(k, v) {
		if (!v) return;

		switch(k) {
			// Cookie: Twitter
			case 'post_twitter':
				numAlerts = numAlerts - 1;
				break;
			// Cookie: Unapproved Post
			case 'post_not_approved':
				title = 'Comment awaiting approval by a moderator';
				message += '<li id="dsq-msg-post-not-approved">Your comment must be approved by a moderator before appearing here.</li>';
				break;
			// Cookie: Profile Found
			case 'post_has_profile':
				numAlerts = numAlerts - 1;
				break;
			case 'user_created':
				var _data = v.split(':');
				title = 'Profile created!';
				message += '<li id="dsq-msg-user-created">You have just created a <span class="logo-disqus">Disqus</span> Profile, the best way to claim, manage, and track your comments all over the web. \
				<br /><br />A confirmation is being sent to <strong>' + _data[1] + '</strong>. Please check for this email in order to verify your profile. \
				<ul class="dsq-list-tick"> \
					<li>Your username is <strong>' + _data[0] +'</strong>. <a href="{{ disqus_url }}/people/' + _data[0] + '/" target="_blank">Click here to view your public profile</a>.</li> \
					<li>Be sure to set your profile picture, as well as connect your <span class="dsq-badge-facebook">Facebook</span> and <span class="dsq-badge-twitter">Twitter</span> accounts. <a href="{{ disqus_url }}/account/" target="_blank">Click here for account settings</a>.</li> \
				</ul> \
				</li>'
				break;
			default:
				break;
		}
		numAlerts++;
	});

	if(numAlerts > 1) {
		message = '<ul class="dsq-list-bluebullet">' + message;
		message += '</ul>';
		title = 'Thanks for posting!';
	}
	if(numAlerts > 0) {
		Dsq.Popup.popModal(message, title);
	}
}

function fbcLoginCallback() {
	LS.request_is_facebook = true;
	// Force display because LifeScript hides this block (which houses the iframe)
	Dsq.$('dsq-post-add').style.display = 'block';
	// Hide their options box
	Dsq.$('disqus-register-box').style.display = 'none';
	// Ensure the reply iframe loads
	LS.showReplyIframe();
	updateContentLayout(); // LifeScript function
}

function fbcLogoutCallback() {
	LS.request_is_facebook = false;
	Dsq.$('disqus-register-box').style.display = 'block';
	Dsq.$('dsq-post-add').style.display = 'none';
	updateContentLayout(); // LifeScript function
}

var LS = new function() {
	this.request_is_facebook = false;
	
	this.setLoginOption = function(el, postId) {
		var registerBox = el.parentNode.parentNode;
		var loginOptions = Dsq.Utils.getElementsByClassName(registerBox, 'div', 'option');
		
		for(var i = 0; i < loginOptions.length; i++) {
			loginOptions[i].className = loginOptions[i].className.replace('selected', '');
		}
		
		el.className += ' selected';
	};

	this.showLoginIframe = function(postId, showRegister) {
		if(postId) {
			var container = Dsq.$('dsq-reply-' + postId);
		} else {
			var container = Dsq.$('dsq-post-add');
		}
		
		Dsq.Iframes.showLoginIframeInContainer(container, postId);
		if(Dsq.Utils.ie) { Dsq.Utils.fixIframesIE(); }
		
		container.style.display = 'block';
	};
	
	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';
		
	};
	
	// Overloaded Dsq.Post.toggleReply()
	this.toggleReply = function(el, id) {
		if(!Dsq.Post.stateReplyToggled[id]) {
			// TOGGLE ON
			
			if(Dsq.$('dsq-auth-reply-' + id)) {
				Dsq.$('dsq-comment-reply-' + id).style.display = 'block';
			} else {
				var authReply, replyBar;
				
				authReply = document.createElement('div');
				authReply.id = 'dsq-auth-reply-' + id;
				authReply.style.backgroundColor = '#F2FAFE';
				authReply.style.padding = '10px 0';
				
				replyBar = document.createElement('div');
				replyBar.innerHTML = Dsq.$('dsq-reply-bar-' + id).innerHTML;
				Dsq.Utils.deleteNode(Dsq.$('dsq-reply-bar-' + id));
				
				// Destroy the default #dsq-reply-{id}
				Dsq.Utils.deleteNode(Dsq.$('dsq-reply-' +id));
				// Create the div where the iframe will be inserted into
				authReply.innerHTML = '<div id="dsq-reply-' + id + '"></div>';

				if(Dsq.jsonData.request.is_authenticated || LS.request_is_facebook) { Dsq.$('dsq-comment-reply-' + id).appendChild(replyBar); }
				Dsq.$('dsq-comment-reply-' + id).appendChild(authReply);
				Dsq.$('dsq-comment-reply-' + id).style.display = 'block';

				if(Dsq.jsonData.request.is_authenticated || LS.request_is_facebook) {
					LS.showReplyIframe(id);
					if(Dsq.Utils.ie) { Dsq.Utils.fixIframesIE(id); }
				} else {
					// Show the LifeScript auth template
					Dsq.$('dsq-auth-reply-' + id).innerHTML = LS.authTemplate(id) + authReply.innerHTML;
				}
			}
		} 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);

		Dsq.Events.fire(Dsq.Events.REPLY_IFRAME_TOGGLED, {
			postId: id,
			opened: Dsq.Post.stateReplyToggled[id]
		});
	};
	
	this.showReplies = function(el, id) {
		var display;
		var replies = Dsq.Utils.getElementsByClassName(Dsq.$('dsq-comments'), 'li', 'dsq-parent-is-' + id);
		
		if(el.className == 'dsq-child-replies-show') {
			el.innerHTML = '<img src="http://images.lifescript.com/images/icon/min.gif" alt="" />';
			display = 'block';
			el.className = 'dsq-child-replies-hide';
		} else {
			el.innerHTML = '<img src="http://images.lifescript.com/images/icon/plus.gif" alt="" />';
			display = 'none';
			el.className = 'dsq-child-replies-show';
		}
		for(var i = 0; i < replies.length; i++) {
			replies[i].style.display = display;
		}
	};
		
	this.onload = function(){		
		var commentsContainer = Dsq.$('dsq-comments');
		var paginationEl = Dsq.$('dsq-pagination');
		var paginationHTML = paginationEl ? paginationEl.innerHTML : '';
		var countEl = Dsq.$('dsq-total-posts');
		var newCountEl = Dsq.jsonData.request.is_initial_load ? document.createElement('h3') : Dsq.$('dsq-comments-count-new');

		if(!countEl) { return; }

		if(!Dsq.jsonData.request.is_initial_load) {
			// Paginating
			Dsq.Utils.deleteNode(newCountEl);
		}

		// Moving the comment count and pagination
		newCountEl.innerHTML = countEl.innerHTML;
		newCountEl.id = 'dsq-comments-count-new';
		newCountEl.className = 'dsq-h3-commentcount';
		newCountEl.style.margin = '20px 0';
		newCountEl.style.display = 'block';
		newCountEl.innerHTML = '<span id="dsq-pagination-new">' + paginationHTML + '</span>' + newCountEl.innerHTML + ' Comments';
		
		Dsq.$('dsq-content').insertBefore(newCountEl, commentsContainer.nextSibling);

		// Change the pagination markup				
		if(Dsq.$('dsq-paginate-next')) { Dsq.$('dsq-paginate-next').innerHTML = '... more comments'; }
 		if(Dsq.$('dsq-paginate-previous')) { Dsq.$('dsq-paginate-previous').innerHTML = '&#9666;'; }

		// iFrame styles
		var height = (Dsq.jsonData.request.is_authenticated ? '180px' : '310px');
		Dsq.Utils.addCssRule('.dsq-post-reply', 'height:' + height + ' !important', -1);
	};
	
	this.authTemplate = function(postId) {
		if(!postId) { postId = null; }
		
		return ' \
			<div class="disqus-register-box" id="disqus-register-box' + (postId ? '-' + postId : '') + '"> \
	            <div class="first-comment"> \
	                <div class="add-comment mb5"><span class="comment-title">Add a Comment</span></div> \
	                <div class="top-right' + (!postId ? ' question' : ' close') + '" onclick="' + (postId ? 'LS.toggleReply(Dsq.$(\'dsq-reply-link-' + postId + '\'),' +  postId + ')' : 'LoadLoginFAQ()') + '">' + (!postId ? 'I have questions' : '') + '</div> \
	                <div class="first-comment-notes"> \
	                    You can add a comment one of three ways:  Through Facebook, Disqus (Lifescript\'s comment engine) or as a Guest. \
					</div> \
				</div> \
				<div class="register-box"> \
		    		<div class="facebook option" onclick="LS.setLoginOption(this, ' + postId + ')"> \
		        		<span class="bold"><br />Facebook</span> \
		        		<div class="mt5"></div> \
		        		<span class="size8">Comment using your Facebook account.</span>'
						+ (Dsq.jsonData.context.use_fb_connect
							? (!postId 
								? '<div id="dsq-fbc-login" onlogin="DisqusFbcParentController.onLogin()" size="medium" background="light" length="short" style="display:inline; margin-right:7px"></div>'
								: '<a href="#" onclick="FB.Connect.requireSession(DisqusFbcParentController.onLogin); return false;"><img src="http://static.ak.fbcdn.net/images/fbconnect/login-buttons/connect_light_medium_short.gif" alt="" /></a>')
							: '')
						+ '</div> \
			    <div class="twitter option" onclick="LS.setLoginOption(this, ' + postId + ')"> \
			        <span class="bold"><br />Twitter</span> \
			        <div class="mt5"></div>	\
			        <span class="size8">Comment using your Twitter account.</span> \
					<a href="#" onclick="Dsq.Twitter.startTwitterConnect(); return false"> \
			               <img id="Img1" src="http://images.lifescript.com/images/icon/twitter.gif" alt="Connect"/> \
			        </a> \
			    </div> \
		    	<div class="disqus option" onclick="LS.setLoginOption(this, ' + postId + ')"> \
		        	<span class="bold"><br />Disqus</span> \
		        	<div class="mt5"></div> \
		        	<span class="size8">Comment using your Disqus account.</span> \
			        <a href="#" onclick="LS.showLoginIframe(' + postId + '); return false"> \
			            <img src="http://images.lifescript.com/images/icon/disqus2.gif"/> \
			        </a> \
		    	</div> \
		    	<div class="guest option" onclick="LS.setLoginOption(this, ' + postId + ')"> \
		        	<span class="bold"><br />Guests</span> \
		        	<div class="mt5"></div> \
		        	<span class="size8">Comment instantly.<br /> No Login Required.</span> \
		        	<a href="#" onclick="LS.showReplyIframe(' + postId + '); return false"> \
		            	<img src="http://images.lifescript.com/images/icon/guest2.gif"/> \
		        	</a> \
		    	</div> \
			</div> \
		</div> \
		';
	}
};

var DsqLocal = {
	'Filters': {
		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-list-style">'
				+ (_meta.can_reply
					? '<li class="dsq-list-first"><a href="#" id="dsq-reply-link-' + post_id +'" onclick="LS.toggleReply(this, ' + post_id +'); return false;">Reply</a></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>'
					: '')
				+ (_meta.votable
					? '<li class="dsq-rate" id="dsq-rate-cont-' + post_id + '">'
					+ (!_meta.up_voted
						? '<a href="#" onclick="Dsq.Post.rate(this, ' + post_id + ', 1); return false;">Like</a>'
						: 'You liked this.') + '</li>'
					: '')
				+ '<li class="dsq-report" id="dsq-post-report-' + post_id + '"><a href="#" class="dsq-post-report" onclick="Dsq.Post.report(' + post_id + ', false); return false;">Report</a></li>'
				+ '</ul>';
			
			html = html.replace(RE, new_html);
			// html = html.replace(RE2, '');
			return html;
		},
		popupProfile: function(html, post_id) {
			// Adding a new target attribute to the "View Full Profile" link
			var str = '<a class="dsq-profile-userurl"';
			var newStr = str + ' target="_blank"';

			html = html.replace(str, newStr);
			return html;
		}
	},
	
	'Templates': {
		postAppendHeader: function(post_id) {
			var _meta = Dsq.jsonData.posts[post_id];

			return ''
				+ (_meta.author_is_moderator
					? '<img class="dsq-mod-star" src="http://media.disqus.com/images/bullet-star.png" title="Moderator" alt="" />'
					: '')
				+ '<span class="dsq-header-meta"> \
					at ' + _meta.date + ' \
				</span>'
				+'<div id="dsq-points-' + post_id + '" class="dsq-likedtxt">' + (_meta.points ? '<img src="http://images.lifescript.com/images/icon/hand-a.gif" alt="" style="vertical-align:bottom"/>&nbsp;' : '') +'<span>'
				+ (_meta.points
					? _meta.points + Dsq.Utils.pluralize(_meta.points, ' person', ' people') + '</span> liked this comment.'
					: '')
				+ '</div>';
		},
		authPost: function() {
			var html = '<div id="dsq-auth">';
			
			html += (!Dsq.jsonData.request.is_authenticated
				? LS.authTemplate()
			: ' \
			<div id="dsq-authenticated" class="dsq-authenticated" style="display: block"> \
				<div class="dsq-authenticated-pic">'
				+ Dsq.Utils.interpolate('<a href="%(url)s" title="%(request_username)s">' +
															'<img class="dsq-post-avatar" src="%(avatar_url)s" alt="" /></a>',
															{avatar_url: Dsq.Urls.REQUEST_USER_AVATAR,
															url: (Dsq.jsonData.request.is_remote
																	 ? Dsq.jsonData.request.url
																	 : Dsq.jsonData.settings.disqus_url + Dsq.Urls.REQUEST_USER_PROFILE) })
				+ '</div> \
				<div class="dsq-authenticated-info"> \
					<ul> \
						<li>'
						+ (Dsq.jsonData.request.is_remote
							? Dsq.FmtStrings.LOGGED_IN_AS(
									Dsq.Utils.interpolate('<a href="%(url)s" title="%(request_username)s">%(request_username)s</a>', {url:Dsq.jsonData.request.url})
							  )
							: Dsq.FmtStrings.LOGGED_IN_AS(
									Dsq.Utils.interpolate('<a href="%(disqus_url)s%(profile_url)s" title="%(request_username)s">%(request_username)s</a>')
								)
						)
					+ '</li> \
					<li class="logout">'
					+ (!Dsq.jsonData.request.is_remote
						? Dsq.Utils.interpolate('<img class="dsq-login-icon" src="%(media_url)s/images/dsqicon12.png" alt="%(logged_in_as)s"/>&nbsp',
							{logged_in_as: Dsq.FmtStrings.LOGGED_IN_AS(Dsq.jsonData.request.username)})
						: '')
					+ 	(!Dsq.jsonData.request.is_remote
							? Dsq.Utils.interpolate('<a href="%(disqus_url)s%(logout_url)s?ctkn=%(csrf_token)s" title="%(logout_from_disqus)s">',
								{logout_url: Dsq.Urls.LOGOUT, csrf_token: Dsq.CSRF_TOKEN, logout_from_disqus: Dsq.FmtStrings.LOGOUT_FROM('DISQUS')})
							: Dsq.Utils.interpolate('using Twitter (<a href="%(disqus_url)s%(logout_url)s?ctkn=%(csrf_token)s" title="Logout">Logout</a>)',
								{logout_url: Dsq.Urls.LOGOUT, csrf_token: Dsq.CSRF_TOKEN})
						)
					+ (!Dsq.jsonData.request.is_remote ? Dsq.FmtStrings.LOGOUT_FROM('<span class="logo-disqus">DISQUS</span>') : '')
					+ '</a> \
					</li> \
				</ul> \
			</div> \
			</div> \
			');
			
			html += (Dsq.jsonData.context.use_fb_connect
				? '<div id="dsq-fbc-authenticated" class="dsq-authenticated"> \
				<div id="dsq-fbc-profilepic" class="dsq-authenticated-pic" uid="loggedinuser" type="FB.XFBML.ProfilePic" size="square" facebook-logo="true"></div> \
				<div class="dsq-authenticated-info"> \
					<ul> \
						<li> \
							Logged in as <span id="dsq-fbc-name" uid="loggedinuser" type="FB.XFBML.Name" linked="true" useyou="false"></span> \
						</li> \
						<li class="logout">using Facebook Connect <a href="#" onclick="javascript:DisqusFbcParentController.logout();return false;">(Logout)</a></li> \
					</ul> \
				</div> \
			</div>'
			: '');
			
			html += '</div>'; // dsq-auth
			
			html += ' \
				<div id="dsq-post-add"'
				+ (!Dsq.jsonData.request.is_authenticated
					? ' style="display: none"'
					: '')
				+ '></div>';
			
			return html;
		},
		
		voted: function(post_id, points, vote) {
			// Update number of points
			Dsq.$('dsq-points-' + post_id).innerHTML = '<img src="http://images.lifescript.com/images/icon/hand-a.gif" alt="" style="vertical-align:bottom"/>&nbsp;<span>' + points + Dsq.Utils.pluralize(points, ' person', ' people') + '</span> liked this comment.';

			// Update link text
			if(vote) {
				Dsq.$('dsq-rate-cont-' + post_id).innerHTML = 'You liked this.';
			}
			
			return post_id;
		}
	}
};
