// Global variable shown while form is being processed
var processing = "<h3 style='display:inline; margin-top:10px;'>Loading . . .&nbsp;&nbsp;</h3><img src='/images/indicator.white.gif' />";
var page='';
var search='';
var target = 'photo_gallery';
var url = '/index-ajax.php?q=assets/snippets/photoManager/galleryManager.php';
var pars='';

$(document).ready(function() {
  // Preload images function
  $.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
  }

  // List images and call the preload function
  $(['/image/search_div/SearchPhoto_Button_on.gif',
      '/image/search_div/SearchPhoto_Button_off.gif',
      '/image/search_div/Reset_on.gif',
      '/image/search_div/Reset_off.gif',
      '/image/search_div/Exit_on.gif',
      '/image/search_div/Exit_off.gif',
      'image/search_div/Search_on.gif',
      'image/search_div/Search_onf.gif']).preload();
      
  // End preload images

    // Toggle off the search form
    $('#SearchPhoto_button').click(function() {
      $('#search_div').show();
      $('#SearchPhoto_button').hide();
      
    });
  // Set button mouseover / mouseout
    $('#SearchPhoto_button').mouseover(function() {
      $(this).attr("src", "/image/search_div/SearchPhoto_Button_on.gif");
    });    
    $('#SearchPhoto_button').mouseout(function() {
      $(this).attr("src", "/image/search_div/SearchPhoto_Button_off.gif");
    });
    
    $('#reset_button').mouseover(function() {
      $(this).attr("src","/image/search_div/Reset_on.gif");
    });    
    $('#reset_button').mouseout(function() {
      $(this).attr("src","/image/search_div/Reset_off.gif");
    });

    $('#cancel_button').mouseover(function() {
      $(this).attr("src","/image/search_div/Exit_on.gif");
    });    
    $('#cancel_button').mouseout(function() {
      $(this).attr("src","/image/search_div/Exit_off.gif");
    });

    $('#search_button').mouseover(function() {
      $(this).attr("src","/image/search_div/Search_on.gif");
    });    
    $('#search_button').mouseout(function() {
      $(this).attr("src","image/search_div/Search_off.gif");
    });
    
    
    // Exit search mode
    $('#cancel_button').click(function() {
      // Reset form inputs
      $(':input','#search_photo')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
      $('#search_div').hide();
      $('#SearchPhoto_button').show();
    });
    
    // Reset form inputs
    $('#reset_button').click(function() {
      $(':input','#search_photo')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    });

    // Start search
    $('#search_button').click(function() {
      
      pars = 'page=1&display=thumb&search=T';   
      pars += '&photog_id=' + $('#photog_id').val() + '&category=' + $('#category').val() + '&display=thumb';
      search_gallery(pars)
    });
    
    getThumbNails('1', 'F');
    $('#search_div').hide();
      
});

// This function builds the url to be sent to server

function getThumbNails(page, search)
{
    pars = 'page=' + page + '&display=thumb&search=' + search;   
    $('#photo_gallery').html(processing);
    if(search == "T") {
        pars += '&photog_id=' + $('#photog_id').val() + '&category=' + $('#category').val() + '&display=thumb';
    }
    // Get Thumbnails
     
    $.ajax({
        type: "POST",
        url: url + '&' + pars,
        success: function(data) {
        $('#photo_gallery').html(data);
        }
    });
}

function getNextPage(last,search)
{

     var target = 'photo_gallery';
     document.getElementById(target).innerHTML = processing;
     var pars = 'next=' + last + '&display=thumb&search=' + search;   
     var url = 'index-ajax.php?q=assets/snippets/photoManager/galleryManager.php';
     var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars});      
}


function editNextPage(last)
{

     var target = 'photo_gallery';
     document.getElementById(target).innerHTML = processing;
     var pars = 'next=' + last + '&display=thumb&search=' + search;   
     var url = 'index-ajax.php?q=assets/snippets/photoManager/editManager.php';
     var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars});      
}

function deleteImage(id)
{
     var confirmed = window.confirm("Click OK to delete image.");
     
     if(confirmed)
     {
          document.delImage.submit();
     } else {
          return false;
     }
}

function search_gallery(pars)
{
    $('#photo_gallery').html(processing);
    // Get Thumbnails
    $.ajax({
        type: "POST",
        url: url + '&' + pars,
        success: function(data) {
        $('#photo_gallery').html(data);
        }
    });
}

function toggle_search(search_id)
{
     var id = $(search_id);
     if(id.style.display == 'none')
     {
          id.style.display = 'block';
     } else {
          id.style.display = 'none';
     }
}

function editThumbNails(page)
{
     var target = 'photo_gallery';
     document.getElementById(target).innerHTML = processing;
     var pars = 'page=' + page + '&display=thumb';     
     var url = 'index-ajax.php?q=assets/snippets/photoManager/editManager.php';
     var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars});      
}



