// class for managing the navigation through the news and how they should be displayed
function newsManager() {
    var oManager = new Object;
    oManager.singleNews = false; // false =hort news mode, true=single-full news mode
    oManager.index = 0;
    oManager.count = 0;
    oManager.lang = "";

    // function for showing the first  5 five news
    oManager.reset = function(lang) {
        this.singleNews = false; 
        this.index = 0;
        this.count = $(".new").length;
        this.lang = lang;
        this.show(0,false);
    }

    // Shows  one full new or five news depending of its state an if  a change is forced into it
    oManager.show = function(newsIndex,mode) {
        var viewer = document.getElementById("newsViewer");
        var isIE = /*@cc_on!@*/false;
        var isIE7 = false
        if (window.XMLHttpRequest) {
            if(document.all){
                isIE7 = true;
            }
        }
        if (this.count==0 || newsIndex>=this.count || newsIndex<0) {
            viewer.innerHTML = "";
            if (this.lang=='es') {
                viewer.parentNode.innerHTML = "Aún no hay noticias.";
            } else {
                viewer.parentNode.innerHTML = "There are no news yet.";
            }
        } else if ((newsIndex!=this.index || mode!=this.singleNews) || (newsIndex==0 && mode==false)) {
            viewer.innerHTML = "";
            this.index = newsIndex;
            this.singleNews = mode;
            for (i=0; i<4; i++) {
                $(".newsButton:eq("+i+")").unbind("click");
            }
            var context = this;
            if (mode) { //managing of the navigator
                $(".newsButton:eq(0)").attr('id','0');
                if (isIE) {
                    $(".newsButton:eq(0)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),true)});
                    $(".newsButton:eq(1)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),true)});
                    $(".newsButton:eq(2)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),true)});
                    $(".newsButton:eq(3)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),true)});
                } else {
                    $(".newsButton:eq(0)").bind("click",function(e){context.show(parseInt(e.target.id),true)});
                    $(".newsButton:eq(1)").bind("click",function(e){context.show(parseInt(e.target.id),true)});
                    $(".newsButton:eq(2)").bind("click",function(e){context.show(parseInt(e.target.id),true)});
                    $(".newsButton:eq(3)").bind("click",function(e){context.show(parseInt(e.target.id),true)});
                }
                if (newsIndex==0) {
                    $(".newsButton:eq(1)").attr('id','0');
                } else {
                    $(".newsButton:eq(1)").attr('id',newsIndex-1);
                }
                if (newsIndex==this.count-1) {
                    $(".newsButton:eq(2)").attr('id',this.count-1);
                } else {
                    $(".newsButton:eq(2)").attr('id',newsIndex+1);
                }
                $(".newsButton:eq(3)").attr('id',this.count-1);
                document.getElementById("newsLocator").innerHTML = newsIndex+1;
                var title = document.createElement("div");
                title.setAttribute('class','firstLevelHeader');
                title.innerHTML = $(".newsTitle:eq("+newsIndex+")").html()+"<span class='dateTitle'> ("+$(".newsDate:eq("+newsIndex+")").html()+")</span>";
                var content = document.createElement("div");
                content.setAttribute('class','newsContentShown');
                content.innerHTML = $(".newsContent:eq("+newsIndex+")").html();
                var summary = document.createElement("div");
                summary.setAttribute('class','newsReadButton');
                if (this.lang=='es') {
                    summary.innerHTML = "Volver al resumen >>"
                } else {
                    summary.innerHTML = "Return to summary >>"
                }
                summary.id=newsIndex-(newsIndex%5);
                if (isIE) {
                    summary.onclick = function(){context.show(parseInt(window.event.srcElement.id),false)};
                } else {
                    summary.onclick = function(e){context.show(parseInt(e.target.id),false)};
                }
                if (isIE7) {
                    jQuery(title).attr("style","font-weight: bold;font-size: 16px;");
                    jQuery(content).attr("style","margin: 8px;");
                    jQuery(summary).attr("style","mmargin: 1px 0 10px 10px;cursor: pointer;font-weight: bold;text-decoration: underline;color: #336600;font-size: 12px;");
                }
                viewer.appendChild(title);
                viewer.appendChild(content);
                viewer.appendChild(summary);
            } else {
                $(".newsButton:eq(0)").attr('id','0');
                if (isIE) {
                    $(".newsButton:eq(0)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),false)});
                    $(".newsButton:eq(1)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),false)});
                    $(".newsButton:eq(2)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),false)});
                    $(".newsButton:eq(3)").bind("click",function(){context.show(parseInt(window.event.srcElement.id),false)});
                } else {
                    $(".newsButton:eq(0)").bind("click",function(e){context.show(parseInt(e.target.id),false)});
                    $(".newsButton:eq(1)").bind("click",function(e){context.show(parseInt(e.target.id),false)});
                    $(".newsButton:eq(2)").bind("click",function(e){context.show(parseInt(e.target.id),false)});
                    $(".newsButton:eq(3)").bind("click",function(e){context.show(parseInt(e.target.id),false)});
                }
                if (newsIndex==0) {
                    $(".newsButton:eq(1)").attr('id','0');
                } else {
                    $(".newsButton:eq(1)").attr('id',newsIndex-5);
                }
                if (newsIndex<=this.count-5) {
                    $(".newsButton:eq(2)").attr('id', newsIndex+5);
                } else {
                    $(".newsButton:eq(2)").attr('id',this.count-(this.count%5));
                }
                $(".newsButton:eq(3)").attr('id',this.count-(this.count%5));
                var limit;
                if (this.count>=newsIndex+5) {
                    document.getElementById("newsLocator").innerHTML = (newsIndex+1)+" - "+(newsIndex+5);
                    limit = newsIndex+5;
                } else {
                    document.getElementById("newsLocator").innerHTML = (newsIndex+1)+" - "+this.count;
                    limit = this.count;
                }
                for (j=newsIndex; j<limit; j++) {
                    var title = document.createElement("div");
                    title.setAttribute('class','firstLevelHeader');
                    title.innerHTML = $(".newsTitle:eq("+j+")").html()+"<span class='dateTitle'> ("+$(".newsDate:eq("+j+")").html()+")</span>";
                    var content = document.createElement("div");
                    content.setAttribute('class','newsContentShown');
                    content.innerHTML = $(".newsParagraph:eq("+j+")").html();
                    var summary = document.createElement("div");
                    summary.setAttribute('class','newsReadButton');
                    if (this.lang=='es') {
                        summary.innerHTML = "Leer más >>"
                    } else {
                        summary.innerHTML = "Read more >>"
                    }
                    summary.id = j;
                    if (isIE) {
                        summary.onclick = function(){context.show(parseInt(window.event.srcElement.id),true)};
                    } else {
                        summary.onclick = function(e){context.show(parseInt(e.target.id),true)};
                    }
                    if (isIE7) {
                        jQuery(title).attr("style","font-weight: bold;font-size: 16px;");
                        jQuery(content).attr("style","margin: 8px;");
                        jQuery(summary).attr("style","mmargin: 1px 0 10px 10px;cursor: pointer;font-weight: bold;text-decoration: underline;color: #336600;font-size: 12px;");
                    }
                    viewer.appendChild(title);
                    viewer.appendChild(content);
                    viewer.appendChild(summary);
                }
            }
        }
    }

    return oManager;
}

// object to invoke the newsManager
var nwManager = new newsManager();

