$(document).ready(function(){
	//fadeGall();
	//$('a:not(:external)').attr('href','/debtceiling/');
	//$('a[href$=253337]').attr('href','/doc-253337/');
	//$('a[href$=253261]').attr('href','/doc-253261/');
	//$('a[href$=253410]').attr('href','/doc-253410/');
	//$('a[href$=253417]').attr('href','/doc-253417/');
	//$('a[href$=253418]').attr('href','/doc-253418/');
	//$('a[href$=253325]').attr('href','/doc-253325/');
	$('div.gallery').cycle({
	  slideExpr:'ul.fader > li',
	  timeout:5000,
	  speed:1200,
	  pager:'ul.paging',
	  pagerAnchorBuilder:function(idx, slideEl)
	    {
	      return '<li><a href="#">' + (idx+1) + '</a></li>';
	    },
	  before:function(currSlideElement, nextSlideElement, options, forwardFlag)
	    {
	      if($(nextSlideElement).hasClass('alt')||$(nextSlideElement).has('object,embed').length>0)
	      {
	        $('.img-border').css('z-index','1');
	      }
	      else
	      {
 	        $('.img-border').css('z-index','99');
	      }
	      pauseAllVideos();
	    },
	  activePagerClass:'active',
	  //pause:true,
	  cleartypeNoBg:true
	});
	$('#writeRepForm .header:contains(Comment)').text('Message');
	$('#writeRepForm .ContactForm_Subscribe').text(' I would like to receive updates via email');
	var responseRequestedTable = $('#writeRepForm table[id$=ReplyChoice]');
	responseRequestedTable.find('input[value=Comment]').attr('checked','checked');
	responseRequestedTable.parent().hide();
  if($('ul.fader > li:first').hasClass('alt')||$('ul.fader > li:first').has('div[id^=ytholder]').length>0)
  {
    $('.img-border').css('z-index','1');
  }
	$('.img-border').hover
	$('ul.news-list').each(function(){
	  $(this).find('li:even').addClass('mark');
	});
	$('form.signup input:text').focus(function() {
    if( this.value == this.defaultValue ) {
        this.value = "";
    }
  }).blur(function() {
      if( !this.value.length ) {
          this.value = this.defaultValue;
      }
  });
  $('.paging li a').each(function(i){
    var pagerLink = $(this);
    var slide = $('.fader > li:eq('+i+')');
    pagerLink.qtip({
      position: {
        corner: {
         target: 'topMiddle', // Use the corner...
         tooltip: 'bottomMiddle' // ...and opposite corner
        },
        adjust: {
          y:-3
        }
      },
      style: {
        border: {
         width: 1,
         radius: 5
        },
        width: {
          max:(slide.find('h2').length>0)?150:200
        },
        padding: 5, 
        textAlign: 'center',
        tip: true // Give it a speech bubble tip with automatic corner detection
      },
      content: (slide.find('h2').length>0) ?
        ((slide.find('div[id^=ytholder]').length>0) ?
          (slide.find('h2').text() + '<br/><img alt="" src="http://i.ytimg.com/vi/'+/\/v\/(.{11})/i.exec(slide.html())[1]+'/default.jpg" style="width:130px;display:block;margin:3px 0 0;"/>') :
          (slide.find('h2').text() + '<br/><img alt="" src="'+slide.find('img').attr('src')+'" style="width:130px;display:block;margin:3px 0 0;"/>')) :
        ('<img alt="" src="'+slide.find('img').attr('src')+'" style="width:180px;display:block;margin:3px 0 0;"/>')
    });
  });
});

function cookiesEnabled(){
  var foundCookie = false;
  if (readCookie("cookiesEnabled") == "true")
  {
    foundCookie = true;
  }
  else
  {
    createCookie("cookiesEnabled", "true", 1000);
    if (readCookie("cookiesEnabled") == "true")
    {
      foundCookie = true;
    }
  }
  createCookie("cookiesEnabled","false",-1);
  return foundCookie;
}

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 onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById(playerId);
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
  //alert("Player's new state: " + newState);
  if(newState == 1)
  {
    $('div.gallery').cycle('pause');
  }
  else if(newState == 2 || newState == 0)
  {
    $('div.gallery').cycle('resume');
  }
}

function pauseAllVideos()
{
  var video = $('object[id*=ytplay], embed[id*=ytplay]');
  video.each(function(){
    try{
      this.stopVideo();
    }
    catch(err)
    {
      //var vidError = err;
      //var nothing = 6;
    }
  }); 
}


// 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 == '/') { return true; }
	if(a.href == 'http://www.speaker.gov/') { return true; }
	if(/#tab\d/.test(a.href)) { return true; }
	if(/\/bio/i.test(a.href)) { return true; }
	if(/\/contact/i.test(a.href)) { return true; }
	if(/253(337|417|261|410|418|325)/.test(a.href)) { return true; }
    return a.hostname && a.hostname !== window.location.hostname;
  }
});

