

function getTypeName(type) {

    var name = '';
    switch (type) {
        case 'serie':
            name = 'Serie';
            break;
        case 'artista':
            name = 'Artista';
            break;
        case 'novidade':
            name = 'Novidade';
            break;
            break;
        default:
            break;
    }

    return name;

}

function abrirBuscaFav(type) {

    var haveModal = parseInt($('#modal-window').length);
    var haveOverlay = parseInt($('#modal-overlay').length);

    if( haveModal < 1) {
        $('body').append('<div id="modal-window" style="display:none;"></div>');
    }
    if( haveOverlay < 1) {
        $('body').append('<div id="modal-overlay" style="display:none;"></div>');
    }

    var typeName = getTypeName(type);

    var html = '<div class="fav">'
             +      '<fieldset style="height:370px;">'
             +          '<div style="float: right;position:relative;top:-35px;"><a id="bt-modal-fechar" href="javascript:closeFloatbox();">Fechar</a></div>'
             +          '<legend style="font-size:15px;">Buscar '+typeName+'</legend>'
             +          '<label>Busca:</label>'
             +          '<input type="text" id="fav-search-term" size="45" onkeyup="if(event.keyCode == 13) searchFavorite(1);"/>'
             +          '<input type="hidden" value="'+type+'" id="search-fav-type"/>'
             +          '<input type="button" value="Buscar" onclick="searchFavorite(1);"/>'
             +          '<div>'
             +            '<ul id="fav-search-results" style="list-style:none;height:285px;">'
             +            '</ul>'
             +          '</div>'
             +          '<div id="fav-search-pagination">'
             +          '</div>'
             +      '</fieldset>'
             +  '</div>';

    $('#modal-window').html(html);

    openFloatbox();

}

function searchFavorite(pag) {

    var term = $('#fav-search-term').val();
    var tipo = $('#search-fav-type').val();

    $.post(
    '/includes/ajax/usuario/adicionar-favoritos.php',
    {'term' : term, 'tipo' : tipo, 'action' : 'search', 'pag' : pag},
    function (resp) {
        if(resp.status == 'sucesso') {

            var html = '';
            $.each(resp.results, function(i, item) {
                html += getResultHtml(item, tipo);
            });

            if(html == '')
                html = '<li>Nenhum resultado encontrado</li>';

            $('#fav-search-results').html(html);
            $('#fav-search-pagination').html(resp.pagination);

        } else if(resp.status == 'erro') {
            alert(resp.msg);
        } else {
            alert('Ocorreu um erro');
        }
    },
    'json'
    );

}

function getResFunction(type) {

    var ret = '';
    switch (type) {
        case 'serie':
            ret = 'adicionarFavorito';
            break;
        default:
            break;
    }

    return ret;

}
var last = 0
function getResultHtml(item, type) {

    var func = getResFunction(type);
    var classe = (last == 0 ? 'alt1' : '');
    last = (last == 0 ? 1 : 0);

    var html =  '<li class="'+classe+'">'
             +      '<a href="javascript:addFavorite('+item.id+', \''+ type +'\');">'+item.name+'</a>'
             +  '</li>';

    return html;

}

/*
 ******************************************************************************************
 *
 *      Funções da floatbox
 *
 ******************************************************************************************
 */

function openFloatbox(){
	var div = document.getElementById("modal-window");
	var b = document.getElementById("modal-overlay");

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
	}

//	div.style.left = (parseInt(windowWidth) - parseInt(div.offsetWidth)) / 2 + 'px';
//	div.style.top = (parseInt(windowHeight) - parseInt(div.offsetHeight)) / 2 + 'px';
        div.style.left = 25 + '%';
	div.style.top = 10 + '%';

	$(b).show().fadeTo("fast", 0.60 );
	$(div).fadeIn("fast");
}
function closeFloatbox(reload){
	var div = document.getElementById("modal-window");
	var b = document.getElementById("modal-overlay");
	$(b).fadeTo("fast",0);
	$(div).slideUp("medium");
	var t = setTimeout('esconderJanela()',350);
        if(reload)
            setTimeout(function() {window.location.reload(true);},800);

}
function esconderJanela() {
	document.getElementById("modal-overlay").style.display = 'none';
	document.getElementById("modal-window").style.display = 'none';
}

function addFavorite(id, type) {
    var url;

    switch (type) {
        case 'serie':
            url = "/includes/ajax/serie/adicionar_favorito.php";
            url +="?cod_serie="+id;
            url +="&cod_usuario="+getCookie('mscodusuario');
            url += "&sid="+Math.random();

            $.get(
            url,
            null,
            function(resp) {
                var split = resp.split('|');
                if(split[1] == 'favorita_inserida' || split[1] == 'favorita_existe') {
                    alert('Série adicionada aos favoritos com sucesso !');
                    $('#bt-modal-fechar').attr('href', 'javascript:closeFloatbox(true);');
                }
            });
            break;
        case 'artista':
            url = "/includes/ajax/artista/favorito.php";
            url+= "?cod_artista="+id;
            url += "&uid="+Math.random();

            $.get(
            url,
            null,
            function(resp) {
                var split = resp.split('|');
                if(split[1] == 'existe' || split[1] == 'adicionado') {
                    alert('Artista adicionado aos favoritos com sucesso !');
                    $('#bt-modal-fechar').attr('href', 'javascript:closeFloatbox(true);');
                }
            });

            break;
        case 'novidade':
            url = "/includes/ajax/novidade/novidade_favorito.php";
            url+= "?cod_novidade="+id;
            url += "&sid="+Math.random();

            $.get(
            url,
            null,
            function(resp) {
                var split = resp.split('|');
                if(split[1] == 'existe' || split[1] == 'adicionada') {
                    alert('Novidade adicionada aos favoritos com sucesso !');
                    $('#bt-modal-fechar').attr('href', 'javascript:closeFloatbox(true);');
                }
            });

            break;
        default:
            break;
    }

}