(function ($) {
  $(document).ready(function() {
    // Any base-theme specific JS goes here.
    // Remove Colorbox overlay and element when clicking edit link for an image in the overlay.
    $('a.cb_edit_link').click(function() {
      $.fn.colorbox.close();
    });

    /*
     * Adds the 'usepie' class to certain elements
     */
    $('#search-block-form').addClass('usepie');
    $('#locationfinder form').addClass('usepie');

    /*
     * Opens and closes the search bar
     */
    $('#search-block-form .drawer-handle').click(function() {
      $('form#search-block-form input[name="op"]').click();

      return false;
    });
    $('a.colorbox-video-trigger').colorbox({innerWidth: 450,innerHeight: 380, inline: false}, function() {
      return false;
    });

    var zipcode = null;
    if (zipcode == null) {
      params = unescape(window.location.search.substring(1)).split('&');
      for (var i = 0; i < params.length; i++) {
        param = params[i].split('=');
        if (param[0] == 'distance[postal_code]') {
          zipcode = param[1];
        }
      }
    }

    $('a.colorbox-directions-trigger').click(function() {
       $.colorbox(
         {
         href: Drupal.settings.basePath + 'studio-directions-iframe/' + $(this).attr('rel') + '?zipcode=' + zipcode,
         innerWidth: "90%",
         innerHeight: "90%",
         width: "95%",
         height: "95%",
         iframe: true,
         open: true
       });
       return false;
    });

    $(".question").click(function () {
      $(this).toggleClass("active");
      $(this).siblings(".answer").slideToggle("slow");
      $(this).parent().siblings().children(".answer").slideUp("slow");
      $(this).parent().siblings().children(".question").removeClass("active");
    });    

    if (typeof(Drupal.settings.lps_search) !== 'undefined') {
      if (Drupal.settings.lps_search.result_count > 0) {
        var search_label = $('#tabs-0-under_center ul.ui-tabs-nav li.first a').text() + ' (' + Drupal.settings.lps_search.result_count + ')';
        $('#tabs-0-under_center ul.ui-tabs-nav li.first a').text(search_label);
      } 
    }

    // Strip page parameter from exposed filters on product gallery pages.
    $('.bef-select-as-links a').each(function() {
      var href = $(this).attr('href');
      var position = href.indexOf("&page=");
      if (position > 0) {
        $(this).attr('href', href.substring(0, position));
      }
    });

  });
})(jQuery);
;

