function debug(message){try{console.log(message);}catch(e){alert(message);}}
function sortText(a,b){return((a<b)?-1:((a>b)?1:0));}
function parse_param(value,key){var re,match;re=re=new RegExp(name+'-(\\S*)');match=value.match(re);return match?match[1]:null;}
function get_param($el,name,use_id){var attr=typeof(use_id)!=='undefined'?'id':'class';return parse_param($el.attr(attr),name);}
var Application={popup:null,init:function(){this.bindEvents();this.UI();},bindEvents:function(){if(window.location.href.match(/feeds/)){this.bindItems();this.bindLinks();}else{this.bindAuth();}
this.fixReformal();this.bindUnsubscribe();},createPopup:function(url,title){var popW,popH,topPos,leftPos;popW=500;popH=400;topPos=(screen.height-popH)/2;leftPos=(screen.width-popW)/2;Application.popup=window.open(url,title,"location=0,status=0,scrollbars=1,menubar=0,toolbar=0,width="+popW+",height="+popH+",top="+topPos+",left="+leftPos);},fixReformal:function(){var box=$('#myotziv_box');(box.length)&&(box.find('.i_prop').html('Я предлагаю вам...'));},bindUnsubscribe:function(){$('.unsubscribe-link').bind('click',function(e){var params=$(this).get(0).onclick()||{},value=params.value||'',form=$(this).closest('form');form.find('input[name="answer"]').val(value);form.submit();});},bindLinks:function(){$(".settings").colorbox({width:"500px",inline:true,href:"#settings-placeholder",scrolling:false,transition:"none"});$('#save-settings').bind('click',function(){$('#settings-placeholder form').trigger('submit');});$('#email, #password').bind('keypress',function(e){if((e.keyCode?e.keyCode:e.which)===13){$('#settings-placeholder form').trigger('submit');}});$('#settings-placeholder form').bind('submit',function(){$.post('/user/settings',{csrf_token:csrf_token,email_notification:$('#email-notification').attr('checked')===true?1:0,timezone:$('#timezone').val(),email:$('#email').val(),password:$('#password').val()},function(json){if(json.type==='success'){setTimeout(function(){$('.settings').colorbox.close();setTimeout(function(){location.reload();},500);},0);}else{Application.flash('#flash-messages',json.response,'error',undefined,'slide');}},'json');return false;});$('#email-notification').bind('change',function(){if($(this).attr('checked')){$('.email-placeholder .f-line').removeClass('disabled').find('input').each(function(){$(this).removeAttr('disabled');});}else{if(get_param($('.auth-type'),'type',true)!=='email'){$('.email-placeholder .f-line').addClass('disabled').find('input').each(function(){$(this).attr('disabled','disabled');});}}});$('#random-link').bind('click',function(){$('#url').val($(this).html());});$('.show-stats').bind('click',function(){var $link=$(this);var $container=$("#footer-stat");if(!$container.html()){$.ajax({type:'POST',url:'/dashboard',data:{csrf_token:csrf_token},dataType:'json',success:function(xhr){if(xhr.type==='success'){$container.hide().html(xhr.response).slideDown();$link.html('Скрыть статистику');}else{Application.flash('#flash-messages',xhr.response,'error',undefined,'slide');$link.html('Показать статистику');}},beforeSend:function(){$link.parents().find('.ajax-loader').css('visibility','visible');$link.html('Загружаем статистику');},complete:function(){$link.parents().find('.ajax-loader').css('visibility','hidden');}});}else{if($container.is(':visible')){$link.html('Показать статистику');}else{$link.html('Скрыть статистику');}
$container.slideToggle();}});},haveToAddFeed:function(){if($('.add-col').hasClass('loading')){return false;}
var url=$.trim($('#url').val());if(!url){Application.flash('#flash-messages',"Укажите адрес темы",'error',undefined,'slide');return false;}
var found=false;$('#feeds tr').filter(':gt(0)').each(function(i){if($(this).find('.title-col a').attr('href')===url&&!found){found=true;}});if(found){Application.flash('#flash-messages',"Вы уже подписаны на эту тему",'error',undefined,'slide');return false;}
if(!url.match(/^(http:\/\/)?rutracker\.org\/forum\/viewtopic\.php\?t=(\d+)/)){Application.flash('#flash-messages',"Некорректный адрес темы. Адрес темы должен выглядеть как http://rutracker.org/forum/viewtopic.php?t=NNNNN или rutracker.org/forum/viewtopic.php?t=NNNNN",'error',undefined,'slide');return false;}
return true;},bindAuth:function(){$('#auth-providers img').bind('click',function(){var index=$(this).parents('li').index();$('.auth-provider').removeClass('visible').eq(index).addClass('visible');$('#auth-providers img').css('opacity',0.2);$(this).css('opacity',1);$('#auth-response').html('').removeClass('error success');var type=$(this).attr('alt');if(type==='facebook'){Application.changeFormState('processing');Application.createPopup('/user/facebook','Facebook');}else if(type==='google'){Application.changeFormState('processing');Application.createPopup('/user/openid?url='+escape($('#google-url').val())+'&csrf_token='+escape(csrf_token));}else if(type==='twitter'){Application.changeFormState('processing');Application.createPopup('/user/twitter','Twitter');}});$('.signup-link').bind('click',function(){$('#mail-switch .f-line').hide();$('.expire-line').hide();$('#mail-switch .register-form').show();$('.email-line, .password-line').show();});$('.signin-link').bind('click',function(){$('#mail-switch .f-line').hide();$('#mail-switch .login-form').show();$('.email-line, .password-line, .expire-line').show();});$('.forgot-link').bind('click',function(){$('#mail-switch .f-line').hide();$('#mail-switch .forgot-form').show();$('.email-line').show();$('.password-line, .expire-line').hide();});$('#signup').bind('click',function(){Application.changeFormState('processing','Регистрируемся');Application.auth('register');});$('#signin').bind('click',function(){Application.changeFormState('processing','Входим');Application.auth('login');});$('#email, #password').bind('keypress',function(e){if((e.keyCode?e.keyCode:e.which)===13){Application.changeFormState('processing','Входим');Application.auth('login');}});$('#mail-form').bind('submit',function(){return false;});$('#forgot').bind('click',function(){Application.changeFormState('processing','Вспоминаем');$.post('/remind',{csrf_token:csrf_token,email:$('#email').val()},function(json){Application.changeFormState('default');$('.signin-link').trigger('click');Application.flash('#auth-response',json.response,'success');},'json');});$('#signin-openid').bind('click',function(){Application.createPopup('/user/openid?url='+escape($('#url').val())+'&expire='+($('#expire_openid').is(':checked')?'1':'0')+'&csrf_token='+escape(csrf_token));});$('#url').bind('keypress',function(e){if((e.keyCode?e.keyCode:e.which)===13){Application.changeFormState('processing');Application.createPopup('/user/openid?url='+escape($('#url').val())+'&expire='+($('#expire_openid').is(':checked')?'1':'0')+'&csrf_token='+escape(csrf_token));return false;}});},changeFormState:function(state,message){var $container=$('.auth-provider').filter('.visible');if(state==='processing'){if(typeof(message)!=='undefined'){$container.find('.process-state-message').html(message);}
$container.find('.process-state').fadeIn();}else{$container.find('.process-state').fadeOut();}},bindItems:function(){$('.add-col a').bind('click',function(){if($('.add-col').hasClass('loading')){return false;}
$(this).closest('form').submit();return false;});$('.suggest-feed').live('click',function(){var $link=$(this).parent();$('input[name=feed]').val($(this).parent().find('.external').attr('href'));$('#feed-add').unbind('complete').bind('complete',function(){if(!$link.parents().find('.w-33').length){setTimeout(function(){$link.fadeOut('slow',function(){$link.remove();Application.subscribesCountChanges();});},250);}else{Application.subscribesCountChanges();}});$('#feed-add').submit();return false;});$('#feed-add').submit(function(e){if(!Application.haveToAddFeed()){return false;}
var $form=$(this);$.ajax({type:'POST',url:$form.attr('action'),data:{csrf_token:csrf_token,feed:$('input[name=feed]',$form).val()},dataType:'json',success:function(response){if(response.type==='success'){var $response=$(response.response);var addTitle=$response.find('.title-col a').html();var index=0;$('#feeds tr').filter(':gt(0)').each(function(i){var rowTitle=$(this).find('.title-col a').html();if(sortText(addTitle,rowTitle)>=0){index=i+1;}});$response.hide().insertAfter('#feeds tr:eq('+index+')').fadeIn('slow');$form[0].reset();Application.feedsCountChange();}else{Application.flash('#flash-messages',response.response,'error',undefined,'slide');}},beforeSend:function(){$('.add-col').addClass('loading');},complete:function(){$('#feed-add').triggerHandler('complete');$('.add-col').removeClass('loading');}});return false;});$('.feed_delete_link').live('click',function(){var $link=$(this);if(!$link.parents('tr').hasClass('active')&&confirm("Удалить подписку?")){$.ajax({type:'POST',url:$link.attr('href'),data:{csrf_token:csrf_token},dataType:'json',success:function(response){if(response.type==='success'){$link.parents('tr').fadeOut('slow',function(){$(this).remove();Application.feedsCountChange();});}else{Application.flash('#flash-messages',response.response,'error',undefined,'slide');}},beforeSend:function(){$link.parents('tr').addClass('active');},complete:function(){$link.parents('tr').removeClass('active');}});}
return false;});$('.feed_new_link').live('click',function(){var $link=$(this);var $parentTr=$link.parents('tr');if(!$parentTr.hasClass('active')){$.ajax({type:'POST',url:$link.attr('href'),data:{csrf_token:csrf_token},dataType:'json',success:function(response){if(response.type==='success'){$link.toggleClass('active',response.data.is_new);}else{Application.flash('#flash-messages',response.response,'error',undefined,'slide');}},beforeSend:function(){$parentTr.addClass('active');},complete:function(){$parentTr.removeClass('active');}});}
return false;});},feedsCountChange:function(){var length=$('#feeds tr').length;if(length>1){$("#no-feeds").hide();$('#feeds-counter').show();$('#feed-count').html(length-1);}else if(length===1){$("#no-feeds").show();$('#feeds-counter').hide();}},subscribesCountChanges:function(){if(!$('#suggestions li').length){$.post('/feeds/suggest',{csrf_token:csrf_token},function(json){$('#suggestions ul').replaceWith($(json.response).filter('ul').html());},'json');}},auth:function(type){$.post('/user/'+type,{csrf_token:csrf_token,email:$('#email').val(),password:$('#password').val(),expire:$('#expire').attr('checked')===true?1:0},function(json){Application.changeFormState('default');if(json.type!=='error'){if(type==='login'){Application.successAuth('email');}else{$('.signin-link').trigger('click');Application.flash('#auth-response',json.response,'success');}}else{Application.errorAuth(json.response);}},'json');},successAuth:function(provider){if(Application.popup){Application.popup.close();Application.changeFormState('default');}
var message='Через пару секунд вы будете перенаправлены на внутреннюю страницу';Application.flash("#auth-response",message,'success');setTimeout(function(){window.location.replace("/feeds");},1500);},errorAuth:function(message){if(Application.popup){Application.popup.close();Application.changeFormState('default');}
message=(typeof(message)==="undefined")?'':message;Application.flash("#auth-response",message,'error');},UI:function(){$('#auth-providers img').filter(':gt(0)').css('opacity',0.2);var $fm=$('#flash-messages');var message=$.trim($fm.html());if(message){var type=get_param($fm,'type');Application.flash('#flash-messages',message,type,undefined,'slide');}
$('#email-notification').trigger('change');},flash:function(el,message,type,timeout,effect){timeout=(typeof(timeout)==='undefined')?10000:timeout;effect=(typeof(effect)==='undefined')?'fade':effect;message=$('<div class="message-container">'+message+'</div>').addClass(type).hide();$(el).html('').html(message).show();if(effect==='fade'){message.fadeIn('slow',function(){setTimeout(function(){message.fadeOut('slow');},timeout);});}else{message.slideDown('slow',function(){setTimeout(function(){$(el).slideUp('slow');},timeout);});}}};$(function(){Application.init();$('.showteam').bind('click',function(){$('.team').slideToggle();});});;(function(c){function r(b,d){d=d==="x"?m.width():m.height();return typeof b==="string"?Math.round(b.match(/%/)?d/100*parseInt(b,10):parseInt(b,10)):b}function M(b){b=c.isFunction(b)?b.call(i):b;return a.photo||b.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i)}function Y(){for(var b in a)if(c.isFunction(a[b])&&b.substring(0,2)!=="on")a[b]=a[b].call(i);a.rel=a.rel||i.rel;a.href=a.href||i.href;a.title=a.title||i.title}function Z(b){i=b;a=c(i).data(q);Y();if(a.rel&&a.rel!=="nofollow"){g=c(".cboxElement").filter(function(){return(c(this).data(q).rel||this.rel)===a.rel});j=g.index(i);if(j<0){g=g.add(i);j=g.length-1}}else{g=c(i);j=0}if(!B){C=B=n;N=i;N.blur();c(document).bind("keydown.cbox_close",function(d){if(d.keyCode===27){d.preventDefault();e.close()}}).bind("keydown.cbox_arrows",function(d){if(g.length>1)if(d.keyCode===37){d.preventDefault();D.click()}else if(d.keyCode===39){d.preventDefault();E.click()}});a.overlayClose&&s.css({cursor:"pointer"}).one("click",e.close);c.event.trigger(aa);a.onOpen&&a.onOpen.call(i);s.css({opacity:a.opacity}).show();a.w=r(a.initialWidth,"x");a.h=r(a.initialHeight,"y");e.position(0);O&&m.bind("resize.cboxie6 scroll.cboxie6",function(){s.css({width:m.width(),height:m.height(),top:m.scrollTop(),left:m.scrollLeft()})}).trigger("scroll.cboxie6")}P.add(D).add(E).add(t).add(Q).hide();R.html(a.close).show();e.slideshow();e.load()}var q="colorbox",F="hover",n=true,e,x=!c.support.opacity,O=x&&!window.XMLHttpRequest,aa="cbox_open",H="cbox_load",S="cbox_complete",T="resize.cbox_resize",s,k,u,p,U,V,W,X,g,m,l,I,J,K,Q,P,t,E,D,R,y,z,v,w,i,N,j,a,B,C,$={transition:"elastic",speed:350,width:false,height:false,innerWidth:false,innerHeight:false,initialWidth:"400",initialHeight:"400",maxWidth:false,maxHeight:false,scalePhotos:n,scrolling:n,inline:false,html:false,iframe:false,photo:false,href:false,title:false,rel:false,opacity:0.9,preloading:n,current:"image {current} of {total}",previous:"previous",next:"next",close:"close",open:false,overlayClose:n,slideshow:false,slideshowAuto:n,slideshowSpeed:2500,slideshowStart:"start slideshow",slideshowStop:"stop slideshow",onOpen:false,onLoad:false,onComplete:false,onCleanup:false,onClosed:false};e=c.fn.colorbox=function(b,d){var h=this;if(!h.length)if(h.selector===""){h=c("<a/>");b.open=n}else return this;h.each(function(){var f=c.extend({},c(this).data(q)?c(this).data(q):$,b);c(this).data(q,f).addClass("cboxElement");if(d)c(this).data(q).onComplete=d});b&&b.open&&Z(h);return this};e.init=function(){function b(d){return c('<div id="cbox'+d+'"/>')}m=c(window);k=c('<div id="colorbox"/>');s=b("Overlay").hide();u=b("Wrapper");p=b("Content").append(l=b("LoadedContent").css({width:0,height:0}),J=b("LoadingOverlay"),K=b("LoadingGraphic"),Q=b("Title"),P=b("Current"),t=b("Slideshow"),E=b("Next"),D=b("Previous"),R=b("Close"));u.append(c("<div/>").append(b("TopLeft"),U=b("TopCenter"),b("TopRight")),c("<div/>").append(V=b("MiddleLeft"),p,W=b("MiddleRight")),c("<div/>").append(b("BottomLeft"),X=b("BottomCenter"),b("BottomRight"))).children().children().css({"float":"left"});I=c("<div style='position:absolute; top:0; left:0; width:9999px; height:0;'/>");c("body").prepend(s,k.append(u,I));if(x){k.addClass("cboxIE");O&&s.css("position","absolute")}p.children().bind("mouseover mouseout",function(){c(this).toggleClass(F)}).addClass(F);y=U.height()+X.height()+p.outerHeight(n)-p.height();z=V.width()+W.width()+p.outerWidth(n)-p.width();v=l.outerHeight(n);w=l.outerWidth(n);k.css({"padding-bottom":y,"padding-right":z}).hide();E.click(e.next);D.click(e.prev);R.click(e.close);p.children().removeClass(F);c(".cboxElement").live("click",function(d){if(d.button!==0&&typeof d.button!=="undefined")return n;else{Z(this);return false}})};e.position=function(b,d){function h(A){U[0].style.width=X[0].style.width=p[0].style.width=A.style.width;K[0].style.height=J[0].style.height=p[0].style.height=V[0].style.height=W[0].style.height=A.style.height}var f=m.height();f=Math.max(f-a.h-v-y,0)/2+m.scrollTop();var o=Math.max(document.documentElement.clientWidth-a.w-w-z,0)/2+m.scrollLeft();b=k.width()===a.w+w&&k.height()===a.h+v?0:b;u[0].style.width=u[0].style.height="9999px";k.dequeue().animate({width:a.w+w,height:a.h+v,top:f,left:o},{duration:b,complete:function(){h(this);C=false;u[0].style.width=a.w+w+z+"px";u[0].style.height=a.h+v+y+"px";d&&d()},step:function(){h(this)}})};e.resize=function(b){function d(){a.w=a.w||l.width();a.w=a.mw&&a.mw<a.w?a.mw:a.w;return a.w}function h(){a.h=a.h||l.height();a.h=a.mh&&a.mh<a.h?a.mh:a.h;return a.h}function f(G){e.position(G,function(){if(B){if(x){A&&l.fadeIn(100);k[0].style.removeAttribute("filter")}if(a.iframe)l.append("<iframe id='cboxIframe'"+(a.scrolling?" ":"scrolling='no'")+" name='iframe_"+(new Date).getTime()+"' frameborder=0 src='"+a.href+"' "+(x?"allowtransparency='true'":"")+" />");l.show();Q.show().html(a.title);if(g.length>1){P.html(a.current.replace(/\{current\}/,j+1).replace(/\{total\}/,g.length)).show();E.html(a.next).show();D.html(a.previous).show();a.slideshow&&t.show()}J.hide();K.hide();c.event.trigger(S);a.onComplete&&a.onComplete.call(i);a.transition==="fade"&&k.fadeTo(L,1,function(){x&&k[0].style.removeAttribute("filter")});m.bind(T,function(){e.position(0)})}})}if(B){var o,A,L=a.transition==="none"?0:a.speed;m.unbind(T);if(b){l.remove();l=c('<div id="cboxLoadedContent"/>').html(b);l.hide().appendTo(I).css({width:d(),overflow:a.scrolling?"auto":"hidden"}).css({height:h()}).prependTo(p);c("#cboxPhoto").css({cssFloat:"none"});O&&c("select:not(#colorbox select)").filter(function(){return this.style.visibility!=="hidden"}).css({visibility:"hidden"}).one("cbox_cleanup",function(){this.style.visibility="inherit"});a.transition==="fade"&&k.fadeTo(L,0,function(){f(0)})||f(L);if(a.preloading&&g.length>1){b=j>0?g[j-1]:g[g.length-1];o=j<g.length-1?g[j+1]:g[0];o=c(o).data(q).href||o.href;b=c(b).data(q).href||b.href;M(o)&&c("<img />").attr("src",o);M(b)&&c("<img />").attr("src",b)}}else setTimeout(function(){var G=l.wrapInner("<div style='overflow:auto'></div>").children();a.h=G.height();l.css({height:a.h});G.replaceWith(G.children());e.position(L)},1)}};e.load=function(){var b,d,h,f=e.resize;C=n;i=g[j];a=c(i).data(q);Y();c.event.trigger(H);a.onLoad&&a.onLoad.call(i);a.h=a.height?r(a.height,"y")-v-y:a.innerHeight?r(a.innerHeight,"y"):false;a.w=a.width?r(a.width,"x")-w-z:a.innerWidth?r(a.innerWidth,"x"):false;a.mw=a.w;a.mh=a.h;if(a.maxWidth){a.mw=r(a.maxWidth,"x")-w-z;a.mw=a.w&&a.w<a.mw?a.w:a.mw}if(a.maxHeight){a.mh=r(a.maxHeight,"y")-v-y;a.mh=a.h&&a.h<a.mh?a.h:a.mh}b=a.href;J.show();K.show();if(a.inline){c('<div id="cboxInlineTemp" />').hide().insertBefore(c(b)[0]).bind(H+" cbox_cleanup",function(){c(this).replaceWith(l.children())});f(c(b))}else if(a.iframe)f(" ");else if(a.html)f(a.html);else if(M(b)){d=new Image;d.onload=function(){var o;d.onload=null;d.id="cboxPhoto";c(d).css({margin:"auto",border:"none",display:"block",cssFloat:"left"});if(a.scalePhotos){h=function(){d.height-=d.height*o;d.width-=d.width*o};if(a.mw&&d.width>a.mw){o=(d.width-a.mw)/d.width;h()}if(a.mh&&d.height>a.mh){o=(d.height-a.mh)/d.height;h()}}if(a.h)d.style.marginTop=Math.max(a.h-d.height,0)/2+"px";f(d);g.length>1&&c(d).css({cursor:"pointer"}).click(e.next);if(x)d.style.msInterpolationMode="bicubic"};d.src=b}else c("<div />").appendTo(I).load(b,function(o,A){A==="success"?f(this):f(c("<p>Request unsuccessful.</p>"))})};e.next=function(){if(!C){j=j<g.length-1?j+1:0;e.load()}};e.prev=function(){if(!C){j=j>0?j-1:g.length-1;e.load()}};e.slideshow=function(){function b(){t.text(a.slideshowStop).bind(S,function(){h=setTimeout(e.next,a.slideshowSpeed)}).bind(H,function(){clearTimeout(h)}).one("click",function(){d();c(this).removeClass(F)});k.removeClass(f+"off").addClass(f+"on")}var d,h,f="cboxSlideshow_";t.bind("cbox_closed",function(){t.unbind();clearTimeout(h);k.removeClass(f+"off "+f+"on")});d=function(){clearTimeout(h);t.text(a.slideshowStart).unbind(S+" "+H).one("click",function(){b();h=setTimeout(e.next,a.slideshowSpeed);c(this).removeClass(F)});k.removeClass(f+"on").addClass(f+"off")};if(a.slideshow&&g.length>1)a.slideshowAuto?b():d()};e.close=function(){c.event.trigger("cbox_cleanup");a.onCleanup&&a.onCleanup.call(i);B=false;c(document).unbind("keydown.cbox_close keydown.cbox_arrows");m.unbind(T+" resize.cboxie6 scroll.cboxie6");s.css({cursor:"auto"}).fadeOut("fast");k.stop(n,false).fadeOut("fast",function(){c("#colorbox iframe").attr("src","about:blank");l.remove();k.css({opacity:1});try{N.focus()}catch(b){}c.event.trigger("cbox_closed");a.onClosed&&a.onClosed.call(i)})};e.element=function(){return c(i)};e.settings=$;c(e.init)})(jQuery);
