﻿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, link, imagem) {
    this.position = position;
    this.guid = guid;
    this.titulo = titulo;
    this.link = link;
    this.imagem = imagem;
}

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 fotos = jQuery('#dvFotoDestaque .img');
    var descricao = jQuery('#dvFotoDestaque .nav .legenda');
    var nav = jQuery('#dvFotoDestaque .nav .seta');
    var total = 0;

    //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('link').text(),
				itm.find('imagem').text()
            );

            //fotos
            var div = document.createElement('div');
            div.id = "destaque_imgmenor" + i;
            div.className = "hide";
            div.innerHTML = "<a href='" + newsrotator_newsList[i].link + "'><img src='" + newsrotator_newsList[i].imagem + "' alt='" + newsrotator_newsList[i].titulo + "' title='" + newsrotator_newsList[i].titulo + "' /></a>";
            fotos.append(div);

            //descricao
            div = document.createElement('span');
            div.id = "destaque_descricao" + i;
            div.className = "hide";
            div.innerHTML = newsrotator_newsList[i].titulo;
            descricao.append(div);

            //next
            total++;
            i++;
        });

        if (total < 1) {
            $("#dvFotoDestaque").hide();
            $(".botoes").hide();
        }

        //contador total
        $("#dvFotoDestaque .total").html(total);

        //Exibe a primeira noticia
        newsrotator_curNews = 0;
        newsrotator_selectThumb(newsrotator_curNews);

        // ao clicar em previous
        nav.find('a.previous').click(function() {
            clearTimeout(newsrotator_timeout);

            if (newsrotator_curNews <= 0)
                newsrotator_curNews = total - 1;
            else
                newsrotator_curNews = newsrotator_curNews - 1;

            $("#dvFotoDestaque .atual").html(newsrotator_curNews + 1);

            //Noticia atual
            newsrotator_selectThumb(newsrotator_curNews);

            return false;
        });

        // ao clicar em next
        nav.find('a.next').click(function() {
            clearTimeout(newsrotator_timeout);

            if (newsrotator_curNews >= total - 1)
                newsrotator_curNews = 0;
            else
                newsrotator_curNews++;

            $("#dvFotoDestaque .atual").html(newsrotator_curNews + 1);

            //Noticia atual
            newsrotator_selectThumb(newsrotator_curNews);

            return false;
        });
    });
};

// seleciona item atual - menu direito
function newsrotator_selectThumb(current) {
    var itens = jQuery('#dvFotoDestaque .img div');

    for (i = 0; i < itens.length; i++) {
        $("#destaque_imgmenor" + i).attr("class", "hide");
        $("#destaque_descricao" + i).attr("class", "hide");
        if (current == i) {
            $("#destaque_imgmenor" + i).attr("class", "");
            $("#destaque_descricao" + i).attr("class", "");
        }
    }
}