/* 
Author: Mitch Gohman with much love to JQuery. The Cross Browser Equalizer.
Date: 2010-11-28
*/
var TCFA = {};

/*Home*/
TCFA.splashCurrent = 1;
TCFA.currSlide = 3; //the 4th element of our looped array. We show the first 4. Reduce server load.
TCFA.totalSlides; //when we loop we will count the number of slides.
TCFA.slides = Array();

TCFA.splash_slide = function() {
	TCFA.totalSlides = TCFA.slides.length;
	
	if (TCFA.currSlide == (TCFA.totalSlides-1))
			{
			TCFA.currSlide = 0;
			}
			else
			{
			TCFA.currSlide++;
			}
	
	var loadNewImage = new Image();
	loadNewImage.src = TCFA.slides[TCFA.currSlide];
	//console.log(TCFA.currSlide);
	
	$(".slideshow img").css({'margin-top':'0px'});
	var topImage = $(".slideshow img:first");	
	
	
	$(topImage).animate({'margin-top':'-383px'},1000,
	function() { 
		$(topImage).attr('src',loadNewImage.src);
		$(topImage).appendTo(".slideshow a");
	
		
		setTimeout(function() { TCFA.splash_slide(); },4000);
	});
		
	
}
TCFA.setUpVizNav = function() {
	var aVizNavImages = $("#vizNav img");
	
	for(i=0; i < aVizNavImages.length; i++)
		{
		var getHover = $(aVizNavImages[i]).attr("src");	
		getHover = getHover.substring(0, getHover.indexOf('.jpg'));
		getHover = getHover + '_hov.jpg';
			
		$(aVizNavImages[i]).parent().css("background-image","url(" + getHover + ")");
		
		


		
		
		$(aVizNavImages[i]).hover(
								function fadeToSwap() { $(this).stop().fadeTo(500,0);},
								function fadeToOriginal() { $(this).stop().fadeTo(1000,1);}
								);	
		}
	
}


TCFA.vertNavSocial_show = function() {
	curScroll = $(window).scrollTop();

    if(curScroll > 100)
		{
   		$('#mainmenu ol#vertical').stop().fadeTo(250,1);
		}
		else
		{
		$('#mainmenu ol#vertical').stop().fadeTo(500, 0);	
		}

}

$(document).ready(function() { 
	$('#mainmenu ol#vertical').css('opacity',0); 
	
	$(window).scroll(TCFA.vertNavSocial_show);
	
});



TCFA.showSchedule = function() {
	$(".theLight, #theDark").show();
	
	$('html, body').animate({
		scrollTop: $("body").offset().top
		}, 1000);
		
}
TCFA.hideSchedule = function() {
	$(".theLight, #theDark").hide();	
}


TCFA.email_show = function (event) {
	$("#theLight h2").html('Send Us An Email');
	$("#theLight .content").html('');
	
	var duplicateForm = $(".emailUs").clone();
	$(duplicateForm).appendTo("#theLight .content");
	
	$("#theLight, #theDark").css({ display:'block' });
	
	$("#theLight .content .emailUs input.sendMessage").click(TCFA.email_process_validate);
	
	event.preventDefault();
	
}


TCFA.email_close = function () { $("#theLight, #theDark").css({ display:'none' }); }

TCFA.email_process_validate = function(event) {
	//check the fields
	var reqFields = $("#theLight .emailUs .required");
	var error = false;
	var feedback = '';
	var validEmail = false;
	var missingField = false;
	
	for (var i = 0; i < reqFields.length; i++)
		{
		var value = $(reqFields[i]).attr('value');
		if (value.length < 1)	
			{
			$(reqFields[i]).addClass('missing');
			missingField = true;
			}
			else
			{
			$(reqFields[i]).removeClass('missing');
			//see if its a valid email
			emailValue = $("#theLight .emailUs .email").attr('value');
			validEmail = TCFA.isValidEmail(emailValue);
			
			if (!validEmail)
				{
				$(reqFields[i]).addClass('missing');
				missingField = true;
				}
				
			}
		}
	
	
	if (!validEmail)
		{
		feedback += 'The email you provided does not appear to be valid. ';
		error = true;
		$("#theLight .emailUs .email").addClass('missing');
		}
	
	if (missingField)
		{
		feedback += 'You forgot to fill out one of the required fields.';
		error = true;
		}
	
	//do not send form
	if (error)
		{
		event.preventDefault();
		alert(feedback + 'Please check the form and try again.');
		}
		else
		{
		event.preventDefault();
		TCFA.email_process();	
		}
}


TCFA.isValidEmail = function(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

$(document).ready(function() {
	//$('.schedule').click(TCFA.showSchedule);
	$('.close, .theLight').click(TCFA.hideSchedule);
	
	$('.theLight .content').click(function(event){ event.stopPropagation(); });
	
	
	
});

/*subscribe animation*/
$(document).ready(function() {
	setTimeout(subscr_intro,1000); 
});

function subscr_intro() {
	$('#mainmenu .menu4 a').animate({'top':'0px'},1000, function() {
		setTimeout(subsc_up,1000); 
	});
}

function subsc_up() {
	$('#mainmenu .menu4 a').animate({backgroundPosition: '0px -14px'},500, function () {
		setTimeout(subsc_down,2000);
	}); 
}

function subsc_down() {
	$('#mainmenu .menu4 a').animate({backgroundPosition: '0px 0px'},500, function () {
		setTimeout(subsc_up,2000);
	}); 
}


