/**
 * @author björn hase
 * 
 */

(function($){
  
  // classes for images
  var $a_thumbnail             = 'a.thumbnail';
  var $div_gallery_thumbnail   = 'div.gallery_thumbnail';
  
  // classes for gallery container
  var $div_gallery_overview    = 'div.gallery_overview';
  var $div_gallery_details     = 'div.gallery_details';
    
  // classes to navigate dialog
  var $a_details               = 'a.details';
  var $div_overview_empty      = 'div.overview_empty';
  var $div_details_empty       = 'div.details_empty';

  // classes to navigate gallery
  var $a_next                  = 'a.next';
  var $a_previous              = 'a.previous';
  var $a_next_detail           = 'a.next_detail';
  var $a_previous_detail       = 'a.previous_detail';
  var $div_navigation_overview = 'div.navigation_overview';
  var $div_navigation_details  = 'div.navigation_details';
  
  // classes
  var $next                    = 'next';
  var $previous                = 'previous';
  var $next_detail             = 'next_detail';
  var $previous_detail         = 'previous_detail';
  var $gallery_details         = 'gallery_details';
  var $gallery_overview        = 'gallery_overview';
  var $navigation_overview     = 'navigation_overview';
  var $navigation_details      = 'navigation_details';

  $.fn.point_gallery_iframe = function($image_per_page)
  {
    /** settings of gallery */
    var $settings = 
    {
      image_index     : 0, 
      image_index_max : $(this).find($a_thumbnail).length,  
      image_per_page  : $image_per_page, 
      page            : 1,
      page_max        : 0,
      image_width     : 1666,
      image_height    : 2500
    };

    /** init page max */
    $settings.page_max = Math.ceil($settings.image_index_max / $settings.image_per_page);

    /** init page next & previos */
    checkPageNavigation();

    /** object of function */
    var $this_object = this;

    var $div_zoom           = $(this).find('div.zoom');
    var $image_detail_1     = $(this).find('img.detail_1');
    var $image_detail_2     = $(this).find('img.detail_2');
    var $image_loading_1    = $(this).find('div.detail_loading_1');
    var $image_loading_2    = $(this).find('div.detail_loading_2');
    var $a_overview         = $(this).find('a.overview');
    var $a_download_pdf_1   = $(this).find('a.download_pdf_1');
    var $a_download_pdf_2   = $(this).find('a.download_pdf_2');
    var $div_tip            = $(this).find('div.tip');
	
	$(this).find($a_next).click(next);
	$(this).find($a_previous).click(previous);
    
    /**
     *  overview
     *  
     *  @param {Object} event
     */
    $a_overview.click(function(event)
    {
      // no link following
      event.preventDefault();
      
      $image_detail_1.hide();
      $image_detail_2.hide();
      $($this_object).find($div_gallery_thumbnail + '>' + $a_thumbnail + '[rel="' + ($settings.page) + '"]').parent().fadeIn('slow');
      
      $image_detail_1.unbind('load');
      $image_detail_2.unbind('load');

      // loading unload
      $image_loading_1.hide();
      $image_loading_2.hide();

      // download links will be hide
      $a_download_pdf_1.hide();
      $a_download_pdf_2.hide();

      $div_tip.hide();

      $a_overview.hide();
      $($this_object).find($div_overview_empty).show();
      $($this_object).find($div_details_empty).hide();
      $($this_object).find($a_details).show();

      // change class for gallery
      $($this_object).find($div_gallery_details).removeClass().addClass($gallery_overview);

      // change class for navigation
      $($this_object).find($div_navigation_details).removeClass().addClass($navigation_overview);

      $($this_object).find($a_previous_detail).removeClass().addClass($previous).unbind('click', previousDetail).bind('click', previous);
      $($this_object).find($a_next_detail).removeClass().addClass($next).unbind('click', nextDetail).bind('click', next);

      checkPageNavigation();
    });

    $image_detail_1.mousemove(zoom);
    $image_detail_2.mousemove(zoom);
    $image_detail_1.mouseout(zoomOff);
    $image_detail_2.mouseout(zoomOff);

    function zoomOff()
    {
      $div_zoom.hide();
    }

    function zoom(event)
    {
      // only function if the element is displayed
      if ($(this).css('display') != 'none')
      {
        $div_zoom.show();
      
        // offset of image 1 for x and y magniftier
        var $offset_zoom = $(this).offset();

        // x and y for the magniftier
        var zoom_x = (event.pageX - ($offset_zoom.left - 160));
        var zoom_y = (event.pageY - ($offset_zoom.top + 100));

        // offset of background
        var $offset_background_position = $(this).offset();

        // position of image
        var background_position_x = ((event.pageX - $offset_background_position.left) * (-4.85));
        var background_position_y = ((event.pageY - $offset_background_position.top) * (-5));

        if (background_position_x <= -$settings.image_width + 200) background_position_x = -$settings.image_width + 200;
        if (background_position_y <= -$settings.image_height + 200) background_position_y = -$settings.image_height + 200;
        if ($(this).hasClass('detail_2')) zoom_x += 330;

        $div_zoom.css({'top' : zoom_y + 'px', 'left' : zoom_x + 'px',
                       'background-image' : 'url(' + event.target.src + ')',
                       'background-position' : background_position_x + 'px ' + background_position_y + 'px'});
      }
    }
    
    /**
     *  details
     *
     *  @param {Object} event
     */
    $(this).find($a_details).click(function(event)
    {
      // no link following
      event.preventDefault();

      if ($settings.image_index == 0)
      {
        $image_detail_2.fadeIn('slow');
        $a_download_pdf_2.show();
      }
        else if (($settings.image_index + 1) >= $settings.image_index_max)
      {
        $image_detail_1.fadeIn('slow');
        $a_download_pdf_1.show();
      }
        else
      {
        $image_detail_1.fadeIn('slow');
        $image_detail_2.fadeIn('slow');
        $a_download_pdf_1.show();
        $a_download_pdf_2.show();
      }
      
      displayDetails();
    });
            
    /**
     *  thumbnail
     * 
     *  @param {Object} event
     */
    $(this).find($a_thumbnail).click(function(event)
    {
      // no link following
      event.preventDefault();
      
      // index of image
      $settings.image_index = $($this_object).find($a_thumbnail).index(this);
      
      // path of the image
      if (($settings.image_index + 1) % 2 == 0) 
      {
        if ($settings.image_index + 1 == $settings.image_index_max)
        {
          setDetail($image_detail_1, $a_download_pdf_1, $image_loading_1, $settings.image_index);
          $a_download_pdf_1.show();
        }
          else
        {
          setDetail($image_detail_1, $a_download_pdf_1, $image_loading_1, $settings.image_index);
          setDetail($image_detail_2, $a_download_pdf_2, $image_loading_2, ++$settings.image_index);
          $a_download_pdf_1.show();
          $a_download_pdf_2.show();
        }
      } 
        else 
      {
        if ($settings.image_index == 0)
        {
          setDetail($image_detail_2, $a_download_pdf_2, $image_loading_2, $settings.image_index);
          $a_download_pdf_2.show();
        }
          else
        {
          setDetail($image_detail_1, $a_download_pdf_1, $image_loading_1, $settings.image_index - 1);
          setDetail($image_detail_2, $a_download_pdf_2, $image_loading_2, $settings.image_index);
          $a_download_pdf_1.show();
          $a_download_pdf_2.show();
        }
      }

      displayDetails();
    });
    
    /**
     * 
     */
    function displayDetails()
    {
      // hide all small pictures
      $($this_object).find($div_gallery_thumbnail).hide();

      $($this_object).find($a_details).hide();
      $($this_object).find($div_overview_empty).hide();
      $($this_object).find($div_details_empty).show();
      $a_overview.show();

      // change class for gallery
      $($this_object).find($div_gallery_overview).removeClass().addClass($gallery_details);
      
      // change class for navigation
      $($this_object).find($div_navigation_overview).removeClass().addClass($navigation_details);
      
      $($this_object).find($a_previous).removeClass().addClass($previous_detail).unbind('click', previous).bind('click', previousDetail);
      $($this_object).find($a_next).removeClass().addClass($next_detail).unbind('click', next).bind('click', nextDetail);

      $div_tip.show();

      checkDetailsNavigation();
    }
       
    /** 
     *  check Image Navigation  
     */
    function checkDetailsNavigation()
    {
      (($settings.image_index >= ($settings.image_index_max - 1)) ? $($this_object).find($a_next_detail).hide() : $($this_object).find($a_next_detail).css({'display' : 'block'}));
      (($settings.image_index <= 0) ? $($this_object).find($a_previous_detail).hide() : $($this_object).find($a_previous_detail).css({'display' : 'table-cell'}));
    }
  
    /**
     *  check page navigation
     * 
     */
    function checkPageNavigation()
    {
      (($settings.page >= $settings.page_max) ? $($this_object).find($a_next).hide() : $($this_object).find($a_next).css({'display' : 'table-cell'}));
      (($settings.page == 1) ? $($this_object).find($a_previous).hide() : $($this_object).find($a_previous).css({'display' : 'table-cell'}));
    }

    /**
     *
     */
    function previousDetail(event)
    {
      if ($settings.image_index >= 1)
      {
        $image_detail_1.unbind('load').hide();
        $image_detail_2.unbind('load').hide();
        $image_loading_1.hide();
        $image_loading_2.hide();
        $a_download_pdf_1.hide();
        $a_download_pdf_2.hide();

        if ($settings.image_index > 2)
        {
          setDetail($image_detail_2, $a_download_pdf_2, $image_loading_2, --$settings.image_index);
          setDetail($image_detail_1, $a_download_pdf_1, $image_loading_1, --$settings.image_index);
        }
          else
        {
          $settings.image_index = 0;
          setDetail($image_detail_2, $a_download_pdf_2, $image_loading_2,  $settings.image_index);
        }

        checkDetailsNavigation();
      }
    }

    function nextDetail(event)
    {
      if ($settings.image_index < $settings.image_index_max)
      {
        $image_detail_1.unbind('load').hide();
        $image_detail_2.unbind('load').hide();
        $image_loading_1.hide();
        $image_loading_2.hide();
        $a_download_pdf_1.hide();
        $a_download_pdf_2.hide();
        
        if (($settings.image_index) % 2 == 0)
        {
          ++$settings.image_index;
        }
          else
        {
          ++$settings.image_index;
          ++$settings.image_index;
        }
        
        if (($settings.image_index + 2) < $settings.image_index_max)
        {
          setDetail($image_detail_1, $a_download_pdf_1, $image_loading_1, $settings.image_index);
          setDetail($image_detail_2, $a_download_pdf_2, $image_loading_2, ++$settings.image_index);
        }
          else
        {
          $settings.image_index = $settings.image_index_max - 1;
          setDetail($image_detail_1, $a_download_pdf_1, $image_loading_1, $settings.image_index);
        }

        checkDetailsNavigation();
      }
    }

    function previous(event)
    {
      if (($settings.page) > 1) 
      {
        setOverview(--$settings.page);
        checkPageNavigation();
      }
    }
  
    function next(event)
    {
      if (($settings.page) < $settings.page_max) 
      {
         setOverview(++$settings.page);
         checkPageNavigation();
      }
    }

    /**
     *  setting detail images
     *  
     *  @param $image_detail object of detail
     *  @param $a_download_pdf name of pdf
     *  @param $image_loading name of pdf
     *  @param $index index of image
     */
    function setDetail($image_detail, $a_download_pdf, $image_loading, $index)
    {
      // path of
      var $image_path = $($this_object).find($a_thumbnail + ':eq(' + ($index) + ')').attr('href');
         
      $image_detail.hide();

      if ($settings.image_index == 0)
      {
        $image_loading.css({'float': 'right'});
        $image_detail.css({'float': 'right'});
      }
        else if ($image_detail.css('float') == 'right')
      {
        $image_loading.css({'float': 'left'});
        $image_detail.css({'float': 'left'});
      }

      // if image is not loaded the loading
      // function will be started
      if ($image_detail.attr('src') != $image_path)
      {
        $image_loading.show();

        // load event will be bind to image detail
        $image_detail.bind('load', function(event)
        {
          $image_loading.hide();
          $(this).fadeIn("slow");
        });
        
        $image_detail.attr('src', $image_path).attr('alt', $image_path);
        $a_download_pdf.attr('href', $image_path.replace('.jpg', '.pdf')).html('Seite ' + ($index + 1) + ' als PDF').show();
      }
        else
      {
        $image_detail.fadeIn('slow');
        $a_download_pdf.show();
      }
    }
    
    /**
     *  setting thumbnails for overview
     *
     *  @param $page number of page
     */
    function setOverview($page)
    {
      $($this_object).find($div_gallery_thumbnail).hide();
      $($this_object).find($div_gallery_thumbnail + '>' + $a_thumbnail + '[rel="' + ($page) + '"]').parent().show();
    }
  }
    
})(jQuery);
