/**
 * @author arthur
 */

(function($){
  $.fn.header = function(options) {

    var defaults = {
      in_delay:          0.5,
      out_delay:         0.25,
      effect_delay:      0.5,
      attr_header:       'header',
      attr_theme:        'theme',
      attr_bg_url:       'bg_url',
      class_change:      'header_link',
      class_header:      'header_text',
      el_bgcolor:        'filaire',
      header_default:    'header_1',
      li_bgcolor:        '#FFFFFF',
      li_lbg_default:    null,
      li_rbg_default:    null,
      border_left:       'border_left',
      border_right:      'border_right'
    };
        
    var options = $.extend(defaults, options);
    
    this.timeout = null;
    this.header_actuel = 'header_1';
    
    var themes = {
      theme_1: '#438E9C',
      theme_2: '#555469',
      theme_3: '#668a73',
      theme_4: '#5C6550',
      theme_5: '#4A4E4F',
      theme_6: '#69982C'
    };
    
    options.default_bg_url = $('#' + options.el_bgcolor).css('background-image');
    
    var beginEffect = function(el) {
      if(this.timeout !== null) clearTimeout(this.timeout);
      
      if (el.is('li')) {
        el.find('.border_left').css('background-image', 'url("' + el.find('.border_left').attr(options.border_left) + '")');
        el.find('.border_right').css('background-image', 'url("' + el.find('.border_right').attr(options.border_right) + '")');
        el.css('background-color', $(themes).attr(el.attr(options.attr_theme))).find('a').css('color', '#FFFFFF');
      }
      
      this.timeout = setTimeout(
        function() { applyEffect(el.attr(options.attr_header)); },
        options.in_delay * 1000
      );
    }
    
    var endEffect = function(el) {
      if(this.timeout !== null) clearTimeout(this.timeout);
      
      if (el.is('li')) {
        el.find('.border_left').css('background-image', 'url("' + options.li_lbg_default + '")');
        el.find('.border_right').css('background-image', 'url("' + options.li_rbg_default + '")');
        el.css('background-color', options.li_bgcolor).find('a').css('color', $(themes).attr(el.attr(options.attr_theme)));
      }
      
      this.timeout = setTimeout(
        function() { applyEffect(options.header_default); },
        options.out_delay * 1000
      );
    }
    
    var applyEffect = function(header_target) {      
      if(header_target != this.header_actuel) {    
        this.header_actuel = header_target;
        el_header_actuel = $('*[' + options.attr_header + '=' + this.header_actuel + ']');
        
        var bg_url = (el_header_actuel.attr(options.attr_bg_url) != null) ? 'url("' + el_header_actuel.attr(options.attr_bg_url) + '")' : options.default_bg_url;
        
        $('#' + options.el_bgcolor).css({
          'background-color': $(themes).attr(el_header_actuel.attr(options.attr_theme)),
          'background-image': bg_url
        });
        
        $('.' + options.class_header + '[' + options.attr_header + ']').not(el_header_actuel).hide(0, function() {
          el_header_actuel.fadeIn();
        });
      }
    }
    
    // Initialisation    
    applyEffect(options.header_default);
    
    $('.' + options.class_change).hover(
      function() { beginEffect($(this)); },
      function() { endEffect($(this)); }
    );
    
    return this;
  };
})(jQuery);
