﻿// All PDFs, all offsite links, and any link flagged as external (even if internal) open in new window:
var setExternalLinks = function() {
  $('a.external, a[href$=.pdf], a[href^="http"]').each(function() {
    $(this).attr("target", "_blank");
  });
}

$(function() {
	setExternalLinks();
	
  // Tabbing mechanisms:
  var switchTab = function(hash) {
    var tabs = $('div.tabs');
    tabs.find('div.content').hide().filter(hash).show();

    tabs.find('ul.tabs-nav li').removeClass('selected');
    tabs.find('ul.tabs-nav li:has(a[href$=' + hash + '])').addClass('selected');
  };

  var getPageName = function(s) {
    return s.substr(s.lastIndexOf('/') + 1).replace(/\?.*$/, '').replace(/#.*$/, '');
  };

  $('a[href*="#"]').click(function() {
    // Only switch tabs if the appropriate tab exists:
    if (this.hash == '' || $('div.tabs ul.tabs-nav li:has(a[href$=' + this.hash + '])').length == 0)
      return true;

    // If a hashed URL points to a different page (NOT path sensitive!) then just follow the link.
    if (getPageName(this.href) != getPageName(window.location.href))
      return true;

    switchTab(this.hash);
    this.blur();
    return false;
  });
  $('div.tabs')
    .find('ul.tabs-nav a:first').click().end()
    .find('ul.tabs-nav li').hover(
      function() { $(this).addClass('hover') },
      function() { $(this).removeClass('hover') }
    ).each(function() {
      // HACK: Only if the page has tabs, push the browser to the top of the page on first visit.
      // Otherwise some browsers (i.e. FF3) won't update the scroll when linking from another page to one with a missing anchor.
      window.scroll(0, 0);
    });

  if (location.hash != '') switchTab(location.hash);

  // Press Room functionality:
  $('a.press_link').click(function() {
    $('#press .index').hide();
    $('#' + $(this).attr('rel')).show();
    return false;
  });
  $('a.back_to_press').click(function() {
    $(this).parents('.press_item').hide();
    $('#press .index').show();
    return false;
  });

  // Form validation and functionality:
  var states = [
    ['AL', 'Alabama'], ['AK', 'Alaska'], ['AS', 'American Samoa'], ['AZ', 'Arizona'], ['AR', 'Arkansas'], ['CA', 'California'], ['CO', 'Colorado'], ['CT', 'Connecticut'], ['DE', 'Delaware'], ['DC', 'District of Columbia'], ['FL', 'Florida'], ['GA', 'Georgia'], ['GU', 'Guam'], ['HI', 'Hawaii'], ['ID', 'Idaho'], ['IL', 'Illinois'], ['IN', 'Indiana'], ['IA', 'Iowa'], ['KS', 'Kansas'], ['KY', 'Kentucky'], ['LA', 'Louisiana'], ['ME', 'Maine'], ['MD', 'Maryland'], ['MA', 'Massachusetts'], ['MI', 'Michigan'], ['MN', 'Minnesota'], ['MS', 'Mississippi'], ['MO', 'Missouri'], ['MT', 'Montana'], ['NE', 'Nebraska'], ['NV', 'Nevada'], ['NH', 'New Hampshire'], ['NJ', 'New Jersey'], ['NM', 'New Mexico'], ['NY', 'New York'], ['NC', 'North Carolina'], ['ND', 'North Dakota'], ['MP', 'Northern Mariana Islands'], ['OH', 'Ohio'], ['OK', 'Oklahoma'], ['OR', 'Oregon'], ['PA', 'Pennsylvania'], ['PR', 'Puerto Rico'], ['RI', 'Rhode Island'], ['SC', 'South Carolina'], ['SD', 'South Dakota'], ['TN', 'Tennessee'], ['TX', 'Texas'], ['UT', 'Utah'], ['VT', 'Vermont'], ['VI', 'Virgin Islands'], ['VA', 'Virginia'], ['WA', 'Washington'], ['WV', 'West Virginia'], ['WI', 'Wisconsin'], ['WY', 'Wyoming'], ['AE', 'Armed Forces Europe'], ['AA', 'Armed Forces Americas'], ['AP', 'Armed Forces Pacific']
  ];
  var loadStates = function(o) {
    var s = [];
    s.push('<option value="">Please Select</option>');
    $.each(states, function() {
      s.push('<option value="' + this[0] + '">' + this[1] + '</option>');
    });
    $(o).html(s.join('\n'));
  }
  $('#request_country').val('US');
  $('#request_state_province').each(function() { loadStates(this) });

  var setValidator = function(o) {
    o = $(o);
    o.unbind();
    var target = $('#' + o.attr('id').replace(/^validator_/, ''));
    target.change(function() {
      if ($(this).val() == '') {
        o.html('This field is required.');
      }
      else {
        o.html('');
      }
    });
  }
  $('.validator').each(function() {
    setValidator(this);
  });

  var countryVal = 'US';
  $('#request_country').change(function() {
    if ($(this).val() == countryVal) return;
    if ($(this).val() != 'US') {
      var current = $('#request_state_province');
      var newField = $('<input type="text" name="request_state_province" />').insertAfter(current);
      current.remove();
      newField.attr('id', 'request_state_province');
      setValidator($('#validator_request_state_province'));
    }
    else {
      var current = $('#request_state_province');
      var newField = $('<select name="request_state_province"></select>').insertAfter(current);
      current.remove();
      newField.attr('id', 'request_state_province');
      loadStates(newField);
      setValidator($('#validator_request_state_province'));
    }
    countryVal = $(this).val();
  });

  $('#request_form').submit(function() {
    $(this).find('input, select, textarea').trigger('change');
    var valid = true;
    $(this).find('.validator').each(function() {
      if ($(this).html() != '') {
        valid = false;
        return false;
      }
    });
    return valid;
  });

  // Store functionality:
  $('.reportTypes a').click(function() {
    var reportType = this.hash.replace('#', '');
    if (reportType == 'All') {
      $('.productBox').show();
    }
    else {
      $('.productBox').hide();
      $('.' + reportType).show();
    }
    this.blur();
    return false;
  });

  // Custom dropdowns:
  $('select#dropdown-geography').change(function() {
    window.location = 'find_solutions_geography.asp?StateCode=' + $(this).val();
  });
  $('select#dropdown-industry').change(function() {
    window.location = 'find_solutions_industry.asp?Industry=' + $(this).val();
  });
  $('select#dropdown-research').change(function() {
    window.location = 'find_solutions_research.asp?ResearchType=' + $(this).val();
  });
	
	// Newsletter submit:
	$('#email').click(function() {
		$(this).val('');
	});
});
