//-----------------------------------------------------------------
function PhotoGallery_ResizeImage(img) {
    var maxsize = img.getAttribute("maxsize");
    
    var newimg=new Image();
    newimg.src=img.src;

    var height=maxsize;
    var width=maxsize;

    img.width=width;
    img.height=height;

    if(img.style.visible!='hidden')
    {
        if (parseInt(newimg.height) >= parseInt(newimg.width) && parseInt(newimg.height) <= parseInt(maxsize))
        {
            height=newimg.height;
            width=null;
        }    
        else if(parseInt(newimg.height)>=parseInt(newimg.width))
        {
            width=null;
        }    
        else if(parseInt(newimg.width)>=parseInt(newimg.height)&&parseInt(newimg.width)<=parseInt(maxsize))
        {
            width=newimg.width;
            height=null;
        }    
        else if(parseInt(newimg.width)>=parseInt(newimg.height))
        {
            height=null;
        }    

        if(height==0||width==0)
        {
            img.height=maxsize;
            img.width=maxsize;
        }
        else
        {
            if(height==null)
                img.removeAttribute("height");
            else
                img.height=height;
                
            if(width==null)
                img.removeAttribute("width");
            else
                img.width=width;
        }
    }
}

//-----------------------------------------------------------------
function PhotoGallery()
{
    var AllImages=new Array();
    var ControlId=null;
    //var PhotoGalleryXML=null;
    var Columns=3;
    var Rows=3;
    var CurrentPageIndex=1;
    var CurrentImageIndex=-1;
    var PageCount=0;
    var ValidThumbnailCount=0;

    var Thumbnails;

    var PhotoWindow;
    var ThumbnailContainer;
    var PhotoContainer;
    var PhotoText;
    var Photo;
    var PhotoGalleryWidth;
    var PhotoGalleryThumbnailSize;
    var PhotoGallerySize;
    
    var locstr_pg_nextpage;
    var locstr_pg_previouspage;
    var locstr_pg_nextphoto;
    var locstr_pg_previousphoto;
    var locstr_pg_listview;
    var locstr_pg_selectphoto;
    var locstr_pg_openphoto;
    var locstr_pg_closephoto;

    //-----------------------------------------------------------------
    this.Init=_Init;
    function _Init(controlID,columns,rows)
    {
        ControlId=controlID;
        Columns=columns;
        Rows=rows;

        locstr_pg_nextpage = document.getElementById("locstr_pg_nextpage" + ControlId).value;
        locstr_pg_previouspage = document.getElementById("locstr_pg_previouspage" + ControlId).value;
        locstr_pg_nextphoto = document.getElementById("locstr_pg_nextphoto" + ControlId).value;
        locstr_pg_previousphoto = document.getElementById("locstr_pg_previousphoto" + ControlId).value;
        locstr_pg_listview = document.getElementById("locstr_pg_listview" + ControlId).value;
        locstr_pg_selectphoto = document.getElementById("locstr_pg_selectphoto" + ControlId).value;
        locstr_pg_openphoto = document.getElementById("locstr_pg_openphoto" + ControlId).value;
        locstr_pg_closephoto = document.getElementById("locstr_pg_closephoto" + ControlId).value;

        var PhotoGalleryMainTable = document.getElementById("PhotoGallery" + ControlId + "MainTable");
        PhotoGalleryWidth = PhotoGalleryMainTable.clientWidth-2;

        ThumbnailContainer = document.getElementById("PhotoGallery" + ControlId + "ThumbnailContainer");
        ThumbnailContainer.width = PhotoGalleryWidth + "px";
        ThumbnailContainer.height = PhotoGalleryWidth + "px";

        PhotoGalleryThumbnailSize = Math.floor(Number(PhotoGalleryWidth) / Number(Columns)) - 6 - 6;
        PhotoGallerySize = (Number(PhotoGalleryThumbnailSize) * Number(Columns)) + (Number(Columns) * 6) + (Number(Columns) * 6);

        var PhotoGalleryBody = document.getElementById("PhotoGallery" + ControlId + "BODY");
        PhotoGalleryBody.width = PhotoGallerySize + "px";
        PhotoGalleryBody.height = PhotoGallerySize + "px";
        PhotoGalleryBody.style.width = PhotoGallerySize + "px";
        PhotoGalleryBody.style.height = PhotoGallerySize + "px";

        PhotoContainer = document.getElementById("PhotoGallery" + ControlId + "PhotoContainer");
        PhotoContainer.width = PhotoGallerySize+"px";
        PhotoContainer.height = PhotoGallerySize + "px";
        PhotoContainer.style.width = PhotoGallerySize + "px";
        PhotoContainer.style.height = PhotoGallerySize + "px";

        Photo = document.getElementById("PhotoGallery" + ControlId + "Photo");
        Photo.width = Number(PhotoGallerySize - 6 - 6)+"px";
        Photo.height = Number(PhotoGallerySize - 6 - 6)+"px";
        Photo.setAttribute("size", Number(PhotoGallerySize - 6 - 6));
        Photo.setAttribute("maxsize", Number(PhotoGallerySize - 6 - 6));
        PhotoGallery_ResizeImage(Photo);

        ThumbnailCells = document.getElementsByName("PhotoGallery" + ControlId + "ThumbnailCell");
        for (var i = 0; i < ThumbnailCells.length; i++) {
            ThumbnailCells[i].height = PhotoGalleryThumbnailSize+"px";
            ThumbnailCells[i].width = PhotoGalleryThumbnailSize+"px";
            ThumbnailCells[i].style.height = PhotoGalleryThumbnailSize+"px";
            ThumbnailCells[i].style.width = PhotoGalleryThumbnailSize+"px";
        }
        
        Thumbnails=document.getElementsByName("PhotoGallery"+ControlId+"Thumbnail");
        for (var i = 0; i < Thumbnails.length; i++) {
            Thumbnails[i].height = PhotoGalleryThumbnailSize+"px";
            Thumbnails[i].width = PhotoGalleryThumbnailSize+"px";
            Thumbnails[i].setAttribute("size",PhotoGalleryThumbnailSize);
            Thumbnails[i].setAttribute("maxsize",PhotoGalleryThumbnailSize);
        }
        //alert(PhotoGalleryWidth + "\n" + PhotoGalleryThumbnailSize + "\n" + PhotoGallerySize);

        //PhotoGalleryXML = CrossBrowser_GetXML("xml/PhotoGalleryXml.aspx?controlid=" + ControlId, null);
        var PhotoAnchors = document.getElementsByName("PhotoAnchor_"+ControlId);
        //alert(PhotoAnchors.length);

        var imgs = PhotoAnchors;
        for(var i=0;i<imgs.length;i++)
        {
            AllImages[i]=new Object();
            AllImages[i].index = i;
            AllImages[i].title = imgs[i].getAttribute("title");
            AllImages[i].desc = imgs[i].getAttribute("desc");
            AllImages[i].linktext = imgs[i].getAttribute("linktext");
            AllImages[i].linkurl = imgs[i].getAttribute("linkurl");
            AllImages[i].thumbnail = imgs[i].getAttribute("thumbnail");
            AllImages[i].mediumurl=imgs[i].getAttribute("mediumurl");
            AllImages[i].largeurl=imgs[i].getAttribute("largeurl");
            
            if(AllImages[i].thumbnail=="") 
            {
                if (AllImages[i].mediumurl == "") {
                    AllImages[i].thumbnail = AllImages[i].largeurl;
                    //AllImages[i].thumbnail = "Thumbnail.aspx?ImagePath=" + AllImages[i].largeurl + "&maxHeight=" + PhotoGalleryThumbnailSize.toString() + "&maxWidth=" + PhotoGalleryThumbnailSize.toString();
                }
                else {
                    AllImages[i].thumbnail = AllImages[i].mediumurl;
                    //AllImages[i].thumbnail = "Thumbnail.aspx?ImagePath=" + AllImages[i].mediumurl + "&maxHeight=" + PhotoGalleryThumbnailSize.toString() + "&maxWidth=" + PhotoGalleryThumbnailSize.toString();
                }
            }
            
            if(AllImages[i].mediumurl=="") 
            {
                if (AllImages[i].largeurl == "") {
                    AllImages[i].mediumurl = AllImages[i].thumbnail;
                    //AllImages[i].mediumurl = "Thumbnail.aspx?ImagePath=" + AllImages[i].thumbnail + "&maxHeight=" + Number(PhotoGallerySize - 6 - 6).toString() + "&maxWidth=" + Number(PhotoGallerySize - 6 - 6).toString();
                }
                else {
                    AllImages[i].mediumurl = AllImages[i].largeurl;
                    //AllImages[i].mediumurl = "Thumbnail.aspx?ImagePath=" + AllImages[i].largeurl + "&maxHeight=" + Number(PhotoGallerySize - 6 - 6).toString() + "&maxWidth=" + Number(PhotoGallerySize - 6 - 6).toString();
                }
            }
            
            if(AllImages[i].largeurl=="") 
            {
                if (AllImages[i].mediumurl == "") {
                    AllImages[i].mediumurl = AllImages[i].thumbnail;
                }
                else {
                    AllImages[i].mediumurl = AllImages[i].mediumurl;
                }
            }
        }


        PhotoText=document.getElementById("PhotoGallery"+ControlId+"PhotoText");

        PageCount = Math.ceil(AllImages.length / (Columns * Rows));

        _RenderPagingControl(CurrentPageIndex);

        _RenderThumbnails(CurrentPageIndex);

        if (Rows == 1 && Columns == 1) {
            _NextImage();
        }
    }

    //-----------------------------------------------------------------
    this.RenderImagePagingControl = _RenderImagePagingControl;
    function _RenderImagePagingControl() {
        var pagingdiv = document.getElementById("PhotoGallery" + ControlId + "Control_ImagePaging");

        var pagingtable_start = '<table cellpadding="0" cellspacing="2" class="PagingControl"><tr>'

        var pagingtable_open = '';
        var pagingtable_close = '';
        var pagingtable_prev = '';
        var pagingtable_next = '';

        if ((CurrentPageIndex <= 1 && CurrentImageIndex <= 0))
            pagingtable_prev = '<td><span><div class="icon">&#9668;</div></span></td>';
        else
            pagingtable_prev = '<td><a href="javascript:PhotoGallery' + ControlId + '.PreviousImage();" title="' + locstr_pg_previousphoto + '"><div class="icon">&#9668;</div></a></td>';

        if ((CurrentPageIndex >= PageCount && Thumbnails.length >= ValidThumbnailCount && CurrentImageIndex >= (ValidThumbnailCount - 1)))
            pagingtable_next = '<td><span><div class="icon">&#9658;</div></span></td>';
        else
            pagingtable_next = '<td><a href="javascript:PhotoGallery' + ControlId + '.NextImage();" title="' + locstr_pg_nextphoto + '"><div class="icon">&#9658;</div></a></td>';
                        
        var pagingtable_end ='</tr></table>';

        pagingdiv.innerHTML = pagingtable_start + pagingtable_open + pagingtable_close + pagingtable_prev + pagingtable_next + pagingtable_end;

        var actionsdiv = document.getElementById("PhotoGallery" + ControlId + "Control_ImageActions");

        var actionstable_start = '<table cellpadding="0" cellspacing="2" class="PagingControl" style="text-align:right" align="right"><tr>'

        var actionstable_select = '';
        var actionstable_open = '';
        var actionstable_close = '';
        var actionstable_prev = '';
        var actionstable_next = '';

        if (PhotoContainer.style.display == '') {
            actionstable_select = '<td><span><div class="icon2">&#43;</div></span></td>';
            actionstable_open = '<td><a href="javascript:PhotoGallery' + ControlId + '.OpenImage();" title="' + locstr_pg_openphoto + '"><div class="icon">&#10063;</div></a></td>';
            actionstable_close = '<td><a href="javascript:PhotoGallery' + ControlId + '.CloseImage();" title="' + locstr_pg_closephoto + '"><div class="icon">&#9747;</div></a></td>';
        }
        else {
            actionstable_select = '<td><a href="javascript:PhotoGallery' + ControlId + '.SelectImage();" title="' + locstr_pg_selectphoto + '"><div class="icon2">&#43;</div></a></td>';
            actionstable_open = '<td><a href="javascript:PhotoGallery' + ControlId + '.OpenImage();" title="' + locstr_pg_openphoto + '"><div class="icon">&#10063;</div></a></td>';
            actionstable_close = '<td><span><div class="icon">&#9747;</div></span></td>';
        }
        var actionstable_end = '</tr></table>';

        actionsdiv.innerHTML = actionstable_start + actionstable_select + actionstable_open + actionstable_close + actionstable_prev + actionstable_next + actionstable_end;
        
    }

    //-----------------------------------------------------------------
    this.RenderPagingControl = _RenderPagingControl;
    function _RenderPagingControl(CurrentPageIndex) {
        PageCount = Math.ceil(AllImages.length / (Columns * Rows));
        
        var pagingdiv = document.getElementById("PhotoGallery" + ControlId + "Control_Paging");

        var PagesPerGroup = 10;
        if (PhotoGalleryWidth < 200)
            PagesPerGroup = 2;
        else if (PhotoGalleryWidth < 400)
            PagesPerGroup = 5;
        else
            PagesPerGroup = 10;
        
        var StartIndex = ((Math.ceil(Number(CurrentPageIndex) / Number(PagesPerGroup)) * Number(PagesPerGroup)) - Number(PagesPerGroup) + 1);
        var EndIndex = PageCount;
        if ((Number(StartIndex) + Number(PagesPerGroup) - 1) < Number(PageCount)) {
            EndIndex = Number(StartIndex) + Number(PagesPerGroup) - 1;
        }

        var pagingtable_start = '<table cellpadding="0" cellspacing="2" class="PagingControl"><tr>';

        var pagingtable_previous = '';

        if (CurrentPageIndex > 1)
            pagingtable_previous = '<td class="previous"><a href="javascript:PhotoGallery' + ControlId + '.PreviousPage();" title = "' + locstr_pg_previouspage + '"><div class="icon">&#9668;</div></a></td>';
        else
            pagingtable_previous = '<td class="previous"><span><div class="icon">&#9668;</div></span></td>';

        if (StartIndex > 1)
            pagingtable_previous = pagingtable_previous+'<td class="ellipse">&#8230;</td>';

        var pagingtable_pages = "";
        for (var i = StartIndex; i <= EndIndex; i++) {
            var cssClass = "";
            if (CurrentPageIndex == i) cssClass = "selected";
            pagingtable_pages = pagingtable_pages + '<td><a class="' + cssClass + '" href="javascript:PhotoGallery' + ControlId + '.ChangePage(' + i + ');"><div>' + i + '</div></a></td>';
        }

        var pagingtable_next = '';
        if (CurrentPageIndex < PageCount)
            pagingtable_next = '<td class="next"><a href="javascript:PhotoGallery' + ControlId + '.NextPage();" title = "' + locstr_pg_nextpage + '"><div class="icon">&#9658;</div></a></td>';
        else
            pagingtable_next = '<td class="next"><span><div class="icon">&#9658;</div></span></td>';

        if (EndIndex < PageCount)
            pagingtable_next = '<td class="ellipse">&#8230;</td>' + pagingtable_next;

        var pagingtable_end = '</tr></table>';

        pagingdiv.innerHTML = pagingtable_start + pagingtable_previous + pagingtable_pages + pagingtable_next + pagingtable_end;

        _RenderImagePagingControl();
    }
    
    //-----------------------------------------------------------------
    this.RenderThumbnails=_RenderThumbnails;
    function _RenderThumbnails(page)
    {
        CurrentImageIndex=0;
        CurrentPageIndex=page;

        PhotoText.innerHTML="";
        PhotoText.style.display='none';

        _UpdateControls();

        ValidThumbnailCount=0;
        var i=(parseInt(page)-1)*(Rows*Columns);
        for(var j=0;j<Thumbnails.length;j++)
        {
            
            Thumbnails[j].height=Thumbnails[j].getAttribute("size");
            Thumbnails[j].width = Thumbnails[j].getAttribute("size");
            Thumbnails[j].src = "img/spacer.gif"; //set them all to blank then reload them.
            Thumbnails[j].setAttribute("index", j);
            Thumbnails[j].setAttribute("controlid",ControlId);
            Thumbnails[j].setAttribute("CurrentPageIndex",CurrentPageIndex);
            Thumbnails[j].setAttribute("maxsize", Thumbnails[j].getAttribute("size"));
            Thumbnails[j].className = "PhotoGalleryThumbnail";

            if(AllImages[i]!=null)
            {
                ValidThumbnailCount++;
                Thumbnails[j].style.visible="visible";
                Thumbnails[j].disabled=false;
                Thumbnails[j].src = AllImages[i].thumbnail;
                //Thumbnails[j].setAttribute("uniqueindex", AllImages[i].index);
                Thumbnails[j].setAttribute("alt", AllImages[i].title);
                
                if (AllImages[i].title != null && AllImages[i].title != '' && AllImages[i].desc != null && AllImages[i].desc != '')
                    Thumbnails[j].setAttribute("title", AllImages[i].title + ": " + AllImages[i].desc);
                else if (AllImages[i].title != null && AllImages[i].title != '')
                    Thumbnails[j].setAttribute("title", AllImages[i].title);
                else
                    Thumbnails[j].setAttribute("title", AllImages[i].desc);

                Thumbnails[j].setAttribute("desc", AllImages[i].desc);
                Thumbnails[j].setAttribute("linktext", AllImages[i].linktext);
                Thumbnails[j].setAttribute("linkurl", AllImages[i].linkurl);
                Thumbnails[j].setAttribute("largeurl", AllImages[i].largeurl);
                Thumbnails[j].setAttribute("mediumurl",AllImages[i].mediumurl);
                Thumbnails[j].setAttribute("thumbnail",AllImages[i].thumbnail);
                Thumbnails[j].style.borderStyle="double";
                
                //window.setTimeout("preloadImage('"+AllImages[i].mediumurl+"');",1);
            }
            else
            {
                Thumbnails[j].disabled=true;
                Thumbnails[j].setAttribute("alt","");
                Thumbnails[j].setAttribute("title","");
                Thumbnails[j].setAttribute("caption","");
                Thumbnails[j].setAttribute("desc", "");
                Thumbnails[j].setAttribute("linktext", "");
                Thumbnails[j].setAttribute("linkurl", "");
                Thumbnails[j].removeAttribute("thumbnail");
                Thumbnails[j].removeAttribute("mediumurl");
                Thumbnails[j].removeAttribute("largeurl");
                Thumbnails[j].style.visible="hidden";
                Thumbnails[j].style.borderStyle="none";
            }
            PhotoGallery_ResizeImage(Thumbnails[j]); //Opera does not always fire the onload event so just fire it manually
            i++;

            if (CurrentImageIndex == j)
                _SelectThumbnail(Thumbnails[j]);
        }
    }
    
    //-----------------------------------------------------------------
    this.RenderFullImage=_RenderFullImage;
    function _RenderFullImage(img)
    {
        CurrentImageIndex=parseInt(img.getAttribute("index"));
        if(img.getAttribute("mediumurl")!=null)
        {
            Photo.width=Photo.getAttribute("size")+"px";
            Photo.height = Photo.getAttribute("size") + "px";
            Photo.src=img.getAttribute("mediumurl");
            Photo.style.display = 'inline-block';
            Photo.setAttribute("alt",img.getAttribute("alt"));
            Photo.setAttribute("title", img.getAttribute("title"));
            Photo.setAttribute("maxsize", Photo.getAttribute("size"));

            PhotoContainer.style.display = '';
            ThumbnailContainer.style.display = 'none';

            _SelectThumbnail(img);

            PhotoGallery_ResizeImage(Photo);//Opera does not always fire the onload event so just fire it manually
        }
        
        _UpdateControls();
    }
    
    //-----------------------------------------------------------------
    this.HideFullImage=_HideFullImage;
    function _HideFullImage()
    {
        PhotoContainer.style.display = 'none';
        ThumbnailContainer.style.display = 'inline-block';
        Photo.style.display = 'none';

        _UpdateControls();
    }
    
    //-----------------------------------------------------------------
    this.SelectThumbnail = _SelectThumbnail;
    function _SelectThumbnail(img) {
        CurrentImageIndex=img.getAttribute("index");
        var thumbnails = document.getElementsByName(img.name);
        for(var i=0;i<thumbnails.length;i++){
            thumbnails[i].className = "PhotoGalleryThumbnail";
        }
        
        img.className = "PhotoGalleryThumbnailSelected";
        
        PhotoText.innerHTML="";
        
        var title=document.createElement("h3");
        title.innerHTML=img.getAttribute("alt");
        if(title.innerHTML!="")
            PhotoText.appendChild(title);

        var desc = document.createElement("div");
        desc.innerHTML = img.getAttribute("desc");
        if (desc.innerHTML != "")
            PhotoText.appendChild(desc);

        if (img.getAttribute("linkurl") != "" && img.getAttribute("linktext") != "" && img.getAttribute("linkurl") != null && img.getAttribute("linktext") != null) {
            var linkdiv = document.createElement("div");
            var linkanchor = document.createElement("a");
            linkanchor.setAttribute("target", img.getAttribute("linktarget"));
            linkanchor.setAttribute("href", img.getAttribute("linkurl"));
            linkanchor.innerHTML = img.getAttribute("linktext");
            linkdiv.appendChild(linkanchor);
            PhotoText.appendChild(linkdiv);
        }

        if(PhotoText.innerHTML=="")
            PhotoText.style.display='none';
        else
            PhotoText.style.display = 'inline-block';

        _UpdateControls();
    }    

    //-----------------------------------------------------------------
    this.PreviousImage=_PreviousImage;
    function _PreviousImage() {
        if (CurrentImageIndex <= 0) {
            if (CurrentPageIndex > 1) {
                CurrentPageIndex--;
                _RenderThumbnails(CurrentPageIndex);
                CurrentImageIndex = Thumbnails.length - 1;
            }
            else
                CurrentImageIndex = 0;
        }
        else
            CurrentImageIndex--;

        if (PhotoContainer.style.display == 'none')
            _SelectThumbnail(Thumbnails[CurrentImageIndex]);
        else
            _RenderFullImage(Thumbnails[CurrentImageIndex]);
            
        _UpdateControls();
    }
    
    //-----------------------------------------------------------------
    this.NextImage=_NextImage;
    function _NextImage() {
        CurrentImageIndex++;
        if (CurrentImageIndex >= Thumbnails.length) {
            CurrentPageIndex++;
            _RenderThumbnails(CurrentPageIndex);
            //CurrentImageIndex++;
        }

        if (PhotoContainer.style.display == 'none')
            _SelectThumbnail(Thumbnails[CurrentImageIndex]);
        else
            _RenderFullImage(Thumbnails[CurrentImageIndex]);

        _UpdateControls();
    }

    //-----------------------------------------------------------------
    this.RenderImage = _RenderImage;
    function _RenderImage() {
        _RenderFullImage(Thumbnails[CurrentImageIndex]);
        _UpdateControls();
    }

    //-----------------------------------------------------------------
    this.SelectImage = _SelectImage;
    function _SelectImage() {

        _RenderFullImage(Thumbnails[CurrentImageIndex]);

        _UpdateControls();
    }
    
    //-----------------------------------------------------------------
    this.OpenImage=_OpenImage;
    function _OpenImage()
    {
        if(CurrentImageIndex<0) CurrentImageIndex++;

        _RenderFullImage(Thumbnails[CurrentImageIndex]);

        PhotoWindow=window.open("PhotoGalleryImageView.aspx?ControlId="+ControlId+"&url="+Thumbnails[CurrentImageIndex].getAttribute("largeurl"),"_new");
        _UpdateControls();
    }
    
    //-----------------------------------------------------------------
    this.CloseImage=_CloseImage;
    function _CloseImage() {
        try{if(PhotoWindow!=null)PhotoWindow.close();}catch(e){;}

        _HideFullImage();
        _UpdateControls();
    }

    //-----------------------------------------------------------------
    this.ChangePage = _ChangePage;
    function _ChangePage(page) {
        CurrentPageIndex = page;

        _RenderPagingControl(CurrentPageIndex);
        _RenderThumbnails(CurrentPageIndex);
        _HideFullImage();

        //CurrentImageIndex++;
        
        if (Columns == 1 && Rows == 1) {
            _RenderImage();
        }
    }

    //-----------------------------------------------------------------
    this.PreviousPage=_PreviousPage;
    function _PreviousPage()
    {
        CurrentPageIndex--;
        if (CurrentPageIndex <= 0)
            CurrentPageIndex = PageCount;

        _ChangePage(CurrentPageIndex);
    }

    //-----------------------------------------------------------------
    this.NextPage=_NextPage;
    function _NextPage()
    {
        CurrentPageIndex++;
        if (CurrentPageIndex > PageCount)
            CurrentPageIndex = 1;

        _ChangePage(CurrentPageIndex);
    }
    
    //-----------------------------------------------------------------
    this.UpdateControls=_UpdateControls;
    function _UpdateControls() {
        _RenderPagingControl(CurrentPageIndex);
        
        if(CurrentPageIndex>=PageCount&&CurrentImageIndex>=(ValidThumbnailCount-1))
            CurrentImageIndex=ValidThumbnailCount-1;
            
    }
}
