// ***************************************************************************
// * Description                                                  *   Date   *
// ***************************************************************************
// *--------------------------------------------------------------+----------*
// * Added Show_Hide_element() for simple upload facility         | 10/06/05 *
// *--------------------------------------------------------------+----------*
// * Added Populate_Default_Dir_Config() for resetting textareas  | 05/07/05 *
// *--------------------------------------------------------------+----------*
// * Added openslide() for slideshow                              | 08/07/05 *
// *--------------------------------------------------------------+----------*
// * Removed Populate_Default_Dir_Config() - no longer needed.    | 15/08/05 *
// * And removed unused functions from BluPlusPlus days...        |          *
// *--------------------------------------------------------------+----------*
// * Added more help for dir config editor screen.                | 18/08/05 *
// *--------------------------------------------------------------+----------*
// * Added 'Add Watermark to Images' help                         | 19/08/05 *
// *--------------------------------------------------------------+----------*
// * Added 'Count Page Views' help and                            | 07/10/05 *
// *       'Show Page Views' help  and                            |          *
// *       'Show Page Last Viewed' help .                         |          *
// *--------------------------------------------------------------+----------*
// * Added ShowThumbStyleColourBoxIfNeeded() and                  | 13/10/05 *
// *       ShowMovieStyleColourBoxIfNeeded() and                  |          *
// *       ShowImageStyleColourBoxIfNeeded().                     |          *
// *--------------------------------------------------------------+----------*
// * Added Softened ShowColourBoxIfNeeded().                      | 14/10/05 *
// *--------------------------------------------------------------+----------*
// * Added keyboard control for moving to Previous                | 25/10/05 *
// * and Next images.                                             |          *
// *--------------------------------------------------------------+----------*
// * Added note about media ordering method being overridden by   | 08/11/05 *
// *       the media ordering text file.                          |          *
// *--------------------------------------------------------------+----------*
// * Fixed Warning produced by JSLint.                            | 23/08/06 *
// *--------------------------------------------------------------+----------*
// * Added Help for Show Image Thumbnails in config screen        | 18/10/06 *
// *--------------------------------------------------------------+----------*
// * Added Show2BoxesIfNeeded(), reworked ShowColourBoxIfNeeded() | 20070123 *
// *--------------------------------------------------------------+----------*
// * Added showElementBlock() and hideElementBlock()              | 20070220 *
// *--------------------------------------------------------------+----------*
// * Added switchbutton() function - no use yet but could be...   | 20070222 *
// *--------------------------------------------------------------+----------*
// * Added: File ordering config help                                   * 20070412 *
// *--------------------------------------------------------------------+----------*
// * Added: List movies first help                                      * 20070904 *
// *--------------------------------------------------------------------+----------*
// * Added: Help for Show Image Thumbnails in config screen             * 20070912 *
// *--------------------------------------------------------------------+----------*
// * Impvd: 'Prev' and 'Next' keyboard commands now work when the p/n   * 20080218 *
// *        item is a dir as well as when its a media.                  *          *
// * Added: 'u' and up arrow go up to parent page.                      *          *
// *--------------------------------------------------------------------+----------*
// * Fixed: Once user has clicked in Search box n/p/u etc keypresses    * 20080219 *
// *        dont result in prev/next pages being reloaded.              *          *
// * Fixed: Keyboard control now works in Firefox.                      *          *
// *--------------------------------------------------------------------+----------*
// * Added: 'i' key now brings up Info window (in both ie and ff)       * 20080220 *
// *--------------------------------------------------------------------+----------*
// * Fixed: 'i' key now brings up Info window for movies too!           * 20080221 *
// *--------------------------------------------------------------------+----------*
// * Added: load_Garmin_plugin() to load the iframe content if needed   * 20081013 *
// *--------------------------------------------------------------------+----------*
// *********************************************************************************

var activeElement = null;
var inSearch = 0;
var garminLoaded = 0;

function showElement(elementID)
{
	if( document.getElementById ) // IE5+ & Gecko
		document.getElementById(elementID).style.visibility = "visible";
	else if( document.all ) // IE4
		document.all[elementID].style.visibility = "visible";
	else if( document.layers ) // Netscape 4
		document.layers[elementID].visibility = "show";
}

function switchbutton() {
  if (document.myform.btnSubmit.disabled) {
  document.myform.btnSubmit.disabled = false;}
  else {
  document.myform.btnSubmit.disabled = true;}

  return true;
}

function hideElement(elementID)
{
	if( document.getElementById ) // IE5+ & Gecko
		document.getElementById(elementID).style.visibility = "hidden";
	else if( document.all ) // IE4
		document.all[elementID].style.visibility = "hidden";
	else if( document.layers ) // Netscape 4
		document.layers[elementID].visibility = "hide";
}

function showElementBlock(elementID)
{
  document.getElementById(elementID).style.display = 'block';
}

function hideElementBlock(elementID) 
{
  document.getElementById(elementID).style.display = 'none';
}

function toggleElementVisibility(elementID)
{
	if( document.getElementById ) // IE5+ & Gecko
	{
		if( document.getElementById(elementID).style.visibility=="visible" )
			document.getElementById(elementID).style.visibility = "hidden";
		else
			document.getElementById(elementID).style.visibility = "visible";
	}
	else if( document.all ) // IE4
	{
		if( document.all[elementID].style.visibility=="visible" )
			document.all[elementID].style.visibility = "hidden";
		else
			document.all[elementID].style.visibility = "visible";
	}
	else if( document.layers ) // Netscape 4
	{
		if( document.layers[elementID].visibility=="show" )
			document.layers[elementID].visibility = "hide";
		else
			document.layers[elementID].visibility = "show";
	}
}

function Show_Hide_element(element)
{
  //~ alert("Show_Hide_element Pressed");
  document.getElementById(element).style.display = (document.getElementById(element).style.display == 'none') ? 'block' : 'none';
}

// **** NOTE! When updating you also MUST update the value below you must also update
// ****       CONSTANT_DEFAULT_THIS_DIR_CONTENT in index.php
// (I tried to pass the content to the function from the form button which works for simple 'hello' text
//  but it didn't work for multiline php content...)
//function Populate_Default_Dir_Config()
//{
//  document.textarea.content.value = "<?PHP\n//thumb=\n#define(\"CONSTANT_IMAGE_WIDTH\", \"800\");\n#define(\"CONSTANT_IMAGE_HEIGHT\", \"600\");\n?>";
//}

function openslide(slide_url) {
  remote = window.open (
    slide_url,"","fullscreen=yes,location=no,status=no,toolbar=no,menubar=no,resizable=no,scrollbar=no");
//  myurl,"_blank","fullscreen=no,location=yes,status=yes,toolbar=yes,menubar=yes,resizable=yes,scrollbar=yes");
}

function showImageOrderingHelp() {
   alert("Choose the order that the images are displayed.\n\nBY_NAME_A:\tBy name Ascending (A-Z)\nBY_NAME_D:\tBy name Descending (Z-A)\nBY_EXIF_DATE_A:\tIn the order the photo's were taken (most recent last)\nBY_EXIF_DATE_D:\tIn the reverse order they were taken (most recent first)\nBY_EXIF_DATE_C:\tComplex method (most recent day first, but within each day the order they were taken)\n\n  Remember that the media ordering text file must be empty for this setting to be effective.");
}

function showFileOrderingHelp() {
   alert("Choose the order that the files are displayed.\n\nBY_NAME_A:\tBy name Ascending (A-Z)\nBY_NAME_D:\tBy name Descending (Z-A)\nBY_DATE_A:\tBy modification time Ascending (most recent last)\nBY_DATE_D:\tBy name Descending (most recent first)\n\n  Remember that the file ordering text file must be empty for this setting to be effective (if this even exists yet...).");
}

function showNavButtonSizeMultHelp() {
   alert("Setting this value to 0.5 will make the navigation buttons half their normal size;\nSetting it to 2 will double their size.");
}

function showAddWatermarkHelp() {
   alert("To add a watermark to each image tick the box and enter the filename of the watermark image in the box that appears.\nThe watermark image should be uploaded to the _gallery_resources/ directory using FTP.");
}

function showCountPageViewsHelp() {
   alert("Set to 'y' if you want each page view to be counted.\n\n(note, page views aren't counted when you're logged in or in debug mode).");
}

function showPageViewsHelp() {
   alert("Set to 'n' if you don't want the number of page views to be visible;\nSet to 'y' if you do want the number of page views to be visible (in the bottom right-hand corner of the page);\nSet to 'x' if you want the page views count to be hidden but it will show up if you move your mouse over the bottom right-hand corner of the page.\n\n(note, the page views count is always shown when you're logged in or in debug mode).");
}

function showPageLastViewedHelp() {
   alert("Set to 'n' if you don't want the last viewed date of the page to be visible;\nSet to 'y' if you do want the last viewed date of the page to be visible (in the bottom left-hand corner of the page);\nSet to 'x' if you want the last viewed date of the page to be hidden but it will show up if you move your mouse over the bottom left-hand corner of the page.\n\n(note, the page last viewed date is always shown when you're logged in or in debug mode).");
}

function showPageLastViewedHelp() {
   alert("Set to 'n' if you don't want the last viewed date of the page to be visible;\nSet to 'y' if you do want the last viewed date of the page to be visible (in the bottom left-hand corner of the page);\nSet to 'x' if you want the last viewed date of the page to be hidden but it will show up if you move your mouse over the bottom left-hand corner of the page.\n\n(note, the page last viewed date is always shown when you're logged in or in debug mode).");
}

function showMoviesFirstHelp() {
   alert("Set to 'n' to mix movies and images according to sort method;\nSet to 'y' to list movies before images - Note: when sorting by exif date each days movie's will be brought to the start of that day;\nSet to 'x' to always force all movies to the front, even when sorting by exif date.");
}

function showImageThumbnailsHelp() {
   alert("Set this to 'n' to prevent images in a directory from being shown.");
}

// page background colour
function ShowColourBoxIfNeeded(index, element) {
//~ alert(index);
  if( index=="Use Colour" ) {
    showElement(element + "_hideable_label");
    showElement(element + "_hideable_textbox");
    showElement(element + "_hideable_text");
  } else {
    hideElement(element + "_hideable_label");
    hideElement(element + "_hideable_textbox");
    hideElement(element + "_hideable_text");
  }
}

// Changes the visibility of 2 different config options on screen
function Show2BoxesIfNeeded(index, element, element2) {
//~ alert(index + element + element2);
  if( index=="Manual" ) {
    showElement(element + "_hideable_label");
    showElement(element + "_hideable_textbox");
    showElement(element + "_hideable_text");
    showElement(element2 + "_hideable_label");
    showElement(element2 + "_hideable_textbox");
    showElement(element2 + "_hideable_text");
  } else {
    hideElement(element + "_hideable_label");
    hideElement(element + "_hideable_textbox");
    hideElement(element + "_hideable_text");
    hideElement(element2 + "_hideable_label");
    hideElement(element2 + "_hideable_textbox");
    hideElement(element2 + "_hideable_text");
  }
}

function activate_next_keys(info_exists, next_page, up_page) {

  document.onkeydown = function getcode(e) {

    if (inSearch == 0) {

    	var evt = e || window.event;
      value = evt.keyCode;
    
      switch(value) {
        case 32:  // space
        case 13:  // enter
        case 78:  // 'n' & 'N'
        case 39:  // right arrow
          remote = window.location=next_page;
          break;
          
        case 85:  // 'u' & 'u'
        case 38:  // up arrow
          remote = window.location=up_page;
          break;
          
        case 73:  // 'i'
          if (info_exists != "") {
            toggleElementVisibility(info_exists)
          }
          break
      }
    }
  };
}

function activate_previous_keys(info_exists, prev_page, up_page) {

  document.onkeydown = function getcode(e) {

    if (inSearch == 0) {

    	var evt = e || window.event;
      value = evt.keyCode;
    
      switch(value) {
        case 8:   // delete
        case 80:  // 'p' & 'P'
        case 37:  // left arrow
          remote = window.location=prev_page;
          break;
          
        case 85:  // 'u' & 'u'
        case 38:  // up arrow
          remote = window.location=up_page;
          break;
          
        case 73:  // 'i'
          if (info_exists != "") {
            toggleElementVisibility(info_exists)
          }
          break
      }
    }
  };
}

function activate_previous_and_next_keys(info_exists, prev_page, next_page, up_page) {

  document.onkeydown = function getcode(e) {

    if (inSearch == 0) {

    	var evt = e || window.event;
      value = evt.keyCode;
      
  //~ alert(value);
      switch(value) {
        case 8:   // delete
        case 80:  // 'p' & 'P'
        case 37:  // left arrow
          remote = window.location=prev_page;
          break;
          
        case 32:  // space
        case 13:  // enter
        case 78:  // 'n' & 'N'
        case 39:  // right arrow
          remote = window.location=next_page;
          break;

        case 85:  // 'u' & 'u'
        case 38:  // up arrow
        //~ alert(up_page)
          remote = window.location=up_page;
          break;

        case 73:  // 'i'
          if (info_exists != "") {
            toggleElementVisibility(info_exists)
          }
          break
      }
    }
  };
}

function activate_up_keys(info_exists, up_page) {

  document.onkeydown = function getcode(e) {

    if (inSearch == 0) {

    	var evt = e || window.event;
      value = evt.keyCode;

      switch(value) {

        case 85:  // 'u' & 'u'
        case 38:  // up arrow
          remote = window.location=up_page;
          break;
          
        case 73:  // 'i'
          if (info_exists != "") {
          //~ alert("here")
            toggleElementVisibility(info_exists)
          }
          break
      }
    }
  };

}

// only change the url of the garminPlugin iFrame if we havent already loaded it
function load_Garmin_plugin(url) {

  if (garminLoaded == 0)  {
    document.getElementById('garminFrame').src = url;
    garminLoaded = 1;
  } 
}
