var timer;
var width = 960;
var num = new Number();
var car_links;

// this variable is set in header.php
var url;

$(function() {
	//Activate Carousel
	if($("#carousel").length != 0) {
		carousel_init();
	}
	
	//Sliding Links on Homepage
	if($("#learnmore").length != 0) {
		$("#learnmore a").bind("mouseenter",function(){
			$(this).children("span").animate({
		   	 	paddingBottom: 45
		  		}, {
		    	duration: 100
		    	} 
			);
		});
		$("#learnmore a").bind("mouseleave",function(){
			$(this).children("span").animate({
		   	 	paddingBottom: 40
		  		}, {
		    	duration: 100
		    	} 
			);
		});
	}
	
	//Sliding Bios
	if($("#bios").length != 0) {
		bios_init();
	}
	
	$('#join').click(email_form);
});

function carousel_init() {
	car_links = $('#car_nav a');
	$("#car_nav a").bind("click",goToItem);
	timer = setInterval(nextItem,8000);
}

function goToItem() {
	clearInterval(timer);
	timer = setInterval(nextItem,8000);
	num = $(this).attr("title") - 1;
		
	scroller(num);
	
	$('#car_nav a').removeClass("on");
	$(this).addClass("on");
	
	return false;
}

function nextItem() {
	var count = $('#car_nav a').length - 1;
	if(num == count) {
		num = 0;
	}
	else {
		num++;
	}
	scroller(num);
	car_links.each(function() {
		$(this).removeClass("on");
	});
	$(car_links[num]).addClass("on");
}

function scroller(num) {
	var margin = -(num * width);

	$('#car_holder').animate({
   	 	marginLeft: margin
  		}, {
    	duration: 600
    	} 
	);
}

function bios_init() {
	$("#bios").addClass("js");
	var open_link = $("a.read,.bio h5");
	$(".bio h5").each(function(){
		var bio = $(this).parent(".bio");
		if(!bio.hasClass("open")) {
		bio.height($(this).outerHeight(true));
		}
	});
	open_link.bind("click",function() {
		var bio = $(this).parent(".bio");
		var h_height = bio.children("h5").outerHeight(true);
		if(bio.hasClass("open")) {
			bio.animate({
		   	 	height: h_height
		  		}, {
		    	duration: 400
		    	} 
			);
			bio.removeClass("open");
			bio.children(".read").html("read bio");
		}
		else {
			bio.css("height","auto");
			var height = bio.height();
			bio.css("height",h_height);
			bio.animate({
		   	 	height: height
		  		}, {
		    	duration: 400
		    	} 
			);
			bio.addClass("open");
			bio.children(".read").html("close bio");
		}
		
		return false;
	});
}
function email_form(el) {
	el.preventDefault();
	
	
	
  var email = $("input#email").val();
  if(validate(email) == false) {
		return false;
	}	
  var dataString = 'email=' + email;
  //alert (dataString);return false;
  $.ajax({
    type: "POST",
    url: url+"/process.php",
    data: dataString,
    success: function() {
      $('#mailing_list').html("<div id='message'></div>");
      $('#message').html("<p>Thanks, you will receive our newsletter shortly.</p>");
    }
  });
  return false;
}

function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      alert('Invalid Email Address');
      return false;
   }
}
  
