
var commentForm = '';

var opt = {
	dataType		: 'json',
	success			: showResponse,
	beforeSubmit	: showLoading
};

var options = {
	rules : {
		nick		: {
			required 	: true,
			maxlength	: 128
		},
		comment		: {
			required 	: true
		},
		captcha_input		: "required"
	},
	errorPlacement: function(error, element) {
		error.appendTo( element.parent().prev() );
	},
	submitHandler: function(form) {
		$(form).ajaxSubmit(opt);
	}
};

$(document).ready(function(){
	$("a[class^='comment-news-']").click(showCommentWindow);
//	$("a[class^='news-more-']").click(showNews);		// przeładowujemy stronę
	$("a[class^='show-comment-']").click(showComment);
	$("a[class^='hide-comment-']").click(hideComment);
	$("a[class^='hide-comment-']").hide();
	var img = new Image(32,32);
	img.src = 'images/loader32.gif';
	var hash = window.location.href.slice(window.location.href.indexOf('#')+1);
	if (hash == 'comment') 
		showCommentWindow(true);

	$("form[id^='comment-']").validate(options);
	getCaptcha();
});

function addCommentForm(id, hash) {
	if (hash)
		commentForm = '<a name="comment"></a>';
	commentForm += '<form id="comment-' + id +'" style="display:none;" action="/news/add-comment-ajax" method="post" class="comment-form"><div class="row"><div class="text">Twój nick / imię i nazwisko</div><div><input type="text" name="nick" size="30"/></div><div class="clear"></div></div><div class="row"><div class="text">Komentarz</div><div><textarea name="comment" cols="43" rows="5"></textarea></div><div class="clear"></div></div><div class="row"><div>&nbsp;</div><div><img src="images/loader16.gif" width="16" height="16" alt="" class="captcha_loader" style="margin: 5px;"/></div><div class="clear"></div></div><div class="row"><div>Wpisz powyższy wyraz:</div><div><input type="text" name="captcha_input" class="captcha" /></div><input type="hidden" value="{$captchaId}" name="captcha_id" /><div class="clear"></div></div><div><input type="hidden" name="news_id" value="' + id + '"/><input type="submit" value="dodaj komentarz" style="float: right;" class="submit"/><img src="images/loader16.gif" width="16" height="16" alt="" class="loader" style="float: right;"/><div class="clear"></div></div></form>';
}

function showCommentWindow() {
	var show = false;
	
	if (arguments[0] == true)
		show = true;
	var id;
	var current;
	if (show) { 
		id = $("a[class^='comment-news-']:first").attr('class').substring(13);
		current = $("a.comment-news-"+id);
	}
	else {
		id = $(this).attr('class').substring(13);
		current = $(this);
	}
	addCommentForm(id, true);
	current.parent().after(commentForm);
	$(".loader").hide();
	$("#comment-" + id).fadeIn();
	$("#comment-" + id).validate(options);
	
	current.parent().find('span.comm').remove();		//usunięcie |
	current.remove();
	if (show)
		window.location.href = '#comment';
	getCaptcha(id);
	return false;
}

function showResponse(json) {
	hideLoading();
	if (json.result) {
		$("#comment-" + json.id).fadeOut('normal', function(){
			$(this).remove();
		});
		$("#comment-" + json.id).before('<p class="info">Komentarz został dodany, obecnie czeka na moderację. Powinien być widoczny na stronie w ciągu kilku godzin</p>');
	}
	else if (json.error == 'captcha') {
		getCaptcha();
		$("input[name='captcha_input']").val('');
		$("input[name='captcha_input']").parent().prev().append('<label class="error" generated="true">Błędny tekst</label>');
	}
}

function showNews() {
	var id = $(this).attr('class').substring(10);
	$(this).parent().parent().find('p.content').after('<img class="loader-big" src="images/loader32.gif" width="32" height="32" alt="" style="margin: 10px 0 0 180px;"/>');
	$(this).parent().find('span:not(.comm)').remove();		//usunięcie
	$(this).hide();
	$.get('/news/get-news/id/' + id, function(data){
		$(".loader-big").remove();
		var div = $("a.news-more-" + id).parent().parent();
		try {
			var src = div.find('img').attr('src').replace('thumb/', '');
			div.find('img').attr('src', src);
		}
		catch(e) {
			
		}
		div.find('p.head').attr('class', 'title');
		div.find('p.content').attr('class', 'head');
		div.find('p.head').after(data);
		$("a.news-more-" + id).remove();
	});
	return false;
}

var commentShown = new Array();
function showComment() {
	var id = $(this).attr('class').substring(13);
	if (!commentShown[id]) {
		$(this).parent().parent().append('<img style="margin: 30px 0 0 250px;" class="loader" src="images/loader32.gif" width="32" height="32" alt=""/>');		//pokaż loader
		$.get('/news/get-comment/id/' + id, function(data){
			$("img.loader").remove();
			commentShown[id] = true;
			$("a.show-comment-" + id).parent().parent().append(data);
			$("a.show-comment-" + id).hide();
			$("a.hide-comment-" + id).show();
		});
	}
	else {
		$(this).parent().parent().find('div.comments').slideDown();
		$(this).hide();
		$("a.hide-comment-" + id).show();
	}
	return false;
}

function hideComment() {
	var id = $(this).attr('class').substring(13);
	$(this).parent().parent().find('div.comments').slideUp();
	$("a.show-comment-" + id).show();
	$(this).hide();
	return false;
}

function getCaptcha() {
	var $form;
	if (arguments.length) {
		var id = arguments[0];
		$form = $('#comment-' + id);
	}
	else
		$form = $(document);
	$(".captcha_img", $form).remove();
	$("img.captcha_loader", $form).show();
	$.get('/adverts/generate-captcha', function(data){
		$("img.captcha_loader", $form).after('<img src="/public/images/captcha/' + data + '.png" class="captcha_img"/>').hide();
		$("input[name='captcha_id']", $form).val(data);
	});
}
