﻿function updateNewsItems()
{
    var html = '';
    var nn = NEWS.news; //JSON object
    var count;
    
    var title;
    var article;
    var teaser;
    var readmore;
    var thumbnail;
    var date;
    
    var found;
    var character;
    var inc;
    
    switch(VIEW)    // VIEW = page
    {
        case 'HOME':
            count = nn.length;
            
            for (var i=0; i<count; i++)
            {
                title = nn[i].title;
                article = nn[i].article;
                thumbnail = nn[i].thumbnail;
                
                found = false;
                inc = 100;
                
                // get the next space after the 100th character
                while (!found)
                {
                    character = article.substr(inc, 1);
                    
                    if (character == ' ' || inc == 150)
                        break;
                        
                    inc += 1;
                }
                
                teaser = article.substr(0,inc) + '...';
                
                html += '<div class="news_teaser_container">';
                html += '<div class="left">';
                html += '<a href="news.aspx">';
                html += '<img class="news_teaser_thumbnail" border="0" src="images/news/' + thumbnail + '">';
                html += '</a>';
                html += '</div>';
                html += '<div class="left">';
                html += '<div class="news_teaser_title tal" ';
                html += 'onmouseover="dC(true);" ';
                html += 'onmouseout="dC(false);" ';
                html += 'onclick="window.location.href=\'news.aspx\'" ';
                html += '>';
                html += title;
                html += '</div>';
                html += '<div class="news_teaser_article tal">';
                html += teaser;
                html += '</div>';
                html += '</div>';
                html += '</div>';
            }
            break;
        case 'NEWS':
            count = nn.length;
            
            for (var i=0; i<count; i++)
            {
                title = nn[i].title;
                article = nn[i].article;
                teaser = article.substr(0,article.indexOf('<br>'));
//                readmore = article.substr(article.indexOf('<br>')+4, article.length-article.indexOf('<br>')+1);
                date = nn[i].date;
                thumbnail = nn[i].thumbnail;

                //html += '<div class="left">';
                if (thumbnail != '')
                    html += '<img class="news_thumbnail left" border="0" src="images/news/' + thumbnail + '">';
                else
                    html += '<div class="news_thumbnail_spacer left"></div>';
                //html += '</div>';
                html += '<div class="news_container left">';
                html += '<div class="news_title tal" ';
                html += 'onmouseover="dC(true);" ';
                html += 'onmouseout="dC(false);" ';
                html += 'onclick="" ';
                html += '>';
                html += title;
                html += ' - ';
                html += date;
                html += '</div>';
                html += '<div class="news_article tal">';
                html += teaser;
                html += '</div>';
//                html += '<div id="news' + i + '" class="tal news_article hide">';
//                html += readmore
//                html += '</div>';
                html += '<a class="readmore left" href="javascript:showFullNews(' + i + ')">read more</a>';
                html += '</div>';
                html += '<div class="clearL"></div>';
            }
            break;
    }
    
    wH('news', html);
}

function showFullNews(index)
{
    var html = '';
    var nn = NEWS.news;
    var title;
    var article;
    var date;
    var w, h;
    var t, l;
    var fullnews = gE('fullnews');

    title = nn[index].title;
    article = nn[index].article;
    date = nn[index].date;

    // build html
    html += '<a id="fullnews_print" class="fullnews_print abs" href="javascript:printNewsArticle();">';
    html += '<img border="0" src="images/printer.gif">';
    html += '</a>';
    
    html += '<img id="fullnews_close" '
    html += 'class="abs" ';
    html += 'src="images/close-button_67.gif" ';
    html += 'onmouseover="dC(true);" ';
    html += 'onmouseout="dC(false);" ';
    html += 'onclick="hideFullNews();" />';
    
    html += '<div class="fullnews_container">';
    html += '<div id="fullnews_title" class="fullnews_title tal">';
    html += title;
    html += ' - ';
    html += date;
    html += '</div>';
    html += '<div id="fullnews_article" class="fullnews_article tal ofA">';
    html += article;
    html += '</div>';
    html += '</div>';
    
    // write html
    wH('fullnews', html);
    
    // size div
    w = 560;
    h = 400;
    
    sW(fullnews, w);
    sH(fullnews, h);
    
    // position div
    t = ((gWinH()-h)/2)+gST();
    l = (gWinW()-w)/2;
    
    sT(fullnews, Math.round(t));
    sL(fullnews, Math.round(l));
    
    // show div
    show(fullnews);
}

function hideFullNews()
{
    var fullnews = gE('fullnews');
    
    // hide div
    hide(fullnews);
    
    // clear contents
    wH('fullnews', '');
}
