var firefox = document.getElementById&&!document.all;
var IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
var IE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? true : false;
var thSize = 34;  // adjust on calculating size to fit all (OnResize)
var shMargin = 5; // <= value of img-shadow margin property
var dbgMin = -1;
var dbgMax = -1;
var imgPaddingActual = 10; // actual room taken up by shadow and border, for positioning of thumbnails
var imgNo = 1;
var otherImgNo = 2;
var FormType = "I"; // "I"ndex (home page), Gallery "H"eader, "G"allery, "C"ontent, "R"ight-panel Content
var selectedThumb; // for caching
var clickedThumb; // selectedThumb not changed when clicking embedded links, clickedThumb is.
var maxImgSize = 750; // DO NOT CHANGE: changed dynamically on generated page, depending on generation setting!!!
var daysNew = 0; // DO NOT CHANGE: changed dynamically on generated page
var pageLeft = 5;
var pageWidth;
var pageTop = 5;
var pageHeight;
var dayNumber; // unique day number for date. Not exact day number of year since each month counts as 31, but it suits our purpose for generating day picture
var picCount;
var zoomTypes = 1;
var showNav = 1; // 0 if we never want to show nav section
var navHeight = 40;
////

function OnLoad(frmType) 
{

    var Today = new Date();
    dayNumber = (Today.getMonth() * 31) + Today.getDate();
	//alert(dayNumber);
	
    // suspend display until everything sorted
    document.getElementById("page").style.visibility = "hidden";
    document.getElementById("loading2").style.visibility = "hidden";
    
    FormType = frmType;
    
    if (showNav == 0 || FormType != "G")
        navHeight = 0;
    
    // turn ON optional DIVs (nav and content) that should be seen by page type (default OFF)   
    if (FormType == "G")
    {
      if (showNav == 1)
          document.getElementById("nav").style.display = "block";       
      document.getElementById("thumbs").style.display = "block"; 
    }
    else if (FormType == "C")
    {
      document.getElementById("content").style.display = "block"; 
      //document.getElementById("scroll").style.display = "block"; 
      //document.getElementById("logo").style.display = "none"; 
    }
    else
    {
      // check content for index (or gallery header?)
      var content = document.getElementById("content");
      if (content.innerHTML.indexOf("[CONTENT]") == -1)
        content.style.display = "block"; 
    }

    OnResize();
        
    if (FormType == "G")
    {
      //document.getElementById("logo").style.display = "none"; 
      
      // show gallery info if there is some
      var ipt = document.getElementById("InfoPanelText");
      if (ipt.innerHTML != "")
      {
        placeInfoPanel(400,400); // quite large, make configurable?
        var ip = document.getElementById("InfoPanel");
        ip.style.visibility = "visible";
        ipt.style.visibility = "visible";
      }
      else
        selectImg(document.getElementById("photo1"));      
    }
    
    if (FormType != "G" && FormType != "R")
    {
        randomImage()
    }

    // now show, could even apply an effect here
    document.getElementById("page").style.visibility = "visible";

    
    //document.getElementById("loading").style.visibility = "visible"; // for debug

    if (FormType == "R")
    {
       // copy Content innerHTML to main
       document.getElementById("main").innerHTML = document.getElementById("content").innerHTML;
       document.getElementById("content").innerHTML = "";
    }
}

function OnResize() {
  var h = browserHeight();  
  var w = browserWidth();
  //document.getElementById("controlPanel").style.minHeight = (h-24) + "px";

      // show the PAGE border
      // if TOO Much width available bring in sides so that image doesnt appear too far away from menu
      
      var page = document.getElementById("page");
      if (w > 400 + maxImgSize)
      {
        pageLeft = ((w-(400+maxImgSize))/2);        
        pageWidth = (400+maxImgSize);
      }
      else
      {
        pageLeft = 0;
        pageWidth = w;
      }
      page.style.left = pageLeft + "px";
      page.style.width = pageWidth + "px"; 

      pageTop = 0;
      pageHeight = h;      
      /*
      if (h > 60 + maxImgSize)
      {
        pageTop = ((h-(60 + maxImgSize))/2);
        pageHeight = (60 + maxImgSize);
      }
      else
      {
        pageTop = 5;
        pageHeight = h - 10;
      }
      */
      page.style.top = pageTop + "px";
      page.style.height = pageHeight + "px"; 

      // do same to page 4      
//      var page4 = document.getElementById("page4");
//      page4.style.left = pageLeft + "px";
//      page4.style.width = pageWidth + "px"; 
//      page4.style.top = pageTop + "px";
//      page4.style.height = pageHeight + "px"; 

      document.getElementById("language").style.top = (h-18) + "px";
      document.getElementById("language").style.left = /*pageLeft +*/ "10px";

//      document.getElementById("advert").style.top = (h-38) + "px";
//      document.getElementById("advert").style.left = /*pageLeft +*/ "50px";

//      document.getElementById("copyright").style.top = "0px"; // (h-16) + "px";
//      document.getElementById("copyright").style.right = "12px"; /*(6 + ((w-pageWidth)/2)) + "px"; */
      //document.getElementById("copyright").style.left = /*pageLeft +*/ "200px";
      if (h-384 > 0)
        document.getElementById("copyright").style.top = (h-384)/2 + "px";
      
      // size the content section, if there is one. Use minHeight so that it can be bigger if needed.
      if (FormType == "G")
      {
          ///if (showNav == 1)              
              document.getElementById("thumbs").style.height = (h-190-navHeight) + "px"; // may need adjusting
          ///else
          ///    document.getElementById("thumbs").style.height = (h-190) + "px"; // may need adjusting
                 
          zoomX = 5 /*pageLeft*/; // fixed
          zoomW = 300; // fixed
          
          if (showNav == 1)
          {
            // position nav bits too
            zoomH = h-190-navHeight; 
            zoomY = 230 /*+ pageTop*/; // fixed
            document.getElementById("prev").style.left = (111) + "px"; 
            ////document.getElementById("circle").style.left = (130) + "px"; 
            document.getElementById("loading").style.left = (146) + "px"; 
            document.getElementById("picLens").style.left = (157) + "px"; 
            document.getElementById("next").style.left = (196) + "px"; 
            document.getElementById("loading2").style.visibility = "hidden"; 
            // do tops as well if we need to adjust pageTop!!!!!!!!!!!!!
          }          
          else
          {
            document.getElementById("loading2").style.top = ((h/2)-25) + "px"; 
            document.getElementById("loading2").style.left = (300+(((w-300)/2)-25)) + "px"; 
            //document.getElementById("loading2").style.visibility = "visible"; 
            zoomH = h-190; 
            zoomY = 170;
          }
          sizeThumbnails();
          
//          document.getElementById("description").style.top = (h-120) + "px";
//          document.getElementById("description").style.left = /*pageLeft +*/ "310px";
//          document.getElementById("description").style.width = /*pageLeft +*/ (pageWidth - 485) + "px";
//          document.getElementById("description").style.height = /*pageLeft +*/ "100px";

//          document.getElementById("price").style.top = (h-120) + "px";
//          document.getElementById("price").style.left = /*pageLeft +*/ (pageWidth - 160) + "px";
//          document.getElementById("price").style.width = /*pageLeft +*/ "150px";
//          document.getElementById("price").style.height = /*pageLeft +*/ "100px";

          
      }
      else 
      {
          // extra room for content if no navigation section
          if (document.getElementById("content").offsetHeight > (h-190))
              document.getElementById("content").style.height = (h-190) + "px"; // may need adjusting
          else
              document.getElementById("content").style.height = ""; 
              
//        var cp = document.getElementById("controlPanel");
//        cp.style.marginTop = ((h - cp.offsetHeight)/2)+"px";
          document.getElementById("menu").style.marginTop = "30px";

 //         document.getElementById("description").style.display = "none";
//          document.getElementById("price").style.display = "none";
      }
      
      if (FormType == "R")
      {
          var rwidth = (w-310-(pageLeft*2));
          document.getElementById("main").style.position = "absolute"; 
          document.getElementById("main").style.left = "310px"; 
          document.getElementById("main").style.width = rwidth + "px"; 
          document.getElementById("main").style.height = (h) + "px"; 
          document.getElementById("main").style.overflow = "auto"; 
          
          // if this contains an iframe of id "iframe" - position it to fill
          var ifr = document.getElementById("guestbook");
          if (ifr)
          {
/*
            // this for boxed centrally:
            var marginTop = 80;
            ifr.style.top = marginTop + "px";
            ifr.style.height = (h-(2*marginTop)) + "px";
            // comments are width 500 so make this width 550
            
            if (rwidth > 550)
                ifr.style.left = (rwidth-550)/2 + "px";
            ifr.style.width = 550 + "px";
*/

            // this for maximised on right
            ifr.style.height = (h-3) + "px";
            ifr.style.width = (rwidth-2) + "px";

          }
      }
      else
      {
          placeImage(); // to reposition
      }
}

function placeInfoPanel(iw, ih)
{
  var h = browserHeight() - (pageTop*2);  
  var w = browserWidth() - (pageLeft*2);

        // position the Info panel over the frame
          var ip = document.getElementById("InfoPanel");
          var ipt = document.getElementById("InfoPanelText");
          
          // should be 300 and 0 but images arent quite centred
          var x_offset = 290 ;
          var y_offset = -10 ;
          var fX = x_offset + (((w-x_offset) - iw) / 2); // relative to pageLeft
          var fY = y_offset + (((h-y_offset) - ih) / 2); // relatibe to pageTop

          ip.style.width = iw + "px";
          ip.style.height = ih + "px";
          ip.style.left = fX + "px";
          ip.style.top = fY + "px";
          //ip.style.visibility = "hidden";
          
          ipt.style.width = (iw-20) + "px";
          ipt.style.height = (ih-20) + "px";
          ipt.style.left = (fX+10) + "px";
          ipt.style.top = (fY+10) + "px";
          //ipt.style.visibility = "hidden";
}

//var topper = 180; // initial top position 
//var scrollPos = 0;
//function doScroll(step)
//{
//    scrollPos = scrollPos + step;
//    document.getElementById("scroll").style.clip = "rect("+scrollPos+"px,300px,"+(browserHeight()-200+scrollPos)+"px,0px)";
//    document.getElementById("scroll").style.top = (topper-scrollPos)+"px";
//    setTimeout("doScroll("+step+")",1000);
//}

function pulseOn(photo)
{
//    applyX(photo,0,5);
    if (IE6) return;
    
	var shadow = document.getElementById(photo.id.replace("photo","shadow"));
	// actually I need the img-shadow element to adjust margin, not the frame
	shadow.style.margin = (5 - shMargin) + "px";
	//photo.style.border = "1px solid white";
	photo.style.width = (thSize+(shMargin*2)) + "px";
	photo.style.height = (thSize+(shMargin*2)) + "px";
}

function pulseOff(photo)
{
//    undoX(photo,0);
    if (IE6) return;
    
	var shadow = document.getElementById(photo.id.replace("photo","shadow"));
	shadow.style.margin = "5px";
	//photo.style.border = "1px solid #593327";
	photo.style.width = thSize + "px";
	photo.style.height = thSize + "px";
}

function randomImage()
{
    var pics = document.getElementById("thumbs").getElementsByTagName("span");
    //var index = Math.floor(Math.random() * pics.length);
    //selectImgPath(pics[index].id, pics[index].title);

    //var index = dayNumber % pics.length; // OK for daily pic but will be strictly sequential.
    //var index = ((dayNumber * 37) + 23) % pics.length; // pseudo-random based on date, works ok
    
    random_seed = dayNumber;
    var index = rand() % pics.length; // a better pseudo random pic     
    //alert(dayNumber+" > "+index+" = "+pics[index].id+" / " + pics[index].title);

    // index of 0 to pics.length-1 will be valid 
    selectImgPath(pics[index].id, pics[index].title);
//    if (window.location.pathname.indexOf("_cz") == -1)
//    {
//        selectImgPath(pics[index].id, "Today's feature photo: "+pics[index].title);
//    }
//    else
//    {
//        selectImgPath(pics[index].id, "Foto dne: "+pics[index].title);
//    }
    
    //dayNumber = dayNumber + 1;
    //setTimeout("randomImage()",20000);
        
}

var random_seed = 0;
function rand()
{
  random_seed = (random_seed * 1103515245) + 12345; 
  return Math.floor((random_seed / 65536) % 32768);
}

var hasLoaded = 0;
function selectImg(thumb)
    {
    try{ 
        //currentImage = thumb;
        //currentFrame = document.getElementById(thumb.id.replace("photo","frame"));

        var ip = document.getElementById("InfoPanel");
        var ipt = document.getElementById("InfoPanelText");
        ip.style.visibility = "hidden";
        ipt.style.visibility = "hidden";

        var img = document.getElementById("actualImage"+otherImgNo);
        var frm = document.getElementById("mainImage"+otherImgNo);

        // must do before setting source or may be loaded before we can set title.
        img.title = thumb.title;
debug(77,"Selected image. Set "+img.id+".title = "+img.title);
//alert("Selected image. Set title = "+thumb.title+" alt="+thumb.alt);

        if (thumb.className != "linkThumb")
        {
            thumb.style.border = "1px solid white";
            if (selectedThumb)
            {
             selectedThumb.style.border = "1px solid #593327"; // turn off previous one
            }
        }
        
        // but show loading animation, must also do this before setting src
        // actually try it after a delay, so will only show if image not displayed immediately
        // use new global variable (set before setting src) to see if loaded
        hasLoaded = 0;
        setTimeout("checkLoaded()",100);

        if (thumb.className != "linkThumb")
            selectedThumb = thumb;
            
        clickedThumb = thumb;
                
        img.src = thumb.src.replace("thumbnails","photos"); // starts loading, only displays when loaded
        img.alt = thumb.alt;
        
        //alert("thumb attr = " + thumb.getAttribute('title')); // verifies this works in FF and IE
        
//        img.setAttribute('title', thumb.getAttribute('title'));
//        alert("img attr set = " + img.getAttribute('title')); // verifies this works in FF and IE

        
        // hide until loaded. DO WE WANT TO THO? Might take a while...
        // but YES we have to since we've already changed src. UNLESS we use TWO images and SWAP
        // then can fade out this one (IF there is one) and fade in next one WHEN it is loaded. So do nothing YET.
        //frm.style.display = "none";

        
        //document.getElementById("ImageComment").innerHTML = element.alt;
        //document.getElementById("MainImage").className = "active";
        //document.getElementById("MainImage").style.visibility = "visible";

    // hide info panel on new image selection        
//    document.getElementById("InfoPanel").style.visibility = "hidden";
//    document.getElementById("InfoPanelText").style.visibility = "hidden";

  } catch(e) {}
}	

function checkLoaded()
{
    // show loading animation if not immediately loaded
    if (hasLoaded == 0)
    {
        if (showNav == 1)
        {
            document.getElementById("loading").style.visibility = "visible";
            document.getElementById("picLens").style.visibility = "hidden";
        }
        else
            document.getElementById("loading2").style.visibility = "visible";
        ////document.getElementById("picc").style.visibility = "hidden";
    }
}

function selectImgPath(path, the_title)
    {
    try{ 
        var img = document.getElementById("actualImage"+otherImgNo);
        var frm = document.getElementById("mainImage"+otherImgNo);

        // must do before setting source or may be loaded before we can set title.
        img.title = the_title;
debug(77,"Selected Image, SET "+img.id+".title = "+img.title);
//        document.getElementById("loading").style.visibility = "visible";
//        document.getElementById("picc").style.visibility = "hidden";

        img.src = path; // starts loading, only displays when loaded
        //img.setAttribute('title', title); 
        //alert("img attr set = " + img.getAttribute('title')); // verifies this works in FF and IE

  } catch(e) {}
}	

function loadedImage()
{

    // hide loading animationalert("4.3");
    hasLoaded = 1;
    if (showNav == 1)
    {
        document.getElementById("loading").style.visibility = "hidden";
        document.getElementById("picLens").style.visibility = "visible";
    }
    else
        document.getElementById("loading2").style.visibility = "hidden";

    swapImage();

    var img = document.getElementById("actualImage"+imgNo);
    if (img.src == "" || img.src.toLowerCase().indexOf(".jpg") == -1)
    {
        return;
    }

    if (FormType == "G" && clickedThumb)
    {
        // placeImage will need to know how much info to display
        var info = replaceLinks(clickedThumb.alt);
        document.getElementById("description"+imgNo).innerHTML = info ;
        //alert("plug in "+info);
    }
    
    placeImage();
    document.getElementById("zoomImage").src = img.src;
        
    // hide last image (If there is one)
    var ofrm = document.getElementById("mainImage"+otherImgNo);
    ofrm.style.display = "none";
    
    // and show this one (immediately if feature photo)
    var frm = document.getElementById("mainImage"+imgNo);
    
    //alert("img attr get = " + img.getAttribute('title')); // verifies this works in FF but NOT IE!! But it DID BEFORE!!
    // can I get at the thumbnail attrubute from here? No since in non gallery pages, there wont be any.
    debug(77,"Loaded "+frm.id+" with source "+img.src+" so now display:");

    if (FormType == "G" && firefox) 
    {
//        frm.style.display = "inline";
//        changeOpac(0,img.id);
//        opacity(img.id, 0, 100, 3000);

        // fade in effect in FF for galleries
debug(78,"changeOpac");
        changeOpac(0,"mainImage"+imgNo);
debug(78,"hideShadow");
        hideShadow();
debug(78,"inline");
        frm.style.display = "inline";
debug(78,"opacity");
        opacity("mainImage"+imgNo, 0, 100, 500);
        // WHY DOESNT THIS WORK VERY WELL ?????? NOT SMOOTH ! even on white bg, even tho its with jpgs
debug(78,"settimeout addshadow");
        setTimeout("addShadow()",500);
    }        
    else
    {
        //changeOpac(100,frm.id);
        frm.style.display = "inline";
    }

    debug(78,"FormType="+FormType);
    
    //document.getElementById("Info"+imgNo).style.display="none";
    
    if (FormType == "G")
    {
    debug(78,"selectedThumb="+selectedThumb);
        if (selectedThumb)
        {
            // show pic # and total in gallery
            debug(78,"Display image number for:"+selectedThumb.id);
            var picno = selectedThumb.id.replace("photo","");
            debug(78,"Image number="+picno);
            ////document.getElementById("no").innerHTML = picno;
            debug(78,"Do Cache");
            cacheNextPrev(); 
            
            // Show Info indication
//            document.getElementById("description"+imgNo).innerHTML = selectedThumb.alt;
//            document.getElementById("price").innerHTML = "<b>Price: </b><br>";
            
            //if (selectedThumb.alt != "")
            //{
               //document.getElementById("Info"+imgNo).style.display="block";
               //document.getElementById("imageTitle"+imgNo).style.left = "28px";
            //}
            
        }
        else 
        {
            debug(78,"No selectedThumb!!");
        }       

    }
}

function replaceLinks(str)
{
    var ret = str;
    var xpos = ret.indexOf("LINKTO(");
    while (xpos != -1)
    {
        var spos = ret.substr(xpos + 7).indexOf(")");
//        if (spos == -1)
//           spos = ret.substr(xpos + 7).length;
        var num = ret.substr(xpos + 7, spos);
        var linkSpan = document.getElementById("link"+num);
        if (linkSpan)
           ret = ret.replace("LINKTO(" + num+")", linkSpan.innerHTML.replace(/LINKTO/g,"templink")); // avoid infinite loop, dont expand links within links
        else   
           ret = ret.replace("LINKTO(" + num+")", "["+num+"]");
        xpos = ret.indexOf("LINKTO(");
    }
    ret = ret.replace(/templink/g,"LINKTO");
    return ret;
}

function hideShadow()
{
  debug(78,"hideShadow "+"actualShadow"+imgNo);
  document.getElementById("actualShadow"+imgNo).style.backgroundImage="url(x.png)"; // THIS TURNS IT OFF, since it doesnt exist 
  debug(78,"hideShadow done");
}
function addShadow()
{
  debug(78,"addShadow "+"actualShadow"+imgNo);
  document.getElementById("actualShadow"+imgNo).style.backgroundImage=""; // THIS ACTUALLY TURNS IT ON!! 
  debug(78,"addShadow done");
}

//function showInfo()
//{
//    document.getElementById("Info"+imgNo).className = "info_hilite";
//}
//function hideInfo()
//{
//    document.getElementById("Info"+imgNo).className = "info";
//}
//function zoomOn()
//{
//    document.getElementById("Zoom"+imgNo).className = "info_hilite";
//}
//function zoomOff()
//{
//    document.getElementById("Zoom"+imgNo).className = "info";
//}

//function toggleInfo()
//{
//    var ip = document.getElementById("InfoPanel");
//    var ipt = document.getElementById("InfoPanelText");
//    ipt.innerHTML = selectedThumb.alt;
//    if (ip.style.visibility == "visible")
//    {
//        ip.style.visibility = "hidden";
//        ipt.style.visibility = "hidden";
//    }
//    else
//    {
//        placeInfoPanel(400,400);
//        ip.style.visibility = "visible";
//        ipt.style.visibility = "visible";
//    }
//}

var imgW; 
var imgH;
var frameX;
var frameY;
var resized;
var origW;
var origH;
var ratio;

function placeImage()
{
    var bw = pageWidth; //browserWidth();
    var bh = pageHeight; //browserHeight();
    var frm = document.getElementById("mainImage"+imgNo);
    var img = document.getElementById("actualImage"+imgNo);
    var ttl = document.getElementById("imageTitle"+imgNo);
    var dsc = document.getElementById("description"+imgNo);
    
    //var inf = document.getElementById("Info"+imgNo);
    //var zm = document.getElementById("Zoom"+imgNo);

    if (img.src == "" || img.src.toLowerCase().indexOf(".jpg") == -1)
    {
        return;
    }
      // position and size the target image
    
    // how big is the original image ?
    var i = new Image();
    i.src = img.src; 

    origW = i.width;
    origH = i.height;

    var dbg = " img[" + origW + "," + origH + "]";
    
    // how much space have we got ?
    
    var w = bw - 400;
    var h = bh - 90; 

    if (FormType == "G")
    {
       // can we work out here how much extra space IS needed for info?
       // only when we know the width of the image - affects the wrapping
       // catch-22 !
       // Depending on whether portrait or landscape, lets say 15 points for each 80
       // characters in landscape, 15 points for each 40 in portrait.
       // consider each line split by <br>
       // can get more sophisticated if needs be (eg. 80 or 40 depends on resolution/pageWidth

       if (dsc.innerHTML != "")
       {
           var infoHeight = 0;
           var x = dsc.innerHTML.split("<br>");
           for(i = 0; i < x.length; i++){
	         var lineText = x[i]; 

	         // remove image placeholders
	         var xpos = lineText.indexOf("<img");
	         while (xpos != -1)
	         {
	             var epos = lineText.substr(xpos).indexOf(">");
	             lineText = lineText.substr(0,xpos)+"[IMG]"+lineText.substr(xpos+epos+1);
	             xpos = lineText.indexOf("<img");
	         }

             if (origW > origH)
               infoHeight = infoHeight + ((1 + Math.floor(lineText.length / 80)) * 15);
             else
               infoHeight = infoHeight + ((1 + Math.floor(lineText.length / 40)) * 15);
             // add an extra 15 if it contains any link pics
             if (lineText.indexOf("[IMG]") > -1)
               infoHeight = infoHeight + 15;
           }
           
           h = h - infoHeight;
       }
       
       // consider possibility of two line title
       //alert(w / img.title.length);
       if (Math.floor(w / img.title.length) <= 8)
         h = h - 13;

    }
       
    dbg = dbg + " max[" + w + "," + h + "]";
    
    // size the image
        resized = 1;
        if (origH > h && origW > w)
        {
            if (origW/w > origH/h)
            {
                // width is the most out, drive by width
                //img.height = origH * (w/origW);
                imgW = w;
                imgH = (origH * w) / origW; 
                dbg = dbg + " -> ww[" + imgW + "," + imgH + "]";
            }
            else
            {
                // for portrait, drive by height
                //img.width = origW * (h/origH);
                imgH = h;
                imgW = (origW * h) / origH; 
                dbg = dbg + " -> hh[" + imgW + "," + imgH + "]";
            }
        }
        else if (origH > h)
        {
	        imgH = h;
            imgW = (origW * h) / origH; 
            dbg = dbg + " -> h[" + imgW + "," + imgH + "]";
	    }
        else if (origW > w)
        {
	        imgW = w;
            imgH = (origH * w) / origW; 
            dbg = dbg + " -> ww[" + imgW + "," + imgH + "]";
        }
        else
        {
	        imgW = origW;
            imgH = origH; 
            dbg = dbg + " no need to resize";
            resized = 0;
        }
        ratio = imgW / origW;
        window.status = "Normal Size x " + ratio;
        if (imgW == origW /* ratio == 1 && zoomLevel == 1*/) 
            zoomLevel = 1.5;
        else
            zoomLevel = 1;
        
    // position the frame
        var iw = imgW + imgPaddingActual; // image width including shadow and padding
        var ih = imgH + imgPaddingActual; // image height including shadow and padding
        frameX = /*pageLeft +*/ 350 + ((w - iw) / 2); // 350 is left-most position (absolute from pageLeft) when full size landscape
        frameY = /*pageTop +*/ 40 + ((h - ih) / 2); // 40 is top-most position (absolute from pageTop) when full size portrait
        dbg = dbg + " xpos="+frameX+" ypos="+frameY+" ratio="+ratio+" initialzoom="+zoomLevel;
        debug(77,dbg);
                
        //alert(dbg);

        // and size the image
    imgW = Math.round(imgW);
    imgH = Math.round(imgH);
    frameX = Math.round(frameX);
    frameY = Math.round(frameY);

        img.style.width = imgW + "px";
        img.style.height = imgH + "px";
        frm.style.left = frameX + "px";
        frm.style.top = frameY + "px";
        //img.style.position = "fixed"; // so stays on view even if left panel scrolls
/***
        // position the Info panel over the frame
          var ip = document.getElementById("InfoPanel");
          var ipt = document.getElementById("InfoPanelText");
          ip.style.width = (imgW-40) + "px";
          ip.style.height = (imgH-40) + "px";
          ip.style.left = (frameX+20) + "px";
          ip.style.top = (frameY+20) + "px";
          ip.style.visibility = "hidden";
          ipt.style.width = ip.style.width;
          ipt.style.height = ip.style.height;
          ipt.style.left = ip.style.left;
          ipt.style.top = ip.style.top;
          ipt.style.visibility = "hidden";
**/


//      Info icon Bottom Left
//        inf.style.top = (imgH + 12) + "px";
//        inf.style.left = "0px";
     
        ttl.style.visibility = "visible";
//        ttl.style.top = (frameY + imgH + 8) + "px";
//        ttl.style.left = (frameX + 4) + "px";
        // now its absolute (to work in IE6), change it to:
        ttl.style.top = (imgH + 8) + "px";        
        ttl.style.left = 4 + "px";

    if (FormType == "G")
      ttl.innerHTML = img.title;
    else
    {
      if (window.location.pathname.indexOf("_cz") == -1)
      {
        ttl.innerHTML = "<span style=\"font-variant: normal; font-style: italic;\">Today's feature photo: </span>"+img.title;
      }
      else
      {
        ttl.innerHTML = "<span style=\"font-variant: normal; font-style: italic;\">Foto dne: </span>"+img.title;
      }
    }

        if (FormType == "G")
        {
          // allow 2 lines for title if may not fit into 1 (allowing 6 pixels per char)
          if (Math.floor(imgW / ttl.innerHTML.length) <= 6)
              dsc.style.top = (imgH + 40) + "px";
          else
              dsc.style.top = (imgH + 27) + "px";
          //alert(imgW / ttl.innerHTML.length);
          dsc.style.left = "4px";
          dsc.style.width = imgW + "px";
          //dsc.style.height = "100px";
        }
        
        // Zoom icon bottom right 
        //zm.style.top = (imgH + 4) + "px";
        //zm.style.left = (imgW + 4) + "px";
      
        
        ////if (FormType == "G")
          //img.title = "Click to Zoom";
        
debug(77,"Placed image, so "+ttl.id+".innerHTML = "+ttl.innerHTML+", x,y="+ttl.style.left+","+ttl.style.top+", vis="+ttl.style.visibility);

//document.getElementById("banner").innerHTML = img.title;

        //ttl.innerHTML = img.getAttribute('title');
        
//        img.style.width = (bw-400) + "px";
//        img.style.height = (bh-90) + "px";
//        frm.style.left = "355px";
//        frm.style.top = "40px";

       ////if (FormType == "G" /* && resized*/)
       {
         img.className = "zoomableImage";
       }
////       else
////       {
////          img.className = "nonzoomableImage";
////       }

}

/*
function maxImage()
{
    var frm = document.getElementById("mainImage"+imgNo);
    var img = document.getElementById("actualImage"+imgNo);
    var ttl = document.getElementById("imageTitle"+imgNo);

    var i = new Image();
    i.src = img.src; 
    
    if (i.width > getPoints(img.style.width) || i.height > getPoints(img.style.height))
    {
        img.style.width = i.width + "px";
        img.style.height = i.height + "px";
        frm.style.left = (pageLeft + 355) + "px";
        frm.style.top = (pageTop + 40) + "px";

        img.style.display = "block";
        img.style.position = "relative"; // so forces scroll bars

//        ttl.style.top = (40 + i.height + 8) + "px";
//        ttl.style.left = (355 + 4) + "px";
        ttl.style.visibility = "hidden";
    }
}
*/

// selected thumbnail in selectedThumb
function cacheNextPrev()
{
        // set prev and next src so can start caching after this one loaded
        var tno = parseInt(selectedThumb.id.replace("photo",""));
        var prevThumb = document.getElementById("photo" + (tno-1));
        var nextThumb = document.getElementById("photo" + (tno+1));
        debug(79,"Cache "+prevThumb+"/"+nextThumb);
        if (nextThumb)
        {
            nextImage = new Image();
            nextImage.src = nextThumb.src.replace("thumbnails","photos"); 
            debug(79,"Next:"+nextImage.src);
        }

        if (prevThumb)
        {
            prevImage = new Image();        
            prevImage.src = prevThumb.src.replace("thumbnails","photos"); 
            debug(79,"Prev:"+prevImage.src);
        }

//        var upos = element.id.indexOf("_");
//        var tno = parseInt(element.id.substring(upos+1));        
//        nextThumbnail = document.getElementById(element.id.substring(0,upos+1) + (tno+1)); 
//        prevThumbnail = document.getElementById(element.id.substring(0,upos+1) + (tno-1)); 
//        if (nextThumbnail)
//        {
//            nextImage.src = nextThumbnail.src.replace("thumbnails","photos");        
////            document.getElementById("fwd").style.visibility = "visible";
//        }
////        else
////        {
////            document.getElementById("fwd").style.visibility = "hidden";
////        }
//        if (prevThumbnail)
//        {
//            prevImage.src = prevThumbnail.src.replace("thumbnails","photos");       
////            document.getElementById("back").style.visibility = "visible";
//        }
////        else
////        {
////            document.getElementById("back").style.visibility = "hidden";
////        }   
}

function sizeThumbnails()
{   
    // 21x21 allows 8 across, 26x26 allows 7, 33=6, 43=5, 58=4, 82=3
    // shadow is 6 and padding is 5 (x2) - so an extra 16 per thumbnail is needed

    // how much height do we have ?
    var maxH = (pageHeight - 190 - navHeight); // (browserHeight()-250);
    
    // how many thumbs do we have ?
    var pics = document.getElementById("thumbs").getElementsByTagName("img");

    //                       cols  * rows
    //                       ----    -------------------------------------------
//    alert("num pics = "+pics.length);
//    alert("max at 21 = "+Math.floor(maxH / (21+16)));

    if      (pics.length <= (3     * (       Math.floor(maxH / (81+16))        )))
    {
        thSize = 81;
    }
    else if (pics.length <= (4     * (       Math.floor(maxH / (57+16))        )))
    {
        thSize = 57; 
    }
    else if (pics.length <= (5     * (       Math.floor(maxH / (42+16))        )))
    {
        thSize = 42;
    }
    else if (pics.length <= (6     * (       Math.floor(maxH / (33+16))        )))
    {
        thSize = 33;
    }
    else if (pics.length <= (7     * (       Math.floor(maxH / (26+16))        )))
    {
        thSize = 26;
    }
    else if (pics.length <= (8     * (       Math.floor(maxH / (21+16))        )))
    {
        thSize = 21;
    }
    //alert("max "+maxH+ ", thumbs "+pics.length+" = "+thSize);
    
    if (IE6)
    {
        thSize = thSize - 2;
    }
    
    for (i=1; i<=pics.length; i++)
    {
        var thm = document.getElementById("photo"+i);
        if (thm)
        {
            thm.style.width = thSize + "px";
            thm.style.height = thSize + "px";            
        }
        
        if (daysNew > 0)
        {
            // Show NEW pics (<=7 days old)
            var crd = document.getElementById("created"+i);
            var shd = document.getElementById("shadow"+i);
            
            if (crd && shd)
            {

                var now = new Date()
                var created = crd.innerHTML;  // ms since 1970
                var days = Math.ceil(now.getTime() / 86400000) - created;
                if (dbgMin > -1)
                    alert(crd.innerHTML+" : created:"+created+" now:"+Math.ceil(now.getTime() / 86400000)+" days:"+days);
                if (days <= daysNew)
                {
                  //shd.style.backgroundImage = "url('ShadowNew.png')";
                  // above doesnt work so have to replicate class rules in css
                  shd.className = "img-shadow-new";
                }
            }
        }
    }
    
    picCount = pics.length;
    if (FormType == "G")
    {
        // show total in gallery
        ////document.getElementById("of").innerHTML = picCount;
    }

}

function prevImg()
{
        var tno = parseInt(selectedThumb.id.replace("photo",""));
        var prevThumb = document.getElementById("photo" + (tno-1));

        if (prevThumb)
        {
            selectImg(prevThumb);
        }
        else 
        {
            var pics = document.getElementById("thumbs").getElementsByTagName("img");
            prevThumb = document.getElementById("photo" + pics.length);
            if (prevThumb)
            {
                selectImg(prevThumb);
            }
        }
}

function nextImg()
{
        var tno = parseInt(selectedThumb.id.replace("photo",""));
        var nextThumb = document.getElementById("photo" + (tno+1));

        if (nextThumb)
        {
            selectImg(nextThumb);
        }
        else 
        {
            nextThumb = document.getElementById("photo1");
            if (nextThumb)
            {
                selectImg(nextThumb);
            }
        }
}

function onPlay()
{   
    document.getElementById("play").src = "css/play_hilite.png";
}
function offPlay()
{   
    document.getElementById("play").src = "css/play.png";
}
function onLeft()
{   
    document.getElementById("prev").src = "css/left_hilite.png";
}
function offLeft()
{   
    document.getElementById("prev").src = "css/left.png";
}
function onRight()
{   
    document.getElementById("next").src = "css/right_hilite.png";
}
function offRight()
{   
    document.getElementById("next").src = "css/right.png";
}

function browserWidth() {
        var myWidth = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myWidth = window.innerWidth;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth - 3;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
        }
        return myWidth ;
    }
    
function browserHeight() {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight - 3;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
        }
        return myHeight;
    }

function getAbsoluteDivs()
{
    var arr = new Array();
    var all_divs = document.body.getElementsByTagName("DIV");
    var j = 0;

    for (i = 0; i < all_divs.length; i++)
        if (all_divs.item(i).style.position=='absolute')
        {
            arr[j] = all_divs.item(i);
            j++;
        }

    return arr;
}

function bringToFront(obj)
{
    if (!document.getElementsByTagName)
        return;

    var divs = getAbsoluteDivs();
    var max_index = 0;
    var cur_index;

    // Compute the maximal z-index of
    // other absolute-positioned divs
    for (i = 0; i < divs.length; i++)
    {
        var item = divs[i];
        if (item == obj ||
            item.style.zIndex == '')
            continue;

        cur_index = parseInt(item.style.zIndex);
        if (max_index < cur_index)
        {
            max_index = cur_index;
        }
    }

    obj.style.zIndex = max_index + 1;
}

function getPoints(styleProperty)
{
  return parseInt(styleProperty.replace("px",""));
}

function debug(level,str)
{
    if (level >= dbgMin && level <= dbgMax)
    {
            alert(level+": "+str);
    }
}

function swapImage()
{
    if (imgNo == 1)
    {
        imgNo = 2;
        otherImgNo = 1;
    } 
    else
    {
        imgNo = 1;
        otherImgNo = 2;
    } 
}

/************************ FADE EFFECTS *************************************/

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	//alert("opacity");

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

/********************** animation ***********************************/
//steps: total steps of animation
//intervals: intervals the animation will be done in miliseconds
//powr: value used for determining ease-in and out.

/*
function doXChangeMem(elem,startX,endX,steps,intervals,powr) { 
//X changer with Memory by www.hesido.com
    if (elem.xChangeMemInt)
	    window.clearInterval(elem.xChangeMemInt);
    var actStep = 0;
    elem.xChangeMemInt = window.setInterval(
	function() { 
	  elem.currentX = easeInOut(startX,endX,steps,actStep,powr);

      // apply currentX to elem in some way pertinent to X
      var shadow = document.getElementById(elem.id.replace("photo","shadow"));
	  shadow.style.margin = (5 - elem.currentX) + "px";
	  //photo.style.border = "1px solid white";
	  elem.style.width = (thSize+(elem.currentX*2)) + "px";
	  elem.style.height = (thSize+(elem.currentX*2)) + "px";

	  actStep++;
	  if (actStep > steps) window.clearInterval(elem.xChangeMemInt);
	} 
	,intervals)
}

function applyX(elem,start,end) { 
    if (!elem.currentX) elem.currentX = start; 
	//if no memory is set, set it first; 
    doXChangeMem(elem,elem.currentX,end,3,5,0.5); 
    } 
function undoX(elem,end) { 
    if (!elem.currentX) return; 
    doXChangeMem(elem,elem.currentX,end,3,5,0.5); 
    }

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
//Generic Animation Step Value Generator By www.hesido.com 
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
    return Math.ceil(stepp) 
    } 
*/

/************************* Zoom stuff *******************************/

var inZoom = 0;
var zoomLevel = 1;

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = showZoom;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0
var zoomType = 0; // 0=unzoomed, 1=zoom with follow, 2=zoom over thumbs
var zoomed = 0;

function swapZoom() {
  if (zoomType == 0)
  {
    zoomType = 1;
  }
  else if (zoomType == 1)
  {
    if (zoomTypes == 2)
      zoomType = 2;
    else
      zoomType = 0;
//    document.getElementById("actualImage"+imgNo).title = "Click to turn Zoom off";
  }
  else
  {
    zoomType = 0;
//    document.getElementById("actualImage"+imgNo).title = "Click to Zoom";
  }
  doZoom();    
}

// Main function to retrieve mouse x-y pos.s

function showZoom(e) {
////if (FormType != "G")
////  return;
  
  // need it relative to PAGE div, so use offsets pageLeft, pageTop
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft - pageLeft;
    tempY = event.clientY + document.body.scrollTop - pageTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX - pageLeft;
    tempY = e.pageY - pageTop;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  //window.status = "x,y="+tempX + "," + tempY + " frame="+ frameX + "," + frameY + " pageLeft="+pageLeft ;
  
  doZoom();
}

function doZoom()
{
  var zib = document.getElementById("zoomImageBackground");
  
  // see if in image box
  // tempX, tempY is mouse position in browser
  // frameX, frameY is position of picture (actually frame) scaled in browser
  // imgW, imgH is size of picture in browser (scaled)
  // origW, origH is actual size of unscaled pic
  // ratio is the scale eg. 0.666
/*
  // try showing zoom image following the mouse
  if (zoomType == 0 &&
      tempX >= frameX && tempX <= frameX + imgW &&
      tempY >= frameY && tempY <= frameY + imgH)
  {
        document.getElementById("zoomIcon").style.top = (tempY-10) + "px";
        document.getElementById("zoomIcon").style.left = (tempX-10) + "px";
        document.getElementById("zoomIcon").style.display = "block";
  }
  else
        document.getElementById("zoomIcon").style.display = "none";
*/
  
  // Zoom is on
  if (zoomType > 0 && 
      tempX >= frameX && tempX <= frameX + imgW &&
      tempY >= frameY && tempY <= frameY + imgH)
  {
    inZoom = 1;
    
    var zi = document.getElementById("zoomImage");
    zib.style.display = "block";

    // SCALE IT, according to zoomLevel
    zi.style.width = origW * zoomLevel + "px";
    zi.style.height = origH * zoomLevel + "px";
    var followBoxSize = 200 + ((zoomLevel-1)*10);
    window.status = "Zoom x" + zoomLevel + ". Use mouse-wheel to zoom in/out. "+(zoomType == 2||(zoomType == 1 && zoomTypes == 1)?"Click to Close Zoom.":"Click to change Zoom View.");    
    
    // x,y is corresponding position in original pic, or SCALED pic (if zoomed)
    var x = Math.round((tempX - frameX)*zoomLevel / ratio);
    var y = Math.round((tempY - frameY)*zoomLevel / ratio);
    
    // set clip bounds for picture, ie. visible area of original or SCALED pic
    // top, right, bottom, and left values respectively. 
    // The top and left values will specify the top-left corner of the rectangle mask, 
    // relative to your image.
    
    // zoomW,H is size of zoom box
    if (zoomType == 2)
    {
        zoomTop =    (y - (zoomH / 2)) ;
        zoomRight =  (x + (zoomW / 2)) ;
        zoomBottom = (y + (zoomH / 2)) ;
        zoomLeft =   (x - (zoomW / 2)) ;
    }
    else
    {
        zoomTop =    (y - (followBoxSize / 2)) ;
        zoomRight =  (x + (followBoxSize / 2)) ;
        zoomBottom = (y + (followBoxSize / 2)) ;
        zoomLeft =   (x - (followBoxSize / 2)) ;
    }
    //window.status = "x,y in orig pic = "+x+","+y+"  T,B / L,R = "+zoomTop+","+zoomBottom+" / "+zoomLeft+","+zoomRight;

//    if (zoomTop < 0) 
//    {
//        zoomBottom = zoomBottom - zoomTop;
//        zoomTop = 0;    
//    }
////    else if (zoomBottom > y)
////    {
////        zoomTop = zoomTop - (y-zoomBottom);
////        zoomBottom = frameY / ratio;
////    }
//    
//    if (zoomLeft < 0)
//    {
//        zoomRight = zoomRight - zoomLeft;
//        zoomLeft = 0; 
//    }
////    else if (zoomRight > x)
////    {
////        zoomLeft = zoomLeft - (x-zoomRight);
////        zoomRight = frameX / ratio;
////    }
       
    zi.style.clip = "rect("+zoomTop+"px,"+zoomRight+"px,"+zoomBottom+"px,"+zoomLeft+"px)";
    
    // zoomX,Y is position of zoom box (top left)
    if (zoomType == 2)
    {
        // position the zoom box background
        zib.style.top = zoomY + "px";
        zib.style.left = zoomX + "px";
        zib.style.width = zoomW + "px";
        zib.style.height = zoomH + "px";

        // and position the top left of the original (or SCALED) image so that visible area appears in right place
        // = position where you want it, minus the top & left clip values
        // or should be, but seems to be just minus the top and left clip values
        zi.style.top = -zoomTop + "px";
        zi.style.left = -zoomLeft + "px";
    }
    else
    {
        // position the zoom box background
        zib.style.top = ((tempY-followBoxSize/2)/*-pageTop*/) + "px";
        zib.style.left = ((tempX-followBoxSize/2)/*-pageLeft*/) + "px";
        zib.style.width = followBoxSize + "px";
        zib.style.height = followBoxSize + "px";

        // and position the top left of the original (or SCALED) image so that visible area appears in right place
        // = position where you want it, minus the top & left clip values
        // or should be, but seems to be just minus the top and left clip values
        zi.style.top = -zoomTop + "px";
        zi.style.left = -zoomLeft + "px";

    }
    //window.status = "scale = x"+zoomLevel+ " zi left,top = "+zi.style.left+","+zi.style.top + " clip = " + zi.style.clip+ " x,y in scaled pic = "+x+","+y;

  }
  else
  {
    inZoom = 0;
    if (zib)
      zib.style.display = "none";
    if (tempX >= frameX && tempX <= frameX + imgW && tempY >= frameY && tempY <= frameY + imgH)
        window.status = "Click to Zoom.";
    else
        window.status = "";    
  }
  
  return true;
}

// these set on OnResize(). Pos of zoom box over thumbnails.
var zoomY; 
var zoomX; 
var zoomW;
var zoomH; 

/***************************** mousewheel stuff ****************************/

function handle(delta) {
    if (inZoom == 0)
        return;
	if (delta < 0)
		zoomLevel = zoomLevel - .1;
	else
		zoomLevel = zoomLevel + .1;
//    if (zoomLevel < 1 && ratio == 1)
//        zoomLevel = 1;
//    if (zoomLevel < ratio)
//        zoomLevel = ratio;        
    if (zoomLevel < .5)
        zoomLevel = .5;        
    else if (zoomLevel > 2.5)
        zoomLevel = 2.5;  
    zoomLevel = Math.round(zoomLevel * 10) / 10;      
    doZoom();
}

function wheel(event){
    if (inZoom == 0)
        return;
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta)
		handle(delta);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}

/* Initialization code. */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;

/******************************* disable right click stuff *****************************/

function clickIE4(){
if (event.button==2){
return false; }}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
return false;}}}

if (document.layers)
{ document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=clickNS4; }
else if (document.all&&!document.getElementById)
{ document.onmousedown=clickIE4; }

document.oncontextmenu=new Function("return false")

/***************************** slideshow *********************************/

var inSlideShow = 0;
function toggleSlideShow()
{
  inSlideShow = (inSlideShow == 1 ? 0 : 1 );
  if (inSlideShow)
  {
    setTimeout("ssNext()",2000); // delay for first
  }
}

function ssNext()
{
    if (inSlideShow)
    {
        nextImg();
        setTimeout("ssNext()",3000); // delay for subsequent
    }
}

// how do we let manual Next reset the clock ?

/************************************** key press stuff *****************************/

document.onkeyup = KeyCheck;       

function KeyCheck(e)
{
    if (inZoom == 0)
        return;

   // If events used use it. IE uses event. FF uses e.
   var KeyID = (window.event) ? event.keyCode : e.keyCode;

   switch(KeyID)
   {
      case 37: // "Arrow Left";
		zoomLevel = zoomLevel - .1;
        break;
      case 38:// "Arrow Up";
		zoomLevel = zoomLevel + .1;
        break;
      case 39:// "Arrow Right";
		zoomLevel = zoomLevel + .1;
        break;
      case 40:// "Arrow Down";
		zoomLevel = zoomLevel - .1;
        break;
   }

    if (zoomLevel < .5)
        zoomLevel = .5;        
    else if (zoomLevel > 2.5)
        zoomLevel = 2.5;  
    zoomLevel = Math.round(zoomLevel * 10) / 10;      
    doZoom();
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function findSize(obj) {
	return [obj.offsetWidth,obj.offsetHeight];
}

function replaceAll( str, replacements ) {
    for ( i = 0; i < replacements.length; i++ ) {
        var idx = str.indexOf( replacements[i][0] );

        while ( idx > -1 ) {
            str = str.replace( replacements[i][0], replacements[i][1] );
            idx = str.indexOf( replacements[i][0] );
        }

    }

    return str;
}
