var PAGE = new Object();
var news_page = /seccion=novedades/;

$(document).ready( function() { getXML() });

function fill_page() {

    if ( !document.URL.match(news_page) ) {
        fill_news_right('redirect');
    }
    else {

        id = _get_id_from_query_string();

        $(".columnaDerechaTexto > a").attr("onClick", "full_list(this); return false;");
        fill_news_list();
        fill_news_main(id);
        fill_news_right('inplace');
    }

}

function getXML() {
  $.ajax({
    type: "GET",
    url: "data/news.xml",
    dataType: "xml",
    success: parseXml
  });
}

function parseXml(xml) {
	
    var articles = new Array();

    $(xml).find("article").each( function() {
        id = $(this).attr("id");
        var article = {
            "id":       id,
            "title":    $(this).find("title").text(),
            "subtitle": $(this).find("subtitle").text(),
            "summary":  $(this).find("summary").text(),
            "body":     $(this).find("body").text(),
        }

        articles[id] = article;
    });

    PAGE["articles"] = articles;

    fill_page();
}

function fill_news_right(action) {
    articles = PAGE.articles;
    id = articles.length - 1;

    if (action == 'inplace') {
        $(".novedad").each(function() {
            $(this).find("h4").text(articles[id].title).wrap(
                "<a href='#' onclick='fill_news_main(" + id + ")' style='text-decoration: none;'></a>"
            );
            $(this).find("p").text(articles[id].summary);
            id--;
        });
    }
    else if (action == 'redirect') {
        
        
        
        $(".novedad").each(function() {
            $(this).find("h4").text(articles[id].title).wrap(
                "<a href='index.php?idioma=" + _get_language() + "&seccion=novedades&id=" + id + "' style='text-decoration: none;'></a>"
                //"<a href='#' onclick='redirect_to_news("+id+")'; return false;'></a>"
            );
            $(this).find("p").text(articles[id].summary);
            id--;
        });

    }
}

function fill_news_list(all) {

    articles = PAGE.articles;
    i = articles.length - 1;
    max = all ? 100 : 3;

    list_elements = "";

    while ( max > 0 && i > 0) {
        list_elements += "<a href='#' onclick='fill_news_main("+ i +"); return false;'>"
        list_elements += "<li>" + articles[i].title + "</li>";
        list_elements += "</a>"
        max--;
        i--;
    }

    $(".noticia").html(list_elements);
}

function fill_news_main(id) {

    var article;

    // last article
    if ( id == null ) {
        article = PAGE.articles[ PAGE.articles.length - 1];
    }
    else {
        article = PAGE.articles[id];
    }

    $(".columnaIzquierdaTexto > h3").text(article.title);
    $(".columnaIzquierdaTexto > #subtitle > i").text(article.subtitle);

    $(".columnaIzquierdaTexto > #body").html("<p>" + article.body.replace(/\n|\n\r/g, "</p><p>") + "</p>");
}

function _get_id_from_query_string() {
    matches = document.URL.match(/id=(\d+)$/);

    if ( matches && matches[1] ) { return matches[1] }

    return null;
}

function _get_language() {
    if (document.URL.match(/idioma=en/)) {
        return 'en';
     }
     else {
        return 'es';
     }
}

function redirect_to_news(id) {
    document.location = "index.php?idioma=" + _get_language() + "&seccion=novedades&id=" + id;
}

function full_list(obj) {
    fill_news_list(1);
    $(obj).hide();
}
