﻿function NSNewsViewer(newsArray, htmlObj, transition, duration, interval)
{
    this.newsIndex=1;
    
    var i;    
    var timer;
    var paused=false;
    var stopped=false;
    
    this.Init = Init;
    this.Play = Play;
    this.GoNext = GoNext;
    this.Pause = Pause;
    this.Continue = Continue;
    this.ComposeNews = ComposeNews;
    this.DoTransition = DoTransition;
    
    
    function Init()
    {
        i = this;    
        htmlObj.style.filter = "revealTrans(transition="+transition+",duration="+duration+")";
        htmlObj.onfilterchange = function(){i.GoNext()};
        htmlObj.attachEvent("onmouseover",function(){NSNews_onmouseover(i)});
        htmlObj.attachEvent("onmouseout",function(){NSNews_onmouseout(i)});                
    }
    
    function Play()
    {
        stopped=false;
        this.newsIndex=1;
        this.ComposeNews();        
        this.GoNext();        
    }      
    
    function GoNext()
    {        
        if (!paused && parseInt(interval)>0)
            timer = window.setTimeout(function(){i.DoTransition()},interval*1000);
    }
    
    function DoTransition()
    {
        this.newsIndex++;
        if(this.newsIndex>newsArray.length-1){
            var numItems    = parseInt(newsArray[0][2]);
            if (newsArray.length-1 <= numItems){
                stopped=true;
                return;
            }
            this.newsIndex=1;
        }
        htmlObj.filters[0].Apply();
        this.ComposeNews();
        htmlObj.filters[0].Play();
    }
    
    function Pause()
    {
        paused=true;        
        window.clearTimeout(timer);
    }
    
    function Continue()
    {
        if (!stopped){
            paused=false;        
            this.GoNext();
        }
    }
    
    function ComposeNews()
    {
        var viewPageUrl = newsArray[0][0];
        var columns     = parseInt(newsArray[0][1]);
        var numItems    = parseInt(newsArray[0][2]);
        var imageHeight = newsArray[0][3];
        var imageWidth  = newsArray[0][4];
        var modulePath  = newsArray[0][5];
        
        var height = htmlObj.clientHeight;
        
        var html = "<table class='NSNews_table' cellpadding=0 cellspacing=0><tr>";
        var count = 0;       
        
        while (this.newsIndex<newsArray.length && count<numItems)        
        {
            if (count%columns == 0 && count>0)
                html += "</tr><tr>";
                
            var itemId      = newsArray[this.newsIndex][0];
            var title       = newsArray[this.newsIndex][1];
            var imageUrl    = newsArray[this.newsIndex][2];
            var content     = newsArray[this.newsIndex][3];
            var complete    = newsArray[this.newsIndex][4];
            var evidence    = newsArray[this.newsIndex][5];
        
            html+="<td width="+100/columns+"% class='NSNews_td NSNews_td"+(count%columns)+"'>";
        
            if (imageUrl!="")
            {
                html += "<a href='"+viewPageUrl+"&newsid="+itemId+"' target='NSNewsViewer' title=\"Leggi l'articolo\"><img id='NSNews_img' class='NSNews_Image' src='"+modulePath+"getImage.aspx?url="+imageUrl+"&height="+imageHeight+"&width="+imageWidth+"' align=left border=0 style='display:none'/></a>";
                //html += "<a href='"+viewPageUrl+"&newsid="+itemId+"'><img id='NSNews_img' src='"+imageUrl+"' align=left border=0 style='display:none'/></a>";
            }
            
            if (evidence)
            {            
                html += "<img src='"+modulePath+"/new.gif' align=left>";
            }
            
            html += "<b><a class='NSNews_Title' href='"+viewPageUrl+"&newsid="+itemId+"' target='NSNewsViewer' title=\"Leggi l'articolo\">"+title+"</a></b><br>";
            html += "<span class='NSNews_Content'>"+content+"</span>";
            if (!complete)
                html += "<a class='NSNews_More' href='"+viewPageUrl+"&newsid="+itemId+"' target='NSNewsViewer' title=\"Leggi l'articolo\"> »»»</a>"
                
            html+="</td>";    
                
            count++;
            this.newsIndex++;
        }
        
        html+="</tr></table>";
        
        this.newsIndex--;
        htmlObj.innerHTML = html;
        
        if (htmlObj.clientHeight<height)
            htmlObj.style.height = height+"px";
        
        NSNews_AdjImages(imageHeight,imageWidth,htmlObj);
           
    }
    
    function NSNews_onmouseover(i)
    {
        i.Pause();
    }
    
    function NSNews_onmouseout(i)
    {   
        if (!htmlObj.contains(event.toElement) && event.toElement!=htmlObj)
            i.Continue();                    
    }
    
}

function NSNews_AdjImages(imageHeight,imageWidth,htmlObj)
{
    if (imageHeight!="")
    {
        var aImg = htmlObj.getElementsByTagName("IMG");
        if (aImg!=null){
            for (var n=0; n<aImg.length; n++){
                if (aImg[n].id.replace("NSNews_img","")!=aImg[n].id) {
                    aImg[n].style.display="inline";                                      
                    if (!isNaN(parseInt(aImg[n].height)) && parseInt(aImg[n].height)>parseInt(imageHeight)){
                        aImg[n].style.height = imageHeight+"px";
                    }
                    if (!isNaN(parseInt(aImg[n].width)) && parseInt(aImg[n].width)>parseInt(imageWidth)){
                        aImg[n].style.height = "";
                        aImg[n].style.width = imageWidth+"px";
                    }
                }
            }
        }
    }               
}
