(function($){
$.fn.Blog = function(method){
var $this = this;
var settings = {
'reply_form': $('div.reply-form', this),
'markitup': {
nameSpace: 'full',
onTab: {keepDefault:false, replaceWith:' '},
markupSet: [
{name:'Заколовок 3', key:'3', openWith:'
', closeWith:'
\n', placeHolder:'Впишите сюда заголовок...', className: 'myH3Button'},
{name:'Заколовок 4', key:'4', openWith:'', closeWith:'
\n', placeHolder:'Впишите сюда заголовок...', className: 'myH4Button' },
{name:'Заколовок 5', key:'5', openWith:'', closeWith:'
\n', placeHolder:'Впишите сюда заголовок...', className: 'myH5Button' },
{name:'Заколовок 6', key:'6', openWith:'', closeWith:'
\n', placeHolder:'Впишите сюда заголовок...', className: 'myH6Button' },
{separator:'---------------' },
{name:'Жирный', key:'B', openWith:'', closeWith:'', className: 'myBButton'},
{name:'Наклонный', key:'I', openWith:'', closeWith:'', className: 'myIButton'},
{name:'Подчёркнутый', key:'U', openWith:'', closeWith:'', className: 'myUButton'},
{name:'Зачёркнутый', key:'S', openWith:'', closeWith:'', className: 'mySButton'},
{name:'Верхний индекс', openWith:'', closeWith:'', className: 'mySupButton'},
{name:'Нижний индекс', openWith:'', closeWith:'', className: 'mySubButton'},
{separator:'---------------'},
{name:'Маркированный список', openWith:'\n', className: 'myUlButton' },
{name:'Нумерованный список', openWith:'\n', closeWith:'
\n', className: 'myOlButton' },
{name:'Элемент списка', openWith:'', closeWith:'', className: 'myLiButton' },
{separator:'---------------' },
{name:'Кат', openWith:'\n', closeWith:'\n', placeHolder:'Читать дальше...', className: 'myCutButton' },
{separator:'---------------'},
{name:'Изображение', key:'P', replaceWith:'\n\n', className: 'myPictureButton'},
{name:'Ссылка', key:'L', openWith:'', closeWith:'', placeHolder:'Текст ссылки...', className: 'myLinkButton'}
]
},
'preview_popup': $('#popup-preview').Popup()
}
var methods = {
init: function(options){
if(options){$.extend(settings, options);}
try{
methods.initEditForm();
}catch(e){}
var controls = $('.post-info',$this);
$('.delete',controls).live('click',function(){
methods.deletePost($(this));
return false;
});
$('.can-vote',controls).live('click',function(){
methods.vote($(this));
return false;
});
$('.category-info > .category-title h2 > span').live('click',function(){
methods.blogInfo($(this));
return false;
});
$('.category-info > .category-title h2 a').live('click',function(){
methods.blogSubscribe($(this));
return false;
});
},
initEditForm: function(){
var form = $('div.editor form',$this);
if(form.length){
var textarea = $('textarea',form).markItUpRemove().markItUp(settings.markitup);
$('input[name="preview"]',form).click(function(){
methods.preview(textarea.val());
return false;
});
form.submit(function(){
if(methods.checkForm(form)){
$('input[type=submit]',form).disabled();
return false;
} else {
return false;
}
return false;
});
}
},
checkForm: function(form){
var check_required = $('label > b',form).parent().parent().find('select,input,textarea').not(':not([name=model]):disabled').checkRequired(function(input){
return (input.val() == '')
});
var check_format = true;
// var check_format = $('textarea',form).checkFormat(function(input){
// return (input.is('.check-cut') && input.val() != '' && !(/()/.test(input.val())))
// }, {
// 'check-cut': 'Пожалуйста вставьте тег '
// });
return (check_required && check_format);
},
deletePost: function(el){
var action = $(el).attr('href');
var post = $(el).parent().parent().parent();
if(confirm('Вы действительно хотите удалить эту запись?')){
// $.ajax({
// url: action,
// success: function(data){
// post.slideUp();
// }
// });
}
},
blogInfo: function(el){
el.parent().parent().next().slideToggle('slow');
},
blogSubscribe: function(el){
var action = el.attr('href');
$.ajax({
url: action,
success: function(data){
el.parent().find('a').fadeToggle('slow');
}
});
},
preview: function(html){
settings.preview_popup.Popup('show',{title: 'Предпросмотр', content: 'Пожалуйста, подождите'});
$.ajax({
type: 'post',
url: '/blogs/preview',
data: {text: html},
dataType: 'html',
error: function(){
settings.preview_popup.Popup('show',{title: 'Предпросмотр', content: 'Ой! Кажется что-то сломалось.'});
},
success: function(data){
settings.preview_popup.Popup('show',{title: 'Предпросмотр', content: data});
}
});
},
vote: function(el){
var votes_count = $(el).parent().find('.vote-count');
var action = $(el).attr('href');
$.ajax({
url: action,
success: function(data){
var vote = parseFloat(votes_count.text());
if(action.search(/down\/?$/) != -1){
vote--;
} else if(action.search(/up\/?$/) != -1){
vote++;
}
var sign = 'sign';
if(vote == 0){
sign += '0';
} else if(vote > 0){
sign += '1';
} else if(vote < 0){
sign += '-1';
}
votes_count.attr('class', 'vote-count ' + sign).text((vote > 0 ? '+' : '') + vote);
$('.can-vote',el.parent()).replaceWith(function(){
return '' + $(this).html().replace('icons\/vote_','icons/vote_cn_') + '';
});
}
});
}
};
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Метод ' + method + ' не существует в jQuery.Blog' );
}
return $this;
}
})(jQuery);
$(document).ready(function(){
try{$("div.blog").Blog();}catch(e){}
});