$(document).ready(
  function(){
      screenWidth = $(document).width();
      imgHeight   = $('div.pruh-galerie').find('img').height();
      imgResize   = 0;
      opaH        = 1.0;
      opaO        = 0.2;

      $.fn.extend($.tooltip.defaults, { track: true, delay: 120, showURL: false, fixPNG: true, fade: 150, top: 15, left: 25 });

/////////////////////////////////
// default state
      if($('div.pruh-galerie').length > 1) {
          $('div.pruh-galerie').find('img').css('opacity', opaO);
      }
      if($('.next, .prev').length) {
          $('.next, .prev').hide();
      }
      if($('.info').length) {
          $('.info').hide();
      }

////////////////////////
//galerie width + height
      imgResize = getHeight();
      $('div.pruh-galerie').each(function() {
          $(this).imgHeightToTarget();
      });
      $('div.pruh-galerie ul').each(function() {
          $(this).ulWidth();
          $(this).dragArea();
      });

      $(window).resize(function() {
          var newImgResize = getHeight();
          
          if(newImgResize != imgResize) {
              imgResize = newImgResize;
              $('div.pruh-galerie').each(function() {
                  $(this).imgHeightToTarget();
              });
          }
          
          $('div.pruh-galerie ul').each(function() {
              $(this).ulWidth();
              $(this).dragArea();
          });
      });

////////////////
// pruh-galerie highlight
    if($('div.pruh-galerie').length > 1) {

////////////////
// URL hash
        if(document.location.hash!='') { //alert($(location).attr('href'));
            var hash = document.location.hash;
            var galerieId = hash.substr(1);
            // submenumenu highlight
            $('a.bgr-'+galerieId).parent().addClass('add-'+galerieId);
            // highlight selected
            $('img', 'div#'+galerieId).fadeTo('slow', opaH);
        }
  
////////////////
// REALIZACE+ARCHIV submenu hover
        $('div.submenu a').hover( 
          function() {
            var hoverClass = $(this).attr('class');
            var currentId = hoverClass.replace('bgr-','');
            // highlight reset
            $('img', 'div.pruh-galerie').fadeTo('fast', opaO);
            // highlight selected
            $('img', 'div#'+currentId).fadeTo('slow', opaH);
            // submenumenu reset highlight
            $('li', 'div.submenu').removeClass();
          },function() {
            var hoverClass = $(this).attr('class');
            var currentId = hoverClass.replace('bgr-','');
            // highlight reset
            $('img', 'div#'+currentId).fadeTo('fast', opaO);
          }
        );
    }

/////////////////////
// pruh-galerie hover
    if($('div.pruh-galerie').length > 1) {
        $('div.pruh-galerie').hoverIntent( galleryHover, galleryOut );
    }

//////////
// tooltip
      $('div.pruh-galerie').each(function() {
          var currentId = $(this).attr('id');
          $(this).find('img').tooltip({ extraClass: 'bgr-'+currentId+'-selected' });
      });

//////////////////////
// scrollto visibility
// draggable
    $('div.obal-galerie, div.obal-galerie-full').hover(
      function(){
        var ulOffset = $(this).find('ul').offset();
	      var ulStart = ulOffset.left;
        var ulEnd = $(this).find('ul').width()+ulStart;

        if(ulEnd>screenWidth) {
          $(this).find('.next').fadeIn('fast');
        }
        if(ulStart<0) {
          $(this).find('.prev').fadeIn('fast');
        }

        if(ulEnd>screenWidth || ulStart<0) {
            $(this).droppable({ accept: '.draggable', drop: function(event, ui){} });
            var container = $(this).find('.obsah-galerie');

		        $('.draggable').draggable({
              delay: 5,
              axis: 'x',
              containment: container,
              scroll: false,
              start: function() {
                  var Startpos = $(this).position();
                  start_left = Startpos.left;
              },
              stop: function() {
                  var Stoppos = $(this).position();
                  var Emptyspace = $(document).width() - ($(this).width() + Stoppos.left);
                  if(Stoppos.left < 0) {
                      $(this).parent().parent().find('.prev').fadeIn('fast');
                    
                      if(Emptyspace >= 0) {
                          $(this).parent().parent().find('.next').fadeOut('fast');
                          $(this).animate({ left: '+='+Emptyspace }, {duration: 'slow', easing: 'easeOutExpo'});
                      }
                      else {
                      $(this).parent().parent().find('.next').fadeIn('fast');
                        if(start_left > Stoppos.left){
                          $(this).animate({ left: '-=25' }, {duration: 'slow', easing: 'easeOutExpo'});
                        }
                        else if(start_left < Stoppos.left){
                          $(this).animate({ left: '+=25' }, {duration: 'slow', easing: 'easeOutExpo'});
                        }
                      }
                  }
                  if(Stoppos.left >= 0) {
                      $(this).parent().parent().find('.next').fadeIn('fast');
                      $(this).parent().parent().find('.prev').fadeOut('fast');
                      $(this).animate({ left: '-='+Stoppos.left }, {duration: 'slow', easing: 'easeOutExpo'});
                  }
               }
            });
	        }
      },
      function(){
        $('.next, .prev', this).fadeOut('fast');
      }
    );

//////////////////////
//scrollto hover
    if($('.next, .prev').length) {
        $('.next, .prev').hoverIntent( scrollHover, scrollOut );
    }


//REFERENCE DETAIL
    if($('.info').length) {
        $('h1').css('cursor', 'pointer');
        $('h1').append('<span class="title-on">(zobrazit podrobné informace)</span>');
        startHeight=$('div.pruh-galerie img').height();

        $('h1').click(function() {
              var ulOffset = $('div.pruh-galerie ul').offset();

              if($('.title-on').length) {
                $('.title-on').remove();
                $(this).append('<span class="title-off">(skrýt podrobné informace)</span>');
              }
              else if($('.title-off').length) {
                $('.title-off').remove();
                $(this).append('<span class="title-on">(zobrazit podrobné informace)</span>');
              }

              $('div.info').toggle('slow');

              if($('div.obal-galerie-full').length) {
                $('div.obal-galerie-full').stop(true, true).removeClass().addClass('obal-galerie');
                $('div.pruh-galerie img').each(function() {
                  var myWidth=$(this).width()*135/$(this).height();
                  $(this).css({ width: myWidth+'px', height: '135px' });
                });
                $('div.pruh-galerie ul').ulWidth();
                if(ulOffset.left<0) {
                  var posun = ulOffset.left*(240/625)/2;
                  $('div.pruh-galerie ul').css({'position' : 'relative', 'left': posun });
                }
              }
              else if($('div.obal-galerie').length) {
                $('div.obal-galerie').stop(true, true).removeClass().addClass('obal-galerie-full');
                $('div.pruh-galerie img').each(function() {
                  var myWidth=$(this).width()*startHeight/$(this).height();
                  $(this).css({ width:myWidth, height: startHeight+'px' });
                });
                $('div.pruh-galerie ul').ulWidth();
                if(ulOffset.left<0) {
                  var posun = ulOffset.left*(625/240)*2;
                  $('div.pruh-galerie ul').css({'position' : 'relative', 'left': posun });
                }
              }
              if($('div.pruh-galerie ul').width() > screenWidth) {
                $('div.pruh-galerie ul').dragArea();
              }
              $('div.pruh-galerie').imgHeightToTarget();
        });
    }


  }
); // close document.ready

(function( $ ){
  $.fn.longScroll = function( options ) {
    var settings = {
      target: '+=500',
      duration: 3500,
      cnt: 1,
      hideScroll: 'next'
    };
    if ( options ) { 
      $.extend( settings, options );
    }
    var cnt = settings.cnt;
    $(this).scrollTo( settings.target, settings.duration, {
        easing:'linear', 
        axis:'x', 
        onAfter:function(){
          if(cnt > 1){
            --cnt;
            $(this).longScroll( {target: settings.target, duration: settings.duration, cnt: cnt, hideScroll: settings.hideScroll} );
          }
          else {
            $(this).parent().find('img.'+settings.hideScroll).fadeOut('fast');
          }
        }
    });
    return false;
  }
})( jQuery );

(function( $ ){
  $.fn.ulWidth = function( options ) {
    var settings = {
      padding: 2
    };
    if ( options ) { 
      $.extend( settings, options );
    }
    var ulWidth=0;
    $(this).find('li').each(function() {
      myWidth=$(this).find('img').width();
      $(this).css({ width: myWidth });
      ulWidth+=myWidth+settings.padding;
    });
    $(this).css({ width: ulWidth });
    return false;
  }
})( jQuery );

(function( $ ){
  $.fn.imgHeightToTarget = function( options ) {
    var settings = {
        margin: 1,
    };
    if ( options ) { 
        $.extend( settings, options );
    }

    $(this).find('img').each(
      function() {
        myHeight=$(this).height();
        var myWidth=myHeight;
        if ( (myHeight + imgResize > 20 && imgResize < 0) || (imgHeight > myHeight && imgResize > 0) ) {
            myHeight = myHeight + imgResize - settings.margin;
            myWidth = $(this).width() * (myHeight / myWidth);
            $(this).height(myHeight);
            $(this).width(myWidth);
            $(this).parent().width(myWidth+settings.margin);
        }
        return myHeight;
      }
    );
    $(this).css({ height: myHeight+settings.margin });
    return false;
  }
})( jQuery );

(function( $ ){
  $.fn.dragArea = function( options ) {
    var settings = {
      margin: 100
    };
    if ( options ) { 
      $.extend( settings, options );
    }

    var screenWidth = $(document).width();
    var liWidth = $(this).find('li').outerWidth();
    var liSize = $(this).find('li').length;
    var ulWidth = liSize * liWidth;

    if(ulWidth > screenWidth) {
      var wrapWidth = ulWidth*2-screenWidth+2*settings.margin;
      var wrapLeft = screenWidth-ulWidth-settings.margin;
      $(this).parent().prev().css({ width: wrapWidth, position: 'relative', left: wrapLeft });
    }
  }
})( jQuery );

(function( $ ){
  $.fn.imgEm = function() {
    $(this).css('line-height', '1px');
    $(this).css('font-size', '100px');
    $(this).find('img').each(function() {
      var width = $(this).width() / 100 + "em";
      var height = $(this).height() / 100 + "em";
      $(this).css("width", width); 
      $(this).css("height", height);
    });
  }
})( jQuery );


function getHeight(){
  var screenHeight = $(window).height();
  var totalHeight = $('body').outerHeight( true );
  var restHeight = screenHeight - totalHeight;
  var imgResize = restHeight / $('div.pruh-galerie').length;
  return imgResize;
}

function galleryHover(){
        var galerieId = $(this).attr('id');
        $('div#'+galerieId+' img').fadeTo('slow ', opaH);
        $('div.pruh-galerie img').not('div#'+galerieId+' img').fadeTo('fast', opaO);
        $('li', 'div.submenu').removeClass();
        $('a.bgr-'+galerieId).parent().addClass('add-'+galerieId);
        $('#tooltip').removeClass().addClass('bgr-'+galerieId+'-selected');
}
function galleryOut(){
        var currentId = $(this).parents().eq(3).attr('id');
        $('div#'+currentId+' img').stop(true, true);
}
function scrollHover(){
        var myDiv = $('div#'+$(this).parent().find('div[id]').attr('id')), goWhere = $(this).attr('class');
        var hidden_left = -(myDiv.find('ul').offset().left);
        var hidden_right = myDiv.find('ul').width() - screenWidth - hidden_left;
        var distance = 500, duration = 3500;

        switch(goWhere){
          case 'prev':
            var goBack = 'next', cnt = hidden_left/distance;
            if(hidden_left < distance) distance = hidden_left;
            var target = '-='+distance;
            break;
          default:
            var goBack = 'prev', cnt = hidden_right/distance;
            if(hidden_right < distance) distance = hidden_right;
            var target = '+='+distance;
            break;
        }

        myDiv.longScroll( {target: target, duration: duration, cnt: cnt, hideScroll: goWhere} );
        myDiv.find('img.'+goBack).fadeIn('fast');
}
function scrollOut(){
        var myDiv = $('div#'+$(this).parent().find('div[id]').attr('id'));
        myDiv.stop();
}

