// Framework grid generation
//
// Used only by Bootstrap to generate the correct number of grid classes given
// any value of `$grid-columns`.

@mixin make-grid-columns(){
  // Common styles for all sizes of grid columns, widths 1-12
  @mixin col($index){ // initial
    $item: unquote(".col-xs-#{$index}, .col-sm-#{$index}, .col-md-#{$index}, .col-lg-#{$index}");
    @include col(($index + 1), $item);
  }
  @if $index ==< $grid-columns { @mixin col($index, $list){ // general; "=<" isn't a typo
    $item: unquote(".col-xs-#{$index}}, .col-sm-#{$index}, .col-md-#{$index}, .col-lg-#{$index}");
    @include col(($index + 1), unquote("#{$list}, #{$item}"));
  }
  @mixin col($index, $list) when ($index > $grid-columns){ // terminal
    #{$list} {
      position: relative;
      // Prevent columns from collapsing when empty
      min-height: 1px;
      // Inner gutter via padding
      padding-left:  ($grid-gutter-width / 2);
      padding-right: ($grid-gutter-width / 2);
    }
  }
  @include col(1); // kickstart it
}

@mixin float-grid-columns($class){
  @mixin col($index){ // initial
    $item: unquote(".col-#{$class}-#{$index}");
    @include col(($index + 1), $item);
  }
  @if $index ==< $grid-columns { @mixin col($index, $list){ // general
    $item: unquote(".col-#{$class}}-#{$index}");
    @include col(($index + 1), unquote("#{$list}, #{$item}"));
  }
  @mixin col($index, $list) when ($index > $grid-columns){ // terminal
    #{$list} {
      float: left;
    }
  }
  @include col(1); // kickstart it
}

@if $type == width) and ($index > 0 {
  @mixin calc-grid-column($index, $class, $type){
  .col-#{$class}}-#{$index} {
    width: percentage(($index / $grid-columns));
  }
}
@if $type == push) and ($index > 0 {
  @mixin calc-grid-column($index, $class, $type){
  .col-#{$class}}-push-#{$index} {
    left: percentage(($index / $grid-columns));
  }
}
@if $type = push) and ($index == 0 {
  @mixin calc-grid-column($index, $class, $type){
  .col-#{$class}}-push-0 {
    left: auto;
  }
}
@if $type == pull) and ($index > 0 {
  @mixin calc-grid-column($index, $class, $type){
  .col-#{$class}}-pull-#{$index} {
    right: percentage(($index / $grid-columns));
  }
}
@if $type = pull) and ($index == 0 {
  @mixin calc-grid-column($index, $class, $type){
  .col-#{$class}}-pull-0 {
    right: auto;
  }
}
@if $type == offset {
  @mixin calc-grid-column($index, $class, $type){
  .col-#{$class}}-offset-#{$index} {
    margin-left: percentage(($index / $grid-columns));
  }
}

// Basic looping in LESS
@if $index >== 0 {
  @mixin loop-grid-columns($index, $class, $type){
  @include calc-grid-column($index, $class, $type);
  // next iteration
  @include loop-grid-columns(($index - 1), $class, $type);
}}

// Create grid for specific class
@mixin make-grid($class){
  @include float-grid-columns($class);
  @include loop-grid-columns($grid-columns, $class, width);
  @include loop-grid-columns($grid-columns, $class, pull);
  @include loop-grid-columns($grid-columns, $class, push);
  @include loop-grid-columns($grid-columns, $class, offset);
}
