/* 
	Port Comment API JS
	Last Update: 11/13/2009
	Author: Ryan Davis <rdavis@qctimes.com>
*/

j = jQuery.noConflict();

j(document).ready(function()
{
	// vars used for bulk request
	var bulkIDs = '';
	var articleCount = 0;
	
	// get article id's to send to bulk viewer
	j('a.blox-comment').each(function(){ 
		articleCount++;
		bulkIDs += j(this).attr('id')+','; 
	});
	
	// if there are any send request
	if(articleCount > 0){
		j.get("/app/port/bulkCommentCount.php", { getIDs:bulkIDs }, 
			function(result){ 
				// let jquery fill in counts from response
				eval(result); 
			}
		);
	}
	
	// comment block
	var commentsDiv = 'loadComments';

	// if comments block present
	if(j('#'+commentsDiv).length > 0){
		var leeComUser = j.cookie('SSID');
		var leeComAdmin = j.cookie('isAdminUser');
		
		// get comment params
		var articleID = j('#comment-settings #comment-doc-id').attr('title');
		var pageID = j('#comment-settings #comment-page-num').attr('title');
		var cPermalink = j('#comment-settings #comment-permalink').attr('title');
		var cTitle = j('#comment-settings #comment-title').attr('title');
		var appSkin = j('#comment-settings #app-skin').attr('title');
		
		// load comments per article id
		j.get("/app/port/articleComments.php", { docID:articleID, page:pageID, app:appSkin, thisPage:cPermalink }, 
			function(result){ 
				// load results
				j('#'+commentsDiv).html(result); 
				// attach functions
				submitComment();
				commentEditor();
				openLoginBox();
				quoteComment();
				// attach facebox events
				j('a[rel*=facebox]').facebox();
				if(leeComUser){ 
					var isAbuseLink = document.location.toString();
					if(isAbuseLink .match('#')){ 
						var isAbuseLink = isAbuseLink.split('#')[1];
						j('#'+isAbuseLink).parent().addClass('report-this-comment');
						window.location.hash = isAbuseLink;
					}
					j('.login-control').show(); 
				} else { 
					j('.not-logged-in').show(); 
				}
				if(leeComAdmin){ j('.admin-control').show(); }
			}
		);
	}

	// submit comment
	function submitComment()
	{
		j('#addComment').submit(function(){
			// the comment
			var aComment = j('#commentText').val();
			
			// stop and alert if no comment set
			if(!aComment){
				formResponse('comment-posted-error','Please Enter a Comment!');
	
			} else {
				// try and prevent multiple posts
				j('#addCommentButton').attr('disabled','disabled');
				j('#addCommentButton').attr('value','Please Wait...');
				
				// send post
				j.post("/app/port/postActions.php", { action:'addComment',commentText:aComment,docID:articleID,itemPermalink:cPermalink,itemTitle:cTitle }, 
					function(result){ 
						if(result==1){
							// posted message
							formResponse('comment-posted-ok','Comment Posted!');
							// open list if first post
							if(j('#'+commentsDiv+' li:first').length==0){
								j('#comment-count').html('You posted the first comment!');
								j('#'+commentsDiv+' #comment-count').after('<ol><li style="display: none;"></li></ol>'); 
							}
							// add comment to list
							j('#'+commentsDiv+' li:first').before('<li class="featured moz-border"><dl><dt></dt><dd>'+aComment+'</dd></dl>');
							// clear textarea
							j('#commentText').val('');
						// error occured
						} else {
							if(leeComAdmin){ alert(result); }
							formResponse('comment-posted-error','An Error Occured!');
						}
						// enable submit button again and remove comment in textarea
						j('#addCommentButton').removeAttr('disabled','disabled');
						j('#addCommentButton').attr('value','Submit Comment');
					}
				);
			}
			return false;
		});
	}
	
	// form messages
	function formResponse(div,message)
	{
		// attach message
		j('#comment-form-response').html('<div id="'+div+'" class="moz-border"><h3>'+message+'</h3></div>');
		// fade out and clear message
		j('#'+div).animate({ opacity: 0}, 2000,function(){ j('#'+div).hide();} );
		j('#comment-form-response').html();
	}
	
	// open comment editor
	function commentEditor()
	{
		j('a.open-comment-editor').click(function(){
			j('.comment-admin').hide();
			var editorID = j(this).attr('title'); 
			j('.admin-id-'+editorID).slideDown('fast');
			return false;
		});
	}
	
	// close open editors
	if(j('a.open-comment-editor')){
		j(document).click(function(){
			j('.comment-admin').hide();
		});
	}
	
	function openLoginBox()
	{
		j('#'+commentsDiv+ ' .openLoginBox').click(function(){
			j.facebox(function(){
				j.get('/app/port/loginForm.php?',function(loginForm){
					j.facebox(loginForm);
				});
			});
			return false;
		});
	}
	
	function quoteComment()
	{
		j('.quote-comment').click(function(){
			j('#commentText').val(null);
			
			var qID = j(this).attr('id').replace('quote-','');
			var qText = j('#'+qID).find('dd.comment-text').html().replace(/<(\w+)[^>]*>((?:(?!<\/?\1\b[^>]*>)[\S\s])*)<\/\1>/g, '').replace('<br>','');
			var qUser = j('#'+qID).find('dd.comment-user a').html();

			j('#commentText').val('[quote]'+ qUser + ' said: "' + qText + '"[/quote]\n\n');
			j('#commentText').focus();

			return false;
		});
	}
});