﻿$(function(){

    // ширина полосы отступа справа
    var padding_right = 0;

    // ширина полосы отступа слева
    var padding_left = 0;

    //минимальная шириан вкладки
    var tabs_min_width = 100;

    //максимальная шириан вкладки
    var tabs_max_width = 500;

    //ширина кнопки закрыть и фавикона
    var favicon_width = 30;

    //ширина кнопки закрыть и фавикона
    var close_width = 20;

    var $Container = $( '.b-browser' );

    $Container.each(function(){

        var $item = $( 'li', $( this ) );
        var $close = $( '.close', $item );

        $item.click(function(){
          if ( !$( this ).hasClass( 'active' ) ) {
            $item.removeClass( 'active' );
            $( this ).addClass( 'active' );
            tabs_resize( $Container );
          }
        });

        $close.click(function(){

          if ( $( this ).closest( 'li' ).hasClass( 'active' ) ) {
            $( this ).closest( 'li' ).prev( 'li' ).addClass( 'active' );
          }

          if ( $( 'li', $( this ).closest( 'ul' ) ).length == 1 ) {
            $( '.name', $( this ).closest( 'li' ) ).html( 'Новая вкладка' );
            $( '.favicon', $( this ).closest( 'li' ) ).html('');
          }
          else {
            $( this ).closest( 'li' ).remove();
          }

          tabs_resize( $Container );

        });

    });

    var tabs_resize = function( container ){

      var $item = $( 'li', container );
      var $label = $( '.name', container );

      var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

      if ( is_chrome ) { return; }

        var contWidth = container.width()-padding_right-padding_left;
        var itemWidth =  contWidth/$item.length;


        if ( (itemWidth > tabs_min_width) && itemWidth < tabs_max_width ) {
            $item.css( 'width', itemWidth );
            $label.css( 'width', itemWidth-favicon_width );
        }
        else if ( itemWidth > tabs_max_width ) {
            $item.css( 'width', tabs_max_width );
            $label.css( 'width', tabs_max_width-favicon_width );
        }
        else {
            $item.css( 'width', tabs_min_width+'px' );
            $label.css( 'width', tabs_min_width-favicon_width+'px' );
        }


        $item.each(function(){
          if ( $( this ).hasClass( 'active' ) ) {
            $( '.name', $( this ) ).css( 'width', $( this ).width()-favicon_width-close_width+'px' );
          }
        });
    };


    var vk_browsers = function() {

        var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

        if ( $.browser.mozilla ) {
          $Container.addClass( 'b-firefox' );
        }
        else if ( $.browser.msie ) {
            $Container.addClass( 'b-ie' );
        }
        else if ( $.browser.opera ) {
          $Container.addClass( 'b-opera' );
        }
        else if ( is_chrome ) {
          $Container.addClass( 'b-chrome' );
        }
        else {
          $Container.addClass( 'b-firefox' );
        }

    };

    vk_browsers();

    tabs_resize( $Container );

    $( window ).resize(function(){
      tabs_resize( $Container );
    });

});

