/**
 *
 * NOTE: The included JavaScript WILL ONLY WORK WITH MOOTOOLS.  It will not work if any other JavaScript
 * framework is present on the page.
 *
 * Current MooTools version: 1.2
 *
 * @author Phil Page <http://www.severninternet.co.uk>
 * @copyright 2009 Philip Page
 * @license LGPL 2.1 <http://creativecommons.org/licenses/LGPL/2.1/>
 */
var Comments = new Class({

	initialize: function(url) {
		
		$$('.comment').each(function(el) {
			el.id = el.getAttribute('id');
			el.textEl = el.getElement('.commenttext');
			el.quote = el.getElement('.quote-comment');
			el.removeComment = el.getElement('.remove-comment');
			el.quoteAuthor = el.getElement('.commentauthor');
			
			if ($chk(el.quote)) {
				el.quote.addEvent('click', function(e){
					e.stop();
					if ($chk($('comments'))) {
						var comments = new Comments();
					}
					var postID = $('comment-form').getElements('input[name=id]').getProperty('value');
					var container = new Element('div', {
					    'class': 'reply-form-container clearfix',
					    'html': '<form id="reply-form" action="/scripts/comments/comments.php" method="post"><dl><dt><label for="replyform-comment">Reply:</label></dt><dd><textarea id="replyform-comment" name="comment" cols="60" rows="10" class="mceNoEditor"></textarea></dd></dl><dl><dt>&nbsp;</dt><dd><input id="reply-form-submit" class="button" type="submit" name="button" value="submit reply" /><input type="hidden" name="comment_id" value="" /><input type="hidden" name="user_id" value="6" /><input type="hidden" name="id" value="" /></dd></dl></form>'					    
					});

					container.inject(el);
					$('reply-form').getElements('input[name=comment_id]').setProperty('value', el.id.replace('comment-', ''));
					$('reply-form').getElements('input[name=id]').setProperty('value', postID);		
					/*tinyMCE.init({
						mode : "textareas",
						theme : "simple",
						width : "300",
						height : "200"
					});
					
					var fixTiny = function(properties) {
						var properties = properties || new Object();
						var instance = properties.instance || 'mce_editor_0';
						tinyMCE.execInstanceCommand(instance,'mceCleanup');
						tinyMCE.triggerSave(true,true);
						return true;
					}*/

					$('reply-form').addEvent('submit', function(el){
						// Prevents the default submit event from loading a new page.
						el.stop();
						
						//fixTiny({instance:'comment'});
						
						// Show the spinning indicator when pressing the submit button...
						$('comments-log').addClass('ajax-loading');
						
						// Hide the submit button while processing...
						$('comment-form-submit').setStyle('display', 'none');
						
						el.updateText = function(text){
							error = text.split('ERROR:')[1];
							$('comment-form-submit').setStyle('display', 'block');
							$('comments-log').removeClass('ajax-loading');
							if (error) {
								el.showError(error);
								return false;
							}
							$('comments').set('html', text);
							setContentHeight();
							comments.initialize();
							if ($chk($('commentform-author'))) {
								$('commentform-author').set('value', '');
							}
							if ($chk($('commentform-author-email'))) {
								$('commentform-author-email').set('value', '');
							}
							$('commentform-comment').set('value', '');
						};
						
						el.showError = function(error){
							$('comments-log').addClass('commentError');
							oldTxt = $('comments-log').get('html');
							$('comments-log').set('html', error);
							(function(){
								$('comments-log').set('html', oldTxt);
								$('comments-log').removeClass('commentError');
							}).delay(5000);
						};
						// Set the options of the form's Request handler.
						// ("this" refers to the $('login') element).
						this.set('send', {
							onComplete: el.updateText								
						});
						
						// Send the form.
						this.send();
					});

				});
			}
			if ($chk(el.removeComment)) {
				el.removeComment.addEvent('click', function(e){
					e.stop();
					var request = new Request({
						url: '/scripts/comments/comments.php',
						link: 'chain',
						method: 'post',
						data: {
							action: 'delete',
							id: el.removeComment.getParent('li').get('id').replace('comment-', ''),
							ajax: 1
						},
						onRequest: function(){
							new Fx.Morph(el.removeComment.getParent('li'), {
								duration: 300
							}).start({
								'opacity': 0
							});
						},
						onSuccess: function(){
							new Fx.Slide(el.removeComment.getParent('li'), {
								duration: 300,
								onComplete: function(){
									el.removeComment.getParent('li').dispose();
								}
							}).slideOut();
						}
					}).send();
				});
			}
		}.bind(this));
		
	}

});
window.addEvent('domready', function() {
	if ($chk($('comments'))) {
		var comments = new Comments();
	}
	if ($chk($('comment-form'))) {
		
		/*var fixTiny = function(properties) {
			var properties = properties || new Object();
			var instance = properties.instance || 'mce_editor_0';
			tinyMCE.execInstanceCommand(instance,'mceCleanup');
			tinyMCE.triggerSave(true,true);
			return true;
		}*/
		
		$('comment-form').addEvent('submit', function(el){
			// Prevents the default submit event from loading a new page.
			el.stop();
			
			//fixTiny({instance:'comment'});
			
			// Show the spinning indicator when pressing the submit button...
			$('comments-log').addClass('ajax-loading');
			
			// Hide the submit button while processing...
			$('comment-form-submit').setStyle('display', 'none');
			
			el.updateText = function(text){
				error = text.split('ERROR:')[1];
				$('comment-form-submit').setStyle('display', 'block');
				$('comments-log').removeClass('ajax-loading');
				if (error) {
					el.showError(error);
					return false;
				}
				$('comments').set('html', text);
				setContentHeight();
				comments.initialize();
				if ($chk($('commentform-author'))) {
					$('commentform-author').set('value', '');
				}
				if ($chk($('commentform-author-email'))) {
					$('commentform-author-email').set('value', '');
				}
				$('commentform-comment').set('value', '');
			};
			
			el.showError = function(error){
				$('comments-log').addClass('commentError');
				oldTxt = $('comments-log').get('html');
				$('comments-log').set('html', error);
				(function(){
					$('comments-log').set('html', oldTxt);
					$('comments-log').removeClass('commentError');
				}).delay(5000);
			};
			// Set the options of the form's Request handler.
			// ("this" refers to the $('login') element).
			this.set('send', {
				onComplete: el.updateText
			});
			
			// Send the form.
			this.send();
		});
	}
});


