/**
 *  Выводим список брендов с сопуствующей инфой.
 */

var  brandsList  =  new(function(){

    /**
     * Выделенный в данный момент бренд.
     */
    this.current  =  false;

    /**
     * Список брендов
     */
    this.brands  =  {};


    /**
     * Получаем список брендов, отрисовываем таблицу
     */
    this.init  =  (function(brands){
        this.brands  =  brands;

        $('#content').ready(function(){
            brandsList.drow();
        });
    });


    /**
     * Добавляем скрытый текст
     */
    this.addInfoBlock  =  (function(numberDiv){
        $('<div id="infoDiv-'+numberDiv+'" \n\
                style="display: none;"\n\
                class="info">\n\
            <div class="info-top">\n\
                <div id="infoText-'+numberDiv+'" class="info-bottom"></div>\n\
            </div>\n\
        </div>').appendTo('#content');
    });


    /**
     * Отрисовываем список брендов
     */
    this.drow  =  (function(){

        var isFirst  =  true;

        for(var i  in  this.brands){
         if((i % 7) == 0){
                var numberDiv  =  i/7;

                if(isFirst){
                    isFirst  =  false;
                }
                else{
                    this.addInfoBlock(numberDiv);
                }

            $('<div id="brands-'+numberDiv+'" class="brands"></div>').appendTo('#content');
         }
         
            $('<div id="'+i+'" class="brand">'+
    '<div class="cover">\n\
        <div >\n\
            <p><a href="#"><img id="i-'+i+'" src="/img/brands/'+
                                this.brands[i]['id']+
                               '.jpg" alt="" /></a>\n\
            </p>\n\
            <p><a href="#">'+this.brands[i]['name']+'</a></p>\n\
        </div>\n\
    </div>\n\
</div>\n\
').appendTo('#brands-'+numberDiv);

            $('#'+i).click(function(){
                brandsList.showInfo(this.id);
                return false;
            });
        }

        if((i % 7) != 0){
            this.addInfoBlock(this.getCounter(i));
        }
    });

    /**
     * Отображаем информацию о бренде.
     */
    this.showInfo  =  (function(id){
          var  counter  =  this.getCounter(id);

        if(this.current === id){
            this.hideInfo();
        }
else{
      if(this.current  !==  false){
          $('#'+this.current).attr('class', 'brand');
          var  imgName  =  parseInt(this.current)+1;
          $('#i-'+this.current).attr('src', '/img/brands/'+imgName+'.jpg');
          if(this.getCounter(id) != (prevCounter = this.getCounter(this.current))){
            $('#infoDiv-'+prevCounter).hide('fast');
          }
      }

          $('#infoText-'+counter).html('\n\
  <h5>'+this.brands[id]['name']+'</h5>\n\
    <p>'+
        this.brands[id]['country']+
    '</p>\n\
     <p><a href="http://'+this.brands[id]['url']+'">'+
        this.brands[id]['url']+
    '</a></p>\n\
    <p><strong>Основная продукция: </strong>'
       +this.brands[id]['products']+
    '</p>\n\
    <p>\n\
     <strong>О компании: </strong>'
     +this.brands[id]['about']+
'</p>\n\
<a class="close" id="closeInfo-'+counter+'">Закрыть</a>');

          $('#infoDiv-'+counter).show('fast');
          $('#'+id).attr('class', 'brand active');
          var imgName  =  parseInt(id)+1;
          $('#i-'+id).attr('src', '/img/brands/colour/'+imgName+'.jpg');
          $('#closeInfo-'+counter).click(function(){
              brandsList.hideInfo();
          })

          this.current  =  id;
 }
    });

    /**
     * Получаем номер строки с брендами
     */
    this.getCounter  =  (function(bNumber){
         return  ((mod = bNumber % 7) == 0)
             ? bNumber / 7+1
             : (bNumber - mod) / 7+1;
    });

    /**
     * Скрываем информацию о бренде
     */
    this.hideInfo  =  (function(){
        var counter  =  this.getCounter(this.current);

        $('#'+this.current).attr('class', 'brand');
        var  imgName  =  parseInt(this.current) + 1;
        $('#i-'+this.current).attr('src', '/img/brands/'+imgName+'.jpg');
        $('#infoDiv-'+counter).hide('fast');
        this.current  =  false;
    });
});
