// constructor
// ========================================================
function pDAFImageGalleryClass() 
// ========================================================
{
  this.init();
}

//========================================================
pDAFImageGalleryClass.prototype = 
//========================================================
{
  // ------------------------------------------------------
  init : function() 
  // ------------------------------------------------------
  {
    this.version = "2.1.1";
    this.option_map = new Object();
  },  
    
  // ------------------------------------------------------
  show : function(sGalleryID, sCloseMsg) 
  // ------------------------------------------------------
  {
    try
    {
      sajax_do_call('../../viewajax', 
                    'getGalleryOptions', 
                    {callback:pDAFImageGallery.setOptions, extra_data:[sGalleryID, sCloseMsg]},
                    [sGalleryID]); 
    }
    catch (e) 
    {
      alert ("Exception caught: " + e);
    }    
  },
  
  //------------------------------------------------------
  setOptions : function(sResultXHTMLSniplet, extra_data) 
  //------------------------------------------------------
  {
    var sGalleryID = extra_data[0];
    var sCloseMsg = extra_data[1];
    eval(sResultXHTMLSniplet);
    var aOptions = aCurOptions;
    pDAFImageGallery.option_map[sGalleryID] = aOptions;
    try
    {
      sajax_do_call('../../viewajax', 
                    'getGalleryContent', 
                    {callback:pDAFImageGallery.startGallery, extra_data:[sGalleryID, sCloseMsg]},
                    [sGalleryID]); 
    }
    catch (e) 
    {
      alert ("Exception caught: " + e);
    }    
  },
  
  //------------------------------------------------------
  getOptions : function(sGalleryID) 
  //------------------------------------------------------
  {
    return pDAFImageGallery.option_map[sGalleryID];
  },
  
  // ------------------------------------------------------
  startGallery : function(sResultXHTMLSniplet, extra_data) 
  // ------------------------------------------------------
  {
    var sGalleryID = extra_data[0];
    var sCloseMsg  = extra_data[1];
    
    // call potential customer specific actions
    if(typeof(customerOnStartGallery) == "function")
    {
      customerOnStartGallery();
    }
    $("body").append(sResultXHTMLSniplet);
    //$('#' + sGalleryID + '_container').removeClass('tmp_gallery_structure');
    pDAFImageGallery.setLoaded(sGalleryID, sCloseMsg);
  },

  // ------------------------------------------------------
  setLoaded : function(sGalleryID, sCloseMsg) 
  // ------------------------------------------------------
  {
    var aOptions = pDAFImageGallery.getOptions(sGalleryID);
    if (aOptions.auto_width || aOptions.auto_height)
    {
      var aDimensions = this.getDimensions();
      var nGalHeight = aDimensions[1];
      var nGalWidth = aDimensions[0];
      if (aOptions.auto_width)
      {
        aOptions.panel_width = nGalWidth;
      }
      if (aOptions.auto_height)
      {
        aOptions.panel_height = nGalHeight;
      }    
    }
    try {
      $('#' + sGalleryID).galleryView(aOptions);
      $('#' + sGalleryID).modal({
          containerCSS: 'height:' + nGalHeight + 'px !important; width:' + nGalWidth + 'px !important;',
          closeHTML: "<a class=\"modalCloseImg simplemodal-close\" title=\"" + sCloseMsg + "\"/>",
          onOpen:  function (dialog) {
                     dialog.overlay.fadeIn('slow', function () {
                       dialog.container.slideDown('slow', function () {
                         dialog.data.fadeIn('slow');
                       });
                     });
                   },
          onClose: function (dialog) {
                     dialog.data.fadeOut('slow', function () {
                       dialog.container.slideUp('slow', function () {
                         dialog.overlay.fadeOut('slow', function () {
                           $.modal.close();
                           $('#' + sGalleryID).remove();
                           $('#' + sGalleryID + '_container').remove();
                           // call potential customer specific actions
                           if(typeof(customerOnCloseGallery) == "function")
                           {
                             customerOnCloseGallery();
                           }                               
                           });
                         });
                       });
                     }
                   });    
    }
    catch (ex)
    {
      window.setTimeout("pDAFImageGallery.setLoaded('" + sGalleryID + "', '" + sCloseMsg + "')", 500);
    }
  },

  // ------------------------------------------------------
  getDimensions : function ()
  // ------------------------------------------------------
  {
    var aDimensions = new Array(2);
    var nAvailableHeight = 0;
    if (window.innerHeight)        
      nAvailableHeight = window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
      nAvailableHeight = document.documentElement.clientHeight;
    var nAvailableWidth = 0;
    if (window.innerWidth)        
      nAvailableWidth = window.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth)
      nAvailableWidth = document.documentElement.clientWidth;
    
    aDimensions[0] = nAvailableWidth - 200;
    aDimensions[1] = nAvailableHeight - 150;
    
    return aDimensions;
  }
}

pDAFImageGallery = window.pDAFImageGallery = new pDAFImageGalleryClass();
