/**
 * @file script.js
 * Included on all pages
 */
(function($) {
  
  Drupal.behaviors.cirrus = {
    attach: function() {
      var tab_groups = $('.view-tabs'),
       resources = $('.view-resources:eq(0)'),  // Special case
       accordion = false,
       need_submit = false;

      // Sidebar menu styling
      $('.sidebar ul.menu li').append('<span class="marker"></span>');

      // Tabs functionality
      // Setup tabs
      if (tab_groups.length) {
        tab_groups.each(function() {
          var j = $(this);
          j.find('.view-inner').each(function(i) { this.id = 'tabs-' + (i + 1); })
          j.tabs();
          j.find('.view-header ul').show();
        }); 
      }
      
      // Resources tabs and accordion
      if (resources.length) {
        resources.find('.view-inner').each(function(i) { this.id = 'tabs-' + (i + 1); })
        resources.tabs({
          show: function(event, ui) {
            // NOTE: this function is a workaround for accordion.  For some reason, 
            // accordion does not work correctly when you attempt to initialize it
            // when is is hidden, which it is if it is not the selected tab on page
            // load.
            if (!accordion && ui.index == 2) {
              $('.accordion').find('.clear').remove().end().accordion();
              accordion = true;
            }
          }
        });
        resources.find('.view-header ul').show();
      }

      // Block forms placeholder text, use HTML5 placeholder if possible
      $('.sidebar form, .region-search-box form, .webform-client-form').find('.form-item').each(function () {
        var j = $(this),
          input = j.find('input[type=text],textarea'),
          text,
          required = false;
        
        if (input.length == 1) {
          text = $.trim(j.find('label').hide().clone().find('span').remove().end().html());
          required = !!j.find('label span.form-required').length;
          if (required) {
            text += ' *';
          }
          if (!!(input[0].placeholder === '' ) && !!(input[0].placeholder !== undefined)) {
            // Use HTML5 placeholder if available
            input[0].placeholder = text;
          }
          else {
            // Use JS fallback
            need_submit = true;
            if ($.trim(input.val()) == '') {
              input.val(text);
            }
            input.data('placeholder', text);
            input.focus(function() {
              var j = $(this);
              
              if (j.val() == text) {
                j.val('');
              }
            });
            input.blur(function() {
              var j = $(this);
              
              if ($.trim(j.val()) == '') {
                j.val(text);
              }
            });
          }
        }
        
      })
      .end()
      .bind('submit', function() {
        if (need_submit) {
          $(this).find('.form-item').each(function() {
            var input = $(this).find('input[type=text],textarea'),
              text = $.trim(input.val()),
              placeholder;
            
            // If this field is set with its placeholder text, clear it
            if (input.length == 1) {
              placeholder = input.data('placeholder');
              if (text == placeholder) {
                input.val('');
              }
            }
          });
        }
      });
      
      // News archive JS functionality
      $('#block-cirrus-site-feature-cirrus-site-feature-news-archive select').change(function() { $(this).parents('form').submit(); });
      $('#block-cirrus-site-feature-cirrus-site-feature-news-archive input[type=submit]').hide();
      
      // Experts block
      $('.featured-content .view-display-id-attachment_2').once().each(function () {
        var curr_index = 0,
          view = $(this),
          slides = view.find('.views-row'),
          pager,
          i;
          
        // Setup
        if (slides.length > 1) {
          pager = '<ul class="pager clearfix"><li><a class="previous button" href="#">&lt;</a></li><li><a class="next button" href="#">&gt;</a></li></ul>';
          pager = $(pager)
            .find('a')
              .bind('click', function (e) { 
                change_slide(this.className == 'next' ? 1 : -1);
                e.preventDefault();
              })
            .end();
          view.find('.view-content').append(pager);
          slides.not(':first').hide();
        }
        
        function change_slide(dir, index) {
          dir = dir || 1;
          index = index || (curr_index + dir);
          
          if (index < 0) {
            index = slides.length - 1;
          }
          curr_index = index = index % slides.length;
          
          // Change slide to index
          slides.stop(true, true).filter(':visible').fadeOut('fast', function () { slides.eq(index).fadeIn(); });
        }
      });
    }
  };
  
})(jQuery);


;

