
// ------------------------------ fotogallery init -----------------------------

  // create instance
     var mfsgallery_instance  = new MFsGallery();
     
  // bind update method to resize and scroll event
      $(window).resize( function () {mfsgallery_instance.WindowUpdated()});
      $(window).scroll( function () {mfsgallery_instance.WindowUpdated()});

// --------------------------- define fs gallery object ------------------------


  function MFsGallery()
  {
    // content
      this.content         = new Array();
      this.tmp_item        = null;
      this.caption         = "";

    // gallery displayed
      this.displayed       = false;

    // screen info
      var screen_width       = 1280;
      var screen_height      = 968;
      var screen_scroll_left = 0;
      var screen_scroll_top  = 0;

    // selected image
      this.selected             = -1;
      this.selected_img_object  = null;

    // img size
      this.max_img_width    = 1200;
      this.max_img_height   = 800;
      this.min_img_width    = 150;
      this.min_img_height   = 100;

    // computed image size
      this.calculated_img_width  = 0;
      this.calculated_img_height = 0;

    // numbers
      this.use_numbers        = false;
      this.show_numbers_count = 20;
      this.avg_number_width_1 = 22;
      this.avg_number_width_2 = 30;

    // thumbnails
      this.use_thumbnails        = false;
      this.thumb_img_width       = 82;
      this.thumb_img_height      = 62;
      this.thumb_btn_width       = 30;
      this.thumb_current_scroll  = 0;
      this.thumb_scroll_timeout  = null;
      this.thumb_scroll_step     = 3;
      this.thumb_scroll_interval = 10;

    // bg
      this.bg_color         = "#000000";
      this.bg_transparency  = 0;

    // frame
      this.frame_size       = 20;

    // img frame
      this.img_frame_color  = "#FFFFFF";
      this.img_frame_size   = 10;

    // description
      this.caption_color                  = "#000000";
      this.text_color                     = "#666666";
      this.description_base_height        = 25;
      this.description_real_height        = this.description_base_height;
      this.description_new_line_size      = 15;
      this.description_caption_char_width = 7.5;
      this.description_char_width         = 5.15;

    // toolbar
      this.toolbar_height   = 20;//14;
      this.hide_box_width   = 100;

    // hide flash
      this.hide_flash = new Array();

// ------------------------------- window resized ------------------------------

    // resize
      this.WindowUpdated = function ()
      {
        if (this.displayed)
        {
          this.Show(true);
        }
      }

// -------------------------------- add image ---------------------------------

    // add image
      this.Add = function (img, thumb, full, caption, description)
      {
        // define item
          var item = new Array();
          item["img"]           = str_replace("&amp;","&",img);
          item["thumb"]         = str_replace("&amp;","&",thumb);
          item["thumb_loaded"]  = false;
          item["full"]          = full;
          item["caption"]       = caption;
          item["description"]   = description;
          item["preloaded_img"] = null;

        // add item
          this.content[this.content.length] = item;

        // return item index
          return this.content.length-1;
      }
      
  // set tmp item
      this.SetTmpItem = function (img,  full, caption, description)
      {
        // define item
          var item = new Array();
          item["img"]           = str_replace("&amp;","&",img);
          item["thumb"]         = "";
          item["thumb_loaded"]  = false;
          item["full"]          = full;
          item["caption"]       = caption;
          item["description"]   = description;
          item["preloaded_img"] = null;

        // add item
          this.tmp_item = item;

        // return true
          return true;
      }

// -------------------------- show prev / next method --------------------------

   // show prev
      this.ShowPrev = function ()
      {
        if (this.selected>0)
        {
          mfsgallery_instance.LoadImage(this.selected-1);
        }
      }

    // show next
      this.ShowNext = function ()
      {
        if (this.selected<this.content.length-1)
        {
          mfsgallery_instance.LoadImage(this.selected+1);
        }
      }

// --------------------------------- hide box ----------------------------------

    // hide function
      this.Hide = function ()
      {
        // set displayed flag
          this.displayed = false;

        // hide gallery
          $("#mfsgallery_main_box").hide();
          $("#mfsgallery_bg").hide();

        // show flash objects
          var key;
          if (this.hide_flash.length>0)
          {
            for (key in this.hide_flash)
            {
              $("#"+this.hide_flash[key]).show();
            }
          }

      }

// --------------------------- load image at index -----------------------------

    // show image with index
      this.LoadImage = function (img_index)
      {
        // set current image index and displayed flag
          this.selected  = img_index;
          this.displayed = true;

        // load image
          if (img_index == "tmp")
          {
            document.getElementById("mfsgallery_img").src = this.tmp_item["img"];

            this.selected_img_object        = new Image;
            this.selected_img_object.src    = this.tmp_item["img"];

            if ($.browser.msie && this.tmp_item["preloaded_img"]) mfsgallery_instance.Show(false);

            this.selected_img_object.onload = function()
            {
              if (mfsgallery_instance.displayed) { mfsgallery_instance.Show(false); }
            }
          }
          else
          {
            document.getElementById("mfsgallery_img").src = this.content[this.selected]["img"];

            this.selected_img_object        = new Image;
            this.selected_img_object.src    = this.content[this.selected]["img"];

            if ($.browser.msie && this.content[this.selected]["preloaded_img"]) mfsgallery_instance.Show(false);

            this.selected_img_object.onload = function()
            {
              if (mfsgallery_instance.displayed) { mfsgallery_instance.Show(false); }
            }

           }

      }

// ------------------------- calculate image size -----------------------------

    // this.calculate
      this.CalculateImageSize = function (description_height)
      {
        // not selected img object
          if (!this.selected_img_object) return {width: 0, height: 0};

        // get screen dimension and scroll, page dimension
          this.screen_width       = $(window).width();
          this.screen_height      = $(window).height();
          this.screen_scroll_left = $(window).scrollLeft();
          this.screen_scroll_top  = $(window).scrollTop();

        // basic avalaible width
          var max_avalaible_width  = this.screen_width  - 2 * this.frame_size - 2 * this.img_frame_size;
          var max_avalaible_height = this.screen_height - 2 * this.frame_size - 2 * this.img_frame_size - description_height - this.toolbar_height - this.img_frame_size;

        // substract thumbnail row if used thubnails
          if (this.use_thumbnails && !this.selected=="tmp") max_avalaible_height = max_avalaible_height - this.thumb_img_height - 1 * this.img_frame_size;

        // aply max value cond
          max_avalaible_width  = Math.max(1, Math.min(max_avalaible_width, this.max_img_width));
          max_avalaible_height = Math.max(1, Math.min(max_avalaible_height, this.max_img_height));

        // get real image height
          var img_width  = this.selected_img_object.width;
          var img_height = this.selected_img_object.height;

        // calculate new dimension if img is bigger than avalible dimension
          if (img_width > max_avalaible_width || img_height > max_avalaible_height)
          {
            // calculage new image width
              if (img_width / img_height > max_avalaible_width / max_avalaible_height)
              {
                img_height = max_avalaible_width * img_height / img_width;
                img_width  = max_avalaible_width;
              }
            // calculage new image height
              else
              {
                img_width  = max_avalaible_height * img_width / img_height;
                img_height = max_avalaible_height;
              }
          }

        // test for min dimesion
          if (img_width < this.min_img_width && img_height <  this.min_img_height)
          {
            // calculage new image width
              if (img_width / img_height > this.min_img_width / this.min_img_height)
              {
                img_height = this.min_img_width * img_height / img_width;
                img_width  = this.min_img_width;
              }
            // calculage new image height
              else
              {
                img_width  = this.min_img_height * img_width / img_height;
                img_height = this.min_img_width;
              }
          }

        // set calculated widh / height
          this.calculated_img_width  = Math.round(img_width);
          this.calculated_img_height = Math.round(img_height);

        // return calculated size
          return {width: img_width, height: img_height};
      }


// ---------------------- calculate description height ------------------------

    this.CalculateDescriptionHeight = function()
    {
      // init width / height
        var description_width  = 0;
        var description_height = 0;
        
      // get current item
        var c_item = this.selected == "tmp" ? this.tmp_item : this.content[this.selected];

      // add catpion width
        description_width += this.description_caption_char_width * (this.caption+(c_item["caption"] ? " - " + c_item["caption"] : "")).length;

      // add description width
        description_width += this.description_char_width * c_item["description"].length;

      // calculate description height
         this.description_real_height = this.description_base_height + this.description_new_line_size * Math.ceil((description_width - this.calculated_img_width) / this.calculated_img_width);

      // return calculated height
         return this.description_real_height;

    }

// -------------------------- Set ImageBox Position ---------------------------

    this.SetImageBoxPosition = function()
    {
      var img_box = $("#mfsgallery_img_box");

      img_box.width(this.calculated_img_width + 2 * this.img_frame_size);
      img_box.height(this.calculated_img_height + 2 * this.img_frame_size);
      img_box.offset
      (
        {
          top:  this.screen_scroll_top  + this.frame_size,
          left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width - 2 * this.img_frame_size)/2)
        }
       );

    }

// ---------------------------- Set Image Position ----------------------------

    this.SetImagePosition = function()
    {
      var img = $("#mfsgallery_img");

      img.width(this.calculated_img_width);
      img.height(this.calculated_img_height);
      img.offset
      (
        {
          top:  this.screen_scroll_top  + this.frame_size + this.img_frame_size,
          left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width)/2)
        }
      );

    }

// -------------------- Set Prev and Next Button Position ---------------------

    this.SetPrevAndNextButtonPosition = function()
    {
      // get elements
        var prev_box     = $("#mfsgallery_prev_box");
        var next_box     = $("#mfsgallery_next_box");
        

      // prev button
        if (this.selected == "tmp" || this.selected<=0)
        {
          prev_box.hide();
        }
        else
        {
          prev_box.show();
          prev_box.width(Math.round(this.calculated_img_width/2));
          prev_box.height(Math.round(this.calculated_img_height));
          prev_box.offset
          (
            {
              top:  this.screen_scroll_top  + this.frame_size+this.img_frame_size,
              left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width)/2)
            }
          );
        }

      // next button
        if (this.selected == "tmp" || this.selected>=this.content.length-1)
        {
          next_box.hide();
        }
        else
        {
          next_box.show();
          next_box.width(Math.round(this.calculated_img_width/2));
          next_box.height(Math.round(this.calculated_img_height));
          next_box.offset
          (
            {
              top:  this.screen_scroll_top  + this.frame_size+this.img_frame_size,
              left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width)/2 + this.calculated_img_width/2)
            }
          );
        }

    }

// -------------------------- Set description Position -------------------------

    this.DisplayDescription = function()
    {
      // get elements
        var descr_frame  = $("#mfsgallery_description_frame");
        var description  = $("#mfsgallery_description");
        
      // get current item
        var c_item = this.selected == "tmp" ? this.tmp_item : this.content[this.selected];

      // description frame
        descr_frame.width(this.calculated_img_width+2*this.img_frame_size);
        descr_frame.height(this.description_real_height  + this.toolbar_height + 1*this.img_frame_size);
        descr_frame.offset
        (
          {
            top:  this.screen_scroll_top  + this.frame_size + 2*this.img_frame_size + this.calculated_img_height,
            left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width - 2 * this.img_frame_size)/2)
          }
        );

      // description
        description.width(this.calculated_img_width);
        description.height(this.description_real_height);
        description.offset
        (
          {
            top:  this.screen_scroll_top  + this.frame_size + 2 * this.img_frame_size + this.calculated_img_height,
            left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width)/2)
          }
        );

      // description content
        var dstr      = "";
        var dcaption  = this.caption ? this.caption + (c_item["caption"] ?  " - " + c_item["caption"] : "")
                                     : c_item["caption"];

        dstr += dcaption ? "<strong>"+dcaption+"</strong>" : "";
        dstr += c_item["description"];

        description.html(dstr);


    }


// ---------------------------- Display Numbers -------------------------------

  // display numbers
    this.DisplayNumbers = function()
    {
      // get element
        var numbers      = $("#mfsgallery_numbers");

      // set position and dimenstion
        numbers.width(this.calculated_img_width - this.hide_box_width);
        numbers.height(this.toolbar_height);
        numbers.offset
        (
          {
            top:  this.screen_scroll_top  + this.frame_size + 2*this.img_frame_size + this.calculated_img_height + this.description_real_height,
            left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width)/2)
          }
        );

      // set nubmers content
        numbers.html(this.BuildNumbers());


    }

  // build numbers method
    this.BuildNumbers = function ()
    {
      // init vars
        var nstr          = "";

      // show 1. of n
        if (!this.use_numbers)
        {
          if (this.selected != "tmp" && this.content.length>1)  nstr += (this.selected+1).toString() + " z " + this.content.length;
          else                                                  nstr += "&nbsp;";
        }

      // build number list
        else
        {

          var max_items     = Math.min(this.show_numbers_count ,Math.round((this.calculated_img_width - this.hide_box_width) / (this.selected > 100 ? this.avg_number_width_2 : this.avg_number_width_1)));
          var selected      = this.selected;
          var item_count    = this.content.length;
          var disp_from     = 0;
          var disp_to       = item_count-1;

        // total items count is greater than max visible count
          if (item_count>max_items)
          {
            disp_from  = Math.floor( Math.max (selected - max_items / 2 , 0 ) );
            disp_to    = Math.floor( Math.min (
                                                disp_from + max_items-1,
                                                item_count - 1
                                              )
                                    );
          }

        // if required add link to first image
          if (disp_from > 0)
          {
              nstr += "<a href='#'";
              nstr += i==this.selected+1?"class='selected'":"";
              nstr += "onClick=\"MFsGallery_Show("+(0).toString()+");return false;\">";
              nstr += "01";
              nstr += "</a>";

              nstr += " ... ";
          }

        // generate numbers
          for (var i=disp_from; i<=disp_to; i++)
          {
              nstr += "<a href='#'";
              nstr += i==this.selected?"class='selected'":"";
              nstr += "onClick=\"MFsGallery_Show("+(i).toString()+");return false;\">";
              nstr += ((i+1)<10?"0":"")+(i+1).toString();
              nstr += "</a>";
              nstr += (i<disp_to)?"&nbsp;&nbsp;":"";
          }

        // if required add link last image
          if (disp_to < item_count - 1)
          {
              nstr += " ... ";

              nstr += "<a href='#'";
              nstr += i==this.selected+1?"class='selected'":"";
              nstr += "onClick=\"MFsGallery_Show("+(item_count-1).toString()+");return false;\">";
              nstr += (item_count<10?"0":"")+item_count.toString();
              nstr += "</a>";

          }

        }

      // return numbers
        return nstr;
    }


// ---------------------------- Display Numbers -------------------------------

    this.SetHideButtonBoxPosition = function()
    {
      // get element
        var hide_box     = $("#mfsgallery_hide_box");

      // hide button
          hide_box.width(this.hide_box_width);
          hide_box.height(this.toolbar_height);
          hide_box.offset
          (
            {
              top:  this.screen_scroll_top  + this.frame_size + 2*this.img_frame_size + this.calculated_img_height + this.description_real_height,
              left: this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width)/2) + this.calculated_img_width - this.hide_box_width
            }
          );


    }

// --------------------------- Display Thumbnails ------------------------------

    this.DisplayThumbnails = function()
    {

      // get elements
        var thumbs_bg           = $("#mfsgallery_thumbnails_bg");
        var thumbs_frame        = $("#mfsgallery_thumbnails_frame");
        var thumbs              = $("#mfsgallery_thumbnails");
        var thumbs_scroll_left  = $("#mfsgallery_thumbs_scroll_left");
        var thumbs_scroll_right = $("#mfsgallery_thumbs_scroll_right");

      // hide thumbnails box if not used
        if (!this.use_thumbnails || this.selected == "tmp")
        {
          thumbs_bg.hide();
        }

      // display thumbnails
        else
        {
          // thumbs position
            var thumbs_top  = this.screen_scroll_top + this.frame_size + 2 * this.img_frame_size + this.calculated_img_height + this.description_real_height  + this.toolbar_height + 1*this.img_frame_size;
            var thumbs_left = this.screen_scroll_left + Math.round((this.screen_width - this.calculated_img_width - 2 * this.img_frame_size)/2);

          // thumbs bg
            thumbs_bg.width(this.calculated_img_width+2*this.img_frame_size);
            thumbs_bg.height(1*this.img_frame_size + this.thumb_img_height);
            thumbs_bg.offset
            (
              {
                top:  thumbs_top,
                left: thumbs_left
              }
            );


          // thumbs frame
            thumbs_frame.width(this.calculated_img_width - 2 * this.thumb_btn_width - 1);
            thumbs_frame.height(1*this.img_frame_size + this.thumb_img_height);
            thumbs_frame.offset
            (
              {
                top:  thumbs_top,
                left: thumbs_left + this.img_frame_size + this.thumb_btn_width
              }
            );

          // thumbs content
            thumbs.offset
            (
              {
                top:  thumbs_top,
                left: thumbs_left + this.img_frame_size + this.thumb_btn_width
              }
            );
            
          // thumbs_scroll_left
            thumbs_scroll_left.width(this.thumb_btn_width + this.img_frame_size);
            thumbs_scroll_left.height(this.thumb_img_height);
            thumbs_scroll_left.offset
            (
              {
                top:  thumbs_top,
                left: thumbs_left // + this.img_frame_size
              }
            );
            
          // thumbs_scroll_right
            thumbs_scroll_right.width(this.thumb_btn_width + this.img_frame_size);
            thumbs_scroll_right.height(this.thumb_img_height);
            thumbs_scroll_right.offset
            (
              {
                top:  thumbs_top,
                left: thumbs_left + this.calculated_img_width +  1 * this.img_frame_size - 1 * this.thumb_btn_width
              }
            );

          // if not thumbs html builded
            if (thumbs.html() == "") thumbs.html(this.BuildThumbnails());

          // focus current thumbnail
            this.FocusThumbnail();

        }
    }


  // build thumnails
    this.BuildThumbnails = function()
    {
      var tstr = "";
      var i;

      for (i=0; i<this.content.length; i++)
      {
        tstr += "<a href='#' onClick='MFsGallery_Show("+i.toString()+");return false;'>";
        tstr += "<span><span>"+((i+1)<10?"0":"")+(i+1).toString()+"</span></span>";
        //tstr += "<img id='mfsgt_"+i.toString()+"' alt='"+((i+1)<10?"0":"")+(i+1).toString()+"'  title='"+((i+1)<10?"0":"")+(i+1).toString()+"'>";
        tstr += "<img src='"+this.content[i]["thumb"]+"' id='mfsgt_"+i.toString()+"' alt='"+((i+1)<10?"0":"")+(i+1).toString()+"'  title='"+((i+1)<10?"0":"")+(i+1).toString()+"'>";
        tstr += "</a>";
      }

      return tstr;
    }

  // build thumnails
    this.FocusThumbnail = function()
    {
      // vars
        var current_thumb;

      // for each thumb
        for (var i=0; i<this.content.length; i++)
        {
          current_thumb     = $("#mfsgt_"+i.toString());

          if (this.selected==i)
          {
            // add selected class
              current_thumb.addClass("selected");
              
            // focus and sroll
              this.ThumbsScroll(0);

          }
          else
          {
            // remove selected class
              current_thumb.removeClass("selected");
          }
        }

    }
    
    this.ThumbsScroll = function(scroll_value)
    {

      // init vars
        var selected_thumb      = $("#mfsgt_"+this.selected.toString());
        var selected_thumb_pos  = selected_thumb.position();

        var last_thumb          = $("#mfsgt_"+(this.content.length-1).toString());
        var last_thumb_pos      = $("#mfsgt_"+(this.content.length-1).toString()).position();

        var thumbs_frame_pos    = $("#mfsgallery_thumbnails_frame").offset();
        var thumbs              = $("#mfsgallery_thumbnails");
        var thumbs_pos          = thumbs.offset();

        var thumb_box_width     = this.calculated_img_width - 2 * this.thumb_btn_width;

        var thumb_scroll_offset = 0;


      // if updating focus
        if (scroll_value==0)
        {
          // get thumg scroll offset
          thumb_scroll_offset = selected_thumb_pos.left - Math.round(thumb_box_width/2) + Math.round(selected_thumb.width()/2);
          
          // test right side
          if (thumb_scroll_offset > last_thumb_pos.left + last_thumb.width() - thumb_box_width)
          {
            thumb_scroll_offset = last_thumb_pos.left - thumb_box_width + last_thumb.width()
          }

          // test left side
          if (selected_thumb_pos.left <= thumb_box_width/2 - selected_thumb.width()/2)
          {
            thumb_scroll_offset = 0 ;
          }
          
        }
      // use current scroll
        else
        {
          thumb_scroll_offset = this.thumb_current_scroll + scroll_value;
          
          // test right side
          if (thumb_scroll_offset > last_thumb_pos.left + last_thumb.width() - thumb_box_width)
          {
            thumb_scroll_offset = last_thumb_pos.left - thumb_box_width + last_thumb.width()
          }

          // test left side
          if (thumb_scroll_offset<0)
          {
            thumb_scroll_offset = 0 ;
          }
        }


      // store current scroll value
        this.thumb_current_scroll = thumb_scroll_offset;


      // set thumbs position
        thumbs.offset(
                      {
                        left: thumbs_frame_pos.left - this.thumb_current_scroll,
                        top:  thumbs_pos.top
                      }
                   );
    }

    this.ThumbsScrollLeft = function(one_step)
    {
      if (one_step)
      {
        this.ThumbsScroll( -1 * Math.round(this.calculated_img_width / 2));
      }
      else
      {
        this.ThumbsScroll( -1 * this.thumb_scroll_step);
        this.thumb_scroll_timeout = setTimeout("MFsGallery_ThumbsScrollLeft(false)",this.thumb_scroll_interval);
      }
    }

    this.ThumbsScrollRight = function(one_step)
    {
      if (one_step)
      {
        this.ThumbsScroll( 1 * Math.round(this.calculated_img_width / 2));
      }
      else
      {
        this.ThumbsScroll( 1 * this.thumb_scroll_step);
        this.thumb_scroll_timeout = setTimeout("MFsGallery_ThumbsScrollRight(false)",this.thumb_scroll_interval);
      }
    }

// ------------------------- Display Bg and MainBox ----------------------------

    this.DisplayBgAndMainBox = function(updating_position)
    {
      // get elements
        var main_box     = $("#mfsgallery_main_box");
        var bg           = $("#mfsgallery_bg");

      // show bg
        if (!updating_position)
        {
          bg.show();
        }
        // now realized with css - much faster and smoother
        /*
        bg.offset({ top: 0, left: 0 });
        bg.width(screen_width);
        bg.height(screen_height);
        */


      // show main box
        if (!updating_position)
        {
          main_box.show();
          this.Show(true);
        }
        // now realized with css - much faster and smoother
        /*
          main_box.offset({ top: screen_scroll_top, left: screen_scroll_left });
          main_box.width(screen_width);
          main_box.height(screen_height);
        */

    }

// ---------------------------- Hide flash objects -----------------------------

    this.HideFlashObjects = function(updating_position)
    {
      var key;
      if (this.hide_flash.length>0) for (key in this.hide_flash)
      {
        $("#"+this.hide_flash[key]).hide();
      }
    }

// ---------------------------- preload images ---------------------------------

    this.PreloadImages = function()
    {
      // do not preload if displaying single image
        if (this.selected == "tmp") return;

      // preload prev and next images
        if (!this.use_numbers && !this.use_thumbnails)
        {
          this.PreloadPrevAndNextImage();
        }

      // preload thumnail images
        if (this.use_thumbnails)
        {
          this.PreloadThumnailsImages();
        }

      // preload numbers images
        if (this.use_numbers)
        {
          this.PreloadNumbersImages();
        }

    }

    // preload image
      this.PreloadImage = function (img_index)
      {
        if (img_index<0 || img_index>this.content.length-1) return;
        if (this.content[img_index]["preloaded_img"] == null)
        {
          this.content[img_index]["preloaded_img"]     = new Image();
          this.content[img_index]["preloaded_img"].src = this.content[img_index]["img"];
        }
      }

    // preload prev and next image
      this.PreloadPrevAndNextImage = function ()
      {
        // preload next image first
          if (this.selected<this.content.length-1)
          {
            this.PreloadImage(this.selected+1);
          }

        // preload prev image
          if (this.selected>0)
          {
            this.PreloadImage(this.selected-1);
          }
      }

    // preload thumbnail images
      this.PreloadThumnailsImages = function()
      {
          // init vars
            var i;
            
            var current_thumb;
            var current_thumb_pos;
            
            var tolerance           = 1.6*this.thumb_img_width;
            var thumb_box_width     = this.calculated_img_width - 2 * this.thumb_btn_width;
            
          // test if visible
            for (i=0; i<this.content.length; i++)
            {
              current_thumb     = $("#mfsgt_"+i.toString());
              current_thumb_pos = current_thumb.position();
              
              if  (    (current_thumb_pos.left > this.thumb_current_scroll - tolerance)
                    && (current_thumb_pos.left < this.thumb_current_scroll + thumb_box_width + this.thumb_img_width + tolerance)
                  )
              {
                 this.PreloadImage(i);
              }


            }

      }

    // preload numbers images
      this.PreloadNumbersImages = function ()
      {
        // first preload next and prev image
          this.PreloadPrevAndNextImage();

        // preaload images related to displayed numbers
          var max_items     = Math.min(this.show_numbers_count, Math.round((this.calculated_img_width - this.hide_box_width) / (this.selected>100 ? this.avg_number_width_2 : this.avg_number_width_1)));
          var selected      = this.selected;
          var item_count    = this.content.length;
          var disp_from     = 0;
          var disp_to       = item_count-1;

        // total items count is greater than max visible count
          if (item_count>max_items)
          {
            disp_from  = Math.floor( Math.max (selected - max_items / 2 , 0 ) );
            disp_to    = Math.floor( Math.min (
                                                disp_from + max_items-1,
                                                item_count - 1
                                              )
                                    );
          }

        // preload first image
          if (disp_from > 0)
          {
            this.PreloadImage(0);
          }

        // preload images
          for (var i=disp_from; i<=disp_to; i++)
          {
            this.PreloadImage(i);
          }

        // preload last image
          if (disp_to < item_count - 1)
          {
            this.PreloadImage(item_count-1);
          }

      }

// ---------------------------------- SHOW -------------------------------------

    // show current image
      this.Show = function (updating_position)
      {

        // if not selected image - exit
          if (!this.selected_img_object) return false;

        // get image dimension with basic description height
          this.CalculateImageSize(this.description_base_height);

        // calculate description size
          this.CalculateDescriptionHeight();

        // recalculate image dimension with real description_height
          this.CalculateImageSize(this.description_real_height);

        // set image box and image position
          this.SetImageBoxPosition();
          this.SetImagePosition();

        // set prev / next button
          this.SetPrevAndNextButtonPosition();

        // set description position
          this.DisplayDescription();

        // set close button position
          this.SetHideButtonBoxPosition();

        // display numbers
          this.DisplayNumbers();

        // display thumbnails
          this.DisplayThumbnails();

        // hide flash objects
          this.HideFlashObjects();

        // display bg
          this.DisplayBgAndMainBox(updating_position);

        // preload images
          this.PreloadImages();

      }

    // return object
      return this;
  }

// -------------------- functions working with gallery instance ----------------


  function MFsGallery_Add(img, thumb, full, caption, description)
  {
    return mfsgallery_instance.Add(img, thumb, full, caption, description);
  }

  function MFsGallery_SetGalleryCaption(caption)
  {
    mfsgallery_instance.caption = caption;
  }

  function MFsGallery_Show(img_index)
  {
    mfsgallery_instance.LoadImage(img_index);
  }

  function MFsGallery_ShowPrev()
  {
    mfsgallery_instance.ShowPrev();
  }

  function MFsGallery_ShowNext()
  {
    mfsgallery_instance.ShowNext();
  }

  function MFsGallery_ThumbsScrollLeft(one_step)
  {
    mfsgallery_instance.ThumbsScrollLeft(one_step);
  }

  function MFsGallery_ThumbsScrollRight(one_step)
  {
    mfsgallery_instance.ThumbsScrollRight(one_step);
  }
  
  function MFsGallery_ThumbsStopScroll()
  {
    clearTimeout(mfsgallery_instance.thumb_scroll_timeout);
  }


  function MFsGallery_Hide()
  {
    mfsgallery_instance.Hide();
  }
  
  function MFsGallery_GetImageCount()
  {
    return mfsgallery_instance.content.length;
  }
  

  function MFsGallery_ShowImage(file_name, caption, file_full)
  {
    var description = "";

    file_full       = file_full ? file_full : file_name;

    if (file_name.indexOf("http://")==-1)
    {

      file_name = str_replace(web_root_web+(web_root_web!="/"?"/":"")+"data/web/","",file_name);
      file_name = web_root_web + "inc/imgresize.php?img="+file_name+"&method=fitbox&nw=1200&nh=900";
    }

    mfsgallery_instance.SetTmpItem(file_name, file_full, caption, description);
    mfsgallery_instance.LoadImage("tmp");
  }


  function DisplayFullscreenFoto(file_name, caption, file_full)
  {
    MFsGallery_ShowImage(file_name, caption ,file_full);
  }
