/*
 *    Copyright (c) 2010 Just Another Video script
 *
 *    This file is part of Just Another Video script which is based on vidiscript.
 *    Come to website for support or buying additional plugins/modules.
 */
var loaded = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
var contents = new Array('', '', '', '', '', '', '', '', '');
var played = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
loadedcount = 0;
maxload = 5; //maximum number of flash thumbnails on the page at any time; warning! not all browsers will load more than 5 simultaneously
function replace_thumb(index, html){
    if (played[index] < 1) {
        document.getElementById("thumb" + index).innerHTML = html;
        played[index]++;
    }
}
//this function keeps a list of loaded video thumbnails and never lets the number currently loaded above 'maxload'
function start_thumb(index, thumbname){
    //increase count of loaded swfs
    loadedcount++;
    //store the contents of this div for later
    this_contents = document.getElementById("thumb" + index).innerHTML;
    //start the video thumbnail
    var so = new SWFObject(thumbname + ".prv", "sotester", "96", "96", "9", "#8080a0");
    so.addParam("loop", "false");
    so.addParam("wmode", "transparent");
    so.addParam("allowScriptAccess", "true");
    so.addParam("cursor", "hand");
    so.write("thumb" + index);
    played[index] = 0;
    //check if we reached the max limit
    if (loadedcount > maxload) {
        //check the one we are about to put back as it was, is not the same one we just clicked       
        if (index != loaded[maxload - 1]) {
            //put the contents of the oldest div back as it was
            document.getElementById("thumb" + loaded[maxload - 1]).innerHTML = contents[maxload - 1];
        }
        //don't let the count go over the limit 
        loadedcount = maxload;
    }
    //shift all the indices and div contents up one
    for (x = loadedcount - 1; x >= 0; x--) {
        loaded[x + 1] = loaded[x];
        contents[x + 1] = contents[x];
    }
    //put this div in the 0 position
    loaded[0] = index;
    //now check this div has not already has it's original contents stored (or we might store a playing video which would be bad)
    found = -1;
    for (x = 1; x < loadedcount; x++) {
        if (loaded[x] == index) {
            found = x;
        }
    }
    if (found < 0) {
        contents[0] = this_contents;
    }
    else {
        contents[0] = contents[found];
    }
    
}