$(document).ready(function() {
    $('#semnatura_modal').dialog({autoOpen: false, width: 350, modal: true, title: $('#semnatura_titlu').val()});
    $('#raspuns_modal').dialog({autoOpen: false, width: 350, modal: true, title: 'Adaugă un răspuns'});
    $('#edit_modal').dialog({autoOpen: false, width: 350, modal: true, title: 'Editează petiția/campania'});
    $('#admin_modal').dialog({autoOpen: false, width: 350, modal: true, title: 'Administrează petiția/campania'});
    $('#email_share_modal').dialog({autoOpen: false, width: 500, modal: true, title: 'Trimite pe e-mail'});
    $('#embedd_modal').dialog({autoOpen: false, width: 500, modal: true, title: 'Include pe site/blog'});

	$("#embedded_code").focus( function()
        {
            $(this).select()
        }).click( function()
        {
            $(this).select()
     });

    $('#semnatura_form').ajaxForm({
        dataType: 'json',
		beforeSubmit: function(){
			$('#buttons').prepend('<img src="/style/images/ajax-loader-green.gif" alt="" />');
		},
        success: function(data){
            $(".error").html("");
            if (data.error) {
                for (error in data.error){
                    $("#error_"+error).html(data.error[error]);
                }
                if (data.error['general']) {
                    $("#reffer").html(data.error['general']);
                }
				$('#buttons img').remove();
            }
            else if(data.msg=='petitie'){
                window.location ="/mesaje/ok/semnatura_adaugata_cu_succes";
            }
            else
                window.location ="/mesaje/ok/adeziune_adaugata_cu_succes";
        }
    });

    $('#form_edit').ajaxForm({
	dataType: 'json',
	success: function(data){
	    $(".error").html("");
	    if (data.error) {
		$("#error").html(data.error);
	    }
	    else {
		window.location = data.url;
	    }
	}
    });
    
    $('#form_admin').ajaxForm({
    	dataType: 'json',
    	success: function(data){
    		$(".error").html("");
    		if (data.error)
    		{
    			$("#error-admin").html(data.error);
    		}
    		else
    		{
    			$('#login_admin').hide();
    			$('#options_admin').show();
    		}	
    	}
    });

    $('#form_raspuns').ajaxForm({
	dataType: 'json',
	success: function(data){
	    $(".error_raspuns").html("");
	    if (data.error) {
		$("#error_raspuns").html(data.error);
	    }
	    else {
		window.location = data.url;
	    }
	}
    });

	$('#form_email').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(){
            $('#email_submit').prepend('<img src="/style/images/ajax-loader.gif" alt="" />');
		},
        success: function(data){
			$(".error_email").html("");
            if (data.error) {
                for (error in data.error){
                    $("#error_"+error).html(data.error[error]);
                }
                $('#email_submit img').remove();
			}
			else
			{
				$('#email_submit img').remove();
                $('#form_email').clearForm();
				$(".error_email").html("");
				$('#email_share_modal').jqmHide();
				alert("Mesajul a fost trimis.");
			}
		}
	});
});

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

