/*
	dc-interactive.net
*/

var Comments = Class.create();

Comments.prototype = {
	
	initialize: function(id) {
		this.id = id;
		this.options = Object.extend({
			locked: false,
			effect: false,
			isChild: true,
			module: 1,
			item: false,
			rating: 0
		}, arguments[1] || {});
		
		if(this.options.isChild) {
			this.element = $('comment-'+id);
			this.parEle = $('r-h-'+id);
			
			if(this.options.rating < 0) {
				this.element.hide();
				$('show-comment-'+id).show();
			}
		}
		
		if(this.options.locked) {
			this.lockKarma();
		}
	},
	
	lockKarma: function() {
		$('karma-tu-'+this.id).addClassName('inactive');
		$('karma-td-'+this.id).addClassName('inactive');
	},
	
	loadComments: function(page, forceload) {
		
		if(page == this.page && !forceload)
			return;
		
		this.page = page;
		this.msgHolder = $('commentHolder');
		this.msgHolder.startWaiting('bigWaiting');
		new Ajax.Request("/ajax/comment.php", {method: 'post', parameters: 'action=getReplies&isChild=0&page='+page+'&module_id='+this.options.module+'&item_id='+this.options.item, 
			onSuccess: function(r)
			{
				if(this.pageCount > 2) {
					if($('commentNavigation')) {
						for(i = 1; i < this.pageCount; i++) {
							if(!$('comment_li_'+i)) {
								var navHolder = $('commentNavigation');
								
								var li = document.createElement('li');
								li.setAttribute('id','comment_li_'+i);
								
								var a = document.createElement('a');
								a.setAttribute('href','#');
								Event.observe(a, 'click', function() {
									co_Main.loadComments(parseInt(i)-1);
									return false;
								});
								a.onclick = function () {
									return false;
								}
								
								var span = document.createElement('span');
								span.innerHTML = i;
								
								a.appendChild(span);
								li.appendChild(a);
								navHolder.appendChild(li);
							}
							
							if(i == this.page)
								$('comment_li_'+i).addClassName('currentpage');
							else
								$('comment_li_'+i).removeClassName('currentpage');
						}
					}
				}
				
				this.msgHolder.stopWaiting();
				this.msgHolder.update(r.responseText);
				
				if(forceload) {
					var latestComment;
					if(!this.msgHolder.descendants().first().siblings().last())
						latestComment = this.msgHolder.descendants().first();
					else
						latestComment = this.msgHolder.descendants().first().siblings().last()
					
					latestComment.scrollTo();
					new Effect.Highlight(latestComment, {endcolor:'#F8F7E7'});
				}
				else
					this.msgHolder.scrollTo();
				//this.msgHolder.scrollTo();
				/*if(this.options.effect)
					effect('blind', this.msgHolder);
				else
					this.msgHolder.toggle();*/
			}.bind(this)
			});
	},
	
	setPagingCount: function(count)
	{
		this.pageCount = count;
		this.page = 1;
	},
	
	show: function() {
		this.element.toggle();
		$('show-comment-'+this.id).hide();
		new Effect.Highlight(this.element);
	},
	
	showReplies: function() {
		if(!$('comment-child-'+this.id)) {
			var elementChild = document.createElement("div");
			elementChild.setAttribute('id','comment-child-'+this.id);
			elementChild.style.display = 'none';
			this.element.appendChild(elementChild);
		}
		
		this.elementChild = $('comment-child-'+this.id);
		
		if(this.elementChild.innerHTML == '') {
			this.parEle.startWaiting('nobg');
			new Ajax.Request("/ajax/comment.php", {method: 'post', parameters: 'action=getReplies&isChild=1&parent_id='+this.id, 
				onSuccess: function(r)
				{
					this.parEle.stopWaiting();
					this.elementChild.addClassName('commentsreplies');
					this.elementChild.update(r.responseText);
					
					if(!this.element.visible()) {
						if(this.options.effect)
							effect('blind', this.element);
						else
							this.element.show();
					}
					
					if(this.options.effect)
						effect('blind', this.elementChild);
					else
						this.elementChild.toggle();
				}.bind(this)
				});
		}
		else {
			if(!this.element.visible()) {
				if(this.options.effect)
					effect('blind', this.element);
				else
					this.element.show();
			}
			
			if(this.options.effect)
				effect('blind', this.elementChild);
			else
				this.elementChild.toggle();
		}
	},
	
	postReply: function() {
		if(!$('reply-h-'+this.id)) {
			var formElement = $('commentForm');
			var replyDiv = document.createElement('div');
			replyDiv.setAttribute('id', 'reply-h-'+this.id);
			replyDiv.style.display = 'none';
			this.parEle.appendChild(replyDiv);
			replyDiv.innerHTML = formElement.innerHTML;
			replyDiv = $('reply-h-'+this.id);
			replyDiv.addClassName('mtop');
			replyDiv.descendants().first().remove();
		}
		else
			var replyDiv = $('reply-h-'+this.id);
		
		if(replyDiv.visible())
			replyDiv.hide();
		else {
			replyDiv.descendants().each(function (ele) {
				ele = $(ele);
				if(ele.tagName.toString().toLowerCase() == 'form') {
					ele.setAttribute('id', 'addCommentForm_'+this.id);
					ele.parent_id.value = this.id;
					var txtArea = $(ele.comments);
					var txtAreaParent = txtArea.ancestors();
					var txtAreaParent2 = txtAreaParent[3];
					/*txtAreaParent.each(function (ell) {
						alert('Tag is '+ell.tagName+' and id is '+ell.id);
					});*/
					txtArea.setAttribute('id', 'comments_textarea_'+this.id);
					txtArea.value = '';
					txtArea.setStyle({'width': parseInt(txtAreaParent2.getWidth()-22)+'px'});
				}
				if(ele.id == 'comments_textarea_prebutton') {
					ele.setAttribute('id', 'comments_textarea_'+this.id+'_prebutton');
				}
				if(ele.id == 'comments_textarea_preview') {
					ele.setAttribute('id', 'comments_textarea_'+this.id+'_preview');
					ele.hide();
					ele.update('');
				}
				if(ele.tagName.toString().toLowerCase() == 'ul' && ele.id == 'comments_textarea_toolbar') {
					ele.descendants().each(function(lis) {
						lis.remove();
					});
					ele.setAttribute('id', 'comments_textarea_toolbar_'+this.id);
				}
				if(ele.tagName.toString().toLowerCase() == 'script') {
					$(ele).remove();
					commentWYSISWYG = new Control.TextArea('comments_textarea_'+this.id);
					commentWYSISWYGToolBar = new Control.TextArea.ToolBar(commentWYSISWYG, 'comments_textarea_toolbar_'+this.id);
					commentWYSISWYGToolBar.setUp();
				}
			}.bind(this));
			replyDiv.show();
		}
	},
	
	thumbs: function(direction) {
		if(!isLoggedIn()) {
			askLogin();
		}
		
		if(this.options.locked)
			return;
		
		if(isLoggedIn()) {
			$('karma-'+this.id).startWaiting();
			new Ajax.Request("/ajax/comment.php", {method: 'post', parameters: 'action=thumbs&direction='+direction+'&id='+this.id, 
				onSuccess: function(r) {
					$('karma-'+this.id).stopWaiting();
					//alert(r.responseText)
					if(r.responseText == 'l')
						askLogin();
					else if(r.responseText == 'e')
						alert(E_INVALID_REQUEST);
					else if(r.responseText == 'v')
						alert('You have already rated this comment');
					else
					{
						this.lockKarma();
						this.options.locked = true;
						alert('Your rating for this comment have been added.');
						if(r.responseText > 0)	
						{
							$('karma-rt-'+this.id).addClassName('blue');
							r.responseText = '+'+r.responseText;
						}
						else
							$('karma-rt-'+this.id).addClassName('red');
						
						$('karma-rt-'+this.id).update(r.responseText);
					}
				}.bind(this)
				});
		}
		else
			askLogin();
	}
	
}

function submitComment(f, c) {
	f = $(f);
	f.startWaiting('bigWaiting');
	f.request({
		onSuccess: function (r) {
			f.stopWaiting();
			response = r.responseText;
			if(response == 'length') {
				markInputField(f.comments, 'Your comment cannot be bigger than 500 characters and shorter than 2 characters')
			} else if(response == 'w') {
				alert('You cannot post more than 1 comment in a minute');
			} else if(response == 'd') {
				alert('You have already posted this comment, please make sure its not a duplicate');
			} else if (response == 'c') {
				markInputField(f.captcha, E_INVALID_CAPTCHA)
				reloadCaptcha($('captcha_relaod_comments'), c);
			} else {
				try {
					$('nocomments_msg').remove();
				} catch(e) {}
				
				f.comments.value = '';
				$('comments_textarea_preview').value = '';
				$('comments_textarea_preview').hide();
				
				f.captcha.value = '';
				reloadCaptcha($('captcha_relaod_comments'), c);
				
				//co_Main.page = response;
				co_Main.pageCount = parseInt(response) + 1;
				co_Main.loadComments(response, true);
				//alert(response);
			}
		}
	});
	return false;
}