$(document).ready(function(){
	InitGallery();
	PopupControl('popup','#modalPopup',[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]);
	WirePlaceholders();
	$('.tour-show').SlideShow({
		speed: 800,
		duration: 7000,
		list: 'ul>li',
		ieVersion: 6
	});
	$('.pageTitle[id*=IssueTitle] ~ br').remove();
	$('.pageTitle[id*=IssueTitle]').remove();
	FormToAkamaiRedirect('nunes.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')); }
      });
    });
  }
}

function InitGallery(){
	$('.gallery').SlideShow({
		speed: 800,
		duration: 14000,
		list: 'ul.slides>li',
		pager: '.thumbnails ul',
		dinamicPagination: false,
		ieVersion: 6
	});
	var _list = $('.slideshow li');
	var _i = Math.round(Math.random()*(_list.length-1));
	_list.eq(_i).fadeIn(800);
};
jQuery.fn.SlideShow = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		duration: 4000,
		list: 'ul.fade>li',
		prev: 'a.prev',
		next: 'a.next',
		pager: 'ul.switcher',
		dinamicPagination: true,
		pause: '.pause',
		ieVersion: 6
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _duration = _options.duration;
		var _list = _hold.find(_options.list);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _ie = _options.ieVersion;
		var _f = true;
		var _p = _options.dinamicPagination;
	/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager);
		if (_p){	_num.empty();
			_list.each(function(i){
				$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
			});
		};
		var _thumb = _num.find('li');
	/*-------------------------------------------------*/
		var _a = _list.index(_list.filter('.active'));
		if(_a == -1) {_a = _thumb.index(_thumb.filter('.active'));}	if(_a == -1) {_a = 0;}
		_list.removeClass('active').eq(_a).addClass('active');
		_thumb.removeClass('active').eq(_a).addClass('active');
		var _i, _old = _a, _t;
		if (jQuery.browser.msie && jQuery.browser.version < _ie){		_list.hide().eq(_a).show();
		}else{		_list.show().css({opacity:0}).eq(_a).css({opacity:1});	}
		Run(_a);
		
		function Run(_a){
			_t = setTimeout(function(){
				_a++; if (_a >= _list.length){_a=0}
				ChangeFade(_a);
			}, _duration);
		};
		function ChangeFade(_new){
			if(_new != _old){
				if(jQuery.browser.msie && jQuery.browser.version < _ie){
					_list.eq(_old).removeClass('active').hide();
					_list.eq(_new).addClass('active').show();
				}else{
					_list.eq(_old).removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
					_list.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
				}
				_thumb.eq(_old).removeClass('active');
				_thumb.eq(_new).addClass('active');
				_old=_new;_a=_new;
				if(_t) clearTimeout(_t);
				if (_f){Run(_new);}
			};
		};
		_pause.click(function(){
			_f = false;
			clearTimeout(_t);
			return false;
		});
		_thumb.click(function(){
			_i = _thumb.index($(this));
			ChangeFade(_i);
			_a = _i;
			return false;
		});
		_next.click(function(){
			_a++; if (_a == _list.length){_a=0}
			ChangeFade(_a);
			return false;
		});
		_prev.click(function(){
			_a--; if (_a == -1){_a = _list.length-1}
			ChangeFade(_a);
			return false;
		});
	});
};

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);
    });
  }
}

