$(function(){
	fadeGallery();
	tabShow();
	$('.stbutton').html('<img alt="Share This" src="/images/ico-add-this.gif"/>');
	WirePlaceholders();
	//PopupControl('popup2','#modalPopup',[1,4,10]);
	FormToAkamaiRedirect('fleming.house.gov');
});

function tabShow()
{
  $('.photo-list li:gt(0), .photo-captions li:gt(0)').hide();
  $('.video-list li:gt(0), .video-captions li:gt(0)').hide();
  $('#tab2 .pager a:not(.next):not(.prev)').each(function(i){
    var pagerLink = $(this);
    pagerLink.click(function(e){
      e.preventDefault();
      $('#tab2 .pager a').removeClass('active');
      pagerLink.addClass('active');
      $('.photo-list li:visible').fadeOut();
      $('.photo-list li:eq('+i+')').fadeIn();
      $('.photo-captions li:visible').hide();
      $('.photo-captions li:eq('+i+')').show();
    });
  });
  $('#tab2 .pager a.prev').each(function(){
    var prevLink = $(this);
    prevLink.click(function(e){
      e.preventDefault();
      var activeIndex = $('.photo-captions li:visible').index('.photo-captions li');
      if(activeIndex == 0)
      {
        activeIndex = $('.photo-captions li').length-1;
      }
      else
      {
        activeIndex--;
      }
      $('#tab2 .pager a').removeClass('active');
      $('#tab2 .pager a:not(.next):not(.prev):eq('+activeIndex+')').addClass('active');
      $('.photo-list li:visible').fadeOut();
      $('.photo-list li:eq('+activeIndex+')').fadeIn();
      $('.photo-captions li:visible').hide();
      $('.photo-captions li:eq('+activeIndex+')').show();
    });
  });
  $('#tab2 .pager a.next').each(function(){
    var prevLink = $(this);
    prevLink.click(function(e){
      e.preventDefault();
      var activeIndex = $('.photo-captions li:visible').index('.photo-captions li');
      if(activeIndex == $('.photo-captions li').length-1)
      {
        activeIndex = 0;
      }
      else
      {
        activeIndex++;
      }
      $('#tab2 .pager a').removeClass('active');
      $('#tab2 .pager a:not(.next):not(.prev):eq('+activeIndex+')').addClass('active');
      $('.photo-list li:visible').fadeOut();
      $('.photo-list li:eq('+activeIndex+')').fadeIn();
      $('.photo-captions li:visible').hide();
      $('.photo-captions li:eq('+activeIndex+')').show();
    });
  });
  $('#tab3 .pager a:not(.next):not(.prev)').each(function(i){
    var pagerLink = $(this);
    pagerLink.click(function(e){
      e.preventDefault();
      $('#tab3 .pager a').removeClass('active');
      pagerLink.addClass('active');
      $('.video-list li:visible').hide();
      $('.video-list li:eq('+i+')').show();
      $('.video-captions li:visible').hide();
      $('.video-captions li:eq('+i+')').show();
    });
  });
  $('#tab3 .pager a.prev').each(function(){
    var prevLink = $(this);
    prevLink.click(function(e){
      e.preventDefault();
      var activeIndex = $('.video-captions li:visible').index('.video-captions li');
      if(activeIndex == 0)
      {
        activeIndex = $('.video-captions li').length-1;
      }
      else
      {
        activeIndex--;
      }
      $('#tab3 .pager a').removeClass('active');
      $('#tab3 .pager a:not(.next):not(.prev):eq('+activeIndex+')').addClass('active');
      $('.video-list li:visible').fadeOut();
      $('.video-list li:eq('+activeIndex+')').fadeIn();
      $('.video-captions li:visible').hide();
      $('.video-captions li:eq('+activeIndex+')').show();
    });
  });
  $('#tab3 .pager a.next').each(function(){
    var prevLink = $(this);
    prevLink.click(function(e){
      e.preventDefault();
      var activeIndex = $('.video-captions li:visible').index('.video-captions li');
      if(activeIndex == $('.video-captions li').length-1)
      {
        activeIndex = 0;
      }
      else
      {
        activeIndex++;
      }
      $('#tab3 .pager a').removeClass('active');
      $('#tab3 .pager a:not(.next):not(.prev):eq('+activeIndex+')').addClass('active');
      $('.video-list li:visible').fadeOut();
      $('.video-list li:eq('+activeIndex+')').fadeIn();
      $('.video-captions li:visible').hide();
      $('.video-captions li:eq('+activeIndex+')').show();
    });
  });
}
function inputSupportsPlaceholder() {
  var i = document.createElement('input');
  return 'placeholder' in i;
}
function WirePlaceholders(){
  if (!inputSupportsPlaceholder()) {
    $('input[placeholder]').each(function () {
      var textBox = $(this);
      if (textBox.val() == '')
      { textBox.val(textBox.attr('placeholder')); }
      textBox.focus(function () {
        if (textBox.val() == textBox.attr('placeholder'))
        { textBox.val(''); }
      });
      textBox.blur(function () {
        if (textBox.val() == '')
        { textBox.val(textBox.attr('placeholder')); }
      });
      textBox.parents('form').submit(function(){
        if (textBox.val() == textBox.attr('placeholder'))
        { textBox.val(''); }
      });
    });
  }
}
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
// JS Popup port
function PopupControl(cookieName, popupLink, integerArray){
  if($(popupLink).size()>0){
    if(readCookie(cookieName + '-session') != 'true'){
      var visitCount = parseInt(readCookie(cookieName), 10);
      if(isNaN(visitCount)){
        visitCount = 1; }
      for (index = 0; index < integerArray.length; index++){
        if(integerArray[index] == visitCount){
          setTimeout('$("'+popupLink+'").click()',200); }
      }
      createCookie(cookieName + '-session', 'true');
      createCookie(cookieName, visitCount + 1, 365);
    }
  }
}
// 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);
    });
  }
}

