var g_ajax_queue = [],
    g_ajax = false,
    g_ajax_busy = false,
    g_template_video,
    g_template_thumbs,
    g_oldom,
    g_nvideos,
    g_ids    = [],
    g_videos = {};

function init(video_id, nvideos, ids)
{
    g_nvideos = nvideos;
    for (var i=0; i < ids.length; ++i) g_ids[i] = ids[i];
    g_template_video  = TrimPath.parseDOMTemplate('t_video');
    g_template_thumbs = TrimPath.parseDOMTemplate('t_thumbs');
    ajax_init();
    if (g_nvideos)
        display_thumbs(video_id ? function() {} : function() {
              display_video(g_ids[0]);
        });
}
function shuffle()
{
    for (var j, x, i = g_ids.length; i;
         j = parseInt(Math.random() * i), x = g_ids[--i], g_ids[i] = g_ids[j], g_ids[j] = x);
    display_thumbs(function() { display_video(g_ids[0]); });
}
function display_thumbs(process)
{
    var ids = new Array();
    for (var i = 0 ; i < g_ids.length && i < g_nvideos ; ++i)
        ids[i] = g_ids[i];

    fetch_videos(ids, function() {
        document.getElementById('images').innerHTML =
            g_template_thumbs.process(
                { ids: ids,
                  videos: g_videos
                });
        process();
    });
}
function display_video(id)
{
    fetch_videos( [ id ], function() {
        document.getElementById('player').innerHTML = g_template_video.process({v: g_videos[id]});
    });
}
function fetch_videos(ids, process)
{
    var fids = [];
    for (var i = 0 ; i < ids.length ; ++i)
        if (!g_videos[ids[i]])
            fids.push(ids[i]);

    if (fids.length) 
        ajax("/ajax?ids=" + fids.join(','),
             function(data) {
                for (var j = 0 ; j < data.length ; ++j)
                    g_videos[ data[j].id ] = data[j];
                process();
             });
    else
        process();
}
function display(w,d)
{
    if (g_oldom)
        g_oldom.style.display = 'none';
    var dom = document.getElementById(w);
    dom.style.display = d;
    document.getElementById('hautlayer').style.display = d;
    g_oldom = dom;
    if (document.documentElement && document.documentElement.scrollTop)
        document.documentElement.scrollTop = 0;
    else if (document.body) 
        document.body.scrollTop = 0;
}
function ajax_init()
{
                try { g_ajax = new XMLHttpRequest(); }
    catch (e) { try { g_ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) { try { g_ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) { g_ajax = null; }}}
    return g_ajax != null;
}
function ajax(uri, process)
{
    g_ajax_queue.push( { uri: uri, process: process} );
    ajax_check();
}
function ajax_check()
{
    if (!g_ajax_busy && g_ajax_queue.length)
        ajax_send(g_ajax_queue.shift());
}
function ajax_send(req)
{
    g_ajax_busy = true;
    g_ajax.open('GET', req.uri, true);
    g_ajax.onreadystatechange = function() {
      try {
        if (g_ajax.readyState == 4) {
            if (g_ajax.status == 200)
                req.process(eval('(' + g_ajax.responseText + ')'));
            g_ajax_busy = false;
            if (g_ajax_queue.length)
                setTimeout(ajax_check, 1);
        }
      }
      catch(e) {
        g_ajax_busy = false;
        if (g_ajax_queue.length)
            setTimeout(ajax_check, 1);
      }
    };
    g_ajax.send(null);
}
