// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(document).ready(function() {

  $('.not_delivered').click(function(e) {
    alert('This feature has not been delivered yet.');
    e.stopImmediatePropagation();
    return false;
  });

  $('li.nav_tab').hover(
    function()  {
      $(this).addClass('active'); // for IE z-index support & hover support
      $(this).children('span').addClass('active').siblings('div').show();
      $(this).children('a').addClass('active').siblings('div').show();
    },
    function()  {
      $(this).removeClass('active'); // for IE z-index support & hover support
      $(this).children('span').removeClass('active').siblings('div').hide();
      $(this).children('a').removeClass('active').siblings('div').hide();
    }
  );

  $(".top_flash").fadeTo(7000, 0, function() { $(this).slideUp(); }).removeClass("top_flash");

  // forms
  $(".datepicker").datepicker();
  
  // On clicking a alternate view for a finish, show that view
  $('div.views li a').click(function() {
    $('div.views li.active').removeClass('active');
    $(this).parent().addClass("active");
    $('#featured_image').attr('src', $(this).attr('name'));
  });

  function showFinish(finish_code) {
    // Show only alternate views with this finish code
    $('div.views li').hide();
    $('div.views li[finish~="' + finish_code + '"]').show();

    // Make the first item in the set of views matching this finish active
    $('div.views li.active').removeClass('active');
    active = $('div.views li a:visible:first')
    $('#featured_image').attr('src', $(active).attr('name'));
    active.parent().addClass("active");
  }

  $('#finishes li').click(function() {
    $('#finishes li.active').removeClass('active');
    finish_code = $(this).attr('finish');
    $(this).addClass("active");
    showFinish(finish_code);
  });

  letter_code_regex = /\d{4}(QW|[A-Z])/

  // Get the initial finish code
  if($('#featured_image').attr('src')) {
    finish_code = $('#featured_image').attr('src').match(letter_code_regex)[1];
    $('#finishes li[finish~="' + finish_code + '"]').addClass('active');
    showFinish(finish_code);
  }

  $('.js_tab a').click(function() {
      activate_tab(this);
  });

  $('.product_info a').click(function() {
      activate_tab($('li#product_info_tab a')[0]);
  });

  function activate_tab(el) {
    $('.js_tab.active').removeClass('active');
    $(el).parent('li').addClass('active');
    $('.left_col li.active').removeClass('active').addClass('inactive');
    $('li#' + $(el).parent('li').attr('id').replace(/_tab$/, "")).addClass('active').removeClass('inactive');
  }

  $("#read_more").click(function() {
    $('.js_tab.active').removeClass('active');
    $('li#reviews_tab').addClass('active');
    $('.left_col li.active').removeClass('active').addClass('inactive');
    $('li#reviews').addClass('active').removeClass('inactive');
  });

  $("a.rate_star").click(function() {
    n = parseInt($(this).text());
    $("#comment_rating").val(n);
    $(".rate_star.active").removeClass("active");
    for (var i=1; i <= n; i++) $("a#rate_star_" + i).addClass("active");
  });
  $(".rate_star").hover(function() {
    n = parseInt($(this).text());
    for (var i=1; i <= n; i++) $("a#rate_star_" + i).addClass("hover");
  }, function() {
    $(".rate_star.hover").removeClass("hover");
  });

  $("select#state").change(function() {
    $.get($(this).parent().parent().attr('action'), $(this).serialize(), null, "script");
  });

  // search prefill
  if (($('#search_container input').val() == '' ) || ($('#search_container input').val() == 'Search Site')) {
    $('#search_container input').addClass('instructions').val('Search Site');
  }
  $('#search_container input').focus(function () {
    if ($(this).val() == 'Search Site') {
      $(this).removeClass('instructions').val('');
    }
  });
  $('#search_container input').blur(function () {
    if ($(this).val() == '') {
      $(this).addClass('instructions').val('Search Site');
    }
  });
  
  $('.finish_swatch').click(function () {
    show_finish = $(this).attr('id').split("_")[1];
    $('.views').children().each(function () {
      finish = $(this).children(":first").attr('name').match(letter_code_regex)[1];
      if (show_finish == finish) {
        $(this).show();
      } else {
        $(this).hide();
      }
    })
  })
  
});


// function validates that at least one site is checked
// and that the current site is checked
// note that it expects a 'global' variable to have been set
// by the page itself (because this file isn't erb)
function validate_site_choice() {
  if ($(".site_choice:checked").length == 0) {
    alert ('You must have one site selected.');
    return false;
  } else if (! $("#" + current_site_checkbox_id).attr('checked') ) {   
    alert ('You must have the current site selected.');
    return false;
  } else {
    return true
  }
}