//*****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/flash-content.swf", "flash-content", "605", "462", "8.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
}
//*****Toggle Green Energy Update**************************************************//
function toggleEneryUpdate() {
  $('#toggle').hide();
  $('#greenEnergyUpdate h4 a').click(function() {
    $('#greenEnergyUpdate #toggle').slideDown();
    return false;
  });
  $('#greenEnergyUpdate p#close').click(function() {
    $('#greenEnergyUpdate #toggle').slideUp();
  });
}
//*****Extend the footer to the bottom of the page**************************************************//
function extendFooter() {
  var footerHeight = $('#footerWrap').height();
  var pageHeight = $(document.body).height();
  var windowHeight = $(document).height();
  var difference = windowHeight - pageHeight;
  if (pageHeight < windowHeight) {
    $('#footerWrap').height(footerHeight + difference);
  } else {
    $('#footerWrap').height('185px');
  }
}
//*****Mask function from http://digitalbush.com/projects/masked-input-plugin**************************************************//
function mask() {
  if ($('#formContactUs').length < 1) return false;
  $('#txtPhone').mask('(999) 999-9999');
}
//*****jQuery form validation from http://bassistance.de/jquery-plugins/jquery-plugin-validation/**************************************************//
function validate() {
  if ($('#formContactUs').length < 1) return false;
  $('#formContactUs').validate({
    //add the error class to the label and element
    highlight: function(element, errorClass) {
		  if ($(element).is(':radio')) {
		    $(element).parent().prev().addClass(errorClass);
		  } else {
        $(element).addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
      }
    },
    //remove error class from label and element once valid
    unhighlight: function(element, errorClass) {
		  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);
      }
    },
    //add the green box and checkmark for valid fields
    success: function(label) {
		  if (label.prev().is(':checkbox')) {
        label.next().addClass("valid");
        label.addClass("valid").text("✓");
      } else {
        label.prev().addClass("valid");
        label.addClass("valid").text("✓");
      }
    },
    //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: {
      'HalfNunc[]': 'required'
    },
    //messages other than the default 'X'
    messages: {
		  'HalfNunc[]': '<br />X Please select one of the options below'
		},
    // 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 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'));
    }
  });
  $('#btnNext').click(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
function populateDefaultValues(){
  $(':input').each(function(){
    if($(this).val() == ''){
      $(this).val($(this).attr('title'));      
    }
  });
}
//*****misc verify functions*****//
function verify() {
  $('.formVerify li:odd').addClass('odd');
  $('.formVerify li:even').addClass('even');
}
//*****pretty photo call http://www.no-margin-for-errors.com/projects/prettyPhoto-jquery-lightbox-clone/*****//
function prettyPhoto() {
  $("a[rel^='prettyPhoto']").prettyPhoto({
  	animationSpeed: 'normal', /* fast/slow/normal */
  	padding: 40, /* padding for each side of the picture */
  	opacity: 0.35, /* Value betwee 0 and 1 */
  	showTitle: true, /* true/false */
  	allowresize: false, /* true/false */
  	counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
  	theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
  	hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
  	modal: false, /* If set to true, only the close button will close the window */
  	changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
  	callback: function(){} /* Called when prettyPhoto is closed */
  });
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  initializeFlash();
  toggleEneryUpdate();
  extendFooter();
  mask();
  validate();
  clearDefaultValue();
  verify();
  prettyPhoto();
});