$(document).ready(function(){
	clearInputs();
	fadeGall();
	ieHover('#menu li');
	initTabs('.tabset');
	//Popup Clicking has been moved to an include.
	//$('#modalPopup').click();
	//PopupControl('popup','#modalPopup',[3,7,10],true);
	SeedSignup();
	// Hide hidden form elements that forms.css shows
	$('input[type=hidden]').hide();
	// Blog fixes
	$('.blogComments"').removeClass('blogComments"').addClass('blogComments');
	$('.blogEntry:first').css('border','0');
	HackTours();
	InitWelcomeVids();
	PopupControl('welcome','#video2',[1]);
	FormToAkamaiRedirect('latta.house.gov');
});

function HackTours()
{
  $('#Table3 input:checkbox[id*=fbi]').parents('tr:first').hide();
  $('#Table3 input:checkbox[id*=mountVernon]').parents('tr:first').hide();
  //find the fbi checkbox
  //recursively -> top get all its parents that match 'tr'
  //hide the first one
}

/*---- clear inputs function ---*/
function clearInputs(){
	$('input:text:not([onblur]), input:password, textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
/*--- fade gallery function ---*/
function fadeGall(){
	$('.gallery').each(function(){
		var _list = $(this).find('.frame li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		if($.browser.msie && $.browser.version < 7) _list.removeClass('active').hide().eq(_a).addClass('active').show();
		else _list.removeClass('active').hide().eq(_a).addClass('active').show();
		$(this).find('.next').click(function(){
			if(_a == _list.length - 1) changeEl(0);
			else changeEl(_a + 1);
			return false;
		});
		$(this).find('.prev').click(function(){
			if(_a == 0) changeEl(_list.length - 1);
			else changeEl(_a - 1);
			return false;
		});
		function changeEl(_ind){
		    StopAllVideos();
			if(_ind != _a){
				if($.browser.msie && $.browser.version < 7){
					_list.eq(_a).removeClass('active').hide();
					_list.eq(_ind).addClass('active').show();
				}
				else{
					_list.eq(_a).removeClass('active').fadeOut();
					_list.eq(_ind).addClass('active').fadeIn();
				}
				_a = _ind;
			}
		}
	});
}
/*--- IE6 hover function ---*/
function ieHover(h_list) {
	if ($.browser.msie && $.browser.version < 7) {
		$(h_list).hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}
}
/*--- tabs function ---*/
function initTabs(h_list) {
	$(h_list).each(function(_ind, _el) {
		var btn_h = $(_el);
		var _btn = $(_el).find('a.tab');
		var _a = 0;
		_btn.each(function(_ind, _el) {
			this._box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				this._box.show();
				_a = _ind;
			}
			else {
				this._box.hide();
			}
			_el.onclick = function() {
			    StopAllVideos();
				if(!$(this).hasClass('active')){
					_btn.get(_a)._box.hide();
					_btn.eq(_a).removeClass('active');
					this._box.show();
					$(this).addClass('active');
					_a = _ind;
				}
				return false;
			}
		});
	});
}
/*--- IE6 png fix ---*/
var transparentImage = "images/transparent.gif";
function fixTrans(){
	if (typeof document.body.style.maxHeight == 'undefined'){
	var imgs = document.getElementsByTagName("img");
	for (i = 0; i < imgs.length; i++){	
		if (imgs[i].src.indexOf(transparentImage) != -1) return;
		if (imgs[i].src.indexOf(".png") != -1){
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
			}
		}	
	}
}
if (document.all && !window.opera) attachEvent("onload", fixTrans);
/*--- font resize function ---*/
function _fontResize(){
	var _element = document.body;
	_default = 64;
	_curent = _default;
	_max = 72;
	_min = 56;
	_element.style.fontSize = _default + '%';
	var _step = 8;
	var _aaa = document.getElementById('aaa');
	var _btn = _aaa.getElementsByTagName('a');
	for (var j = 0; j < _btn.length; j++)
	{
		_btn[j].onclick = function()
		{
			if(this.className == 'zoom-in')
			{
				_curent += _step;
				if(_curent > _max)
				_curent = _max;
				_element.style.fontSize = _curent + '%';
			}
			else if(this.className == 'zoom-out')
			{
				_curent -= _step;
				if(_curent < _min)
				_curent = _min;
				_element.style.fontSize = _curent + '%';
			}
			else 
			{
				_element.style.fontSize = _default + '%';
				_curent = _default;
			}
		}
	}
}
/*---- Email Signup Seed ----*/
function SeedSignup()
{
  // Hack to fix the back button reseeding
  var homePageEmail = $('#HomeEmailInput').val();
  if (homePageEmail != null)
  {
    $('#HomeEmailInput').val(homePageEmail.replace(/%40/g,'@'));
  }
  
  var formID = $.url().param('ID');
  if(formID == '328')
  {
    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);
  }
}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function videoChangeVol(flash, str) {
  if(getFlashMovie(flash)!=null)
  {
    getFlashMovie(flash).jsChangeVol(str);
  }
}

if (window.addEventListener) window.addEventListener("load", _fontResize, false);
else if (window.attachEvent) window.attachEvent("onload", _fontResize);


function StopAllVideos()
{
  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;
}
// 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);
    }
  }
}
function f(o)
{
	o.value=o.value.replace(/([^0-9])/g,"");
}
var state = "OH";
var district = "05";

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
	if (val) { nm=args[i+1]; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.match(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/gi);
      if (p==null) errors+='- '+nm+' must be a valid e-mail address.\n';
	} else if (test!='R') { num = parseFloat(val);
	if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
	if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
	min=test.substring(8,p); max=test.substring(p+1);
	if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
	} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	} if (errors) alert('The following error(s) occurred:\n'+errors);
	document.MM_returnValue = (errors == '');
}

function InitWelcomeVids(){
  $('.video-btn').each(function(){
    var vidBtn = $(this);
    vidBtn.click(function(){
     $('body').append('\
        <div id="WelcomeVid">\
          <a id="KillVid">Close</a>\
          <div class="flashy">\
            <iframe allowTransparency="true" border="0" scrolling="no" src="/Welcome/'+vidBtn.attr('name')+'.html"></iframe>\
          </div>\
        </div>');
    $('#WelcomeVid #KillVid').click(function(){
      $('#WelcomeVid').remove();
      });
    });
  });
}
// 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);
    });
  }
}


