/**
* Comments
**/

var comment = (function(){
	var mod, mid, guest, page, type, formtype, gmod, gtype;
	var comform, combox, compage, comadd, comload, commsgadd, comsubscribe;
	var ajax = new Ajax(), ajaxlock = 0, act, act_id, module='comments';
	
	function init(){
		var comments = jWps('#comments')[0];
		mod = jWps('#commod')[0].value;
		mid = jWps('#commid')[0].value;
		guest = jWps('#comguest')[0].value;
		page = jWps('#compage')[0].value;
		type = jWps('#comtype')[0].value;
		formtype = jWps('#comformtype')[0].value;
		gmod = jWps('#comgmod')[0].value;
		gtype = jWps('#comgtype')[0].value;
		comform = jWps('#commentform');
		combox  = jWps('#commentbox');
		compage = jWps('#commentpages');
		comadd  = jWps('.commentadd',comments);
		comload = jWps('.commentload',comments);
		comtext = jWps('.commenttextadd',comments);
		commsgadd = jWps('.commentmsgadd',comments);
		comsubscribe = jWps('.commentsubscribe',comments);
		return;
	}
	
	ajax.setup({
		onStart: function(){
			ajaxlock = 1;
			if(act == 'add'){
				commsgadd.hide();
				comload.show();
			}
		},
		onComplete: function(data, text, query){
			if(data.error > 0){
				alert(data.error_msg);
			}else{
				var commentsload = false;
				if(act == 'add'){
					if(data.msg != ''){
						commsgadd.html(data.msg);
						commsgadd.show();
					}
					comload.hide();
					if(formtype != 1) comadd.hide();
					comtext[0].value = '';
					commentsload = true;
				}
				if(commentsload || act == 'page'){
					if(jWps.isArray(data.comments) && data.tpl_comment){
						var html = '', l = data.comments.length;
						for(var i=0; i<l; i++){
							var item = data.comments[i];
							html+= data.tpl_comment;
							html = jWps.replace('[#comid]',item['comid'],html);
							html = jWps.replace('[#userid]',item['userid'],html);
							html = jWps.replace('[#srcphoto]',item['srcphoto'],html);
							html = jWps.replace('[#name]',item['name'],html);
							html = jWps.replace('[#sex]',item['sex'],html);
							html = jWps.replace('[#time]',item['time'],html);
							html = jWps.replace('[#text]',item['text'],html);
							html = jWps.replace('[#action]',item['action'],html);
							html = jWps.replace('[#other]',item['other'],html);
						}
						combox.html(html);
					}
					if(data.pages){
						compage.html(jWps.htmlspecialchars_decode(data.pages));
					}
				}
				if(act == 'subscribe' && data.text){
					comsubscribe.html(data.text);
				}
				if(act == 'edit'){
					if(data.msg){
						jWps('#message'+query.id).html(data.msg);
						jWps('#message'+query.id).show();
						setTimeout(function(){
							var id = query.id;
							jWps('#message'+id).hide();
						},5000);
					}else if(data.tpl_edit){
						var html = data.tpl_edit, comment = jWps('#comment'+query.id)[0];
						html = jWps.replace('[#text]',data.edittext,html);
						jWps('.commenttext', comment).html(html);
						jWps('.commentaction', comment).html(jWps.htmlspecialchars_decode(data.action));
					}
					
				}
				if(act == 'save'){
					var comment = jWps('#comment'+act_id)[0];
					if(data.text) jWps('.commenttext', comment).html(data.text);
					else jWps('.commenttext', comment).html('');
					
					if(data.action) jWps('.commentaction', comment).html(jWps.htmlspecialchars_decode(data.action));
					else jWps('.commentaction', comment).html('');
				}
				if(act == 'del'){
					if(data.msg){
						jWps('#message'+data.id).html(data.msg);
						jWps('#message'+data.id).show();
						setTimeout(function(){
							var id = data.id;
							jWps('#message'+data.id).hide();
						},5000);
					}else if(data.tpl_comment){
						jWps('#comment'+data.id).html(data.tpl_comment);
					}
				}
				if(act == 'restore'){
					if(jWps.isArray(data.comments) && data.tpl_comment){
						var item = data.comments[0];
						var html = data.tpl_comment;
						html = jWps.replace('[#comid]',item['comid'],html);
						html = jWps.replace('[#userid]',item['userid'],html);
						html = jWps.replace('[#srcphoto]',item['srcphoto'],html);
						html = jWps.replace('[#name]',item['name'],html);
						html = jWps.replace('[#sex]',item['sex'],html);
						html = jWps.replace('[#time]',item['time'],html);
						html = jWps.replace('[#text]',item['text'],html);
						html = jWps.replace('[#action]',item['action'],html);
						html = jWps.replace('[#other]',item['other'],html);
						jWps('#comment'+item['comid']).html(html);
					}
				}
			}
			ajaxlock = 0;
		},
		onError: function(text){
			
		}
	});
	
	return{
		'show': function(){
			init();
			comadd.toggle();
		},
		'add': function(){
			init();
			if(ajaxlock == 1) return;
			
			act = 'add'
			var text = comtext[0].value;
			text = text.replace(/\+/g,'%2B',text);
			
			if(guest == '1'){
				ajax.post({'module':module,'mod':mod,'mid':mid,'act':act,'name':name,'email':email, 'text':text});
			}else{
				ajax.post({'module':module,'mod':mod,'mid':mid,'act':act,'text':text});
			}
		},
		'page': function(start){
			init();
			if(ajaxlock == 1) return;
			act = 'page';
			ajax.post({'module':module,'mod':mod,'mid':mid,'type':type,'act':act,'start':start,'gmod':gmod,'gtype':gtype});
		},
		'subscribe': function(){
			init();
			if(ajaxlock == 1) return;
			act = 'subscribe';
			ajax.post({'module':module,'mod':mod,'mid':mid,'act':act});
		},
		'edit': function(id){
			init();
			if(ajaxlock == 1) return;
			act = 'edit';
			act_id = id;
			ajax.post({'module':module,'mod':mod,'mid':mid,'type':type,'act':act,'id':id});
		},
		'save': function(id){
			init();
			if(ajaxlock == 1) return;
			act = 'save';
			act_id = id;
			var text = jWps('#comment'+id).find('.edittext')[0].value;
			ajax.post({'module':module,'mod':mod,'mid':mid,'type':type,'act':act,'id':id,'text':text});
		},
		'cancel': function(id){
			init();
			if(ajaxlock == 1) return;
			act = 'save';
			act_id = id;
			ajax.post({'module':module,'mod':mod,'mid':mid,'type':type,'act':act,'id':id});
		},
		'del': function(id){
			init();
			act = 'del';
			act_id = id;
			ajax.post({'module':module,'mod':mod,'mid':mid,'type':type,'act':act,'id':id});
		},
		'restore': function(id){
			init();
			act = 'restore';
			act_id = id;
			ajax.post({'module':module,'mod':mod,'mid':mid,'type':type,'act':act,'id':id});
		},
		'wysiwyg': function(){
			init();
			var popup_box = new PopupBox(uiLang.comment_wysiwyg, '<textarea id="comment_wysiwyg" style="width:600px; height:300px;"></textarea>', 
				{Send: {title:uiLang.send, func: function(){ comtext[0].value = window[editorParams.editorName].htmlToWiki(); comment.add(); popup_box.hide(); }},
				Cancel: {title:uiLang.cancel}}, 
			620);
			popup_box.show();
			
			try{
				var comment_wysiwyg = document.getElementById('comment_wysiwyg');
				comment_wysiwyg.value = comtext[0].value;
				var editorParams = {
					editorName: 'comment_wysiwyg',
					replaceElem: comment_wysiwyg,
					height: 300,
					simpleToolBar: 'bold,italic,underline,strike,sub,sup,left,center,right,justify,marker_list,numeric_list,h1,h2,h3,link,unlink,image,video,color1,color2,color3,smile,wiki',
					extendedToolBar: 'bold,italic,underline,strike,sub,sup,left,center,right,marker_list,numeric_list,h1,h2,h3,link,unlink,image,wiki',
					defaultMode: 'simple',
					debug: false
				};
				window.editorName = editorParams.editorName;
				window[editorParams.editorName] = new Wysiwyg(editorParams);
			}catch(e){};
		}
	}
})();
