//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName('a')) return false;
  $('a.newwindow').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
}
//*****Add swfobject flash call**************************************************//
function initializeFlash() {
  if (!document.getElementById('flash-content')) return false;
  var flashvars = {};
  var params = {wmode: 'transparent'};
  var attributes = {};
  attributes.id = "flash";
  swfobject.embedSWF("swf/header-home-flash.swf", "flash", "998", "167", "8.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnSubmit').click(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
//*****Mask function from http://digitalbush.com/projects/masked-input-plugin**************************************************//
function mask() {
  if ($('#formValidate').length < 1) return false;
  $('#zip').mask('99999');
  $('#phone').mask('(999) 999-9999');
  $('#date').mask('99/99/9999');
  $('#ssn').mask('999-99-9999');
  $('#year').mask('9999');
  $('#state').mask('aa');
}
//*****jQuery form validation from http://bassistance.de/jquery-plugins/jquery-plugin-validation/**************************************************//
function validate() {
  if ($('#formValidate').length < 1) return false;
  $('#formValidate').validate({
    highlight: function(element, errorClass) {//add the error class to the label and element
		  if ($(element).is(':radio')) {
		    $(element).parent().prev().addClass(errorClass);
		  } else {
        $(element).addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
      }
    },
    unhighlight: function(element, errorClass) {//remove error class from label and element once valid
		  if ($(element).is(':radio')) {
		    $(element).parent().prev().removeClass(errorClass);
		    $(element).parent().prev().addClass('valid');
		  } else {
        $(element).removeClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
      }
    },
    success: function(label) {//add the green box and checkmark for valid fields
		  if (label.prev().is(':checkbox')) {
        label.next().addClass("valid");
        label.addClass("valid").text("✓");
      } else {
        label.prev().addClass("valid");
        label.addClass("valid").text("✓");
      }
    },
    onfocusout: false,
    onkeyup: false,
    onclick: false,
    //do not use the title attribute as the error message
    ignoreTitle: true,
    //make the error text wrapped in a span
    errorElement: 'span',
    //javascript instead of class specified rules
    rules: {
      'rdoLiveWith[]': 'required'
    },
    //messages other than the default 'X'
    messages: {
		  'rdoLiveWith[]': '<br />X Please select one of the options below<br />'
		},
    // the errorPlacement has to take the h2 of radio buttons into account
		errorPlacement: function(error, element) {
		  if ( element.is(':radio') )
		    error.appendTo( element.parent().prev() );
		  else
				error.insertAfter(element);
		}
  });
}
//*****jQuery switch county map on rollover*****//
function countyImageMap() {
  if (!document.getElementById('selectCounties')) return false;
  //submit button rollover from http://www.appelsiini.net/2007/6/sequentially-preloading-images
  //preload the rollover images
  $(window).bind('load', function() {
    var preload = new Array();
    s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_nw.$1");
    preload.push(s);
    s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_ne.$1");
    preload.push(s);
    s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_sw.$1");
    preload.push(s);
    s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_se.$1");
    preload.push(s);
    var img = document.createElement('img');
    $(img).bind('load', function() {
      if(preload[0]) {
        this.src = preload.shift();
      }
    }).trigger('load');
  });
  
  $('area:first').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      // do something on mouseover
      s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_nw.$1");
      $('#countyMap').attr("src",s);
    } else {
      // do something on mouseout
      s = $('#countyMap').attr("src").replace('_nw', '');
      $('#countyMap').attr("src",s);
    }
  });
  $('area:eq(1)').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      // do something on mouseover
      s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_ne.$1");
      $('#countyMap').attr("src",s);
    } else {
      // do something on mouseout
      s = $('#countyMap').attr("src").replace('_ne', '');
      $('#countyMap').attr("src",s);
    }
  });
  $('area:eq(2)').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      // do something on mouseover
      s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_sw.$1");
      $('#countyMap').attr("src",s);
    } else {
      // do something on mouseout
      s = $('#countyMap').attr("src").replace('_sw', '');
      $('#countyMap').attr("src",s);
    }
  });
  $('area:last').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      // do something on mouseover
      s = $('#countyMap').attr("src").replace(/\.(.+)$/i, "_se.$1");
      $('#countyMap').attr("src",s);
    } else {
      // do something on mouseout
      s = $('#countyMap').attr("src").replace('_se', '');
      $('#countyMap').attr("src",s);
    }
  });
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  initializeFlash();
  clearDefaultValue(); 
  mask();
  validate();
  countyImageMap();
});
