/*

	Quote Comments JS
	
*/
	function addQuote(comment,quote){
		/*
		
			Derived from Alex King's JS Quicktags code (http://www.alexking.org/)
			Released under LGPL license
			
		*/
		
		// IE support
		if (document.selection) {
			comment.focus();
			sel = document.selection.createRange();
			sel.text = quote;
			comment.focus();
		}
		// Mozilla support
		else if (comment.selectionStart || comment.selectionStart == '0') {
			var startPos = comment.selectionStart;
			var endPos = comment.selectionEnd;
			var cursorPos = endPos;
			var scrollTop = comment.scrollTop;
			if (startPos != endPos) {
				comment.value = comment.value.substring(0, startPos)
							  + quote
							  + comment.value.substring(endPos, comment.value.length);
				cursorPos = startPos + quote.length
			}
			else {
				comment.value = comment.value.substring(0, startPos) 
								  + quote
								  + comment.value.substring(endPos, comment.value.length);
				cursorPos = startPos + quote.length;
			}
			comment.focus();
			comment.selectionStart = cursorPos;
			comment.selectionEnd = cursorPos;
			comment.scrollTop = scrollTop;
		}
		else {
			comment.value += quote;
		}
		
		// If Live Preview Plugin is installed, refresh preview
		try {
			ReloadTextDiv();
		}
		catch ( e ) {
		}	
	}
	function quote(postid, author, commentarea, commentID, textile) {
	
		// should quotes begin with "<author> said:"? If so, remove the next line:
		author = null;
	
		// begin code
		var posttext = '';
		if (window.getSelection){
			posttext = window.getSelection();
		}
		else if (document.getSelection){
			posttext = document.getSelection();
		}
		else if (document.selection){
			posttext = document.selection.createRange().text;
		}
		else {
			return true;
		}
		
		if (posttext=='') {		// quoting entire comment
		
	
			// quote entire comment as html
			var editcomments = "edit-comment"+commentID.split("div-comment-")[1];
			if (!document.getElementById(editcomments)) {
				var posttext = document.getElementById(commentID).innerHTML;
			} else {
				var posttext = document.getElementById(editcomments).innerHTML;
			}
			
			// remove nested divs
			var posttext = posttext.replace(/<div(.*?)>((.|\n)*?)(<\/div>)/ig, "");


			// do basic cleanups
			var posttext = posttext.replace(/	/g, "");
			var posttext = posttext.replace(/<p>/g, "\n");
			var posttext = posttext.replace(/<\/\s*p>/g, "");
			var posttext = posttext.replace(/<br>/g, "")
			
			// remove superfluous linebreaks
			var posttext = posttext.replace(/\s\s/gm, "");
	
			// remove nested spans
			var posttext = posttext.replace(/<span(.*?)>((.|\n)*?)(<\/span>)/ig, "");

			// remove nested blockquotes
			while (posttext != (posttext = posttext.replace(/<blockquote>[^>]*<\/\s*blockquote>/g, "")));

			// build quote
			if (author) {
				var quote='<strong><a href="#comment-'+postid+'">'+author+'</a></strong> said: \n\n<blockquote>\n'+posttext+'\n</blockquote>\n\n';
			} else {
				var quote='<blockquote cite="comment-'+postid+'">\n'+posttext+'\n</blockquote>\n\n';
			}
	
			var comment=document.getElementById(commentarea);
			addQuote(comment,quote);
			
		} else {	// quoting selection
			
			// quote selection a html or textile
			if (textile) {
				if (author) {
					var quote='*'+author+'* said: \n\nbq. '+posttext+'\n\n';
				} else {
					var quote='bq. '+posttext+'\n\n';
				}
			} else {
				if (author) {
					var quote='<strong>'+author+'</strong> said: \n\n<blockquote>\n'+posttext+'\n</blockquote>\n\n';
				} else {
					var quote='<blockquote cite="comment-'+postid+'">\n'+posttext+'\n</blockquote>\n\n';
				}
			}
			var comment=document.getElementById(commentarea);
			addQuote(comment,quote);
			
		}
		return false;
	}
