var countThumbs = myImages.length;
var offsetThumb = 0;
var offsetImg = 0;


if(countThumbs>6){countThumbs=6;}

function PreloadImages(a){
    var d = document;
    d.imgs = new Array();
    k = 0;
    for (var i = 0; i < a.length; i++){
        d.imgs[k] = new Image;
        d.imgs[k].src = thumbsDir + a[i];
        d.imgs[(k+1)] = new Image;
        d.imgs[(k+1)].src = imagesDir + a[i];
        k += 2;
    }
}

function goodIndex(idx){
    if(idx >= 0){
       idx = idx % myImages.length;
    }else{
       idx = Math.abs(idx % myImages.length);
       if(idx == 0) idx = 13;
       idx = myImages.length - idx;
    }
    return idx;
}


function thumbScroll(c){
    offsetThumb += c;
    for(i=0; i<countThumbs; i++){
        ind = goodIndex(i+offsetThumb);
        img = document.getElementById("thumb_" + i + "_");
        aimg = document.getElementById("aimg_" + i + "_");
        img.src = thumbsDir + myImages[ind];
    }
}


function imgScroll(c){
    offsetImg += c;

    if(offsetImg == 0){
        imgL = document.getElementById("img_l");
        imgL.innerHTML = "&nbsp;";
    }else{
        imgL = document.getElementById("img_l");
        imgL.innerHTML = "<a href=\"javascript:void(0);\" onClick=\"imgScroll(-1); thumbScroll(-1);\"><img src=\"i/scroll_rwnd.gif\" hspace=\"5\" alt=\"Назад\" border=\"0\" /></a>";
    }
    if(offsetImg == myImages.length-1){
        imgR = document.getElementById("img_r");
        imgR.innerHTML = "&nbsp;";
    }else{
        imgR = document.getElementById("img_r");
        imgR.innerHTML = "<a href=\"javascript:void(0);\" onClick=\"imgScroll(1); thumbScroll(1);\"><img src=\"i/scroll_fwd.gif\" hspace=\"5\" border=\"0\" alt=\"вперёд\"/></a>";
    }
    document.getElementById('bigImg').src= imagesDir + myImages[offsetImg];

    ee=document.getElementById('elem_desc');
    ee.innerHTML=myDescs[offsetImg];
}
//thumbScroll
//imgScroll

function switchImg(i){
    ind = goodIndex(offsetThumb + i);
    document.getElementById('bigImg').src= imagesDir + myImages[ind];

    ee=document.getElementById('elem_desc');
    ee.innerHTML=myDescs[ind];
    offsetImg = ind;
    imgScroll(0);
}

function switchImg(i){
    ind = goodIndex(offsetThumb + i);
    document.getElementById('bigImg').src = imagesDir + myImages[ind] + '?rnd=' + Math.random();

    ee=document.getElementById('elem_desc');
    ee.innerHTML=myDescs[ind];
    offsetImg = ind;
    imgScroll(0);
}

function showTable(){
	PreloadImages(myImages);
    textHtml = "<div class=\"rndbx grndbx\"><b class=\"trb\"><b></b><i></i><u></u></b><div class=\"rndbxC\"><div style=\"width: 100%;\">";  
                   
    textHtml = textHtml + "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"left\" id=\"img_l\"><a href=\"javascript:void(0);\" onClick=\"imgScroll(-1); thumbScroll(-1);\"><img src=\"i/scroll_rwnd.gif\" hspace=\"5\" alt=\"Назад\"/></a></td>";

    width_b = Math.round(100/countThumbs);
    for(i=0; i<countThumbs; i++){
        textHtml = textHtml + "<td width=\"" + width_b + "%\">";
        textHtml = textHtml + "<div align=\"center\" style=\"height:75px; margin-left:3px; margin-right:3px; overflow:hidden;\"><a href=\"javascript:void(0);\" id=\"aimg_" + i + "_\" onclick=\"switchImg(" + i + ");\"><img id=\"thumb_" + i + "_\" src=\"" + thumbsDir + myImages[i] + "\" style=\"border:1px #777777 solid;\" onMouseOver=\"this.style.border='1px #000000 solid;'\"  onMouseOut=\"this.style.border='1px #777777 solid;'\"  border=\"0\"></a></div>";
        textHtml = textHtml + "</td>";
    }
    

    textHtml = textHtml + " <td align=\"left\" id=\"img_r\"><a href=\"javascript:void(0);\" onClick=\"imgScroll(1); thumbScroll(1);\"><img src=\"i/scroll_fwd.gif\" hspace=\"5\" alt=\"вперёд\"/></a></td>";
    textHtml = textHtml + "</tr>";
    textHtml = textHtml + "</table></div></div><b class=\"brb\"><u></u><i></i><b></b></b></div>";
    //textHtml = textHtml + "<tr>";
    //textHtml = textHtml + "<td style=\"padding:10;\" colspan=\"" + countThumbs + "\" align=\"center\" bgcolor=\"#FFFFFF\">";
    textHtml = textHtml + "<br><br><center><div align=\"center\" style=\"width:650px;\"><img name=\"bigImg\" id=\"bigImg\" src=\"" + imagesDir + myImages[0] + "\" style=\"border:1px #000000 solid; max-width:600px;\"></div></center>";
    //textHtml = textHtml + "</td>";
    //textHtml = textHtml + "</tr>";
   // textHtml = textHtml + "<tr>";
   // textHtml = textHtml + "<td colspan=2>";
    textHtml = textHtml + "<div id=\"elem_desc\" style=\"font-weight: bold;\" align=\"center\"></div>";
  //  textHtml = textHtml + "</td>";
   // textHtml = textHtml + "</tr>";
   // textHtml = textHtml + "</table>";

    document.write(textHtml);
    imgScroll(0);
}
