var curHashVal = window.location.hash; //global reference
var timer;
var windowLength=7.5;		//Length in seconds each window appears

largeImages={
	image: ["product.jpg","new-approach.jpg"],
	links: ["learning-center-bv.php","learning-center.php","products-home-test.php"]
};
smallImages={
	images: ["yeast-infection.jpg","yeast-infection-pink.jpg","products.jpg"],
	links: ["learning-center-bv.php","learning-center.php","products.php"]
};

windowLength=windowLength * 1000;

$(document).ready(function() {
	$("#referenceTable td[colspan!=4], #referenceTable th").hover(
		function() {
			$(this).parent("tr").addClass("tableHover");
			var index = $(this).parent().children().index(this); 
			$("#referenceTable td[colspan!=4]").filter(":nth-child(" + (index + 1) + ")").addClass("tableHover");
			$("#referenceTable th").filter(":nth-child(" + (index + 1) + ")").addClass("tableHover");
			if(index > 0) {
				$("#referenceTable td[colspan=3]").addClass("tableHover");
			}
		},
		function() {
			$(".tableHover").removeClass("tableHover");
		}
	);
	
	if($("#rotatingImage").length > 0) {
		oSrc=$(".rotatingHeader").attr("src").split("/")[2];
		curIndex=$.inArray(oSrc, largeImages.image);
		$("#rotatingImage a").attr("href", largeImages.links[curIndex]);
		curIndex++;
		highlightNumber(curIndex);
		timer=setInterval(function() { rotateImages(0) }, windowLength);
		$.each(largeImages.image, function(index, value) {
			$("<img />").attr("src", "images/headers/"+value).css("display", "none").appendTo("body");
		});
	}
	
	if($("#infoBar").length > 0) {
		oSrc=$("#infoBar img").attr("src").split("/")[2];
		timer=setInterval(rotateSmallImages, windowLength);
	}
	
	$("#numbers img").click(function() {
		window.clearInterval(timer);
		newIndex=$(this).attr("id").split("_")[1];
		rotateImages(newIndex);
		timer=setInterval(rotateImages, windowLength);
	});
	
	$(".bottomBoxes, .lcBox")
		.css("cursor","pointer")
		.click(function() {
			newUrl=$(this).find("a").attr("href");
			if(!newUrl.match(/http:/)) {
				window.location=newUrl;
			} else {
				window.open(newUrl, "vhPop");
			}
			return false;
		});
	
	$("#faqInstructions").css("display","inline");
	$(".answer").css("display","none");
	$(".question").click(function() {
		if($(this).next(".answer").is(":visible")) {
			$(this).next(".answer").slideUp();
			return;
		}
		
		$(".answer").slideUp();
		$(this).next(".answer").slideDown();
		clickedID=$(this).next(".answer").attr("id").split("_")[1]
		window.location.hash=clickedID;
		curHashVal = window.location.hash;
		
		if(typeof(pageTracker) != "undefined") {
			pageTracker._trackPageview("/faq.php/" + clickedID);
		}
	});
	if(window.location.hash != "") {
		selectedID=window.location.hash.replace("#", "");
		$("#answer_"+selectedID).slideDown();
	}
	setInterval(function(){
		if(curHashVal!=window.location.hash){ //changed
			selectedID=window.location.hash.replace("#", "");
			$(".answer").slideUp();
			$("#answer_"+selectedID).slideDown();
			curHashVal = window.location.hash;
		}
	}, 200);
	
	$(".defaultValue")
		.addClass("inactiveInput")
		.focus(function() {
			if($(this).val()==$(this)[0].defaultValue) {
				$(this).val("").removeClass("inactiveInput");
			}
		})
		.blur(function() {
			if($(this).val()=="") {
				$(this).val($(this)[0].defaultValue).addClass("inactiveInput");
			}
		});
		
	
	$("a[href^=http://]").click(function() {
		if(!$(this).attr("href").match(/vhessentials.com/)) {
			extUrl=$(this).attr("href").replace("http://", "");
			if(typeof(pageTracker)!="undefined") {
				pageTracker._trackPageview("/external-links/"+extUrl);
			}
		}
	});
	
	$("a[href$=pdf]").click(function() {
		if(typeof(pageTracker)!="undefined") {
			pageTracker._trackPageview($(this).attr("href"));
		}
	});
});

function rotateSmallImages() {
	oSrc=$("#infoBar img").attr("src").split("/")[2];
	curIndex=$.inArray(oSrc, smallImages.images);
	curIndex++;
	if(curIndex == smallImages.images.length) curIndex=0;
	newImage=$("<img />").attr({src: "images/headers-small/"+smallImages.images[curIndex], id: "fadingIn", width: "757"}).css("display","none");
	$("#infoBar a").append(newImage);
	$("#infoBar a").attr("href", smallImages.links[curIndex]);
	newImage.fadeIn();
	$("#infoBar img:not(#fadingIn)").fadeOut(function() {
		$(this).remove();
		$("#fadingIn").attr("id","");
	});
}

function rotateImages(newIndex) {
	oSrc=$(".rotatingHeader").attr("src").split("/")[2];
	if(newIndex === undefined || newIndex < 1) {
		curIndex=$.inArray(oSrc, largeImages.image);
		curIndex=curIndex+1;
		if(curIndex == largeImages.image.length) curIndex=0;
	} else {
		curIndex=newIndex - 1;
	}
	newSrc="images/headers/"+largeImages.image[curIndex];
	newImage=$("<img />").attr({"src": newSrc, "id": "fadingIn", "class": "rotatingHeader"}).css("display","none");
	$("#rotatingImage a").append(newImage);
	newImage.fadeIn();
	highlightNumber(curIndex + 1);
	$(".rotatingHeader:not(#fadingIn)").fadeOut(function() {
		$(this).remove();
		$("#fadingIn").attr("id","");
		$("#rotatingImage a").attr("href", largeImages.links[curIndex]);
	});
}

function resetNumbers() {
	$("#numbers img[src$=over.png]").each(function() {
		newSrc=$(this).attr("src").replace("-over", "");
		$(this).attr("src", newSrc);
	});
}

function highlightNumber(number) {
	resetNumbers();
	$("#numbers img[src$="+number+".png]").attr("src", "images/numbers/"+number+"-over.png");
}


function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
