
$().ready(function() {
	/* favorites */
	$('.delajax').click(function(){
		var el = $(this);
		var id = $(this).attr('id');
		var type = $(this).attr('type');
		var name = $(this).attr('name');
		$.ajax({
			type: "GET",
			dataType: "json",
			url: "/engine/ajax/ajax_controller.php",
			data: "m=favorites&a=" + name + "&id=" + id + "&type=" + type,
			success: function(json){
				if( json.success ) {
					//alert('ok');
					//ppp.remove();
					var name = el.attr('name');
					var name_gif = (name == 'add')?'minus':'plus';
					name = (name == 'add')?'del':'add';
					el.attr('name', name);
					el.attr('src', '/templates/spletnik2/i/fav_' + name_gif + '.gif');
					
				} else {
					alert(json.error);
				}
			}
		});
	});
});

// calendar
function updateCalc(dmy) {
	var cal = $(".calendar");
	$.ajax({
		type: "GET",
		dataType: "text",
		url: "/engine/ajax/ajax_controller.php",
		data: "m=calendar&a=get&dmy=" + dmy,
		success: function(text){
			if( text != 'error' ) {
				cal.html(text);
			} else {
				//alert();
			}
		}
	});
}

function changeFavGal() 
{
	var id = $(".favor").attr("id");
	var action = $(".favor").attr("title");
	alert(action);
	action = (action == "add")?"remove":"add";
	$(".favor").attr("title", action);
	
}

function addVote(vote_id, obj)
{
    var num_answer = $("input[name=vote_" + vote_id + "]:checked").val(); 
    if (isNaN(num_answer)) {
        alert('Укажите вариант ответа!');
        return;
    }
    var sec_code = $(obj).parent().find("#sec_code_vote").val();
    //alert(sec_code);
    $.ajax({
            type: "GET",
            dataType: "json",
            url: "/engine/ajax/ajax_controller.php",
            data: "m=vote&a=add&vote_id=" + vote_id + "&num_answer=" + num_answer + "&sec_code=" + sec_code,
            success: function(json){
                if( json.success == true ) {
                    location.reload();
                } else {
                    alert(json.error);
                    $('#captcha')[0].src='/engine/modules/antibot.php?r='+Math.round(Math.random()*10000);
                    $("#sec_code_vote").val('');
                }
            }
        }); 
}

function showVoteResult(vote_id)
{
    $("ul.vote_" + vote_id + " input").remove();    
}
