/* 
 * This file is Copyright (c) The Hawk Conservancy, 2005. 
 * See: http://www.hawk-conservancy.org/
 *
 * If you find these utilities useful, you are welcome to use them but we would ask 
 * that you retain the URL and copyright notice above. Also - we can recommend that 
 * you come and visit if you can - the park is open every day from mid-February to
 * the end of October and provides an excellent day out! We are located near Andover
 * in Hampshire, England. 
 *
 * Author: Euain Drysdale
 *
 * CVS Info: 
 * $Revision: 1.4 $  $Date: 2005/09/02 07:10:40 $   $Author: euain $
 */


 /*
  * Define some useful variables in the scope of the calling document. It 
  * may not necesarily use these.
  */  
  var imageWindow = null;
  var referenceWindow = null;
  var imageCount=0;
  
  // Set this to the location of this script so that child windows can find it.
  //var scriptLocation="image_utils.js ";  // A test location
  var scriptLocation="http://www.theaccipiter.com/image_utils.js ";  // On the Accipiter server
   
   
   /*
    * This function resizes the window to pretty much fit the 
    * image - with some padding for buttons.
    */
   function resizeToFit()
   {
        // Resize this window to fit the contents.
        window.resizeTo(
             window.document.mainImage.width + 30, 
             window.document.mainImage.height + 105);
   }

   /*
    * This function loads the selected picture in a seperate window.
    */
   function loadPic(theUrl, theStatusComment, theInfoLocation)
   {
        // See if the target window is available. Create a new one if it is not.
        // Opera seems to need the third option, imageWindow does not get properly set 
        // to closed.
        if ( imageWindow == null || imageWindow.closed || imageWindow.name == undefined )
        {
           imageWindow = window.open("", "ImageViewer","location=no,menubar=no,resizable=yes");
        }
        else 
        {
          // Bring target to the front. We can leave it at the back as well.
          imageWindow.focus();
        }
        
    
        // Open a new document
        imageWindow.document.open();
        // Grab a reference to it
        var targetDoc=imageWindow.document;
        
        // Add some style declarations
        targetDoc.write("<head>");
        targetDoc.write("<title>Photos from the Hawk Conservancy</title>");
        targetDoc.write("<style> hr {width: 90%;} </style>");
        
        targetDoc.write("<script src='" + scriptLocation + " '></script>");
        targetDoc.write("</head>");
        
        imageWindow.document.write("<body>");
        
                
        
        // And the image proper
        targetDoc.write("<img src='"+ theUrl + "' name='mainImage' onload='resizeToFit()'/>");

        // Add some controls to the window.
        targetDoc.write("<hr/>");
        targetDoc.write("<form name='controlForm'>");
        targetDoc.write("<input type='button' value='Close' name='close' onclick='window.close()'/>");
        
        
        
        // Specify some events for the controls
        //targetDoc.close.onmouseover=function(){imageWindow.status="Close this window";};

        // Add a about button if possible
        if (theInfoLocation != null)
        {
           targetDoc.write("<input type='button' value='About this bird...' name='butNew'/>") ; 
        }
        
        // Set the default status.
        //imageWindow.defaultStatus=theStatusComment;
        
        targetDoc.write("</form>");
        
        targetDoc.write("<script>document.onload=addReferenceButton('" + theInfoLocation + "');</scr" + "ipt>");
        
        targetDoc.write("</body>");
        targetDoc.close();
        
    }

    /*
     * This should be scope private if such a thing existed in JavaScript. Convenience
     * function to add the handler for the get reference button.
     *
     */
    function addReferenceButton(theLocation)
    {
            if (document.controlForm.length > 1)
            {
              document.controlForm.butNew.onclick=function(){
               if (referenceWindow == null || referenceWindow.closed )
                   referenceWindow = window.open(theLocation, "AboutWindow");
               else
                   referenceWindow.location.href=theLocation;
               }
            }     
    }
   
   /*
    * Add the image to the current page. 
    * theThumbnail should be a reference to the thumbnail to add
    * theFullSize is the full-size image
    * theComment is a comment relevent to this image
    * theReferenceUrl is an optional reference URL. If provided, the user will get an additional 
    * button that will allow them to open the referenced URL in a new window.
    */
   function addImage(theThumbnail, theFullsize, theComment, theReferenceUrl)
   {
     var thisName = "image" + imageCount;
     imageCount++;
     document.write("<img src='" + theThumbnail + "' name='" + thisName + "' />");
     document.images[thisName].onclick=function() {loadPic(theFullsize, theComment, theReferenceUrl)};
     document.images[thisName].onmouseover=function(){window.status=theComment;};
   }
   

