var g_nStartIdx = 0;
var g_aoThumbs = null;
var g_nCurThumbIdx = 0; // Index within the thumb array
function GetThumbs(){
if(g_aoThumbs == null){	// Build the array
g_aoThumbs = new Array();
var oAnchor = GetFirstChild(g_divThumbs);
while(oAnchor != null){
var oThumb = GetFirstChild(oAnchor);
if(oThumb != null){
g_aoThumbs.push(oThumb);}
oAnchor = GetNextSibling(oAnchor);}}
return g_aoThumbs;}
function GetIdxByThumb(oCurThumb){
var nCurIdx = 0;
var aoThumbs = GetThumbs();
for(var nIdx = 0; nIdx < aoThumbs.length; nIdx++){
var oThumb = aoThumbs[nIdx];
if(oThumb.id == oCurThumb.id){
nCurIdx = nIdx;
break;}}
return nCurIdx;}
function GetThumbByIdx(nIdx){
var oThumb = null;
var aoThumbs = GetThumbs();
if(nIdx < aoThumbs.length){
oThumb = aoThumbs[nIdx];}
return oThumb;}
function ChooseImage(strThumbId){
var oThumb = Xtt.Get(strThumbId);
ChooseImageByThumb(oThumb);}
function ChooseImageByThumb(oThumb){
var nIdx = GetIdxByThumb(oThumb);
ChooseImageByIdx(nIdx);}
function ChooseImageByIdx(nNewThumbIdx){
var oPicItem = g_aoPicGalItems[g_nStartIdx+nNewThumbIdx];
g_wimgImage.src = oPicItem.Url;
g_divCation.innerHTML = oPicItem.Caption;
g_nCurThumbIdx = nNewThumbIdx;
if(g_nStartIdx == 0){	// We are at the beginning.
g_wlinkGoLeft.className = "NoMorePicture";}
else{	// We still can go left.
g_wlinkGoLeft.className = "";}
if(g_nStartIdx+4 >= g_aoPicGalItems.length){	// The last picture of the gallery is visible
g_wlinkGoRight.className = "NoMorePicture";}
else{	// We still can go right.
g_wlinkGoRight.className = "";}}
function MoveToLeftPic(){
if(g_nCurThumbIdx > 0){
ChooseImageByIdx(g_nCurThumbIdx-1);}
else{
SetImages(g_nStartIdx-1);}}
function MoveToRightPic(){
if(g_nCurThumbIdx < 3){
var aoThumbs = GetThumbs();
if(g_nCurThumbIdx < aoThumbs.length-1){
ChooseImageByIdx(g_nCurThumbIdx+1);}}
else{
SetImages(g_nStartIdx+1);}}
function SetImages(nStartIdx){
if(nStartIdx >= 0 && nStartIdx <= g_aoPicGalItems.length-4){
var aoThumbs = GetThumbs();
var nUpperBound = Math.min(g_aoPicGalItems.length, nStartIdx+4)-1;
var nAnchorIdx = 0;
for(var nIdx = nStartIdx; nIdx <= nUpperBound && nAnchorIdx < aoThumbs.length; nIdx++){
var oItem = g_aoPicGalItems[nIdx];
var oThumb = aoThumbs[nAnchorIdx];
oThumb.src = oItem.ThumbUrl;
oThumb.alt = oItem.Caption;
nAnchorIdx++;}
g_nStartIdx = nStartIdx;
ChooseImageByIdx(g_nCurThumbIdx);}}

