var uid;var doReload;var dPath='/jsr_default/EN';var edPath='/jsr_wysiwygeditor/EN';var msgInt=90000;var myInt;var myTInt;$(function(){if($.cookie('showUserMenu')=='no'&&$('#tUserMenu').hasClass('tUserMenu'))$('#tUserMenu').css('display','none');$('#tUserName').click(function(){$('#tUserMenu').slideToggle('fast');if($.cookie('showUserMenu')=='no')$.cookie('showUserMenu','yes');else $.cookie('showUserMenu','no');});setChkMsgTime=function(){if(uid){if($.cookie('lcheck')){var d=new Date();var diff=d.getTime()-$.cookie('lcheck');if(diff>msgInt){myInt=setTimeout(checkMsg,2000);}else{myInt=setTimeout(checkMsg,msgInt-diff);}}else{myInt=setTimeout(checkMsg,2000);}}}
setChkMsgTime();function checkMsg()
{var d2=new Date();$.cookie('lcheck',d2.getTime());$.ajax({url:dPath+'/checkmsg',type:'POST',cache:false,timeout:2000,dataType:"json",success:function(res){if(res[0]<1)return;$('#mailHolder').html(res[1]);$('#mailHolder').animate({opacity:'show'},'slow');setTimeout(function(){$('#mailHolder').animate({opacity:'hide'},'slow');},10000);}});myInt=setTimeout(checkMsg,msgInt);}
loginMenu=function(res,msgObj){if(res[0]>0)return $('#'+msgObj).text(res[1]).animate({opacity:'show'},'slow');uid=res[3];if(msgObj=='msg'){$('#uName').html(res[2]);$('#tUseLogin').slideToggle('fast');$('#tUser').animate({opacity:'show'},'slow');$('#tUserMenu').html(res[1]).slideToggle('slow');$('#pwd').val('');setChkMsgTime();}}
doUserLogin=function(l,p,msgObj,btnObj,ldrObj){if(l==""){return $('#'+msgObj).html("Enter your Login").animate({opacity:'show'},'slow');}if(p==""){return $('#'+msgObj).text("Enter password").animate({opacity:'show'},'slow');}if(l.length<4){return $('#'+msgObj).text("Login can not consist of less than four symbols").animate({opacity:'show'},'slow');}if(p.length<4){return $('#'+msgObj).text("Password can not consist of  than four symbols").animate({opacity:'show'},'slow');}
$.ajax({url:dPath+'/dologin',type:'POST',cache:true,timeout:2000,dataType:"json",data:'l='+l+'&p='+p,beforeSend:function(){$('#'+btnObj).attr('disabled','disabled');$('#'+ldrObj).css('display','inline');},error:function(){$('#'+msgObj).animate({opacity:'show'},'slow');$('#'+msgObj).text("Account is not active");},success:function(res){loginMenu(res,msgObj);$.cookie('showUserMenu','yes');if(res[0]==0&&doReload)doReload();}});$('#'+ldrObj).animate({opacity:'hide'},'slow');if($('#'+btnObj).length>0)$('#'+btnObj).attr('disabled','');}
$('#ln_btn').click(function(){var l=jQuery.trim($('#ln').val());var p=jQuery.trim($('#pwd').val());doUserLogin(l,p,'msg','ln_btn','lldr');});$('#msg').click(function(){$('#msg').animate({opacity:'hide'},'slow');});$('#ln').click(function(){$('#msg').animate({opacity:'hide'},'slow');});$('#pwd').click(function(){$('#msg').animate({opacity:'hide'},'slow');});changeRand=function(){$('#randImg').fadeOut(200);$.ajax({url:dPath+'/rands',type:'POST',cache:false,dataType:'html',timeout:2000,success:function(res){$('#randImg').html(res);}});$('#randImg').fadeIn(200);}
$('#tExit').click(function(){$.ajax({url:dPath+'/logout',type:'POST',cache:false,timeout:2000,dataType:'html',beforeSend:function(){$('#lldr2').css('display','inline');},success:function(res){$('#tUser').animate({opacity:'hide'},'slow');$('#tUserMenu').animate({opacity:'hide'},'fast');$('#tUseLogin').slideToggle('fast');if(doReload)doReload();uid=false;clearTimeout(myInt);}});$('#lldr2').animate({opacity:'hide'},'slow');});addMask=function(){var mask=$('<div></div>');mask.css({'width':$(document).width(),'height':$(document).height()});mask.addClass('mask');$('body').append(mask);$('.mask').show();}
delMask=function(){$('.mask').remove();$('#mailForm').animate({opacity:'hide'},'fast').removeClass('tBox');$('#mailForm').unbind("click",delMask);$("#msgSaveBtn").unbind("click",saveMsg);}
setMailFormPos=function(){$('#mailForm').css({left:($(window).width()-$('#mailForm').width())/2});$('#mailForm').css({top:($('body').scrollTop()+($(window).height()-$('#mailForm').height())/2)});}
function FCKeditor_OnComplete(editorInstance){var doc=editorInstance.EditorDocument.body;addEvent(doc,'click',onClickCapture);}
function addEvent(obj,evType,fn,useCapture){if(obj.addEventListener){obj.addEventListener(evType,fn,useCapture);return true;}else if(obj.attachEvent){var r=obj.attachEvent("on"+evType,fn);return r;}else{return false;}}
$('.umsg').click(function(){var id2=$(this).attr('id2')>0?$(this).attr('id2'):"";var ln=$("#us"+id2).length>0?$('#us'+id2).text():"";callMsgForm(id2,ln,'um',false);});callMsgForm=function(id2,ln,mtype,callBack){$.ajax({url:edPath,type:'POST',cache:false,timeout:2000,dataType:'json',data:'m='+mtype+'&ln='+ln+'&id2='+id2,beforeSend:function(){$('#mailForm').addClass('tBox');$('#mailForm').html('<span class="ldr2"></span>').css('display','inline');setMailFormPos();},error:function(){alert("Message saving error!");},success:function(res){addMask();$('#mailForm').hide().html(res[1]).animate({opacity:'show'},'fast');setMailFormPos();$('#mailForm').animate({opacity:'show'},'slow');if(res[0]>0){$('#mailForm').bind('click',delMask);return setTimeout(function(){delMask();},3000);}
$('#headline').focus();$('#clbtn').bind('click',delMask);$('#capReload').bind('click',changeRand);$("#msgSaveBtn").bind("click",function(){saveMsg(callBack);});}});}
saveMsgErr=function(msg,mode,callBack){$('#msgErr').html(msg).animate({opacity:'show'},'slow');clearTimeout(myTInt);myTInt=setTimeout(function(){$('#msgErr').animate({opacity:'hide'},'slow'),(mode==0?delMask():false),(callBack==false?null:callBack());},2000);}
function saveMsg(addParam)
{var headline=$('#headline').val();var msg;var c=$('#msg_code').length>0?$('#msg_code').val():0;if($.browser.opera==true&&$.browser.version<9.63){msg=jQuery.trim($('#editor').val());}else{msg=jQuery.trim(FCKeditorAPI.GetInstance('editor').GetXHTML());if($.browser.msie==true)msg=msg.replace(/&#160;/gi,'');}
if(headline.length<3){return saveMsgErr("Your message\'s headline missed!",1,false);}
if(msg.length<4){return saveMsgErr("Your message empty or too short!",1,false);}
if(!uid&&c.length<4){return saveMsgErr("Incorrect or empty validation code!",1,false);}
$.ajax({url:edPath+'/savemsg',type:'POST',cache:false,timeout:2000,dataType:'json',data:'m='+$('#mto').val()+'&idto='+$('#idto').val()+'&headline='+headline+'&msg='+msg+'&code='+c,beforeSend:function(){$('#msgErr').html('<span class="ldr2"></span>');$('#msgSaveBtn').attr('disabled','disabled');},error:function(){$('#msgSaveBtn').attr('disabled','');return saveMsgErr("Message saving error!",1,false);},success:function(res){if(res[0]!=0)$('#msgSaveBtn').attr('disabled','');saveMsgErr(res[1],res[0],addParam);}});}
function checkKey(e){if(e.keyCode==27){delMask();clearTimeout(myTInt);}}
if($.browser.mozilla){$(document).keypress(checkKey)}
else{$(document).keydown(checkKey)}});
