﻿
        var currGal = 0;//was 0 b4 changed to one to selcet an editable gal
        var currImg = 0;
        var stopSS = false;
        var ssRunning = false;

        //var gThumbW = 200;
        var gScreenW = document.documentElement.clientWidth;
        var gLostW = 234;
      
      function gallSelect(galleryId,e) {
      
    
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE

         else
              key = e.which;     //firefox

    
        if (key == 13)
        {
          gallerySelect(galleryId);
        }
      
      
      }
        var galImages;   
        var y; 
        function gallerySelect(galleryId) {
     
       //clear the pic td -------------------------------------------------------------------------------------------------------
           var tdPic=document.getElementById('tdPic');
           var tdBody=document.getElementById('tdBody');
           var tblBody=document.getElementById('tblBody');
           var tblPic= document.getElementById('tblPic');
           tdPic.innerHTML='';
           tblPic.style.display='none';
           tblBody.style.display='block';
           //alert("here 1");
           var nav = document.getElementById('tdNav');
           nav.innerHTML='';
       //-----------------------------------------------------------------------------------------------------------------------
             y=0;
             var x=0;  
            galImages=new Array();   
            var contTtl = document.getElementById('tdTitle');
            var contBdy = document.getElementById('tdBody');
            
            var tblHtml = '';
           
             
            stopSS = true;

            var html;
            for(i=0;i<galCount;i++) {
                if(gals[i][0]==galleryId) {
                    html = gals[i][1] + ' gallery';
                    html += '&nbsp;&nbsp;<a href="javascript:startSlideShow(' + gals[i][0] + ');" class="slideshowlink" onmouseover="document.getElementById(\'img' + gals[i][0] + '\').src=\'images/icons/icn-slideshow_over.jpg\'" onmouseout="document.getElementById(\'img' + gals[i][0] + '\').src=\'images/icons/icn-slideshow_out.jpg\'" onfocus="document.getElementById(\'img' + gals[i][0] + '\').src=\'images/icons/icn-slideshow_over.jpg\'" onblur="document.getElementById(\'img' + gals[i][0] + '\').src=\'images/icons/icn-slideshow_out.jpg\'"><img id="img' + gals[i][0] + '" alt="View ' + gals[i][1] + ' Slideshow" src="images/icons/icn-slideshow_out.jpg" ></a>';
                }
            }
            contTtl.innerHTML = html;
            
            //calculate the number of thumbnails to show per row
            //var imgPerRow = parseInt((gScreenW-gLostW)/(MaxW+25));

            //var imgPerRow = parseInt((974- gLostW) / (MaxW +25));
              var  imgPerRow = 5;
            for(i=0;i<imgCount;i++) {
                if(imgs[i][1]==galleryId) {
                    
                         galImages[y] = new Array();
                         galImages[y][0]=imgs[i][0];
                         galImages[y][1]=imgs[i][1];
                         galImages[y][2]=imgs[i][2];
                         galImages[y][3]=imgs[i][3];
                         galImages[y][4]=imgs[i][4];
                         galImages[y][5]=imgs[i][5];
                         galImages[y][6]=imgs[i][6];
                    
                    var cellH = imgs[i][6];
                    if(MaxH > cellH) {
                        cellH = MaxH;
                    }
                   
                    var LongName = new String(imgs[i][2]);
                    var ShortName = new String(LongName);
                    if(LongName.length>=20) {
                        ShortName = LongName.substr(0,9) + '...';
                    }
                    //alert(x);
                   
                    if(x < imgPerRow){ 
                        
                        tblHtml += '<td><table cellpadding="0" cellspacing="0" class="RemoveLineBreak" border="0">';
                        tblHtml += '<tr><td height="' + cellH + '" valign="top" align="center"><a href="javascript:imageSelect(' + imgs[i][0] + ');" title="' + LongName + '">';
                        tblHtml += '<img border="0" src="' + imgPath + 'small/' + imgs[i][4] + '" alt="' + LongName + '" width="100" height="100"></a></td></tr>';
                        tblHtml += '<tr><td class="NormalText" align="center"><a href="javascript:imageSelect(' + imgs[i][0] + ');" title="' + LongName + '" class="NormalText">' + ShortName + '</a></td></tr>';
                        tblHtml += '</table></td><td width="10px"></td>';
                        
/*
                        tblHtml += '<table cellpadding="0" cellspacing="0" style="display:inline;">';
                        tblHtml += '<tr><td height="' + cellH + '" valign="top"><a href="javascript:imageSelect(' + imgs[i][0] + ');" title="' + LongName + '">';
                        tblHtml += '<img border="0" src="' + imgPath + imgs[i][4] + '" alt="' + LongName + '" width="' + imgs[i][5] + '" height="' + imgs[i][6] + '"></a></td></tr>';
                        tblHtml += '<tr><td class="NormalText" align="center"><a href="javascript:imageSelect(' + imgs[i][0] + ');" title="' + LongName + '" class="NormalText">' + ShortName + '</a></td></tr>';
                        tblHtml += '</table>';

                        tblHtml += '<table style="display:inline;"><tr><td>hisdfsdefwerw</td></tr></table>';  
*/                     
                         x=x+1;

                    }
                    
                    if (x==imgPerRow)
                    {
                        tblHtml='<tr>' + tblHtml + '</tr>'
                        x=0;
                    }
                    
                    y=y+1;
                }//end if 
            }//end for
            y=y-1;
            tblHtml='<table cellspacing="4" cellpadding="3" width="50">' + tblHtml + '</table>'

            contBdy.innerHTML = tblHtml;
            currGal = galleryId;
            
        }

        function iwh(i)
        {
            x=new Image;
            x.src=i;

            iw=x.width;
            ih=x.height;
            alert("Image width is: "+iw+" pixels, and image height is: "+ih+" pixels.");
        }

        
        function imageSelect(imageId) {
           var contTtl = document.getElementById('tdTitle');
           var contBdy = document.getElementById('tdPic');
           var nav = document.getElementById('tdNav');
           var tdBody=document.getElementById('tdBody');
           var tblBody=document.getElementById('tblBody');
           var tblPic=document.getElementById('tblPic');
           var tblHtml = '';
        
        //clear tdBody and show tdPic
          tdBody.innerHTML='';
          //tdBody.style.display='none';
          tblBody.style.display='none';
          tblPic.style.display='block';
          //contBdy.style.display='block';
          
        
            for(i=0;i<imgCount;i++) {
                if(imgs[i][0]==imageId) {
                    var ttlTbl = '<table cellpadding="0" cellspacing="0" width="50%"><tr>';
                    ttlTbl += '<td class="TileHead">' + imgs[i][2] + '</td>';
                    ttlTbl += '<td align="right" style="padding-right:5px;"><a class="headerlink" href="javascript:stopSS=true;gallerySelect(' + currGal + ');"><img alt="Close Gallery Slideshow" src="images/icons/icn-closegallery_out.jpg" onmouseover="this.src=\'images/icons/icn-closegallery_over.jpg\'" onmouseout="this.src=\'images/icons/icn-closegallery_out.jpg\'"></a></td>';
                    ttlTbl += '</tr></table>'
                    contTtl.innerHTML = ttlTbl;
                    
                    contBdy.innerHTML = '<img name="imgMain" id="imgMain" src="' + imgPath + 'large/' + imgs[i][4] + '" alt="' + imgs[i][2] + '">'
                    contBdy.innerHTML += '<br>' + imgs[i][3];
                
                }
            }
          
        
          
             for (i=0;i<y;i++){
              
               
                     if (galImages[i][0]==imageId) 
                     {
                        //alert(imageId);  
                       break;
                     }
              } 
                    
            nav.innerHTML='';
           if (!ssRunning) { //only write if not slide show
                if (i > 0 ){
                
                    nav.innerHTML= '<a href="javascript:imageSelect(' + galImages[i-1][0] + ');"><b><img src="images/leftarrow.jpg" alt="View previous image"/></b></a>'
                
                }
                nav.innerHTML += "<span style=\"padding-bottom:5px\" valign=\"top\">  image " + (i+1) + " of " + (y+1) + "</span>"
                if (i<y && typeof(galImages[i][0]) !='undefined'){
                
                 nav.innerHTML += '&nbsp;&nbsp;<a href="javascript:imageSelect(' + galImages[i+1][0] + ');"><b><img src="images/rightarrow.jpg" alt="View next image"/></b></a>'
                
                }
            }
            
            //resize image to fit window
            var optimumW =550 //document.body.clientWidth - 450;
            var optimumH = 550//document.body.clientHeight - 250;
            var img=new Image();
            img = document.getElementById("imgMain");
            //alert(img.offsetWidth);
          
            var bNum=bName();
           
            if (bNum==1)//browser Detection-IE
           
            {
            
                if(img.width > optimumW && (img.width > img.height)) {
                   img.width = optimumW;
                 }
                else if(img.height > optimumH && (img.height > img.width)) {
                   img.height = optimumH;
                }
            }
            else { //fire fox
             if(img.width > optimumW && (img.width > img.height)) {
                   img.width = optimumW;
                 }
                else if(img.height > optimumH && (img.height > img.width)) {
                   img.height = optimumH;
                }
            //alert(img.height);
            }
           
            
        }//end of function
        
        function startSlideShow(galleryId) {
            if(!ssRunning) {
                currGal = galleryId;
                currImg = 0;
                
                stopSS = false;
                ssRunning = true;
                slideShow();
            }
            else {
                stopSS = true;
            }
        }
        
        function slideShow() {
            if(!stopSS) {

                for(i=0;i<galCount;i++) {
                    if(gals[i][0]==currGal) {
                        galArrId = i;
                    }
                }


                var theImgId = -1;
            
                if(currImg==imgCount) {
                    currImg = 0;
                }
               
                theImgId = getImageId(currGal, currImg);//gets the index of the first image that belongs to that gallery
                if(theImgId==-1) {
                    currImg = 0;
                    theImgId = getImageId(currGal, currImg);
                }
                currImg = theImgId + 1;//setting the starting value of for loop in getImageId function  for next Iteration
             
                if (theImgId==-1) //if there is no image found for the selected gallery
                {
                    stopSS = false;
                    ssRunning = false;
                    return;
                }
             
                var theId = imgs[theImgId][0];
               
                imageSelect(theId);
           
                window.setTimeout('slideShow()',gals[galArrId][2]);
            }
            else {
               
                ssRunning = false;
            }
        }
        
        function getImageId(galleryId, currentImg) {
            var theImgId = -1;
            
            for(i=currentImg;i<imgCount;i++) {
                if(imgs[i][1]==galleryId && theImgId==-1) {
                    theImgId = i;
                }
            }
            
            return theImgId;
        }
        
        function editGallery() {
                if (currGal==0){currGal=1;}//by default edit set for gallery 1
            window.location.href='ContentMan/GalleryEdit.aspx?ID=' + currGal;
        }
     
//     function getImageWidth(myImage) {
//	    var x, obj;
//	    if (document.layers) {
//		    var img = getImage(myImage);
//		    return img.width;
//	    } else {
//		    return getElementWidth(myImage);
//	    }
//	    return -1;
//    }

//    function getImageHeight(myImage) {
//	    var y, obj;
//	    if (document.layers) {
//		    var img = getImage(myImage);
//		    return img.height;
//	    } else {
//		    return getElementHeight(myImage);
//	    }
//	    return -1;
//    }

function bName() {
  // return 1 for Internet Explorer
  if (navigator.appName == "Microsoft Internet Explorer")
    return 1;

  // return 2 for Navigator
  if (navigator.appName == "Netscape")
    return 2;

  // return 0 for other browsers
  return 0;
}
