
$(document).ready(function() {
    $(".star-rating a").hover(
		function (e) {
			$("#midia-user-rating").html($(this).html());
			$("#midia-user-rating").show();
		},
		function () {	$("#midia-user-rating").hide(); }
	);

    $('.star-rating a').css('cursor','pointer');

    $("#midia-user-rating").hide();
});

// Estrelinhas //

/**
 * Seleciona estrelinha.
 */
function midiaSelecionarVoto(midiaEstrela, numero_estrela) {
	document.getElementById("midia-current-rating").style.width = midiaEstrela.clientWidth+'px';
	document.getElementById("midia-nota").value = numero_estrela;
	$("#midia-user-rating").html(midiaEstrela.innerHTML);

	// Envia nota por ajax
	var url = '/includes/ajax/fotos/votar-fotos.php';
	var parametros = 'foto=' + $('#cod_foto').val() + '&nota=' + numero_estrela;

	$.post(	url,
			 	parametros,
				function(result){
					result = eval(result);
					if ( result['status'] == 'sucesso'){
						var fbDenuncia;
						fbDenuncia = new FloatBox('Avalie', '<h3>Foto avaliada com sucesso!</h3>');
						fbDenuncia.abrir();
					}
					else if ( result['status'] == 'erro-login' ){
						naoLogado();
						document.getElementById("midia-current-rating").style.width = 0+'px';
						document.getElementById("midia-nota").value= 0;
						$("#midia-user-rating").html('');
					}
					else {
						FloatBoxAlert();
						document.getElementById("midia-current-rating").style.width = 0+'px';
						document.getElementById("midia-nota").value= 0;
						$("#midia-user-rating").html('');
					}
				},
				"json");
}


var currPage = 1;
var serverPage = 1;
var totalPages = 2;

function moveToPage(page) {
    if(page == 1) {
        currPage = 1;
    }
    $(".list-other").animate({
			  top: (-348*(page-1)) + "px"
			}, 1500 );
}

function nextPage() {
    
        currPage++;
        var totPages = Math.ceil($('.list-other li').length/4);
        if(currPage > totPages) {
            currPage--;
            return;
        }

        if(totPages < currPage+1 && serverPage < totalPages) {
            getFotoAlbumPage(++serverPage);
        }
        //if(currPage <= $('.list-other li').length/4) {
        moveToPage(currPage);
        //} else {
        //    currPage--;
        //}
}

function previousPage() {
        currPage--;
        if(currPage < 1) {
            currPage++;
            return;
        }

        moveToPage(currPage);
}

function lastPage() {
    var lastPage = getFotoAlbumPage(currPage, true);
    currPage = lastPage;
    moveToPage(currPage);
}

var ret = null;

function getFotoAlbumPage(page, all) {
    var url = "/includes/ajax/usuarios/perfil-publico/albuns.php";
    var params = "acao=get_album_fotos&cod_album="+($('#cod_album').val() > 0?$('#cod_album').val():0)+"&page="+page+
                 "&cod_usu="+$('#cod_usu').val();

    if(all) {
        params += "&last=true";
    }

    var x = 0;

    $.ajax({
       type: "GET",
       url: url,
       data: params,
       async: false,
       dataType: "json",
       success: function(resposta) {
            if(resposta["status"] == "sucesso") {
                for(x = 0; x < resposta["fotos"].length ; x++) {
                    $('.list-other').append(
                            '<li>'+
                                '<a href="javascript:mudarFoto('+resposta["fotos"][x].id+', '+(resposta["fotos"][x]['isApi']?'true':'false')+');" class="pict-thumb"><img src="'+resposta["fotos"][x].img+'" alt="'+resposta["fotos"][x].comentario+'"/></a>'+
                                '<span class="photo-info">'+
                                    '<a href="javascript:mudarFoto('+resposta["fotos"][x].id+', '+(resposta["fotos"][x]['isApi']?'true':'false')+');"/><br/>'+
                                    ''+resposta["fotos"][x].visualizacoes+' visualizações<br/>'+
                                    ''+resposta["fotos"][x].num_comentarios+' comentários'+
                                    //'<small>Tags: <a href="#">tag</a>, <a href="#">tag</a></small>'+
                                '</span>'+
                            '</li>');
                }
                totalPages = resposta["total"];
            } else if(resposta["status"] == "erro-login"){
                naoLogado();
            } else {
                alert("Ocorreu um erro ao fazer a paginaÃ§Ã£o !");
            }
       }
    });
    return x;
}

function mudarFoto(codFoto, isApi) {
   var url = "/foto"+(isApi?"-api":"")+"/"+codFoto+"/ajax/";

   putLoading('shownPhoto');
   putLoading('photo-comments');

   $.getJSON(url, null, function (resp) {
       if(resp.status == "sucesso") {

            $('#shownPhoto').replaceWith(resp.html);
            $('#photo-comments').html(resp.comentarios);
            $('#cod_foto').val(codFoto);
            if(resp.isApi == 'true') {
                $('#isApi').val('true');
            } else {
                $('#isApi').val('false');
            }

       } else if(resp.status == "erro") {
           alert(resp.msg);
       } else {
           alert("Ocorreu um erro ao mudar a foto, por favor contate a administração.");
       }
   });
   

}

function mudarComentario(pag, codFoto, isApi) {
   isApi = (isApi == 'true' || isApi == true ? true:false);

   var url = "/foto"+(isApi?"-api":"")+"/"+codFoto+"/comentarios/"+pag+"/";

   putLoading('photo-comments');

   $.getJSON(url, null, function (resp) {
       if(resp.status == "sucesso") {

            $('#photo-comments').html(resp.html);

       } else if(resp.status == "erro") {
           alert(resp.msg);
       } else {
           alert("Ocorreu um erro ao mudar os comentários da foto, por favor contate a administração.");
       }
   });
}

function putLoading(divId) {
   $('#'+divId).html('<img src="/imagens/layout/ajax-loader-blue.gif" alt="Carregando..." style="position:absolute;left:50%;margin-left:-16px;margin-top:30px;top:50%;" />');
}

function reloadComments() {
   mudarComentario(1, $('#cod_foto').val(), $('#isApi').val());
   closefloatbox();
}
