/*
 * @class   Home
 * @author  EtienneLem
 * @company Hatem+D
 */

// Auto init
$(document).ready(function(){ new Home(); });

// Class
var Home = function() { this.initialize.apply(this, arguments) };
Home.prototype = (function() { var pro = {};

  //  Contants
      
  //  Variables
  var blocks       = $('.block'),
      highlight    = $('.current-block'),
      loadTimer    = null,
      autoTimer    = null,
      messageTimer = null,
      headerImage  = $('header .image');
      
  //  public
  pro.initialize = function(opts) {
      initialize();
  };
  
  //  private
  var initialize = function()
  {
      initCategories();
      setAutoTimer();
  }
  
  var setAutoTimer = function() {
      clearInterval(autoTimer);
      autoTimer = setInterval(gotoNext, 8000);
  };
  
  //  Categories management
  var initCategories = function() {
      blocks.bind('mouseenter', onBlockOver);
  };
  
  var onBlockOver = function ( e )
  {
      var block = $(this);
      if ( block.hasClass('current') ) return;
      
      $('.block.current').removeClass('current');
      block.addClass('current');
      
      var index = blocks.index(block) + 1;
      highlight.css('left', index * 25 + '%');
      
      setAutoTimer();
      clearTimeout(loadTimer);
      loadTimer = setTimeout(function(){
        loadImage(block);
        //animateHeaderText();
      }, 450);
  };
  
  var gotoNext = function ( e )
  {
      var current = $('.block.current'),
          next    = current.next('.block');
      
      if ( next.size() == 0 ) next = blocks.first();
      next.trigger('mouseenter');
  };
  
  var loadImage = function ( block )
  {
      var bigSrc  = block.data('src'),
          thumb   = block.find('.image > div');
      
      var image = new Image();
  			$(image).bind('load', function(e){
  			  onImageLoaded(this.src, thumb.data('bgcolor'));
  			});
  			
  			image.src = bigSrc;
  };
  
  var onImageLoaded = function ( src, bgColor )
  {
      var copy      = headerImage.clone(),
          src       = src,
          oldHeader = headerImage;
      
      oldHeader.before(copy);
      headerImage = copy;
      
      copy.css({
        'background-image': 'url('+src+')',
        'background-color': bgColor
      });
      
      oldHeader.fadeOut('normal', function(){
        $(this).remove();
        clearInterval(messageTimer);
        messageTimer = setTimeout(function(){
          
        },1000);
      });
      
  };
  
  /*var animateHeaderText = function ()
  {
    $('.part1').fadeOut(200, function(){
      setTimeout(function(){
        $('.part1').show("slide", { direction: "left"}, 200, function(){
          $('.part1 .text').animate({opacity: "1"}, 350)
        });
      },800)
    });
    
    $('.part2').fadeOut(200, function(){
      setTimeout(function(){
        $('.part2').show("slide", { direction: "left"}, 200, function(){
          $('.part2 .text').animate({opacity: "1"}, 350)
        });
      },800)
    });
  };*/
  
return pro })();
