$(document).ready(function(){
  $('#gallery').cycle({
    fx:    'fade',
    speed:    1000, 
    timeout:  7000,
    pager: 'ul.range'
  });
  $('ul.range').append('\
    <li class="play"><a href="#">Play</a></li>\
		<li class="pause"><a href="#">Pause</a></li>');
	$('.play').hide();
	$('.pause a').click(function(e){
	  e.preventDefault();
	  $('#gallery').cycle('pause');
	  $('.play').show();
	  $('.pause').hide();
	});
	$('.play a').click(function(e){
	  e.preventDefault();
	  $('#gallery').cycle('resume');
	  $('.pause').show();
	  $('.play').hide();
	});
	$('ul.range a').wrap('<li></li>');
	$('#issueslist img').wrap('<span></span>');
	AddExitMsg();
	HackIssues();
	$('.CalendarControl tr').each(function(i){
	  var row = $(this);
	  if(row.find('td:not(:empty), th:not(:empty)').length < 1)
	  {
	    row.hide();
	  }
	});
	$('th.CalendarDayHeaderStyle:first').text('Sun');
	$('th.CalendarDayHeaderStyle:last').text('Sat');
	SelectHeader('.slide img','header');
	WirePlaceholders();
	PopupControl('popup','#modalPopup',[1,4,10]);
	FormToAkamaiRedirect('smallbusiness.house.gov');
});
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 HackIssues()
{
  $('.pageTitle[id*=IssueTitle] ~ br').remove();
  $('.pageTitle[id*=IssueTitle]').remove();
}

// JavaScript External Link Exit Message
function exitMsg(){
 var answer = confirm("You are now leaving the House Small Business Committee 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.hostname.search(/house\.gov/gi)!=-1) {return false;}
    if(a.hostname.search(/addthis\.com/gi)!=-1) {return false;}
    if(a.href.search(/youtube\.com\/SmallBizRepublicans/gi)!=-1) {return false;}
    if(a.href.search(/twitter\.com\/SmallBizRepub/gi)!=-1) {return false;}
    return a.hostname && a.hostname !== window.location.hostname;
  }
});

/* --- Add Exit Message ---*/
function AddExitMsg()
{
  $('a:external').click(function(){
    return(exitMsg());
  });
}

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;
}

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=/";
}
// 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);
    });
  }
}
// Cookie-based header selection
function SelectHeader(selector, cookieName)
{
  var headerIndex = parseInt(readCookie(cookieName), 10);
  if(isNaN(headerIndex) || headerIndex>=$(selector).size())
  {headerIndex = 0;}
  $(selector).hide();
  $(selector).filter('(:eq('+headerIndex+')').fadeIn();
  createCookie(cookieName,headerIndex+1,3);
}
