var animateTimer, timerLength, stepLength;
var animationLength=1000;
var animating=false;

$(document).ready(function() {
	$("#faq_q_box a").click(function() {
		/*if(animating) {
			return false;
		}
		animating=true;*/
		
		$(".faq_question").removeClass("faq_question_active");
		$(this).parent().addClass("faq_question_active");
		
		questionID=$(this).attr("id").split("_");
		questionID=questionID[1];
		questionTop=this.offsetTop - 30;
	
		if ($.browser.msie && $.browser.version.substr(0,1)==7) {
			questionTop=questionTop+40;
		}
		
		/*oldBoxHeight=$("#faq_box").height();*/
		
		$.get("faqAjax.php", {questionID: questionID}, function(data) {
			$("#faq_a_box").html(data.answer);
			
			document.title=data.question + " - FAQ - Rite Hite - Revolution HV/LS Fans";
			
			newUrl="/faq.php?questionID=" + questionID + "&catID=" + data.catID
			if(typeof(console) != "undefined") {
				console.log("Tracking: " + newUrl);
			}
			pageTracker._trackPageview(newUrl);
			
			//Set a top margin to move the answer down to line up with the question
			$("#faq_a_box").css("marginTop", questionTop);
			
			//Pad the bottom so that we can scroll all the way to the answer without running out of room
			/*marginBtm=320-$("#faq_a_box").height();
			if(marginBtm > 0) {
				$("#faq_a_box").css("paddingBottom", marginBtm);
			} else {
				$("#faq_a_box").css("paddingBottom", "0px");
			}*/
			
			//Scroll the box to the question
			/*$("#faq_inner_box").animate({
				scrollTop: questionTop + 25
			}, 1000, function() {
				//Get the height of the answer box
				newBoxHeight=$("#faq_a_box").height() + 10;
				
				//Set min/max heights
				if(newBoxHeight < 330) {
					newBoxHeight=330;
					$("#faq_close_button").hide();
				} else if(newBoxHeight > 553) {
					newBoxHeight=553;
				}
				
				//If the height isn't the same as it already is, animate it
				if(newBoxHeight != oldBoxHeight) {
					animateBox(newBoxHeight, questionTop + 25);
				} else {
					animating=false;
				}
			});*/
		}, "json");
		return false;
	});
	$("#faq_close_button").click(function() {
		animating=true;
		animateBox(332, $("#faq_inner_box").scrollTop());
		$(this).hide();
	});
});

function findPos ( obj ) {
	var left = !!obj.offsetLeft ? obj.offsetLeft : 0,
		top = !!obj.offsetTop ? obj.offsetTop : 0;
	
	while( (obj = obj.offsetParent) ) {
		left += !!obj.offsetLeft ? obj.offsetLeft : 0;
		top += !!obj.offsetTop ? obj.offsetTop : 0;
	}
	
	return{ x:left, y:top };
}

function animateBox(targetHeight, oldScroll, direction) {
	oldHeight=$("#faq_box").height();
	
	if((oldHeight == targetHeight) || (direction=="smaller" && oldHeight < targetHeight) || (direction=="bigger" && oldHeight > targetHeight)) {
		clearTimeout(animateTimer);
		if(direction=="bigger") $("#faq_close_button").show();
		animating=false;
		return true;
	}
	
	if((direction=="smaller" && (oldHeight - stepLength) < targetHeight) || (direction=="bigger" && (oldHeight + stepLength) > targetHeight)) {
		$("#faq_box").height(targetHeight);
		$("#faq_inner_box").height(targetHeight - 15);
		clearTimeout(animateTimer);
		animating=false;
		return true;
	}
	
	if(typeof(direction)=="undefined") {
		if(oldHeight < targetHeight) {
			direction="bigger";
		} else {
			direction="smaller";
		}
		timerLength=(animationLength / 20);
		stepLength=Math.abs((targetHeight-oldHeight) / 20);
	} else {
		if(direction=="bigger") {
			oldHeight=oldHeight+stepLength;
		} else {
			oldHeight=oldHeight-stepLength;
		}
	}
	
	featureBoxHeight=oldHeight-15;
	$("#faq_inner_box").height(featureBoxHeight);
	$("#faq_inner_box").scrollTop(oldScroll);
	$("#faq_box").height(oldHeight);
	animateTimer=setTimeout("animateBox("+targetHeight+", "+oldScroll+", '"+direction+"')", timerLength);
}