//****Home Slider Controls****/
$.slider = {}
$.slider.stopped = "false";
$.slider.wait = "false"
function homeSlider(){
	$('#slider').mouseover(function() {                  
		$(".slide").stop(true, true);
		$.slider.stopped = "true";
	});
	$('#slider').mouseleave(function() {       
		if($.slider.wait == "false"){
			$.slider.stopped = "false";
		}
	});
	
	$('.slide:eq(0)').addClass('active').show();
	$('#slide-nav li a:eq(1)').addClass('active');
	
	$('#slide-nav li a').not("li.start a").not("li.end a").click(function() {
	  var temp = $("#slide-nav li a").index($(this));
		temp = temp - 1;
		$('#slide-nav li a').removeClass('active');
		$(this).addClass('active');
		$('.slide').hide().removeClass('active');
		$('.slide:eq(' + temp + ')').fadeIn('slow').addClass('active');
		return false;
  });
	
	var slider_size = $(".slide").length - 1;
	
	$('#slide-nav li.end a').click(function() {						//right arrow click
		var temp = $(".slide").index($("#slider div.active"));
		var next = temp+1;
		temp = next+1;
		if(next <= slider_size){
			$('.slide').removeClass('active').hide();
			$('.slide:eq(' + next + ')').addClass('active').fadeIn('slow');
			$('#slide-nav li a').removeClass('active');
			$('#slide-nav li a:eq(' + temp + ')').addClass('active');
		}
		if(next > slider_size){
			$('.slide').removeClass('active').hide();
			$('.slide:eq(0)').addClass('active').fadeIn('slow');
			$('#slide-nav li a').removeClass('active');
			$('#slide-nav li a:eq(1)').addClass('active');
		}
		return false;
	});			
	$('#slide-nav li.start a').click(function() {						//left arrow click
		var temp = $(".slide").index($("#slider div.active"));
		var prev = temp-1;
		var temp2 = slider_size+1;
		if(prev >= 0){
			$('.slide').removeClass('active').hide();
			$('.slide:eq(' + prev + ')').addClass('active').fadeIn('slow');
			$('#slide-nav li a').removeClass('active');
			$('#slide-nav li a:eq(' + temp + ')').addClass('active');
		}
		if(prev < 0){
			$('.slide').removeClass('active').hide();
			$('.slide:eq(' + slider_size + ')').addClass('active').fadeIn('slow');
			$('#slide-nav li a').removeClass('active');
			$('#slide-nav li a:eq(' + temp2 + ')').addClass('active');
		}
		return false;
	});
	if(slider_size >= 1){
		setTimeout('rotate();', 9000);
	}
}
function rotate() {	
	if($.slider.stopped == "false"){
		$.slider.wait = "false";
		var slider_size = $(".slide").length-1;
		var temp = $(".slide").index($("#slider div.active"));
		var next = temp+1;
		temp = next+1;
		
		if(next <= slider_size){
			$('.slide').removeClass('active').hide();
			$('.slide:eq(' + next + ')').addClass('active').fadeIn('slow');
			$('#slide-nav li a').removeClass('active');
			$('#slide-nav li a:eq(' + temp + ')').addClass('active');
		}
		if(next > slider_size){
			$('.slide').removeClass('active').hide();
			$('.slide:eq(0)').addClass('active').fadeIn('slow');
			$('#slide-nav li a').removeClass('active');
			$('#slide-nav li a:eq(1)').addClass('active');
		}
	}
	setTimeout('rotate();', 9000);
}
//*****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() {
  if ($('a[rel^="prettyPhoto"]').length < 1) return false;
  $("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 */
  });
}
//*****add google page tracking code on the fly*****//
(function(a){a(document).ready(function(){var b=window.location.hostname;b=b.split(".");if(b.length==1){b=b[0]}else{if(b.length==4&&window.location.hostname.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)){b=window.location.hostname}else{b=b[b.length-2]+"."+b[b.length-1]}}a("a[href^='http']:not([href*='"+b+"'])").live("click",function(f){f.preventDefault();var c=this.href.replace(/^(http|https):\/\//,"").replace(/www\./i,"").replace(/\./gi,"_");var d="/outgoing/"+c;if(typeof(pageTracker)!="undefined"){pageTracker._trackPageview(d)}else{if(window.console){window.console.log("Could not track external link: "+d)}}window.open(a(this).attr("href"))})})})(jQuery);
//*****Load all functions**************************************************//
$(document).ready(function(){
  replaceAlign();
  replaceTarget();
	//strictNewWindow();
  //initializeFlash();
  toggleEneryUpdate();
  extendFooter();
  mask();
  validate();
  clearDefaultValue();
  verify();
  prettyPhoto();
	homeSlider();
});