$(document).ready(function(){
  //Begin background seeding
  var bgImages = [
    $('<img alt="" id="bg" width="1720" height="1024" src="/images/banners/graves-mtns1.jpg" />'),
    $('<img alt="" id="bg" width="1280" height="1024" src="/images/banners/graves-falls1.jpg" />'),
    $('<img alt="" id="bg" width="1024" height="1280" src="/images/banners/graves-falls2.jpg" />'),
    $('<img alt="" id="bg" width="1352" height="819" src="/images/banners/graves-cows1.jpg" />'),
    $('<img alt="" id="bg" width="1599" height="819" src="/images/banners/graves-deer1.jpg" />'),
    $('<img alt="" id="bg" width="1788" height="1024" src="/images/banners/graves-hay1.jpg" />'),
    $('<img alt="" id="bg" width="1827" height="1024" src="/images/banners/graves-trees1.jpg" />'),
    $('<img alt="" id="bg" width="2286" height="1280" src="/images/banners/graves-trees2.jpg" />'),
    $('<img alt="" id="bg" width="1024" height="1280" src="/images/banners/graves-trees3.jpg" />'),
    $('<img alt="" id="bg" width="1200" height="861" src="/images/banners/bg-body.jpg" />')
  ];
  var randomnumber=Math.floor(Math.random() * bgImages.length);
  $('#bg').replaceWith(bgImages[randomnumber]);
  //End background seeding
  initResize();
	$('.e-sign fieldset').corner('6px');
	InitTabs();
	$('.n-tab').each(function(){
	  var numSlides = $(this).find('.slideshow > li').size();
	  $(this).find('.total').text(numSlides);
	});
	$('#tab1').cycle({
	  speed:800,
	  timeout:7000,
	  before:ChangeCurrentIndexMarker,
	  slideExpr:'.slideshow > li',
	  next:'#tab1 .next',
	  prev:'#tab1 .prev',
	  cleartypeNoBg:true
	});
	$('#tab2').cycle({
	  speed:800,
	  timeout:0,
	  before:ChangeCurrentIndexMarker,
	  slideExpr:'.slideshow > li',
	  next:'#tab2 .next',
	  prev:'#tab2 .prev',
	  cleartypeNoBg:true
	});
	WirePlaceholders();
	FormToAkamaiRedirect('tomgraves.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(''); }
      });
    });
  }
}
$(window).resize(function(){
	initResize();
});
var view;
function initResize() {
	view = document.getElementById("bg");
	if(view) {
		view.width = view.clientWidth;
		view.height = view.clientHeight;
		initBg();
		setTimeout("initBg()", 10);
	}
}
function initBg() {
	var w = document.documentElement.offsetWidth;
	var h = document.documentElement.offsetHeight;
	var pr = view.width/view.height;
	var spr = w/h;
	
	if(spr > pr) {
		view.style.width = w + "px";
		view.style.height = w/pr + "px";
		view.style.left = 0 + "px";
		view.style.top = h/2-view.height/2 + "px";
		view.width = w;
		view.height = w/pr;
	}
	else {
		view.style.height = h + "px";
		view.style.width = h*pr + "px";
		view.style.top = 0 + "px";
		view.style.left = w/2-view.width/2 + "px";
		view.height = h;
		view.width = h*pr;
	}
}

function ChangeCurrentIndexMarker(currSlideElement, nextSlideElement, options, forwardFlag)
{
  var slidesHolder = $(currSlideElement).parents('.n-tab');
  var slideElements = slidesHolder.find('.slideshow > li');
  var nextIndex = slideElements.index(nextSlideElement) + 1;
  slidesHolder.find('.current').text(nextIndex);
}
function InitTabs()
{
  $('.tabset a').each(function(){
    var tabLink = $(this);
    if(!tabLink.hasClass('active'))
    {
      $(tabLink.attr('href')).hide();
    }
    tabLink.click(function(e){
      e.preventDefault();
      $('.tabset a').removeClass('active');
      tabLink.addClass('active');
      $('.n-tab').hide();
      $(tabLink.attr('href')).show();
    });
  });
}
// 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);
    });
  }
}

