var numberSlides = $('#carousel-mask ul li').size(); 
var justACounter = 0;
var campaignWidgetBG = "1";

$(document).ready(function() {

	// ##### LANDING PAGE CAROUSEL
	// ----- create a carousel nav item for each image, show first
	$('#carousel-mask ul li').each(function(index){
		justACounter++;
		$('#carousel-nav ul').append('<li><a href="#" class="ir" rel="image' + justACounter + '"><img alt="Slide ' + justACounter +'" title="Slide ' + justACounter +'" src="images/carousel_navOff.gif" /></a></li>');
	});
	$('#carousel-nav ul li a.ir img').removeClass('nav-active');
	$('#carousel-nav ul li a.ir:first img').addClass('nav-active');
	$('#carousel-nav ul li a.ir:first img').attr("src","images/carousel_navOn.gif");
	
	// ----- hide all carousel images, show first
	$("#carousel-mask ul li").hide();
	$("#carousel-mask ul li:first").show();
	$('#carousel-mask ul li').removeClass('active');
	$('#carousel-mask ul li:first').addClass('active');

	// ----- start rotating carousel
	if (justACounter > 0){
		autostep = setInterval(function(){
			autoGoToPos();
		}, 10000);
	}
	
	// ----- hide current image, show next image
	function autoGoToPos(){
		if ($("#carousel-mask ul li:last").hasClass('active')) {	// ----- handle going from last item to first
		
			// ----- change navs
			$("#carousel-nav ul li a.ir img.nav-active").attr("src","images/carousel_navOff.gif");
			$("#carousel-nav ul li:first a.ir img").addClass('nav-tempActive');
			$("#carousel-nav ul li a.ir img.nav-active").removeClass('nav-active');
			$("#carousel-nav ul li a.ir img.nav-tempActive").attr("src","images/carousel_navOn.gif");
			$("#carousel-nav ul li a.ir img.nav-tempActive").addClass('nav-active');
			$("#carousel-nav ul li a.ir img.nav-tempActive").removeClass('nav-tempActive');
		
			// ----- change images
			$("#carousel-mask ul li.active").hide();
			$("#carousel-mask ul li:first").addClass('tempActive');
			$("#carousel-mask ul li.active").removeClass('active');
			$("#carousel-mask ul li.tempActive").show('slide');
			$("#carousel-mask ul li.tempActive").addClass('active');
			$("#carousel-mask ul li.tempActive").removeClass('tempActive');
			
		} else {												// ----- go to next item
			
			// ----- change navs
			$("#carousel-nav ul li a.ir img.nav-active").attr("src","images/carousel_navOff.gif");
			$("#carousel-nav ul li a.ir img.nav-active").parent('a').parent('li').next('li').children('.ir').children('img').addClass('nav-tempActive');
			$("#carousel-nav ul li a.ir img.nav-active").removeClass('nav-active');
			$("#carousel-nav ul li a.ir img.nav-tempActive").attr("src","images/carousel_navOn.gif");
			$("#carousel-nav ul li a.ir img.nav-tempActive").addClass('nav-active');
			$("#carousel-nav ul li a.ir img.nav-tempActive").removeClass('nav-tempActive');
			
			// ----- change images
			$("#carousel-mask ul li.active").hide();
			$("#carousel-mask ul li.active").next('li').addClass('tempActive');
			$("#carousel-mask ul li.active").removeClass('active');
			$("#carousel-mask ul li.tempActive").show('slide');
			$("#carousel-mask ul li.tempActive").addClass('active');
			$("#carousel-mask ul li.tempActive").removeClass('tempActive');
			
		}
	}
	
	
	// ----- change carousel navs on hover
	$("#carousel-nav ul li a.ir img").hover(function(){
		$(this).attr("src","images/carousel_navOn.gif");
	});
	
	
	// ----- change carousel nav back on mouseout (unless it's active)
	$("#carousel-nav ul li a.ir img").mouseout(function(){
		if ($(this).hasClass('nav-active')) {
		} else {
			$(this).attr("src","images/carousel_navOff.gif");
		}
	});
	
	
	// ----- change image on click; stop carousel automation
	$("#carousel-nav ul li a.ir").click(function(){
		var connectedImage = $(this).attr('rel');
		$("#carousel-nav ul li a.ir img").attr("src","images/carousel_navOff.gif");
		$("#carousel-nav ul li a.ir img").removeClass("nav-active");
		$(this).children('img').addClass("nav-active");
		$(this).children('img').attr("src","images/carousel_navOn.gif");
		$("#carousel-mask ul li").hide();
		$('#carousel-mask ul li#' + connectedImage).show('slide');
		
		// ----- stop the automation if you want
		clearInterval(autostep);
		
		return false;
	});
	
	
	
	
	
	
	
	// ##### SHOW/HIDE FAQ
	// ----- r4ny show/hide
	$(".accordian_child").hide();
	$(".accordion_headings").click(function(){
		var thisRel = $(this).attr('rel');
		$(".accordian_child").hide();
		$("#" + thisRel).toggle("fast");
	});
	
	
	
	
	
	
	
	// ##### CAMPAIGN WIDGET
	// ----- close everything
	$("#listOfAds li").hide();
	$("#item-tv2").show();
	// ----- change background of buttons when hovered over
	$("#campaignWidget-buttons div a").hover(function(){
		var thisRel = $(this).attr('rel');
		thisRel = "url(/images/campaignWidget-header-" + thisRel + "on.jpg)";
		$("#campaignWidget-header").css("background-image", thisRel);
	});
	
	// ----- change background of buttons to default when mouse out
	$("#campaignWidget-buttons div a").mouseout(function(){
		var thisRel = "url(/images/campaignWidget-header-" + campaignWidgetBG + "on.jpg)";
		$("#campaignWidget-header").css("background-image", thisRel);
	});
	
	// ----- change background of buttons, change carousel at bottom when clicked
	$("#campaignWidget-buttons div a").click(function(){
		var thisRel = $(this).attr('rel');
		campaignWidgetBG = thisRel;
		thisRel = "url(/images/campaignWidget-header-" + thisRel + "on.jpg)";
		$("#campaignWidget-header").css("background-image", thisRel);
		
		$("#campaignWidget-bottom-online").hide();
		$("#campaignWidget-bottom-radio").hide();
		$("#campaignWidget-bottom-print").hide();
		$(".campaignWidget-thumbnail").removeClass('active');
		if (campaignWidgetBG==1) {
			$("#campaignWidget-bottom-online").show();
			$("#campaignWidget-bottom-online a").first().addClass('active');
			$("#campaignWidget-bottom-online a").first().click();
		}
		if (campaignWidgetBG==2) {
			$("#campaignWidget-bottom-radio").show();
			$("#campaignWidget-bottom-radio a").first().addClass('active');
			$("#campaignWidget-bottom-radio a").first().click();
		}
		if (campaignWidgetBG==3) {
			$("#campaignWidget-bottom-print").show();
			$("#campaignWidget-bottom-print a").first().addClass('active');
			$("#campaignWidget-bottom-print a").first().click();
		}
		
		return false;
	});
	
	// ----- change main image when thumbnail is clicked
	$(".campaignWidget-thumbnail").click(function(){
		var thisRel = $(this).attr('rel');
		thisRel = "#" + thisRel;
		$("#listOfAds li").hide();
		$(thisRel).show();
		$(".campaignWidget-thumbnail").removeClass('active');
		$(this).addClass('active');
		/*var thisTitle = $(this).attr('title')
		thisRel = "/images/" + thisRel + ".jpg";
		$("#campaignWidget-image").attr("src", thisRel);
		$("#campaignWidget-image").attr("alt", thisTitle);
		$("#campaignWidget-image").attr("title", thisTitle);
		$(".campaignWidget-thumbnail").removeClass('active');
		$(this).addClass('active');*/
		return false;
	});
	
	// ----- change previous
	$("#campaignWidget-body #prevButton").click(function(){
		$("#campaignWidget-bottom div a.active").prev("#campaignWidget-bottom div a").click();
		return false;
	});
	
	// ----- change next
	$("#campaignWidget-body #nextButton").click(function(){
		$("#campaignWidget-bottom div a.active").next("#campaignWidget-bottom div a").click();
		return false;
	});
	
	
	
	
	
	
	// ##### EMAIL SUBMIT
	// ----- remove "email" from email submit on click
	$("#emailSignup").click(function(){
		var textHere = $(this).val();
		if (textHere == "Email") {
			$(this).val("");	
		}
	});
	
	// ----- return "email" to email submit when mouseouted
	$("#emailSignup").blur(function(){
		var textHere = $(this).val();
		if (textHere == "") {
			$(this).val("Email");	
		}
	});
	
	// ----- when submit arrow is clicked
	$("#emailSubmit").click(function(){
		var okToSubmit=1;
		var errorMessage="";
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var textHere = $("#emailSignup").val();
		
		if (textHere == "") {
			errorMessage = "Please enter in your email address";
			okToSubmit=0;
		}
		if (!emailReg.test(textHere)) {
			errorMessage = "Please enter in a valid email address";
			okToSubmit=0;
		}
		if (okToSubmit == 1) {		// ----- successful submit
			window.location = "http://www.safesecurevital.com/emailRight.php?ezx=" + textHere;
		} else {					// ----- unsuccessful submit
			alert(errorMessage);
		}
		
		return false;
	});
	
	
	
	
	// ##### PETITION SUBMIT
	$("#petitionSubmit").click(function(){
		var okToSubmit=1;
		var errorMessage="";
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var textFirst = $("#firstName").val();	// ----- first name
		if (textFirst == "") {
			errorMessage = "first name";
			okToSubmit=0;
		}
		var textLast = $("#lastName").val();	// ----- last name
		if (textLast == "") {
			if (errorMessage!="") {
				errorMessage = errorMessage + ", ";
			}
			errorMessage = errorMessage + "last name";
			okToSubmit=0;
		}
		var textEmail = $("#emailAddress").val();	// ----- email address
		if (textEmail == "") {
			if (errorMessage!="") {
				errorMessage = errorMessage + ", ";
			}
			errorMessage = errorMessage + "email address";
			okToSubmit=0;
		}
		var textStreet = $("#streetAddress").val();	// ----- street address
		if (textStreet == "") {
			if (errorMessage!="") {
				errorMessage = errorMessage + ", ";
			}
			errorMessage = errorMessage + "address";
			okToSubmit=0;
		}
		var textCity = $("#cityAddress").val();	// ----- city
		if (textCity == "") {
			if (errorMessage!="") {
				errorMessage = errorMessage + ", ";
			}
			errorMessage = errorMessage + "city";
			okToSubmit=0;
		}
		var textState = $("#stateAddress").val();	// ----- state
		if (textState == "") {
			if (errorMessage!="") {
				errorMessage = errorMessage + ", ";
			}
			errorMessage = errorMessage + "state";
			okToSubmit=0;
		}
		var textZip = $("#zipAddress").val();	// ----- zip
		if (textZip == "") {
			if (errorMessage!="") {
				errorMessage = errorMessage + ", ";
			}
			errorMessage = errorMessage + "zip code";
			okToSubmit=0;
		}
		
		if (okToSubmit==1) {
			if (!emailReg.test(textEmail)) {
				errorMessage = "Please enter in a valid email address";
				okToSubmit=0;
			}
		}
		
		if (okToSubmit == 1) {		// ----- successful submit
			var textRadio = $("input[name=displayName]:checked").val();
			window.location = "http://www.safesecurevital.com/petitionRight.php?f=" + textFirst + "&l=" + textLast + "&e=" + textEmail + "&a=" + textStreet + "&c=" + textCity + "&s=" + textState + "&z=" + textZip + "&d=" + textRadio;
		} else {					// ----- unsuccessful submit
			if (errorMessage != "Please enter in a valid email address") {
				errorMessage = "Please enter in your " + errorMessage;
			}
			alert(errorMessage);
		}
		
		return false;
	});

});
