var Articles = function(){};

Articles.options = { 
									   comments_limit: 1,
									   loader: $('<img src="/images/ajax-loader-darkgrey.gif" />'),
									   loader_grey: $('<img src="/images/ajax-loader-grey.gif" />')
									 };


Articles.ready = function(){	
	Articles.options.id = $('#article-comments').attr('rel');
	Articles.beforeLoadComments();
	Articles.loadComments();
	
	$('#add-comment-form').ajaxForm({
		url: IntLib.getRB() + 'IntModule/articles/addComment',
		type: 'post',
		dataType: 'json',
		beforeSubmit: function(){
			if(!$('#add-comments-accept-rules').is(':checked')){
				alert('Akceptacja regulaminu jest obowiązkowa');
				return false;
			}
			
			$('#add-comment-submit').attr('disabled', true);
			$('#add-comment-form-load').html(Articles.options.loader.clone());
			$('.add-comment-form-msg').hide();				
		},
		
		success: function(json){
			$('#add-comment-submit').attr('disabled', false);
			$('#add-comment-form-load').html('');
			$('.add-comment-form-msg').html(json.msg).slideDown('fast');
			
			if(json.reset_form){
				$('#add-comment-textarea').attr('value', '');
				Articles.cancelAnswer();
			}
			
			if(json.reload){
				Articles.loadComments(function(){
					Articles.scrollToComments('slow');
				});
				
			}
		},
		error: function(r){
			alert(r.responseText);
			$('#add-comment-submit').attr('disabled', false);
			$('#add-comment-form-load').html('');
			$('#add-comment-textarea').attr('value', '');
		}
	});
};

Articles.loadComments = function(callback){
	//Articles.beforeLoadComments();
	
	$.ajax({
		url: IntLib.getRB() + 'IntModule/articles/getComments',
		type: 'post',
		dataType: 'html',
		data: { limit: Articles.options.comments_limit, id: Articles.options.id },
		
		success: function(html){
			$('#article-comments').html(html);
			Articles.afterLoadComments();
			
			if(typeof callback == 'function'){
				callback();
			}
		}
	});
};

Articles.showAllComments = function(e){
	e.preventDefault();
	e.stopPropagation();
	//alert('jeje');
	
	Articles.options.comments_limit = Articles.options.comments_limit ? 0 : 1;
	Articles.loadComments(function(){	
		if(Articles.options.comments_limit == 1)
			$('.show-hide-comments').text('Zobacz wszystkie');
		else
			$('.show-hide-comments').text('Ukryj');
		
		$('#article-comments').scrollTo('slow');
	});
	
	return false;
};

Articles.scrollToComments = function(e){
	if(typeof e == 'obiect')
		e.preventDefault();

	var speed = (typeof e == 'number' || typeof e == 'string') ? e : 0;
	
	$('#article-comments').scrollTo(speed);
	
	return false;
};

Articles.beforeLoadComments = function(){
	$('#article-comments').addClass('article-commets-loading');	
	$('.scroll-to-comments').click(Articles.scrollToComments);
	$('.show-all-comments').click(Articles.showAllComments);	
	$('.add-comment-link').click(function(e){
		e.preventDefault();
		$('#add-comment-form').scrollTo(0);
		$('#add-comment-form textarea').focus();
		
		return false;
	})
};

Articles.afterLoadComments = function(){
	var $cont = $('#article-comments').removeClass('article-commets-loading');
	
	$cont.find('.comment-positive-vote').click(Articles.positiveVote);
	$cont.find('.comment-negative-vote').click(Articles.negativeVote);
	$cont.find('.comment-moderate').click(Articles.moderate);
	$cont.find('.comment-answer').click(Articles.answer);
	
	$all = $cont.find('.show-all-comments');
	$all.click(Articles.showAllComments);
	
	$cont.find('.scroll-to-comments').click(Articles.scrollToComments);
	
	if($all.is('a') && $('.scroll-to-comments').length > 0){
		$('.scroll-to-comments').unbind('click');
		$('.scroll-to-comments').removeClass('scroll-to-comments').addClass('show-all-comments');
		$('.show-all-comments').not($all).click(Articles.showAllComments);
	}
};

Articles.positiveVote = function(e){
	e.preventDefault();	
	Articles.vote('positive', this);
	
	return false;
};

Articles.negativeVote = function(e){
	e.preventDefault();
	Articles.vote('negative', this);
	
	return false;
};

Articles.vote = function(delta, ob){
	var id   = $(ob).attr('rel');
	var $cont = $(ob).parents('.aticle-comment:first');
	var $vote = $cont.find('.' + delta);
	var $msg  = $cont.find('.vote-msg');
	
	
	
	$msg.html(Articles.options.loader_grey.clone());
	
	$.ajax({
		url: IntLib.getRB() + 'IntModule/articles/vote/delta/' + delta,
		dataType: 'json',
		type: 'post',
		data: { id: id },
		
		success: function(json){
			if(json.error){
				$msg.html(json.error);
				return;
			}			
			var c = parseInt($vote.text());
			
			$msg.html(json.msg);
			$vote.text(++c);
		},
		error: function(r){
			$msg.html('');
		}
		
	});
}

Articles.moderate = function(e){
	e.preventDefault();	
	var id   = $(this).attr('rel');
	var $cont = $(this).parents('.aticle-comment:first');
	var $msg  = $cont.find('.vote-msg');
	
	$msg.html(Articles.options.loader_grey.clone());
	
	$.ajax({
		url: IntLib.getRB() + 'IntModule/articles/moderate',
		dataType: 'json',
		type: 'post',
		data: { id: id },
		
		success: function(json){
			$msg.html(json.msg);
		},
		error: function(r){
			alert(r.responseText);
			$msg.html('');
		}
		
	});
	
	return false;
};

Articles.cancelAnswer = function(e){
	if(e)
		e.preventDefault();
	$('.add-comment-label').html('Dodaj Komentarz:');
	$('#article-answer').attr('value', 0);
	return false;
}

Articles.answer = function(e){
	e.preventDefault();
	var $cur = $(this);
	var id = $cur.attr('rel');
	var $con = $cur.parents('.aticle-comment:first');
	var $cancel = $('<a href="#" title="Anuluj odpowiedź" class="answer-cancel">X</a>');
	var author = $con.find('.comment-author').text();
	
	$cancel.bind('click', Articles.cancelAnswer);
	
	$('#article-answer').attr('value', id);
	$('.add-comment-label').html('<small>Odpowiedz użytkownikowi: </small>' + author + ' ');
	$('.add-comment-label').append($cancel);
	
	$('#article-add-comment').scrollTo('slow');
	return false;
};

$(document).ready(Articles.ready);
