$(document).ready(function() {
	$("#titlu").charCounter(150, {container: "#counter-titlu", format: "(%1)"});
	$("#email").charCounter(150, {container: "#counter-email", format: "(%1)"});
	$("#website").charCounter(150, {container: "#counter-website", format: "(%1)"});
	$("#parola").charCounter(20, {container: "#counter-parola", format: "(%1)"});
	$('.tip').cluetip({
		splitTitle: '|',
		arrows: true,
		dropShadow: false,
		cluetipClass: 'jtip'
	});

	$('#adauga_sondaj').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(){
			$('#buttons-adauga').prepend('<img src="/style/images/ajax-loader.gif" alt="" />');
		},
		success:   function(data){
			$('#buttons-adauga img').remove();
			$(".error_long").html("");
			if (data.error) {
				var error_scroll = null
				for (error in data.error){
					$("#error_"+error).html(data.error[error]);
					if (data.error[error])
						error_scroll = error_scroll == null ? error : error_scroll;
				}
				if (data.error['general']) {
					$("#error_general").html(data.error['general']);
				}
				if (data.error['captcha']) {
				    refreshCaptcha();
				}
				$.scrollTo("#"+error_scroll, 500);
			}
			else {
				if (data.msg == 'sondaj'){
                    window.location ="/mesaje/ok/sondaj_adaugat_cu_succes";
				}
                else if (data.msg == 'edit_sondaj'){
			        window.location ="/mesaje/ok/sondaj_editat_cu_succes";
				}
				else if (data.msg == 'edit_sondaj_mail'){
					window.location ="/mesaje/ok/sondaj_editat_cu_succes_mails";
				}
			}
		}
	});
});

function refreshCaptcha()
{
    $.ajax({
	type: 'GET',
	url: 'ajax/refreshCaptcha.html',
	dataType: 'text',
	success: function(image){
	    $('#captcha_image').attr('src' ,image);
	}
    });
}

var count_intrebari = 1;

function adauga_raspuns(intrebare) {
    var count_raspunsuri = $("#intrebare_"+intrebare+"_count").html();
    $.ajax({
	type: "POST",
	url: "/ajax/answer",
	data: "intrebare="+intrebare+"&count="+count_raspunsuri,
	dataType: "html",
	success: function(data){
	    $("#containter_intrebare_"+intrebare+" .container_raspunsuri").append(data);
	    count_raspunsuri++;
	    $("#intrebare_"+intrebare+"_count").html(count_raspunsuri);
	}
    });
}

function sterge_raspuns(id1, id2)
{
    $('#answer_container_'+id1+'-'+id2).remove();
    $('#delete_answer_'+id1+'-'+id2).remove();
	count_rapunsuri--;
}

function sterge_intrebare(el)
{
    $(el).parent().parent().remove();
	count_intrebari--;
}

function adauga_intrebare() {
	$.ajax({
		type: "POST",
		url: "/ajax/question",
		data: "count="+count_intrebari,
		dataType: "html",
		success: function(data){
			$(".container_intrebare").append(data);
			count_intrebari++;
		}
	});
}