﻿ //to have the image upload stuff work properly, you must do the following setup things on the page:
 //* call the setUploadParms() function with the appropriate values
 //* if you want to use the save reminder, you must have a div with id=saveReminder defined in the page
 //* for each photo displayed, you should have:
 //* an image with id=<imageID>;
 //* a div to contain the change/delete links with id=<imageID>Links;
 //* a "Change image" link with id=lnkUpload<imageID>;
 //* a "Delete" link with id=lnkDelete<imageID>;
 //* a js variable that stores the hidden field id to store the filename, with the format id=hdn<imageID>;
 //* the div containing the iframe for uploading with id=uploadImage;
 //* the iframe in that div with id=uploadFrame.
 
 var divOffset = 30;
 var imageType;
 var picLoc;
 var showReminder = false;
 var currentImage = '';

 function setUploadParms(divTopOffset, imageTypeCode, picLocation, showSaveReminder)
 {
    divOffset = divTopOffset;
    imageType = imageTypeCode;
    picLoc = picLocation;
    showReminder = showSaveReminder;
 }
 
 function toggleUpload(imgID)
 {
       var iframeURL = "/admin/uploadimage.aspx?img=" + imageType;
       
       //change link text to Cancel
        var imageLink = $("#lnkUpload"+imgID);
        
        if (imageLink.text() == "Cancel")
        {
            $("#uploadImage").css('display','none');
            imageLink.text("Change Image");
            currentImage = '';                        

           //reload the content of the iframe so that if there were any errors, etc they will be gone
           $('#uploadFrame').attr("src", iframeURL);
           
        }
        else
        {
           //make sure to hide the upload window if it's open somewhere else
           if (currentImage != '')
           {
                $("#lnkUpload"+currentImage).text("Change Image");
           }

           currentImage = imgID;
                
           //move the iframe to the correct postion and show it
           var pos = $('#'+currentImage+'Links').position();
           var top = pos.top + divOffset;
           $('#uploadImage').css('top',top + 'px').css('left',pos.left + 'px');
           $('#uploadImage').show("fast");

           imageLink.text("Cancel")
        }
       
       return false;
 }
 
 function uploadFinished(fileName)
 {
    //set the hidden field with the new path
    $('#' +eval('hdn'+currentImage)).val(fileName);
   
    //change the image displayed
    $('#'+currentImage).attr("src",picLoc + fileName);
    
    //show the delete link
    $('#lnkDelete'+currentImage).show();
    
    //hide the iframe
    toggleUpload(currentImage);
    
    //shows a reminder next to save button
    if (showReminder)
    {
        showSaveReminder();
    }
 }
 
 function deleteImage(imgName)
 {
    if (confirm('Are you sure you want to delete this image?'))
    {
        //change image source to "no image"
         $('#'+imgName).attr("src",'/images/noimage.gif');
       
        //clear out hidden field so we can delete the image on save
        $('#'+eval('hdn'+imgName)).val('');
  
         //hide the delete link
        $('#lnkDelete'+imgName).hide();
        
        //shows a reminder next to save button
        if (showReminder)
        {
            showSaveReminder();
        }

    }
 }

 function showSaveReminder()
 {
    $('#saveReminder').fadeIn("slow");
    
    //make it go away in 5 seconds
    window.setTimeout("$('#saveReminder').fadeOut('slow');",3000);
 }
     

