﻿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, categoria, titulo, titulo_menor, descricao, imagem, imagem_thumb, link, data) {
    this.position = position;
    this.guid = guid;
    this.categoria = categoria;
    this.titulo = titulo;
    this.titulo_menor = titulo_menor;
    this.descricao = descricao;
    this.imagem = imagem;
    this.imagem_thumb = imagem_thumb;
    this.link = link;
    this.data = data;
}

jQuery.fn.newsrotator = function(xmlFile, settings) {
    var newsContainer = this;
    settings = jQuery.extend({
        fade: 750,
        timeout: 7000,
        timeout_thumb: 7200
    }, settings);

    newsrotator_fadeNewsSpeed = settings.fade;
    newsrotator_newsTimeout = settings.timeout;
    newsrotator_newsTimeoutThumb = settings.timeout_thumb;

    //Navegacao
    var nav = jQuery('.newsrotator_navigation');

    //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('categoria').text(),
				itm.find('titulo').text(),
				itm.find('titulo_menor').text(),
				itm.find("descricao").text(),
				itm.find('imagem').text(),
				itm.find('imagem_thumb').text(),
				itm.find('link').text(),
				itm.find('data').text()
            );

            //Navegacao
            var div = document.createElement('div');
            div.className = "noticia";

            var content = "<a rel=" + i + "><div class='img'><img src='" + newsrotator_newsList[i].imagem_thumb + "' title='" + newsrotator_newsList[i].titulo_menor + "' alt='" + newsrotator_newsList[i].titulo_menor + "'/></div>";
            content += "<div class='titulo'>" + newsrotator_newsList[i].categoria + "</div>";
            content += "<div class='resumo'>" + newsrotator_newsList[i].titulo_menor + "</div></a>";

            div.innerHTML = content;

            nav.append(div);

            //Linha divisória
            var div_linha = document.createElement('div');
            div_linha.className = "linha";
            nav.append(div_linha);

            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 + "'/>";

            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.alt = newsrotator_newsList[newsrotator_curNews].titulo;
        img.src = newsrotator_newsList[newsrotator_curNews].imagem;
        var div = newsrotator_getDiv(img);
        jQuery(newsrotator_backNewsFrame).html(jQuery(div).html());

        newsrotator_selectThumb(newsrotator_curNews);

        // start toggling!
        newsrotator_toggleNews();
    });
};

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 + "'/>";

    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);
}

function newsrotator_fadeNews() {
    // fade in news and repeat
    jQuery(newsrotator_backNewsFrame).fadeIn(newsrotator_fadeNewsSpeed, newsrotator_toggleNews);
}

//Seleciona thumbnail - menu direito
function newsrotator_selectThumb(current) {
    var nav = jQuery('.newsrotator_navigation');
    var links = nav.find('a');

    for (i = 0; i < links.length; i++) {
        if (current == i) {
            links[i].className = "destaqueHome";
        } else {
            links[i].className = "";
        }
    }
}

function newsrotator_getDiv(img) {
    var div = document.createElement('div');

    var div_img = document.createElement('div');
    div_img.className = "imgprincipal";

    //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);
    } else {
        jQuery(div_img).append(img);
    }

    jQuery(div).append(div_img);

    var div_container_titulo = document.createElement('div');
    div_container_titulo.className = "bgtituloprincipal";

    var div_categoria = document.createElement('div');
    div_categoria.className = "categoria-image"
    div_categoria.innerHTML = newsrotator_newsList[newsrotator_curNews].categoria;

    jQuery(div_container_titulo).append(div_categoria);

    var div_titulo = document.createElement('div');
    div_titulo.className = "titulo-image";
    div_titulo.innerHTML = newsrotator_newsList[newsrotator_curNews].titulo;

    //Link sobre o titulo
    if (newsrotator_newsList[newsrotator_curNews].link.length > 0) {
        var link = document.createElement('a');
        link.href = newsrotator_newsList[newsrotator_curNews].link;
        jQuery(link).append(div_titulo);
        jQuery(div_container_titulo).append(link);
    } else {
        jQuery(div_container_titulo).append(div_titulo);
    }

    jQuery(div).append(div_container_titulo);

    var div_container_descricao = document.createElement('div');
    div_container_descricao.className = "bg-principal";

    var div_descricao = document.createElement('div');
    div_descricao.className = "titulo-principal";
    div_descricao.innerHTML = newsrotator_newsList[newsrotator_curNews].descricao;

    //Link sobre a descricao
    if (newsrotator_newsList[newsrotator_curNews].link.length > 0) {
        var link = document.createElement('a');
        link.href = newsrotator_newsList[newsrotator_curNews].link;
        jQuery(link).append(div_descricao);
        jQuery(div_container_descricao).append(link);
    } else {
        jQuery(div_container_descricao).append(div_descricao);
    }

    jQuery(div).append(div_container_descricao);

    return div;
}
