$(function(){
	$('.TextOnly').click(function(event){
    event.preventDefault();
    if(readCookie('textonly')!='true')
    {
      createCookie('textonly','true');
      $('head link[id$=MainStylesheet]').attr('href','/css/textonly.css');
    }
    else{
      createCookie('textonly','false');
      $('head link[id$=MainStylesheet]').attr('href','/css/all.css');
    }
  });
	if(readCookie('textonly')=='true')
	{
	  $('head link[id$=MainStylesheet]').attr('href','/css/textonly.css');
	}
	FormToAkamaiRedirect('paulryan.house.gov');
	SlideShow();
	$('.pageTitle[id*=IssueTitle] ~ br').remove();
	$('.pageTitle[id*=IssueTitle]').remove(); 
	if($('.crumbs [id$=PageCrumb]').text() == $('.crumbs [id$=SectionLink]').text())
	{
	  $('.crumbs [id$=PageCrumb]').hide();
	}
	$('.buffer a:not([href])').css('color','#000').css('text-decoration','none');
	ExtendedSectionUrl();
	Seeding();
});

function SlideShow(){
  $('.slideshow').each(function(){
    var _gallery = $(this);
    var _toggle = _gallery.find('.pause');
    _gallery.cycle({
      slideExpr:'.gallery-holder li',
      timeout:6000,
      prev:_gallery.find('a.prev'),
      next:_gallery.find('a.next'),
      pager:_gallery.find('.links-block'),
      pagerAnchorBuilder:function(i){return '<li><a href="#">'+(i+1)+'</a></li>';},
      speed:500,
      before:onBefore
    });
    _toggle.click(function(e){
      e.preventDefault();
      ToggleSlideshow();
    });});
}

function ToggleSlideshow()
{
  var linkButton = $('.pause');
  if(linkButton.text()=='pause')
  {
    linkButton.text('play');
    $('.slideshow').cycle('pause');
  }
  else
  {
    linkButton.text('pause');
    $('.slideshow').cycle('resume');
    pauseAllVideos();
  }
}
function onBefore(currSlideElement, nextSlideElement, options, forwardFlag)
{
	pauseAllVideos();
}
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById(playerId);
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
  //alert("Player's new state: " + newState);
  if(newState == 1)
  {
    $('.slideshow').cycle('pause');
    $('.swicher .pause').text('play');
  }
  else if(newState == 2 || newState == 0)
  {
    $('.slideshow').cycle('resume');
    $('.swicher .pause').text('pause');
  }
}
function pauseAllVideos()
{
  var video = $('object[id*=ytplay], embed[id*=ytplay]');
  video.each(function(){
    try{
      this.stopVideo();
    }
    catch(err)
    {
      //var vidError = err;
      //var nothing = 6;
    }
  }); 
}

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;
}

function PrintContent()
{
  var DocumentContainer = document.getElementById('printInfo');
  var WindowObject = window.open('about:blank', "Printing", 
                        "width=740,height=325,top=200,left=250,toolbars=no,scrollbars=yes,status=no,resizable=no");
  WindowObject.document.writeln(DocumentContainer.innerHTML);
  WindowObject.document.close();
  WindowObject.focus();
  WindowObject.print();
  WindowObject.close();
}

var DirectoryArray = new Object();
DirectoryArray['casework2'] = 'Casework FAQ';

function ExtendedSectionUrl()
{
  if($.url().segment(1))
  {
    if(!/\.htm|\.aspx|issue$/gi.test($.url().segment(1)))
    {
      var linkName = DirectoryArray[$.url().segment(1)] ? DirectoryArray[$.url().segment(1)] : $.url().segment(1);
      $('.SectionLink').after(' &raquo; <a class="cap" href="/'+$.url().segment(0)+'/'+$.url().segment(1)+'">'+linkName+'</a>');
    }
  }
}

function Seeding()
{
  $('.formFieldError[style*=hidden]').css('display','none');
  if(/contactform|tourrequest/i.test($.url().attr('source')))
  {
    $('input#zip5').val($.url().param('Zip'));
    $('input#zip4').val($.url().param('Zip4'));
    
    if($('input#zip5').val())
    {
      $('input#zip5').hide().after($('input#zip5').val());
      $('input#zip4').hide().after($('input#zip4').val());
    }
  }
  if($.url().attr('source').match(/emailsignup/i)!=null)
  {
    $('input[id*=fname]').val($.url().param('firstname'));
    $('input[id*=lname]').val($.url().param('lastname'));
  }
  var formID = $.url().param('ID');
  if(formID == '464')
  {
    SeedHelper('email', $('input[id$=EmailControl]'));
    SeedHelper('firstname', $('#' + $('label:contains(First Name)').attr('for')));
    SeedHelper('lastname', $('#' + $('label:contains(Last Name)').attr('for')));
  }
}
function SeedHelper (parameterString, targetInput)
{
  var param = $.url().param(parameterString);
  if(param!=null)
  {
    param = unescape(param.replace(/\+/g, ' '));
    targetInput.val(param);
  }
}
// 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);
    });
  }
}


