$(document).ready(function(){

	var inputs = $("input[type='text'],textarea");
	$.each(inputs,function(i,e) {
		var placeholder = $(e).attr("placeholder");
			$(e).val(placeholder);	
			$(e).blur(function() {
				resetInput($(this));
			});
			$(e).focus(function() {
				if($(this).val() == placeholder) {
					$(this).val("");
				}
			});
		});

	function resetInput(elem) {
		if($(elem).val() == "") {
				var placeholder = $(elem).attr("placeholder");
				$(elem).val(placeholder);
			}
	}

	if($("#callouts").length){
		$("body").addClass("homePage");
	}
	
	if($("#gallery").length){
		$('#gallery a').lightBox();
	}
	
	var windowURL = document.URL.replace(/^.*[\\\/]/,'');
	$.each($("#nav>ul>li"), function(i){		
		$(this).addClass("navItem" + i).attr("pos",i);
	});
	$("#nav").attr("class","back-"+$("#nav a[href='"+windowURL+"']").parents("li:last").attr("pos"));
	
	prepServicesCallouts();
	
	//Testimonials Stuff
	var testimonials = $(".testimonial"),
		testNumber = testimonials.length,
		testExpand = $(".expand"),
		testClose = $(".testClose"),
		testMain = $("#testimonials"),
		expandableContent = $(".expandContent"),
		testSide = $("#testimonialSideBar"),
		testNav = $("#testimonialsNav"),
		testScroller = $("#testScroller"),
		overlay = $(".testimonialOverlay");

	$.each(testimonials, function(i){
		testNav.append("<span>" + (i+1) + "</span");
	});
	
	
	var testNavItems = testNav.children("span");
	
	testNavItems.first().addClass("testActive");
	$(testimonials).first().addClass("testActive");
	
	$(testNavItems).click(function(){
		if(!$(this).hasClass("testActive")){
			testScroller.css("margin-top", "0px");
			$(".testActive").removeClass("testActive");
			$(this).addClass("testActive");
			$('.testimonial:nth-child(' + ($(this).index()+1) + ')').addClass("testActive");
		}
	});
	
	testExpand.click(function(){
		testMain.addClass("background");
		testExpand.hide();
		testNav.addClass("expanded", 1000);
		overlay.fadeIn(500);
		testMain.addClass("expanded", 1000, function(){
			expandableContent.slideDown();
			testSide.show();
		});
	});
	
	testClose.click(function(){
		expandableContent.hide();
		testSide.hide();
		testScroller.css("margin-top", "0px");
		overlay.fadeOut(500);
		testMain.removeClass("expanded", 1000, function(){
			testExpand.show();
			testMain.removeClass("background");
		});
		testNav.removeClass("expanded", 1000);
	});
	overlay.bind("click",function() {
		testClose.trigger("click");
	});
	$(".testDown").mouseover(scrollDown);
	$(".testUp").mouseover(scrollUp);

});
function prepServicesCallouts() {
	var callouts	=	$("#callouts > div");
	$.each(callouts,function(i,e) {
		var finalHeight	=	$(e).find(".servicesCalloutOverflow > div").height();
		$(e).data("finalHeight",finalHeight);
		$(e)
			.mouseenter(function(){
				var fh	=	$(this).data("finalHeight");
				$(this).find(".servicesCalloutOverflow").stop().animate({
					height: fh
				});
			})
			.mouseleave(function(){
				var fh	=	$(this).data("finalHeight");
				$(this).find(".servicesCalloutOverflow").stop().animate({
					height: "54px"
				});
			});
	});
}

function scrollDown(){
	var trigger = true,
		activeHeight = $(".testimonial.testActive").height();
		testScroller = $("#testScroller"),
		windowHeight = $("#testimonials").height(),
		currentPosition = testScroller.css("margin-top");
		
	currentPosition = parseFloat(currentPosition.replace("px", ""));
	maxScroll = windowHeight - activeHeight - 30;
		
	if(currentPosition >= maxScroll){
		testScroller.animate({
			marginTop: "-=10px"
		}, 50, function(){
			if(trigger){
				scrollDown();
			}
		});
		$(".testDown").mouseleave(function(){
			trigger = false;
		});
	}
}

function scrollUp(){

	var testScroller = $("#testScroller");

	if(testScroller.css("margin-top") != "0px"){
		var trigger = true;
		testScroller.animate({
			marginTop: "+=10px"
		}, 50, function(){
			if(trigger){
				scrollUp();
			}
		});
		$(".testUp").mouseleave(function(){
			trigger = false;
		});
	}
}

