/**
 * Copyright 2014 Vecna Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

@mixin button($background-color: $button-default-background-color, $color: $button-default-color) {
  background-color: $background-color;
  border: solid 1px $background-color;
  border-bottom-color: darken($background-color, 10%);

  &, &:link, &:visited, &:active {
    color: $color;
  }

  // Do not create a hover state when site is only for touch devices
  @if not $touch-only {
    &:hover, .switch:hover > &:not(.selected), &:focus {
      background-color: lighten($background-color, 10%);
    }
  }

  &:active {
    background-color: lighten($background-color, 15%);
  }

  &.selected {
    border: solid 1px darken($background-color, 10%);
    background: lighten($button-primary-background-color, 59%);
  }

  &:focus {
    outline: solid 2px darken($button-primary-background-color, 5%);
    outline-offset: -2px;
  }

  &[disabled], &.disabled {
    position: relative;
    background: none;
    border: solid 1px #aaa;
    color: #6d6d6d;
    border-radius: 3px;
    box-shadow: none;
    text-shadow: none;

    &:hover {
      background: none;
      cursor: not-allowed;
    }
  }
}

////
// Button Groups
////

.btn-group {
  display: inline-block;
  position: relative;
  margin-bottom: 1ex;
  text-align: left;
  overflow: visible;
  border-radius: 7px;
  outline: none;
  vertical-align: middle;

  > .btn, > button, > input[type='submit'] {
    display: block;
    float: left;
    margin-left: -1px;
    border-style: solid;
    border-width: 1px;
    border-radius: 0;

    &:first-of-type {
      margin-left: 0;
      border-top-left-radius: 2px;
      border-bottom-left-radius: 2px;
    }

    &:last-of-type {
      border-top-right-radius: 2px;
      border-bottom-right-radius: 2px;
    }
  }

  &.btn-group-full {
    display: block;

    > .btn {
      float: none;
      text-align: left;
    }
  }
}

////
// Base Button Styles
////

.btn, button, input[type='submit'] {
  display: inline-block;
  @include user-select(none);
  padding: $base-size/2 $gutter-width/2;
  outline: none;
  border: solid 1px $border-color;
  border-radius: 2px;
  box-shadow: none;
  font-family: $base-font;
  font-size: $base-size;
  font-weight: 300;
  text-align: center;
  text-decoration: none;
  vertical-align: middle;
  cursor: pointer;
  @include button;
}

label.btn {
  margin: 0;
  font-family: $light-font;
  color: $base-color;

  input[type=checkbox] + &, input[type=radio] + & {
    border: solid 1px $border-color;
    padding: 5px $gutter-width/2;
    background: none;
    box-shadow: inset 0 1px #fff, 0 1px #fff;

    &:hover {
      background: white;
    }
  }

  input[type=checkbox] + & {
    width: 100%;
    margin-bottom: 1ex;
    text-align: left;
  }

  input[type=radio] + & {
    display: block;
    float: left;
    margin-left: -1px;
    border-radius: 0;

    &:first-of-type {
      margin-left: 0;
      border-top-left-radius: 2px;
      border-bottom-left-radius: 2px;
    }

    &:last-of-type {
      border-top-right-radius: 2px;
      border-bottom-right-radius: 2px;
    }
  }

  input[type=checkbox] + &:before, input[type=radio] + &:before {
    display: inline-block;
    margin: 0 0.5ex 0 -0.5ex;
    font-family: $icon-font;
  }

  input[type=checkbox] + &:before, input[type=radio] + &:before {
    color: #aaa;
  }

  input[type=checkbox] + &:before {
    content: $icon-check-empty;
  }

  input[type=radio] + &:before {
    content: $icon-circle-blank;
  }

  input:checked + & {
    border-color: $link-default-color !important;
    background: $button-selected-background-color;
    color: $link-default-color !important;
    z-index: 1;
  }

  input[type=radio]:checked + & {
    position: relative;
  }

  input[type=checkbox]:checked + &:before {
    content: $icon-check;
    color: $link-default-color;
  }

  input[type=radio]:checked + &:before {
    content: $icon-circle + ' ';
    color: $link-default-color;
  }

  input[type=checkbox]:focus + &, input[type=radio]:focus + & {
    box-shadow: 0 0 3px $link-default-color;
    outline: solid 2px $link-hover-color;
    outline-offset: -2px;
    z-index: 2;
  }
}

  // Hide radio buttons and checkboxes within button groups
  input[type=checkbox], input[type=radio] {
    position: absolute;
    left: -9999em;
  }

.btn.primary, button.primary, input[type='submit'].primary {
  @include button($button-primary-background-color, $button-primary-color);

  // Small screen (smartphones)
  @media only screen and (max-width : $small-max-width) {
    width: 100%;
    margin-bottom: 1ex;
  }
}

.btn.warning, button.warning, input[type='submit'].warning {
  @include button($button-warning-background-color, $button-warning-color);
}

.btn.subtle, button.subtle, input[type='submit'].subtle {
  @include button(lighten($base-color, 10%), #fff);
  border: none;
}

////
// Drop-down Buttons
////

.dropdown-toggle {
  &:after {
    content: " " + $icon-caret-down;
    font-family: $icon-font;
  }
}

.dropdown-list {
  display: none;
  clear: both;
  position: absolute;
  top: 38px;
  left: 0;
  right: 0;
  min-height: 0;
  margin: 0;
  padding: 0;
  background: $border-color;
  border-radius: 0 0 3px 3px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
  list-style: none;
  z-index: 1;
  overflow: hidden;

  > li {
    margin: 0;
    padding: 0;

    &:last-child {
      border: none;
    }

    > a {
      display: block;
      margin-top: 1px;
      padding: 1ex;
      background: #fff;
      color: $button-default-color;
      text-decoration: none;

      &:hover {
        background: #fff;
      }
    }

    &:first-child > a{
      box-shadow: none;
    }
  }
}

.cancel-link {
  display: inline-block;
  margin-left: 1em;
}
