$(document).ready(function(){  FormToAkamaiRedirect('edworkforce.house.gov');  	Seeding();	HackLinksWithFS21();	$('div.gal-fade').gallFade({		duration: 700,		autoSlide: 5000	});	//$('#nav > li > a').contents().wrap('<span><span></span></span>');	//$('#nav .drop li a').contents().wrap('<span></span>').before('<img src="/images/arrow01.gif" alt="">');	$('#nav .drop li a').prepend('<img src="/images/arrow01.gif" alt="">');		if(!$.support.borderRadius)	{	  $('#int .r-hold').corner('16px top cc:#cec8ba');	  $('#int .rounded').corner('16px bottom cc:#e4dcce');	  $('.tab-links h2 a').corner('7px top cc:#c7c2b5');	}	else	{	  $('#int .r-hold').corner('16px');	  $('#int .rounded').corner('16px bottom');	  $('.tab-links h2 a').corner('7px top');	}	var foo = $.browser.mozilla;	if(!$.support.borderRadius || $.browser.mozilla) //FF3 does not support border-radius on images.	{	  $('.feature .img-holder').corner('left 7px cc:#e5e1d2');	}	else {	  $('.feature .img-holder img').corner('left 7px');	}	$('#int .rounded h2.PageLink').corner('10px top');		InitTabs();		$('ul.quick-links a img').wrap('<span class="image"></span>');	$('ul.quick-links a').textNodes().wrap('<span class="text"><span></span></span>');		$('#int .aside .sb li a').before('<span class="sb-t"></span>').after('<span class="sb-b"></span>').wrapInner('<strong></strong>');		$('.col .survey a').addClass('button').wrapInner('<span></span>').append('<em></em>');		$('input[name=EventTypeID]').val($('input[id$=EventTypeIDs]').val());	AddExitMsg();	OversightAccordion();	$('.CalendarControl tr').each(function(i){
	  var row = $(this);
	  if(row.find('td:not(:empty), th:not(:empty)').length < 1)
	  {
	    row.hide();
	  }
	});
	$('#int th.CalendarDayHeaderStyle:first').text('Sun');
	$('#int th.CalendarDayHeaderStyle:last').text('Sat');
});
/* Gallery (fade-on-click, auto-fade-left) */
jQuery.fn.gallFade = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('div.inner > ul > li');
		var _el = _hold.find('ul.switch > li');
		var _count = _el.index(_el.filter(':last'));
		var _t;
		var _active = _el.index(_el.filter('.active:eq(0)'));
		if (_active < 0) _active = 0;
		var _last = _active;
		_wrap.css({opacity:0}).eq(_active).css({opacity:1});
		_wrap.removeClass('active').eq(_active).addClass('active');
		_el.removeClass('active').eq(_active).addClass('active');
		function fadeEl(){
			_wrap.eq(_last).animate({
				opacity:0
			}, {queue:false, duration: _speed});
			_wrap.eq(_active).animate({
				opacity:1
			}, {queue:false, duration: _speed});
			_wrap.removeClass('active').eq(_active).addClass('active');
			_el.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count)) _active = 0;
				fadeEl();
			}, _timer);
		}
		runTimer();
		_el.click(function(){
			_active = _el.index($(this));
			if(_t) clearTimeout(_t);
			fadeEl();
			runTimer();
			return false;
		});
	});
}

$.fn.textNodes = function()
{
  return $(this).contents().filter(function(){ return this.nodeType == 3 || this.nodeName == "BR" ; });
}


function Seeding()
{
//  // This is being handled in individual EventSingle code. 
//  // The /Legislation section wasn't using it anyway
//  if(jQuery.url.attr('source').match(/legislation|eventsingle/i)!=null)
//  {
//    getThomasLinks();
//  }

  var formID = $.url().param('ID');
  if(formID == '684')
  {
    SeedHelper('email',$('input[id*=EmailControl]'));
    SeedHelper('firstname',$('#'+$('label:contains(First Name)').attr('for')));
    SeedHelper('lastname',$('#'+$('label:contains(Last Name)').attr('for')));
    $('.formFieldContainer label:contains(Zip)').text('* Zip');
    $('.formFieldContainer label:contains(Home Address)').hide();
  }
}
function SeedHelper (parameterString, targetInput)
{
  var param = $.url().param(parameterString);
  if(param!=null)
  {
    param = unescape(param.replace(/\+/g, ' '));
    targetInput.val(param);
  }
}

function getThomasLinks()
{
  // Javascript Regex does not have lookbehinds, so we make our own.
  // Replaces bill numbers with links to the bills, assuming that the bill name is not inside an html tag (ie already the href of a link)
  // Possible further improvement: Do not link bills that are the text of a hyperlink.
  //var pageContent = $('.buffer').html();
  //$('.buffer').html(pageContent.replace(/(<[^>]*)?(H\.)(\s?)(R(?:es)?\.)(\s?)(\d{1,4})/gi, function($0,$1){ return $1?$0:$0.replace(/(H\.)(\s?)(R(?:es)?\.)(\s?)(\d{1,4})/gi,'<a href="http://thomas.loc.gov/cgi-bin/query/z?c111:$1$3$5:">$1$2$3$4$5</a>');}));
  
  // The above function is incomplete.
  // The below iterates through the tree, skipping hyperlinks and their children
  // and linking instances of bills to their Thomas page.
  iterativeThomas($('.buffer'), (new Date().getFullYear() - 1789) / 2 + 1);
}

function linkThomas(element, congress)
{
  element.contents().filter(function() {
    return this.nodeType == 3;
  }).each(function(){
    $(this).before($(this).text().replace(/(H\.)(\s?)(R(?:es)?\.)(\s?)(\d{1,4})/gi,
      '<a href="http://thomas.loc.gov/cgi-bin/query/z?c'+congress+':$1$3$5:">$1$2$3$4$5</a>'));
    $(this).remove();
  });
}

function iterativeThomas(element, congress)
{
  linkThomas(element, congress);
  element.children(':not(a)').each(function(){
    iterativeThomas($(this), congress);
  });
}

// JavaScript External Link Exit Message
function exitMsg(){
  var answer = confirm("You are now leaving the Committee on Education and the Workforce website. Thank you for visiting. Neither this office nor the U.S. House of Representatives is responsible for the content of the non-House site you are about to access.")
  if (!answer)
    return false;
  return true;
  //end of Exit Message
}


// Check whether links are external:
// (Only works with elements that have href):
$.extend($.expr[':'],{
  external: function(a,i,m) {
    if(!a.href) {return false;}
    if(a.href.toLowerCase()=='http://www.facebook.com/pages/Washington-DC/Committee-on-Education-and-Labor-Republicans/53010329859'.toLowerCase()) {return false;}
    if(a.href.toLowerCase()=='http://www.youtube.com/EdLaborRepublicans'.toLowerCase()) {return false;}
    if(a.href.toLowerCase()=='http://twitter.com/EdLaborRepub'.toLowerCase()) {return false;}
    if(/house\.gov/gi.test(a.hostname)) {return false;}
    if(/edwork\.edgeboss\.net/gi.test(a.hostname)) {return false;}
    if(/frwebgate\.access\.gpo\.gov/gi.test(a.hostname)) {return false;}
    if(/javascript/gi.test(a.protocol)) {return false;}
    return a.hostname && a.hostname !== window.location.hostname;
  }
});

/* --- Add Exit Message ---*/
function AddExitMsg()
{
  $('a:external').click(function(){
    return(exitMsg());
  });
}

function HackLinksWithFS21()
{
  $('a[href*=fireside21]').each(function(){
    var templink = $(this).attr('href');
    templink = templink.replace(/edlabor\.fireside21\./gi,'republicans.edlabor.');
    $(this).attr('href',templink);
  });
}

function OversightAccordion()
{
  //if(jQuery.url.attr('source').match(/oversight/i)!=null)
  {
    $('#accordion h3 a').replaceWith('<input type="button" class="plusminus"/>');
    $('#accordion h3:not(:has(input.plusminus))').prepend('<input type="button" class="plusminus"/>');
    $('#accordion > :not(h3)').hide().addClass('print');
    //$('#accordion h3 .plusminus').attr('href','javascript:void(0);');
    $('#accordion h3 .plusminus').each(function(){
      var toggleButton = $(this);
      toggleButton.click(function(){
        toggleButton.parents('h3:first').nextUntil('h3').toggle();
        toggleButton.toggleClass('active');
      });
    });
  }
}

function InitTabs()
{
  $('.tab-links a:first').addClass('active');
  $('.tab-links a').each(function(){
    var tabLink = $(this);
    var tab = $(tabLink.attr('href'));
    if(!tabLink.hasClass('active'))
    {
      tab.hide();
    }
    tabLink.click(function(e){
      e.preventDefault();
      $('.tab').hide();
      $('.tab-links a').removeClass('active');
      tab.show();
      tabLink.addClass('active');
    });
  });
}
// Send relative links to unSSL canonical DNS
function FormToAkamaiRedirect(defaultDomain)
{
  if(location.hostname != defaultDomain
    || location.protocol != 'http:')
  {
    $('a[href^=\\/]').each(function(){
      var origUrl = $(this).attr('href');
      $(this).attr('href', 'http://' + defaultDomain + origUrl);
    });
  }
}

