﻿var newsrotator_frontNewsFrame; //primeiro frame
var newsrotator_backNewsFrame; //segundo frame
var newsrotator_newsList = []; // array de noticias
var newsrotator_curNews = -1; //posicao da noticia atual
var newsrotator_fadeNewsSpeed; // fade settings
var newsrotator_newsTimeout;
var newsrotator_newsTimeoutThumb;
var newsrotator_timeout; // js timeout function

function newsrotator_newsItem(position, guid, titulo, subtitulo, descricao, imagem, imagemmenor, link) {
    this.position = position;
    this.guid = guid;
    this.titulo = titulo;
    this.subtitulo = subtitulo;
    this.descricao = descricao;
    this.imagem = imagem;
    this.imagemmenor = imagemmenor;
    this.link = link;
}

jQuery.fn.newsrotator = function(xmlFile, settings) {
    var newsContainer = this;
    settings = jQuery.extend({
        fade: 750,
        timeout: 5000,
        timeout_thumb: 5200
    }, settings);

    newsrotator_fadeNewsSpeed = settings.fade;
    newsrotator_newsTimeout = settings.timeout;
    newsrotator_newsTimeoutThumb = settings.timeout_thumb;

    //Navegacao
    var nav = jQuery('#profissional .esq .botoes');
    var fotos = jQuery('#profissional .esq .destaques_fotos');
    var descricao = jQuery('#profissional .dir .destaques_desc');

    //Carrega dados do XML
    jQuery.get(xmlFile, function(xml) {
        var i = 0;
        jQuery(xml).find('item').each(function() {
            var itm = jQuery(this);
            newsrotator_newsList[i] = new newsrotator_newsItem(
				itm.attr('order'),
				itm.find('guid').text(),
				itm.find('titulo').text(),
				itm.find('subtitulo').text(),
				itm.find("descricao").text(),
				itm.find('imagem').text(),
				itm.find('imagemmenor').text(),
				itm.find('link').text()
            );

            //cria a navegacao
            var span = document.createElement('span');
            span.innerHTML = "<a rel=" + i + "><div class='botao'>" + (i + 1) + "</div></a>";
            nav.append(span);

            //fotos
            var div = document.createElement('div');
            div.id = "destaque_imgmenor" + i;
            div.className = "hide";
            if (newsrotator_newsList[i].link != "")
                div.innerHTML = "<a href='" + newsrotator_newsList[i].link + "'><img src='" + newsrotator_newsList[i].imagemmenor + "' alt='" + newsrotator_newsList[i].titulo + "' title='" + newsrotator_newsList[i].titulo + "' /></a>";
            else
                div.innerHTML = "<img src='" + newsrotator_newsList[i].imagemmenor + "' alt='" + newsrotator_newsList[i].titulo + "' title='" + newsrotator_newsList[i].titulo + "' />";
            fotos.append(div);

            //descricao
            div = document.createElement('div');
            div.id = "destaque_descricao" + i;
            div.className = "hide";
            div.innerHTML = "<div class='nome'>" + newsrotator_newsList[i].titulo + "</div>";
            div.innerHTML += "<div class='profissao'>" + newsrotator_newsList[i].subtitulo + "</div>";
            if (newsrotator_newsList[i].link != "")
                div.innerHTML += "<div class='texto'><a href='" + newsrotator_newsList[i].link + "'>" + newsrotator_newsList[i].descricao + "</a></div>";
            else
                div.innerHTML += "<div class='texto'>" + newsrotator_newsList[i].descricao + "</div>";
            descricao.append(div);

            //next
            i++;
        });

        // only create element if it's not already there
        var div_absolute = document.createElement('div');
        div_absolute.className = "absolute";
        if (newsContainer.length == 1) newsContainer.append(div_absolute);

        // get array of div elements to swap
        var divs = jQuery('.newsrotator_frame div');
        newsrotator_frontNewsFrame = divs[1];
        newsrotator_backNewsFrame = divs[0];


        // ao clicar no link para troca de noticia
        nav.find('a').click(function() {
            clearTimeout(newsrotator_timeout);

            //Noticia atual
            newsrotator_curNews = parseInt(this.rel);
            newsrotator_selectThumb(newsrotator_curNews);

            //imagem
            var img = document.createElement('div');
            img.innerHTML = "<img src='" + newsrotator_newsList[newsrotator_curNews].imagem + "' alt='" + newsrotator_newsList[newsrotator_curNews].titulo + "' title='" + newsrotator_newsList[newsrotator_curNews].titulo + "'/>";

            var div = newsrotator_getDiv(img);

            // swap frames
            var temp = newsrotator_frontNewsFrame;
            newsrotator_frontNewsFrame = newsrotator_backNewsFrame;
            newsrotator_backNewsFrame = temp;

            // set current image to hide next
            newsrotator_frontNewsFrame.className = "absolute";
            newsrotator_frontNewsFrame.removeAttribute('style');

            // prepare to swap image
            newsrotator_backNewsFrame.className = "hide absolute";
            jQuery(newsrotator_backNewsFrame).html(jQuery(div).html());

            //Fade in e gera novo timeout para trocar a noticia
            jQuery(newsrotator_backNewsFrame).fadeIn(newsrotator_fadeNewsSpeed);
            newsrotator_timeout = setTimeout(function() { newsrotator_fadeNews(); }, newsrotator_newsTimeout);

            return false;
        });

        //Exibe a primeira noticia
        newsrotator_curNews = 0;
        newsrotator_frontNewsFrame.className = "absolute";
        newsrotator_frontNewsFrame.removeAttribute('style');
        newsrotator_backNewsFrame.className = "hide absolute";

        var img = new Image();
        img.src = newsrotator_newsList[newsrotator_curNews].imagem;
        img.alt = newsrotator_newsList[newsrotator_curNews].titulo;
        img.title = newsrotator_newsList[newsrotator_curNews].titulo;
        var div = newsrotator_getDiv(img);
        jQuery(newsrotator_backNewsFrame).html(jQuery(div).html());

        newsrotator_selectThumb(newsrotator_curNews);

        // start toggling!
        newsrotator_toggleNews();
    });
};
// alterna os itens
function newsrotator_toggleNews() 
{
    // move to next image	
    if (++newsrotator_curNews >= newsrotator_newsList.length) {
        newsrotator_curNews = 0;
    }

    //Carrega proxima imagem
    var img = document.createElement('div');
    img.innerHTML = "<img src='" + newsrotator_newsList[newsrotator_curNews].imagem + "' alt='" + newsrotator_newsList[newsrotator_curNews].titulo + "' title='" + newsrotator_newsList[newsrotator_curNews].titulo + "'/>";

    var div = newsrotator_getDiv(img);

    // swap frames
    var temp = newsrotator_frontNewsFrame;
    newsrotator_frontNewsFrame = newsrotator_backNewsFrame;
    newsrotator_backNewsFrame = temp;

    // set current image to hide next
    newsrotator_frontNewsFrame.className = "absolute";
    newsrotator_frontNewsFrame.removeAttribute('style');

    // prepare to swap image
    newsrotator_backNewsFrame.className = "hide absolute";
    jQuery(newsrotator_backNewsFrame).html(jQuery(div).html());

    // fade in next image and repeat
    newsrotator_timeout = setTimeout(function() { newsrotator_fadeNews(); }, newsrotator_newsTimeout);
    setTimeout(function() { newsrotator_selectThumb(newsrotator_curNews); }, newsrotator_newsTimeoutThumb);
}
// fade
function newsrotator_fadeNews() 
{
    // fade in news and repeat
    jQuery(newsrotator_backNewsFrame).fadeIn(newsrotator_fadeNewsSpeed, newsrotator_toggleNews);
}

// seleciona item atual - menu direito
function newsrotator_selectThumb(current) {
    var itens = jQuery('.item');

    for (i = 0; i < itens.length; i++) {
        itens[i].className = itens[i].className.replace(" selected", "");
        $("#destaque_imgmenor" + i).attr("class", "hide");
        $("#destaque_descricao" + i).attr("class", "hide");
        if (current == i) {
            itens[i].className = itens[i].className + " selected";
            $("#destaque_imgmenor" + i).attr("class", "");
            $("#destaque_descricao" + i).attr("class", "");
        }
    }

    //navegacao
    var botoes = jQuery('.botao');
    for (i = 0; i < botoes.length; i++) {
        botoes[i].className = "botao";
        if (current == i)
            botoes[i].className = "botao botao-sel";
    }
}
// exibe a div principal com a imagem
function newsrotator_getDiv(img) {
    var div = document.createElement('span');

    //Link sobre a imagem
    if (newsrotator_newsList[newsrotator_curNews].link.length > 0) {
        
        var link = document.createElement('a');
        link.href = newsrotator_newsList[newsrotator_curNews].link;
        jQuery(link).append(img);
        //jQuery(div_img).append(link);
        jQuery(div).append(link);
    } else {
        jQuery(div).append(img);
    }

    return div;
}