//
// utils for displaying video list; using movies.css
//
function htmlMovieThumb(movie, index) {

    var sLastWatchedCookie = $_COOKIES('v_' + movie.id);
    var sLastWatchedDateTime = '';
    if( sLastWatchedCookie!=null && sLastWatchedCookie!='' ) {
        var arFields = sLastWatchedCookie.split('|');
        var dtLastWatched = new Date();
        dtLastWatched.setTime(arFields[3]);
        sLastWatchedDateTime = VIET_vnl_util_ToDateString(dtLastWatched, lang, 'xs');
    }

    var s = '<table class="movieItem" cellpadding="0" cellspacing="0" border="0">'
        +   '<tr><td class="movieItemIndex">' + index + '. </td>'
        +       '<td class="movieItemImg"><a class="movieTitle" href="javascript:playVideo(' + movie.id + ');"><img class="movieThumb' + (sLastWatchedCookie!=null && sLastWatchedCookie!='' ? '_visited' : '') + '" src="' + movie.image_link + '" title="Click to play: ' + movie.title + '" width="80" height="110" /></a></td>'
        +   '</tr>'
        +   '<tr><td></td>'
        +       '<td class="movieItemCnt"><div class="movieTitle"><a class="movieTitle" href="javascript:playVideo(' + movie.id + ');">' + movie.title + '</a></div>'
        +           '<div class="movieInfo">'
        +           '(' + movie.clips_count + ' ' + (lang=='vn' ? 'đoạn' : 'clips') + ')<br />'
        +           (sLastWatchedDateTime!='' ? (lang=='vn' ? 'Bạn đã xem' : 'Last watched') + ': ' + sLastWatchedDateTime: '')
        +           '</div>'
        +       '</td>'
        +   '</tr></table>';
    return s;
}

function htmlMovieList(arMovies, iFirstIndex) {

    if( iFirstIndex==null )
        iFirstIndex = 1;

    var colNum = 4;
    var s = '<table class="movieList" cellpadding="0" cellspacing="0" border="0">';
    for( var i=0; i<arMovies.length; i++ ) {

        var isFirstCol = (i%colNum==0);
        var isLastCol = (i%colNum==(colNum-1));
        var rowIndex = Math.floor(i/colNum);
        var isAltRow = (rowIndex%2==1);

        if( isFirstCol )
            s += '<tr' + ( isAltRow ? ' class="movieBgAlt"' : '') +'>';

        var movie = arMovies[i];
        if( movie['image_link'].indexOf('http:')<0 )
            movie['image_link'] = CrossSite_url + movie['image_link'];
        s += '<td class="movieItem">' + htmlMovieThumb(movie, iFirstIndex+i) +  '</td>';

        if( isLastCol )
            s += '</tr>';
    }

    if( arMovies.length%colNum!=0 ){  // empty cells in last rows
        for( var i=parseInt(arMovies.length%colNum); i<colNum; i++ ) {
            s += '<td class="movieItem"></td>';
        }
        s += '</tr>';
    }
    s += '</table>';

    return s;
}