$(document).ready(function(){
	clearInputs();
	fadeGall();
	ieHover('#main-nav li');
	initTabs('.tabset');
	HomeMedia();
	Seeding();
	PopupControl('popup','#modalPopup',[1,10]);
	FormToAkamaiRedirect('calvert.house.gov');
});
/*---- clear inputs function ---*/
function clearInputs(){
	$('input:text, 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(){
	var wait_time = 6000;
	$('#header .gallery').each(function(){
		var _list = $(this).children();
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var _t;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		function changeEl(_ind){
			_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:500});
			_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:500});
			_a = _ind;
			if(_t) clearTimeout(_t);
			_t = setTimeout(function(){
				if(_a == _list.length - 1) changeEl(0);
				else changeEl(_a + 1);
			}, wait_time);
		}
		_t = setTimeout(function(){
			if(_a == _list.length - 1) changeEl(0);
			else changeEl(_a + 1);
		}, wait_time);
	});
}
/*--- 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() {
				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/none.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 = 80;
	_min = 48;
	_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;
			}
		}
	}
}
/*--- Homepage Tab Content ---*/
function HomeMedia(){
    $('.HomeFeature:first').show();
    $('.tab-holder ul a').click(function(event){
        event.preventDefault();
        $('.HomeFeature').hide();
        $($(this).attr('href')).show();
    });    
}
if (window.addEventListener) window.addEventListener("load", _fontResize, false);
else if (window.attachEvent) window.attachEvent("onload", _fontResize);

function PrintPage()
    {
        var DocumentContainer = document.getElementById('PrintPage');
        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();
    }

/* --- Seed Contact with Zips & Email Signup with Name/Addy --- */
function Seeding()
{
  var formID = $.url().param('ID');
  if(formID == '580')
  {
    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 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);
    });
  }
}

