// =========================================================
// Fluid grid system for scss
// =========================================================


//////////////////////////////////////// GRID VARIABLES

$gs_width: 100%
$gs_gutter-width: 0
$gs_gutter-height: 0
$gs_gutter-width-fallback: 0

$column_count: 1
$column_total-count: 12
$column_gutter-height: 2%
$column_gutter-width: 2%
$column_gutter-height-fallback: 2%
$column_gutter-width-fallback: 2%
$column_height: auto

// Predefined class names for generator
// $gs-container: 'gs-container'
// $gs-column: 'gs-column'
// $gs-row: 'gs-row'
// $gs-push: 'gs-push'
// $gs-pull: 'gs-pull'



//////////////////////////////////////// INTERNAL mixins

// Calc() mixin
=calc($property, $expression)
  #{$property}: -webkit-calc(#{$expression})
  #{$property}: -moz-calc(#{$expression})
  #{$property}: calc(#{$expression})

// Clearfix
=gs_clear
  clear: both
  &:before,
  &:after
    display: block
    content: ''
    clear: both
    height: 0



//////////////////////////////////////// GRID CONTAINER mixin

// GS Mixins
// Grid system container mixin, should be used for grid container and rows.

=gs($gs_width: $gs_width, $gs_gutter-width: $gs_gutter-width, $gs_gutter-height: $gs_gutter-height, $gs_gutter-width-fallback: $gs_gutter-width-fallback)

  // Grid width and margin math for calc() and fallback
  $half-gutter-height: $gs_gutter-height / 2
  $half-gutter-width: $gs_gutter-width / 2
  $half-gutter-width-fallback: $gs_gutter-width-fallback / 2

  // Check gutter width and it fallback for `auto` values, if false - return no math variant
  @if $gs_gutter-width != auto and $gs_gutter-width-fallback != auto

    // Check gutter value, if its units not equal to grid units - return calc math with fallback<
    // else - common math
    @if unit($gs_gutter-width) != unit($gs_width) and unitless($gs_gutter-width) == false

      // Check gutter fallback value for valid  units,
      // if true - continue math with calc() property and fallback
      @if unit($gs_gutter-width-fallback) == unit($gs_width) and unitless($gs_gutter-width-fallback) == false
        width: $gs_width - $gs_gutter-width-fallback
        +calc(width, #{$gs_width} - #{$gs_gutter-width})
        margin: $half-gutter-height $half-gutter-width-fallback
        +calc(margin-left, $half-gutter-width)
        +calc(margin-right, $half-gutter-width)

      @else
        @error "Fallback gutter units must be the same as grid width units `#{unit($gs_width)}`"

    @else
      width: $gs_width - $gs_gutter-width
      margin: $half-gutter-height $half-gutter-width

  @else
    width: $gs_width
    margin: $half-gutter-height $gs_gutter-width
  +gs_clear



//////////////////////////////////////// COLUMN mixin

// Column mixin has next variables: column count for current element, total column count in row, horizontal gutter and vertical gutter and their fallbacks.
// By default it sets to 1 column of 12 total columns wit 2% gutters.

=gs-column($column_count: $column_count, $column_total-count: $column_total-count, $column_gutter-width: $column_gutter-width, $column_gutter-height: $column_gutter-height, $column_height: $column_height, $column_gutter-width-fallback: $column_gutter-width-fallback, $column_gutter-height-fallback: $column_gutter-height-fallback, $gs_width: $gs_width)
  float: left

  // Grid gutter width and margin math for calc() and fallback
  $half-gutter-width: $column_gutter-width / 2
  $half-gutter-height: $column_gutter-height / 2
  $half-gutter-width-fallback: $column_gutter-width-fallback / 2
  $half-gutter-height-fallback: $column_gutter-height-fallback / 2

  // Column width math
  $column_width: $gs_width / $column_total-count * $column_count

  // Check gutter width and it fallback for `auto` values, if false - return no math variant
  @if $column_gutter-width != auto and $column_gutter-width-fallback != auto

    // Check gutter value, if its units not equal to grid units - return calc math with fallback<
    // else - common math
    @if unit($column_gutter-width) != unit($gs_width) and unitless($column_gutter-width) == false

      // Check gutter fallback value for valid  units,
      // if true - continue math with calc() property and fallback
      @if unit($column_gutter-width-fallback) == unit($gs_width) and unitless($column_gutter-width-fallback) == false
        width: $column_width - $column_gutter-width-fallback
        +calc(width, #{$column_width} - #{$column_gutter-width})
        margin-left: $half-gutter-width-fallback
        margin-right: $half-gutter-width-fallback
        +calc(margin-left, $half-gutter-width)
        +calc(margin-right, $half-gutter-width)

      @else
        @error "Fallback gutter units must be the same as grid width units `#{unit($gs_width)}`"

    @else
      width: $column_width - $column_gutter-width
      margin-left: $half-gutter-width
      margin-right: $half-gutter-width

  @else
    width: $column_width
    margin-left: $column_gutter-width
    margin-right: $column_gutter-width

  // Column height math
  // Check gutter height and it fallback for `auto` values, if false - return no math variant
  @if $column_gutter-height != auto and $column_gutter-height-fallback != auto and $column_height != auto

    // Check gutter value, if its units not equal to grid units - return calc math with fallback<
    // else - common math
    @if unit($column_gutter-height) != unit($gs_width) and unitless($column_gutter-height) == false

      // Check gutter fallback value for valid  units,
      // if true - continue math with calc() property and fallback
      @if unit($column_gutter-height-fallback) == unit($column_height) and unitless($column_gutter-height-fallback) == false
        height: $column_height - $column_gutter-height-fallback
        +calc(height, #{$column_height} - #{$column_gutter-height})
        margin-top: $half-gutter-height-fallback
        margin-bottom: $half-gutter-height-fallback
        +calc(margin-top, $half-gutter-height)
        +calc(margin-bottom, $half-gutter-height)

      @else
        @error "Fallback gutter units must be the same as column height units `#{unit($column_height)}`"

    @else
      height: $column_height - $column_gutter-height
      margin-top: $half-gutter-height
      margin-bottom: $half-gutter-height

  @else
    height: $column_height
    margin-top: $column_gutter-height
    margin-bottom: $column_gutter-height



//////////////////////////////////////// PUSH and PULL mixins

// Offset mixin allows to push column left or right on some distance.
// Expected settings: distance-column count to move on, total column count in row, horizontal gutter like in other columns.
// By default it sets to 1 column of 12 total columns with 2% gutters.

=gs-push($column_count: $column_count, $column_total-count: $column_total-count, $column_gutter-width: $column_gutter-width, $column_gutter-width-fallback: $column_gutter-width-fallback, $gs_width: $gs_width)

  // Grid gutter width and margin math for calc() and fallback
  $half-gutter-width: $column_gutter-width / 2
  $half-gutter-width-fallback: $column_gutter-width-fallback / 2

  // Column width math for calc() and fallback
  $position_width: $gs_width / $column_total-count * $column_count

  // Check gutter width and it fallback for `auto` values, if false - return no math variant
  @if $column_gutter-width != auto and $column_gutter-width-fallback != auto

    // Check gutter value, if its units not equal to grid units - return calc math with fallback<
    // else - common math
    @if unit($column_gutter-width) != unit($gs_width) and unitless($column_gutter-width) == false

      // Check gutter fallback value for valid  units,
      // if true - continue math with calc() property and fallback
      @if unit($column_gutter-width-fallback) == unit($gs_width) and unitless($column_gutter-width-fallback) == false
        margin-left: $position_width + $half-gutter-width-fallback
        +calc(margin-left, #{$position_width} + #{$half-gutter-width})

      @else
        @error "Fallback gutter units must be the same as grid width units `#{unit($gs_width)}`"

    @else
      margin-left: $position_width + $half-gutter-width

  @else
    margin-left: $position_width


=gs-pull($column_count: $column_count, $column_total-count: $column_total-count, $column_gutter-width: $column_gutter-width, $column_gutter-width-fallback: $column_gutter-width-fallback, $gs_width: $gs_width)

  // Grid gutter width and margin math for calc() and fallback
  $half-gutter-width: $column_gutter-width / 2
  $half-gutter-width-fallback: $column_gutter-width-fallback / 2

  // Column width math for calc() and fallback
  $position_width: $gs_width / $column_total-count * $column_count

  // Check gutter width and it fallback for `auto` values, if false - return no math variant
  @if $column_gutter-width != auto and $column_gutter-width-fallback != auto

    // Check gutter value, if its units not equal to grid units - return calc math with fallback<
    // else - common math
    @if unit($column_gutter-width) != unit($gs_width) and unitless($column_gutter-width) == false

      // Check gutter fallback value for valid  units,
      // if true - continue math with calc() property and fallback
      @if unit($column_gutter-width-fallback) == unit($gs_width) and unitless($column_gutter-width-fallback) == false
        margin-right: $position_width + $half-gutter-width-fallback
        +calc(margin-right, #{$position_width} + #{$half-gutter-width})

      @else
        @error "Fallback gutter units must be the same as grid width units `#{unit($gs_width)}`"

    @else
      margin-right: $position_width + $half-gutter-width

  @else
    margin-right: $position_width



//////////////////////////////////////// GENERATED GRID

// Generate classnames for grid
// .#{$gs-container}
//   +gs();

// @for $i from 1 through $column_total-count
//   .#{$gs-column}-#{$i}
//     +gs-column($i)


// @for $i from 1 through ($column_total-count - 1)
//   .#{$gs-push}-#{$i}
//     +gs-push($i)

//   .#{$gs-pull}-#{$i}{
//     +gs-pull($i)
