/* Initialize */
jQuery(  
  function ($) {
    $.Body = $('body');    
    $.Window = $(window);    
    $.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');    
    $.Body
    .Keyboard()    
    $('[data-controller]').Instantiate();
    // Draw Lines
    // Bug with IE canvas implementation requires onload
    if ($.browser.msie)
      $(window).bind('load',function(){$('[data-canvasline]').Line();})
    else
      $('[data-canvasline]').Line();
    $('[data-target=_blank]').TargetBlank();    
  }   
);
/* ---------------------------------- */
/* Events */
(function($) {
  $.Events = {  
     SECTION_ENTER: 'sectionEnter', 
     SCROLL_TO: 'scrollTo',
     SCROLL: 'windowScroll',
     SCROLL_ENTER: 'windowScrollEnter',
     SCROLL_LEAVE: 'windwScrollLeave',
     KEY_UP: 'keyUp',
     KEY_DOWN: 'keyDown',
     KEY_LEFT: 'keyLeft',
     KEY_RIGHT: 'keyRight',
     KEY_ESC: 'keyEsc',
     KEY_SPACE: 'keySpace',
     PROOF_POINT: 'proofPointMore'
  } // Events  
  $.Views = {
  } // Views 
})(jQuery);
/* ---------------------------------- */
/* Auto Instantiate */
(function($) {
  $.fn.Instantiate = function(settings) {     
    var config = {}; 
    if (settings) $.extend(config, settings);  
      this.each(function() { 
          var $self = $(this),
              $controller = $self.attr('data-controller');                  
          if ($self[$controller])
            $self[$controller]();              
      });      
  }
})(jQuery);
/* ---------------------------------- */
/* Shell */
(function($) {
  $.fn.SHELL = function(settings) {   
    var config = {};
    if (settings) $.extend(config, settings);  
      this.each(function() {          
      });  
      return this;
  }
})(jQuery); 
/* ---------------------------------- */
/* Share */
(function($) {
  $.fn.Line = function(settings) {
    var config = {};
    if (settings) $.extend(config, settings);
      this.each(function() {  
        var $self = $(this),
            $canvas = $self.find('canvas'),
            $canvas_id = $canvas.attr('id'),
            $target = $self.attr('data-target'),
            $top = $self.attr('data-top'),
            $coord = $self.attr('data-coord') ? $self.attr('data-coord').split(',') : new Array(0,0,0,0),
            _canvas_element = document.getElementById($canvas_id),
            _canvas_context = _canvas_element.getContext('2d'),
            _height = 0;
        $self.css({top: parseInt($top), height: parseInt($coord[3]), marginBottom:0, width: 900,overflow:'hidden'})
        $canvas.attr({height: parseInt($coord[3]),width: 900})
        $.html5.canvas.draw.dashedline(_canvas_context, parseInt($coord[0]), parseInt($coord[1]), parseInt($coord[2]), parseInt($coord[3]), _canvas_element,6);
      }); 
      return this;
  }
})(jQuery); 

/* ---------------------------------- */
/* Scrollable */
(function($) {
  $.fn.Scrollable = function(settings) {   
     var config = { threshold: -100, offset_scroll: 6, offset_intertia: .15 }; 
     if (settings) $.extend(config, settings);    
     this.each(function() {       
        var $self = $(this),
            $id = $self.attr('id');            
        config.threshold = 0        
        if ($.Mobile || $.Unsupported) {  
          $self.css({backgroundAttachment:'scroll'})
        }else{        
        $.Window
          .bind('scroll',
            function(e){
             if ( $.inview($self,{threshold:config.threshold}) ) {                
                if (!$self.hasClass('_active')){                
                  $self.addClass('_active');                  
                  if (config.is_nav)
                    $.Body.triggerHandler($.Events.SECTION_ENTER,$id);                  
                  $self.triggerHandler($.Events.SCROLL_ENTER);                  
                }        
                _scroll_background();                  
                $self.triggerHandler($.Events.SCROLL,$.distancefromfold($self,{threshold:config.threshold}) - config.threshold)                
              }else{                
                if ($self.hasClass('_active')){                
                  $self.removeClass('_active');                  
                  $self.triggerHandler($.Events.SCROLL_LEAVE);                  
                }              
              }         
            })      
        }   
        function _scroll_background() {
          var _x = '50% '                  
          var bpos = _x + (-($.distancefromfold($self,{threshold:config.threshold}) - config.threshold) * config.offset_intertia) + 'px';          
          $self.css({'backgroundPosition':bpos})
        }
        
        if (config.auto_scroll)
          _scroll_background();
            
     });     
    return this;
     
  } //ecran
  
// INNOVATION  

   //accueil Innovation
  $.fn.home = function() {
     this.each(function() { 
        var $self = $(this),
            $id = $self.attr('id'),
			$span = $self.find('span'),
            _threshold = -200;
        $self
          .Scrollable({threshold: _threshold,is_nav:true,auto_scroll:true})  
		  
		 function on_scroll(e,distance) {  
          $span
          .css({marginTop:75-Math.floor(distance/10)})    
        }
    
     });
    return this;   
  } 
  
  //about
  $.fn.about = function() {
     this.each(function() { 
        var $self = $(this),
            $bg = $self.find('.bg'),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)
          .bind($.Events.SCROLL_ENTER,on_scroll_enter)
          .bind($.Events.SCROLL_LEAVE,on_scroll_leave);      
        function on_scroll(e,distance) {        
          var bpos = '70% ' + ($.Window.height()/2.5-distance/2.5) + 'px';                  
          $bg.css({'backgroundPosition':bpos})          
        }
        function on_scroll_enter(e) {        
        }        
        function on_scroll_leave(e) {        
        }           
     }); 
    return this;  
  } 
  
  //Product
  $.fn.product = function() {
     this.each(function() { 
        var $self = $(this),
            $id = $self.attr('id'),
            $bg = $self.find('.bg'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)               
        function on_scroll(e,distance) {          
           var bpos = '70% ' + (200+$.Window.height()/2.5-distance/3.5) + 'px';                  
           $bg.css({'backgroundPosition':bpos})  
        }             
     });   
    return this;   
  } 
  
  
  //Work
  $.fn.work = function() {   
     this.each(function() {       
        var $self = $(this),
            $bg = $self.find('.bg'),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)
          .bind($.Events.SCROLL_ENTER,on_scroll_enter)
          .bind($.Events.SCROLL_LEAVE,on_scroll_leave);      
        function on_scroll(e,distance) {        
          var bpos = '70% ' + ($.Window.height()/2.5-distance/2.5) + 'px';                  
          $bg.css({'backgroundPosition':bpos})          
        }
        function on_scroll_enter(e) {        
        }        
        function on_scroll_leave(e) {        
        }           
     }); 
    return this;     
  } 
  
  //Client
  $.fn.client = function() {   
     this.each(function() {       
        var $self = $(this),
            $id = $self.attr('id'),
            $bg = $self.find('.bg'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)               
        function on_scroll(e,distance) {          
           var bpos = '70% ' + (200+$.Window.height()/2.5-distance/2.5) + 'px';                  
           $bg.css({'backgroundPosition':bpos})  
        }             
     });     
    return this;     
  } 
  
  //Services 
  $.fn.services = function() {   
     this.each(function() {       
        var $self = $(this),
            $id = $self.attr('id'),
            $bg = $self.find('.bg'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)               
        function on_scroll(e,distance) {          
           var bpos = '70% ' + (200+$.Window.height()/2.5-distance/3.5) + 'px';                  
           $bg.css({'backgroundPosition':bpos})  
        }             
     });   
    return this;     
  } 
  
  //Jobs
  $.fn.jobs = function() {   
	
     this.each(function() {       
        var $self = $(this),
            $bg = $self.find('.bg'),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)
          .bind($.Events.SCROLL_ENTER,on_scroll_enter)
          .bind($.Events.SCROLL_LEAVE,on_scroll_leave);      
		  
        function on_scroll(e,distance) {   
			
          	var bpos = '70% ' + ($.Window.height()/2.5-distance/2.5) + 'px';                  
          	$bg.css({'backgroundPosition':bpos}) ;
			
			/*$('#box-content-job li').each(function () {
				var $div = $('> div.viewdetail', this);
			
				$(this).mouseenter(function() {//Mouseover, fadeIn the hidden hover class	
					$div.stop(false,true).fadeIn('slow');
				});
				$(this).mouseleave(function() {
					$div.stop(false,true).fadeOut('slow');
				});
			});
			
			$('#so li').each(function () {
				$(this).append('<div class="hover"><\/div>');
				var $div = $('> div.hover', this);
		
				$(this).mouseenter(function() {//Mouseover, fadeIn the hidden hover class	
					$div.stop(true,true).slideDown('slow');
				}); 
				$(this).mouseleave(function() {
					$div.stop(true,true).slideUp('slow');
				});
			});*/
        }
        function on_scroll_enter(e) {      
        }        
        function on_scroll_leave(e) { 
			/*$('#box-content-job li').each(function () {
				$(this).unbind('mouseenter').unbind('mouseleave');
			});
			
			$('#so li').each(function () {
				$(this).find('div.hover').remove();
				$(this).unbind('mouseenter').unbind('mouseleave');
			});*/
        }           
     }); 
    return this;     
  } 
  
    //Contact Us
  $.fn.contactus = function() {   
     this.each(function() {       
        var $self = $(this),
            $bg = $self.find('.bg'),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)
          .bind($.Events.SCROLL_ENTER,on_scroll_enter)
          .bind($.Events.SCROLL_LEAVE,on_scroll_leave);      
        function on_scroll(e,distance) {        
          var bpos = '70% ' + ($.Window.height()/2.5-distance/2.5) + 'px';                  
          $bg.css({'backgroundPosition':bpos})          
        }
        function on_scroll_enter(e) {        
        }        
        function on_scroll_leave(e) {        
        }           
     }); 
    return this;     
  } 
  
//   
   //accueil Stec
  $.fn.accueilStec = function() {
     this.each(function() { 
        var $self = $(this),
            $bg = $self.find('.bg'),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})        
     }); 
    return this;  
  } 
  
 
  //
  $.fn.graissesStec = function() {
     this.each(function() { 
        var $self = $(this),
            $header = $self.find('header'),
            $bg = $self.find('.bg'),
            $h1 = $self.find('h1'),
            $h2 = $self.find('h2'),
            $id = $self.attr('id'),
            $img = $self.find('img'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)
          .bind($.Events.SCROLL_ENTER,on_scroll_enter)
          .bind($.Events.SCROLL_LEAVE,on_scroll_leave);      
        function on_scroll(e,distance) {        
          var bpos = '0px ' + '-'+($.Window.height()/2-distance/5) + 'px';                  
          $bg.css({'backgroundPosition':bpos})          
        }
        function on_scroll_enter(e) {        
        }        
        function on_scroll_leave(e) {        
        }           
     }); 
    return this;  
  } 
  
  //Galvanisation Stec
  $.fn.galvanisationStec = function() {
     this.each(function() { 
        var $self = $(this),
            $id = $self.attr('id'),
            _threshold = -200;
        $self
          .Scrollable({threshold: _threshold,is_nav:true})      
     });
    return this;   
  } 
  
  
  //Lubrifiants Stec 
  $.fn.lubrifiantsStec = function() {   
     this.each(function() {       
        var $self = $(this),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})            
     });     
    return this;     
  } 
  
  //Produits Spécifiques Stec
  $.fn.produitsSpecifiquesStec = function() {   
     this.each(function() {       
        var $self = $(this),
            $id = $self.attr('id'),
            $bg = $self.find('.bg'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)               
        function on_scroll(e,distance) {          
           var bpos = '90% ' + (200+$.Window.height()/2-distance/3) + 'px';                  
           $bg.css({'backgroundPosition':bpos})  
        }             
     });     
    return this;     
  } 
  
  
// MRO  
   //accueil MRO
  $.fn.accueilMRO = function() {
     this.each(function() { 
        var $self = $(this),
            $bg = $self.find('.bg'),
            $id = $self.attr('id'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})        
     }); 
    return this;  
  } 
  
 
  //Solvants Nettoyants Industriels
  $.fn.SolvantsNettoyantsIndustriels = function() {
     this.each(function() { 
        var $self = $(this),
            $header = $self.find('header'),
            $bg = $self.find('.bg'),
            $h1 = $self.find('h1'),
            $h2 = $self.find('h2'),
            $id = $self.attr('id'),
            $img = $self.find('img'),
            _threshold = -200;        
        $self
          .Scrollable({threshold: _threshold,is_nav:true})
          .bind($.Events.SCROLL,on_scroll)
          .bind($.Events.SCROLL_ENTER,on_scroll_enter)
          .bind($.Events.SCROLL_LEAVE,on_scroll_leave);      
        function on_scroll(e,distance) {        
          var bpos = '0px ' + ($.Window.height()/2.5-distance/2.5) + 'px';                  
          $bg.css({'backgroundPosition':bpos})          
        }
        function on_scroll_enter(e) {        
        }        
        function on_scroll_leave(e) {        
        }           
     }); 
    return this;  
  } 
  
  //Nettoyants Poudres Industriels
  $.fn.NettoyantsPoudresIndustriels = function() {
     this.each(function() { 
        var $self = $(this),
            $id = $self.attr('id'),
            _threshold = -200;
        $self
          .Scrollable({threshold: _threshold,is_nav:true})      
     });
    return this;   
  }   
  
   //footer
  $.fn.contact = function() {
     this.each(function() { 
        var $self = $(this),
            $id = $self.attr('id'),
			$span = $self.find('span'),
            _threshold = -200;
        $self
          .Scrollable({threshold: _threshold,is_nav:true,auto_scroll:true})  
		  
		 function on_scroll(e,distance) {  
          
		  $span
          .css({marginTop:75-Math.floor(distance/10)})    
        }
    
     });
    return this;   
  } 
    
})(jQuery);



/* ---------------------------------- */

/* MainNav */

(function($) {

  
   $.fn.MainNav = function() {
   
     this.each(function() { 
      
        var $self = $(this),
            $ul = $('<ul/>').appendTo($self),
            $sections = $('[data-nav]'),
            _sections = new Array(),
            $navs = new Array(),
            _active = 0;
            
            
       if (!$.Mobile && !$.Unsupported) { 
        $sections.each(
          function(i){
            _sections.push($(this))
            $('<li/>').appendTo($ul).DotNav({id:$(this).attr('id'),name:$(this).attr('data-nav')});
        
          })
          
        $self.css({marginTop:-$self.height()/2})
        
        }
        
        $.Body
          .bind($.Events.SECTION_ENTER,
            function(e,id){
              
              $sections.each(
                function(i){
                  if ($(this).attr('id')==id)
                    _active = i;
              
                })
            
            })
          .bind($.Events.KEY_RIGHT,
            function(e){
              _active++;
              if (_active>$sections.length-1)
                _active=$sections.length-1;
              _seek();
          })
          .bind($.Events.KEY_LEFT,
            function(e){
              _active--;
              if (_active<0)
                _active=0;
              _seek()
              
          })
          
          
          function _seek() {
            $.Body.triggerHandler($.Events.SCROLL_TO,_sections[_active].attr('id'))
          }
        
            
     });
     
    return this;
     
  } // Main Nav
  
  $.fn.DotNav = function(settings) {
     
    var config = {};
 
    if (settings) $.extend(config, settings);
   
     this.each(function() { 
      
        var $self = $(this),
            $a = $('<a/>'),
            $h1 = $('<h1/>').appendTo($self),
            $span = $('<span/>').html(config.name).appendTo($h1),
            $id = config.id;

            $a
              .attr('href',"#/"+config.name.split(' ').join('_'))
              .html($id)
              .appendTo($self)
              .bind('click',
                function(e){                  
                  $.Body.triggerHandler($.Events.SCROLL_TO,$id)                  
                  e.preventDefault();                  
                })            
            $self
              .attr('data-id',$id);    
        $a
          .bind('mouseenter',
            function(e) {
              if ($('._playing').length==0)
              if ($.browser.msie)
                $h1.stop().css({display:'block',left: 30})
              else
                $h1.stop().css({display:'block'}).animate({left:30,opacity:1},450,'easeOutQuart')
            })
          .bind('mouseleave',
            function(e) {
              if ($.browser.msie)
                $h1.stop().css({display:'none',left: 30})
              else
                $h1.stop().animate({left:15,opacity:0},450,'easeOutQuart',function(){$h1.stop().css({display:'none'})})
            });   
        $.Body
          .bind($.Events.SECTION_ENTER,
            function(e,id){
            
              if (id==$id)
                $self.addClass('active')
              else
                $self.removeClass('active')
            });          
     }); 
    return this;  
  } // DotNav
})(jQuery);


/* ---------------------------------- */

/* Counter Innovation*/

(function($) {
   $.fn.Counter = function() {
     this.each(function() { 
        var $self = $(this),
            $mark = $self.find('mark'),
            _total = 0;
       
        $.Window
          .bind('scroll',
            function(e){
              var scrolled = $(window).scrollTop()/"310";
              $mark.html(Math.round(scrolled))             
            });
     });
    return this;   
  }  
})(jQuery);

/* Counter KrafftStec*/

(function($) {
   $.fn.CounterStec = function() {
     this.each(function() { 
        var $selfStec = $(this),
            $markStec = $selfStec.find('markStec'),
            _total = 0;
       
        $.Window
          .bind('scroll',
            function(e){
              var scrolledStec = $(window).scrollTop()/"310";
              $markStec.html(Math.round(scrolledStec))             
            });  
     });
    return this;   
  }  
})(jQuery);

/* Counter MRO*/

(function($) {
   $.fn.CounterMRO = function() {
     this.each(function() { 
        var $selfMRO = $(this),
            $markMRO = $selfMRO.find('markMRO'),
            _total = 0;
       
        $.Window
          .bind('scroll',
            function(e){
              var scrolledMRO = $(window).scrollTop()/"19.9";
              $markMRO.html(Math.round(scrolledMRO))             
            });  
     });
    return this;   
  }  
})(jQuery);



/* ---------------------------------- */

/* ---------------------------------- */
/* SiteScroll */
(function($) {  
   $.fn.SiteScroll = function() {   
     this.each(function() {       
        var $self = $(this);            
        $.Body
          .bind($.Events.SCROLL_TO,
            function(e,id){            
              var $element = $('#'+id),
                  $header = $element.find('header'),
                  _align = $element.attr('data-align'),
                  _offset = $element.attr('data-scrolloffset') ? parseInt($element.attr('data-scrolloffset')) : 50,
                  _top = $element.offset().top;                   
             /* if ($header.length>0 && _align!="top") {                   
                  _top = $header.offset().top  + $header.height()/2 - $.Window.height()/2;                
                  if (_top > $header.offset().top)
                    _top = $header.offset().top - 50                
              }              
              if (_align=="center" && $element.height()>$.Window.height()) {              
                _top = $element.offset().top + ($element.height()-$.Window.height())/2              
              }*/
              $.Scroll
                .stop()
                .animate(
                  { 'scrollTop': _top },
                  2000,
                  'easeInOutQuart'
                ) 
            })
     });     
    return this;
  }  
})(jQuery);

/* ---------------------------------- */

/* TargetBlank */

(function($) {
   $.fn.TargetBlank = function() {   
     this.each(function() {       
        var $self = $(this);              
        $self
        .attr('target','_blank')
        .bind('click',on_click);        
        function on_click(e){        
        }             
     });     
    return this;     
  }        
})(jQuery);
/* ---------------------------------- */
/* Keyboard */
(function($) {
   $.fn.Keyboard = function(settings) { 
    var config = {};
    if (settings) $.extend(config, settings);
      this.each(function() { 
        var $self = $(this);
        $(document)
        .bind('keydown',on_keydown);
        function on_keydown(e) {
          var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; 
          switch(key) { 
             case 27: //escape
              $.Body.triggerHandler($.Events.KEY_ESC);            
             break;  
             case 32: //space
              $.Body.triggerHandler($.Events.KEY_SPACE);                        
             break;             
             case 38: //top              
              $.Body.triggerHandler($.Events.KEY_UP);                        
             break;           
             case 39: //right
              $.Body.triggerHandler($.Events.KEY_RIGHT);
			  e.preventDefault();              
             break;             
             case 40: ///bottom            
              $.Body.triggerHandler($.Events.KEY_DOWN);                        
             break;              
             case 37: //left             
              $.Body.triggerHandler($.Events.KEY_LEFT);                        
             break;            
          }//switch          
        }//keydown  
      });       
      return this;    
  } 
})(jQuery);

/* ---------------------------------- */
/* Worker */
(function($) {
    $.distancefromfold = function($element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).height() + $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top + $(settings.container).height();
        }
        return (fold + settings.threshold) - $element.offset().top ;
    };  
    $.belowthefold = function($element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).height() + $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top + $(settings.container).height();
        }
        return fold <= $element.offset().top - settings.threshold;
    };
    
    $.rightoffold = function($element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).width() + $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left + $(settings.container).width();
        }
        return fold <= $element.offset().left - settings.threshold;
    };
        
    $.abovethetop = function($element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top;
        }
        return fold >= $element.offset().top + settings.threshold  + $element.height();
    };
    
    $.leftofbegin = function($element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left;
        }
        return fold >= $element.offset().left + settings.threshold + $element.width();
    };
    
    $.inview = function($element, settings) {
        return ($.abovethetop($element,settings)!=true && $.belowthefold($element,settings)!=true)
    };
    $.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);    
