var Comments = (function(){
	var ajax = new Ajax();
	var 
		mod  = '',
		mid  = 0,
		offset = 0;
	
	var 
		comments_box = null,
		comments_form = null,
		comments_more = null,
		comments_text = null;
		comments_load = null;
	
	var comment_edit_cache = null;
	
	var c_wrap = null;
	function init(e){
		while(e && e != e.parentNode){
			if(e.className == 'comments_wrap'){
				c_wrap = e;
				break;
			}
			e = e.parentNode;
		}
		if(c_wrap){
			var c_data = c_wrap.getElementsByTagName('INPUT')[0].value;
			if(c_data != ''){
				c_data = c_data.split('|');
				mod  = c_data[0];
				mid  = c_data[1];
				offset = c_data[2];
			}
			
			comments_form = jWps('.comments_form', c_wrap)[0];
			comments_more = jWps('.comments_more', c_wrap);
			comments_text = jWps('.comment_add_text', c_wrap)[0];
			comments_load = jWps('.commentload', c_wrap);
			comments_box = jWps('.comments', c_wrap);
		}
		return;
	}
	
	function changeData(){
		var c_data = c_wrap.getElementsByTagName('INPUT')[0];
		c_data.value = mod + '|' + mid + '|' + offset;
		return;
	}
	
	function closeTextarea(){
		jWps(".comments_add_action", comments_form).hide();
		jWps(".comment_add_text", comments_form).removeClass('open');
	}
return{
	open: function(e){
		init(e);
		
		if(!jWps(e).hasClass("open")){
			e.className += " open";
			jWps(".comments_add_action", comments_form).show();
			
			jWps('body').bind('mousedown', function(e){
				var outside = true, t = e.target;

				while(t && t != t.parentNode){
					if(t == comments_form){
						outside = false;
						break;
					}
					t = t.parentNode;
				}
				if(outside){
					closeTextarea();
				}
			});
		}
	},
	send: function(e){
		init(e);
		
		var text = comments_text.value; 
		var placeholder = comments_text.getAttribute('placeholder');
		
		if(text != '' && text != placeholder){
			comments_load.show();
			ajax.post({module:'comments', mod:mod, mid:mid, act:'send', text:text}, function(data){
				comments_load.hide();
				if(data.error > 0) alert(data.error_msg);
				
				if(jWps.isArray(data.comments) && data.tpl_comment){
					var html = '', l = data.comments.length;
					for(var i=0; i<l; i++){
						html+= jWps.tpl_compare(data.tpl_comment, data.comments[i]);
					}
					comments_box.html(html);
				}
				
				comments_text.value = '';
				closeTextarea();
			});
		}
	},
	edit: function(e, id, mode){ // mode = edit|save|cancel
		init(e);
		
		if(mode == 'cancel'){
			if(comment_edit_cache) jWps('#comment' + id).html(comment_edit_cache);
		}
		else if(mode == 'save'){
			var text = jWps('#comment'+ id).find('.edittext')[0].value;
			
			if(text != ''){
				ajax.post({module:'comments', mod:mod, mid:mid, act:'save', 'text':text, id:id}, function(data){
					var comment = jWps('#comment'+ id)[0];
					if(data.text) jWps('.commenttext', comment).html(data.text);
					else jWps('.commenttext', comment).html('');
					
					if(data.action) jWps('.commentaction', comment).html(data.action);
					else jWps('.commentaction', comment).html('');
				});
			}
		}
		else{
			comment_edit_cache = jWps('#comment' + id)[0].innerHTML;
			
			ajax.post({module:'comments', mod:mod, mid:mid, act:'edit', id:id}, function(data){
				if(data.message){
					jWps('#message' + id).html(data.message);
					jWps('#message' + id).show();
					setTimeout(function(){ jWps('#message' + id).hide(); }, 5000);
				}else if(data.tpl_edit){
					var html = data.tpl_edit, comment = jWps('#comment' + id)[0];
					html = jWps.replace('[#text]', data.edittext, html);
					jWps('.commenttext', comment).html(html);
					jWps('.commentaction', comment).html(data.action);
				}
			});
		}
	},
	restore: function(e, id){
		init(e);
		ajax.post({module:'comments', mod:mod, mid:mid, act:'restore', id:id}, function(data){
			if(jWps.isArray(data.comments) && data.tpl_comment){
				var	html = jWps.tpl_compare(data.tpl_comment, data.comments[0]);
				jWps('#comment' + id).html(html);
			}
		});
	},
	remove: function(e, id){
		init(e);
		ajax.post({module:'comments', mod:mod, mid:mid, act:'remove', id:id}, function(data){
			if(data.message){
				jWps('#message' + id).html(data.message);
				jWps('#message' + id).show();
				setTimeout(function(){ jWps('#message' + id).hide(); },5000);
			}else if(data.tpl_comment){
				jWps('#comment' + id).html(data.tpl_comment);
			}
		});
	},
	more: function(e){
		init(e);
		ajax.post({module:'comments', mod:mod, mid:mid, act:'more', offset:offset}, function(data){
			if(data.offset){
				offset = data.offset;
				changeData();
			}
			if(data.more == 'hide'){ comments_more.hide(); }
			
			if(jWps.isArray(data.comments) && data.tpl_comment){
				var html = '', div = null, l = data.comments.length;
				for(var i=0; i<l; i++){
					div = document.createElement('DIV');
					div.innerHTML = jWps.tpl_compare(data.tpl_comment, data.comments[i]);
					comments_box.append(div.firstChild);
				}
				div = null;
			}
		});
	},
	wysiwyg: function(){
		
	}
}})();
