﻿$(document).ready(function(){oMain.comments.init();});oMain.comments={sForm:'',xmlComments:'',htmlComments:'',formLoaded:false,formShowing:false,type_id:2,obj_id:0,parent_id:0,editor:'',init:function(){$('#add_comment').click(function(event){event.preventDefault();if(oMain.ctrl.logined){if(!oMain.comments.formLoaded){oMain.comments.loadForm(0);}else{oMain.comments.showForm(0);}}else{$('<p>необходимо авторизироваться!</p>').prependTo('.comment_msg').fadeOut(2000);}});$('a.answer').live('click',function(event){var iComment=$(this).attr('href');event.preventDefault();if(oMain.ctrl.logined){if(!oMain.comments.formLoaded){oMain.comments.loadForm(iComment);}else{oMain.comments.showForm(iComment);}}else{$('<p>необходимо авторизироваться!</p>').prependTo('.comment_msg').fadeOut(2000);}});$('a.show_all_comments').click(function(event){event.preventDefault();oMain.comments.showAll();});$('a.answers').live('click',function(event){event.preventDefault();oMain.comments.showAnswers($(this).attr('href'));$(this).remove();});$('a.thanks').live('click',function(event){event.preventDefault();if(oMain.ctrl.logined){oMain.comments.doThanks($(this).attr('href'));$(this).remove();}else{$('<p>необходимо авторизироваться!</p>').prependTo('.comment_msg').fadeOut(2000);}});if($('.cont_header').hasClass('place')){oMain.comments.type_id=1;}oMain.comments.obj_id=$('.cont_header span').attr('class');$('a.claim').live('click',oMain.comments.doClaim);$('#editor .submit').live('click',oMain.comments.post);},showForm:function(parent_id){oMain.comments.parent_id=parent_id;if(oMain.comments.formLoaded){if(oMain.comments.formShowing){oMain.comments.destroyForm();}if(parent_id>0){$('#comments dd[id='+parent_id+'] .c_cont:first').after('<div id="editor">'+oMain.comments.sForm+'</div>');}else{$('.comment_msg').after('<div id="editor">'+oMain.comments.sForm+'</div>');}oMain.comments.editorInit();}oMain.comments.formShowing=true;},destroyForm:function(){oMain.comments.editor.remove();$('#editor').remove();oMain.comments.formShowing=false;},showAnswers:function(parent){oMain.comments.parent_id=parent;$.ajax({type:"GET",url:"/ajax/comments.html",data:{mode:'list',type_id:oMain.comments.type_id,obj_id:oMain.comments.obj_id,parent_id:parent},dataType:"html",success:function(sResult,textStatus){if(sResult!=='error'){$("#comments dd[id='"+oMain.comments.parent_id+"'] dl:first").append(sResult);}else{$('<p>возникла ошибка, попробуйте еще раз!</p>').prependTo('.comment_msg').fadeOut(2000);}},error:function(XMLHttpRequest,textStatus,errorThrown){$('<p>возникла ошибка, попробуйте еще раз!<br />'+textStatus,errorThrown+'</p>').prependTo('.comment_msg').fadeOut(2000);}});},showAll:function(){$.ajax({type:"GET",url:"/ajax/comments.html",data:{mode:'showAll',type_id:oMain.comments.type_id,obj_id:oMain.comments.obj_id},dataType:"html",success:function(sResult,textStatus){if(sResult!=='error'){$('.comments dl:first').html(sResult);$('a.show_all_comments').remove();}else{$('<p>возникла ошибка, попробуйте еще раз!</p>').prependTo('.comment_msg').fadeOut(2000);}},error:function(XMLHttpRequest,textStatus,errorThrown){$('<p>возникла ошибка, попробуйте еще раз!<br />'+textStatus,errorThrown+'</p>').prependTo('.comment_msg').fadeOut(2000);}});},loadForm:function(parent_id){oMain.comments.parent_id=parent_id;$.ajax({type:"GET",url:"/ajax/comments.html",data:{mode:'form',type_id:oMain.comments.type_id,obj_id:oMain.comments.obj_id,parent_id:oMain.comments.parent_id},dataType:"html",success:function(sResult,textStatus){if(sResult!=='error'){oMain.comments.sForm=sResult;oMain.comments.formLoaded=true;oMain.comments.showForm(parent_id);}else{$('<p>возникла ошибка, попробуйте еще раз!</p>').prependTo('.comment_msg').fadeOut(2000);}},error:function(XMLHttpRequest,textStatus,errorThrown){$('<p>возникла ошибка, попробуйте еще раз!<br />'+textStatus,errorThrown+'</p>').prependTo('.comment_msg').fadeOut(2000);}});},doThanks:function(comment_id){$.ajax({type:"POST",url:"/ajax/comments.html",data:{mode:'thanks',comment_id:comment_id,type_id:oMain.comments.type_id,obj_id:oMain.comments.obj_id},dataType:"text",success:function(sResult,textStatus){if(sResult==="ok"){$('<p>Ваш голос учтен.</p>').prependTo('.comment_msg').fadeOut(2000);}else{$('<p>возникла ошибка, попробуйте еще раз!</p>').prependTo('.comment_msg').fadeOut(2000);}},error:function(XMLHttpRequest,textStatus,errorThrown){$('<p>возникла ошибка, попробуйте еще раз!<br />'+textStatus,errorThrown,XMLHttpRequest.status+'</p>').prependTo('.comment_msg').fadeOut(2000);}});},doClaim:function(event){event.preventDefault();var oClaim=$(this),id=oClaim.attr('href');$.ajax({type:"POST",url:"/ajax/comments.html",data:{mode:'claim',id:id},dataType:"text",success:function(sResult,textStatus){if(sResult==="ok"){oClaim.parent().append('<p>Ваша жалоба принята.</p>');oClaim.remove();}else{$('<p>возникла ошибка, попробуйте еще раз!</p>').prependTo('.comment_msg').fadeOut(2000);}},error:function(XMLHttpRequest,textStatus,errorThrown){$('<p>возникла ошибка, попробуйте еще раз!<br />'+textStatus,errorThrown,XMLHttpRequest.status+'</p>').prependTo('.comment_msg').fadeOut(2000);}});},post:function(event){event.preventDefault();var fields=$("#editor > form").serializeArray();fields.push({name:event.target.name,value:event.target.value});oMain.comments.editor=tinyMCE.get('comment');var sComment=oMain.comments.editor.getContent();$.each(fields,function(i,f){if(f.name==='comment'){f.value=sComment;}});$.ajax({type:"POST",url:"/ajax/comments.html",data:fields,dataType:"text",success:function(sResult,textStatus){if(sResult!=="error"){if(sResult==="mail"){$('<p>Ваш комментарий отправлен по почте.</p>').appendTo('.comment_msg').fadeOut(2000,function(){oMain.comments.destroyForm();oMain.comments.formLoaded=false;});}else{var iComment_id=sResult;oMain.comments.addPost(oMain.comments.parent_id,iComment_id,oMain.ctrl.authname,sComment);oMain.comments.destroyForm();oMain.comments.formLoaded=false;}}else{$('<p>возникла ошибка, попробуйте еще раз!</p>').appendTo('.comment_msg').fadeOut(2000);}},error:function(XMLHttpRequest,textStatus,errorThrown){$('<p>возникла ошибка, попробуйте еще раз!<br />'+XMLHttpRequest.status+'</p>').prependTo('.comment_msg').fadeOut(2000);}});},addPost:function(iParent_id,iComment_id,sUsername,sComment){var conteiner=$('.comments dl:first');if(iParent_id>0){conteiner=$("#comments dd[id='"+iParent_id+"'] .c_cont:first");}var dt=new Date();var cont=$('<dd id="'+iComment_id+'"></dd>');$('<div class="c_cont"></div>').appendTo(cont).append('<p><span class="c_data">'+dt.getDate()+'.'+(dt.getMonth()+1)+'.'+dt.getFullYear()+'</span>'+'<span class="c_username"><a href="/user/'+oMain.ctrl.user_id+'/">'+oMain.ctrl.authname+'</a></span></p>').append('<div class="c_comment"><span>'+sComment+'</span><div class="c_ctrl"><a href="'+iComment_id+'" class="answer">Ответить</a><a href="'+iComment_id+'" class="claim">Пожаловаться</a></div>'+'</div>');if(iParent_id>0){$('<dl></dl>').append(cont).insertAfter(conteiner);}else{cont.prependTo(conteiner);}},getQuote:function(){if(oMain.comments.parent_id>0){var jCom=$('#comments dd[id='+oMain.comments.parent_id+']'),username=$('span.c_username a',jCom).text(),comment=$('div.c_comment span',jCom).html();return'<p><blockquote><strong>'+username+'</strong><br />'+comment+'<br /></blockquote></p><p></p>';}else{return'';}},editorInit:function(){tinyMCE.init({content_css:"/css/color.css",language:"ru",mode:"exact",elements:"comment",theme:"advanced",plugins:"spellchecker,paste",paste_auto_cleanup_on_paste:true,spellchecker_languages:"+Русский=ru,English=en",spellchecker_rpc_url:"/tiny_mce/plugins/spellchecker/rpc.php",theme_advanced_buttons1:"bold,italic,undo,redo,removeformat,cleanup,spellchecker",theme_advanced_buttons2:"",theme_advanced_buttons3:"",theme_advanced_toolbar_location:"bottom",theme_advanced_toolbar_align:"left",skin:"o2k7",skin_variant:"silver",force_br_newlines:true,forced_root_block:'',valid_elements:"strong/b,em/i,strike,u,br,blockquote",setup:function(ed){ed.onInit.add(function(ed){oMain.comments.editor=ed;ed.focus();ed.setContent(oMain.comments.getQuote());});}});}};
