﻿function zmienOpis(el, text) {
	zamienTekst($(el).parents('.statusContent').children('.statusText'), text);
}

function modyfikuj(el) {
	var options = $(el).parent().parent().find('.displayOptions');
	if (options.css('display') == 'none') {
		clearOptions();
		options.css('display', 'block');
	} else {
		clearOptions();
		options.css('display', 'none');
	}
}

function zamienTekst(el, text) {
	var a = el.children('a');
	if (a.length) {
		a.html(text);
	} else {
		el.html(text);
	}
	
}

function clearOptions() {
	$('.displayOptions').css('display', 'none');
}

function vote(el, alias, isPlus) {
	$.ajax({
		url: '/ajaxAction/vote/' + alias + '/' + (isPlus ? 1 : 0),
		success: function(data, request, text){
			if (data.match(/^[0-9]+$/) && data > 0) {
				var text = $(el).parent().children('.rating').html();
				text = text.replace(/-*[\d]+/, data);
				$(el).parent().children('.rating').html(text);
				alert('Dziękujemy za oddanie głosu.');
			} else if (data == 'voted') {
				alert('Oddałeś już dzisiaj głos na ten opis.');
			}
		}
	});
}

function addComment() {
	var form = $('#commentForm');
	var author = form.find('#commentAuthor').val()
	var text = form.find('#commentText').val()
	var code = form.find('#securityCode').val();
	if (!author.match(/[a-zA-Z]+/)) {
		alert("Podaj autora komentarza");
		return false;
	}
	if (!text.match(/[a-zA-Z]+/)) {
		alert("Podaj treść komentarza");
		return false;
	}
	return true;
}
