$(document).ready(function ($) {

    $.Body = $('body');
    
    $.Window = $(window);
    
    $.HlavniObsah = $('#hlavniObsah');
    
    $.Scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $.Body;
    
    $.Mobile = ($.Body.hasClass('webkit-mobile') || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))),
    
    $.Unsupported = $.Body.hasClass('unsupported-browser');
    
    $('[data-controller]').Instantiate();
    
  } 
  
);
  
/* Events */

(function($) {

  $.Events = {     
     
     SECTION_ENTER: 'sectionEnter',
     
     SCROLL_TO: 'scrollTo',
     
     SCROLL: 'windowScroll',   
     
     PROOF_POINT: 'proofPointMore'
  
  } // Events  
  
})(jQuery);
  
(function($) {

  $.fn.Instantiate = function() {
      
      this.each(function() { 

          var $self = $(this),
              $controller = $self.attr('data-controller');
                  
          if ($self[$controller])
            $self[$controller]();
              
      });
      
  }

})(jQuery);

(function($) {
  
  $.fn.Scrollable = function() {
      
      this.each(function() {
          
          var $self = $(this);
              
          if ($.Mobile || $.Unsupported) {
              $self.css({backgroundAttachment:'scroll'});
          }
          else {
              $.HlavniObsah
                .bind('scroll', function(e) {
                    if ( $.inview($self) ) {
                        if ($.HlavniObsah.scrollTop() > 0) {
                            $self.triggerHandler($.Events.SCROLL);
                        }
                    }
                });
          }
      });
      
      return this;

  } //Story

  $.fn.strategie = function() {

      var $self = $(this),
          _slow = [0.5, 1], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });

      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);
          $self.find('div').each(function(index) {
              var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
	
	$.fn.onas = function() {

      var $self = $(this),
          _slow = [0.5], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });
      
      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);

          $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
							var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
	
	$.fn.wellen = function() {

      var $self = $(this),
          _slow = [1, 1, 1, 1, 1, 1], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });

      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);
          $self.find('div').each(function(index) {
              var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
  
  $.fn.kreativa = function() {

      var $self = $(this),
          _slow = [0.5], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });
      
      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);

          $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
							var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
  
  $.fn.produkce = function() {

      var $self = $(this),
          _slow = [1], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });
      
      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);

          $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
              var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
  
  $.fn.instalace = function() {

      var $self = $(this),
          _slow = [1.5], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });
      
      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);

          $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
              var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
  
  $.fn.kariera = function() {

      var $self = $(this),
          _slow = [1.5], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });
      
      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);

          $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny)').each(function(index) {
              var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              else if (_top < 0)
                  _top = 0;
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }
  
  $.fn.kontakt = function() {

      var $self = $(this),
          _slow = [3, 2, 0.5, 1, 0.1/*, 2.3*/], // cim mensi cislo, tim rychlejsi pohyb
          element_tops = [];
          
      $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny, .lupa, #mapa)').each(function(index) {
          var _top = parseInt($(this).css('top').replace('px', ''));
          element_tops[index] = _top;
      });
      
      $self
        .Scrollable()
        .bind($.Events.SCROLL, on_scroll);

      function on_scroll(e) {
          var pos_top = $.distancefromtop($self);
          $self.find('div:not(.posunoutVpravo, .posunoutVlevo), img:not(.obrazekPokoj, .arrow, .akustickeVlny, .lupa, #mapa)').each(function(index) {
              var _top = element_tops[index] - (pos_top/_slow[index]);
              if (_top > ($self.height() - $(this).height()))
                  _top = $self.height() - $(this).height();
              $(this).css({'top': _top});
          });

      }
      
      return this;
      
  }

})(jQuery);

/* Worker */

(function($) {

    var height = 700;

    $.distancefromfold = function($element) {
        var fold = height + $('#hlavniObsah').scrollTop();
        return fold - parseInt($element.css('top').replace('px', ''));
    };
    
    $.distancefromtop = function($element) {
        var id = parseInt($element.parent().attr('class').replace('radek', ''));
        return ($('#hlavniObsah').scrollTop() - (id * height));
    }
    
    $.belowthefold = function($element) {
        var id = parseInt($element.parent().attr('class').replace('radek', ''));
        var _top = id * height;
        var $hl = $('#hlavniObsah');
        var dolni_okraj = _top + height;
        if (dolni_okraj <= $hl.scrollTop() || dolni_okraj >= ($hl.scrollTop() + height)) return false;
        else return true;
    };
    
    $.rightoffold = function($element, settings) {
        var fold = 0;
        if (settings.container === undefined || settings.container === window) {
            fold = $(window).width() + $(window).scrollLeft();
        } else {
            fold = $(settings.container).offset().left + $(settings.container).width();
        }
        return fold <= $element.offset().left - settings.threshold;
    };
        
    $.abovethetop = function($element) {
        var id = parseInt($element.parent().attr('class').replace('radek', ''));
        var _top = id * height;
        var $hl = $('#hlavniObsah');
        if (_top <= $hl.scrollTop() || _top >= ($hl.scrollTop() + height)) return false;
        else return true;
    };
    
    $.leftofbegin = function($element, settings) {
        var fold = 0;
        if (settings.container === undefined || settings.container === window) {
            fold = $(window).scrollLeft();
        } else {
            fold = $(settings.container).offset().left;
        }
        return fold >= $element.offset().left + settings.threshold + $element.width();
    };
    
    $.inview = function($element) {
        var above = $.abovethetop($element),
            below = $.belowthefold($element);

        return (above || below);
    };

    $.extend($.expr[':'], {
        "below-the-fold" : "$.belowthefold(a, {threshold : 0, container: window})",
        "above-the-fold" : "!$.belowthefold(a, {threshold : 0, container: window})",
        "right-of-fold"  : "$.rightoffold(a, {threshold : 0, container: window})",
        "left-of-fold"   : "!$.rightoffold(a, {threshold : 0, container: window})"
    });
    
})(jQuery);
