(function(){
  ff.ManufacturerSelect = function(farbfinder, options) {
    this._initialize(farbfinder, options);
  }
  
  ff.ManufacturerSelect.CATEGORY_AUTOLACKE      = 'auto';
  ff.ManufacturerSelect.CATEGORY_INDUSTRIELACKE = 'industrie';

  ff.ManufacturerSelect.MANUFACTURER_HEADLINES = {};
  ff.ManufacturerSelect.MANUFACTURER_HEADLINES[ff.ManufacturerSelect.CATEGORY_AUTOLACKE] = "Fahrzeughersteller";
  ff.ManufacturerSelect.MANUFACTURER_HEADLINES[ff.ManufacturerSelect.CATEGORY_INDUSTRIELACKE] = "Industrielacke";
    
  ff.ManufacturerSelect.prototype = jQuery.extend(ff.ManufacturerSelect.prototype, {
    _initialize: function(farbfinder, options)
    {
      this._farbfinder = farbfinder;
      this._options = jQuery.extend({
        container: '#manufacturers',
        headline: '#farbfinder_step_1_headline',
        url: ff.SERVICE_URL + '/jsonService/manufacturers',
        manufacturer_id: null
      }, options);
      this._manufacturers = [];
      this._url = this._options.url;
      this._container = jQuery(this._options.container);
      this._headline = jQuery(this._options.headline);
      this._headlineHtml = this._headline[0].innerHTML;
    },
    startup: function()
    {
      this._get();
    },
    _get: function() {
      var self = this;
      jQuery.getJSON(this._url + '/?callback=?', {}, function()
      {
        self._manufacturers = {};
        self._manufacturers[ff.ManufacturerSelect.CATEGORY_AUTOLACKE] = arguments[0][ff.ManufacturerSelect.CATEGORY_AUTOLACKE];
        self._manufacturers[ff.ManufacturerSelect.CATEGORY_INDUSTRIELACKE] = arguments[0][ff.ManufacturerSelect.CATEGORY_INDUSTRIELACKE];
        self._updateHtml.call(self);
        var manufacturer;
        if(self._options.manufacturer_id  && (manufacturer = self.getManufacturerById(self._options.manufacturer_id)))
        {
          jQuery(self).trigger('logoClicked', { link: null, 'manufacturer': manufacturer});
        }
      });
    },
    _applyManufacturerHtml : function(ul, manufacturers)
    {
      var self = this;
      jQuery.each(manufacturers, function(i, manufacturer)
      {
        var li = jQuery(document.createElement('li'));
        var img = jQuery(document.createElement('img'));
        var a = jQuery(document.createElement('a'));
        var avatar_image = jQuery(document.createElement('div'));
        var description = jQuery(document.createElement('span'))
          .attr({'class': 'description'})
          .html(manufacturer.name);
          
        avatar_image.attr({
          'src' : '/code/images/ajax-loader.gif',
          'style' : 'height: 82px; width: 82px',
          'class' : 'loading'
        });

        img.attr({
          'src' : ff.SERVICE_URL + '/logo/' + manufacturer.id + '/70/70',
          'alt' : manufacturer.name,
          'title' : manufacturer.name,
          'width' : 82,
          'height' : 82,
          'style' : 'display: block',
          'class' :  'fade-me'
        });

        a.attr('href', 'javascript:void(0)');
        a[0]._manufacturer = manufacturer;
        a.append(avatar_image).append(description);
        li.append(a);
        a.prepend('<span class="manuFadeOver"></span>');
        ul.append(li);
        img.bind('load', function()
        {
          avatar_image.replaceWith(img)
          img.fadeTo(1000, 1);



        jQuery('span.manuFadeOver').hover(function()
        { 
          //jQuery(this.prev('span')).fadeTo(800,1);
          jQuery(this).stop().fadeTo(500,0.65);
        }, function()
        {
         jQuery(this).stop().fadeTo(500,0.25);
        });



        });


        a.bind('click keypress', function()
        {
          jQuery(self).trigger('logoClicked', { link : this, manufacturer: this._manufacturer });
        });
      });
    },
    _updateHtml: function()
    {
      this._container.empty();

      for (var i in this._manufacturers)
      {
        var h4 = jQuery(document.createElement('h4')).html(ff.ManufacturerSelect.MANUFACTURER_HEADLINES[i]);
        if(this._manufacturers[i].length>0)
        {
          var ul = jQuery(document.createElement('ul')).attr('class', 'manufacturers');
          this._applyManufacturerHtml(ul, this._manufacturers[i]);
          this._container.append(h4).append(ul);
        }
      }
    },
    resetHeadline: function()
    {
      this._headline.html(this._headlineHtml);
    },
    getManufacturerById : function(id)
    {
      for(var i in this._manufacturers)
      {
        for(var j=0; lenj = this._manufacturers[i].length, j<lenj; j++)
        {
          if(this._manufacturers[i][j].id == id)
            return this._manufacturers[i][j];
        }
      }
      return null;
    }
  });
})();