var curRotatorText = 0;
var rotatorTextLeftPositions = new Array(0, -942, -1884);
var lengthRotatorText;
var rotatorInterval;

var curTickerText = 0;
var lengthTickerText;
var tickerInterval;

$(document).ready(function() {
	// setup top navigation
	if ($(".top_nav_dropdown").length) {
		$(".top_nav_dropdown").hide();
		var hideTimer;
		$(".top_nav_item").hover(function() {
			if (hideTimer != null) {
				clearTimeout(hideTimer);
			}
			$(".top_nav_dropdown").stop(true, true);
			$(".top_nav_item").not("#" + $(this).attr("id")).find(".top_nav_dropdown").hide();
			$(".top_nav_item").not("#" + $(this).attr("id")).find("a").removeClass("current_over");
			$(this).find("a").addClass("current_over");
			$(this).find(".top_nav_dropdown").slideDown("fast");
		}, function() {
			hideTimer = setTimeout(
				"$('#" + $(this).attr("id") + "').find('a').removeClass('current_over');" +
				"$('.top_nav_dropdown').stop(true, true);" +
				"$('.top_nav_dropdown').hide();"
			, 700);
		});
	}

	// setup search rollover
	if ($(".searchsubmit").length) {
		$(".searchsubmit").hover(function() {
			$(this).addClass("searchsubmit_over");
		}, function() {
			$(this).removeClass("searchsubmit_over");
		});
	}

	// setup expandable text blocks
	if ($(".textBlockExpandButton").length > 0) {
		$(".textBlockExpandButton").click(function() {
			if ($(this).parent().find(".textBlockExpansion").css("display") == "block") {
				$(this).parent().find(".textBlockExpandButton").addClass("btnOpen");
				$(this).parent().find(".textBlockExpandButton").removeClass("btnClose");
			} else {
				$(this).parent().find(".textBlockExpandButton").addClass("btnClose");
				$(this).parent().find(".textBlockExpandButton").removeClass("btnOpen");
			}
			$(this).parent().find(".textBlockExpansion").slideToggle("fast", null);
		});
		$(".textBlockExpandButton").hover(function() {
			$(this).addClass("btnOver");
		}, function() {
			$(this).removeClass("btnOver");
		});
	}

	// setup homepage rotator
	// previous button
	if ($("#rotator_prev").length) {
		// we know we're on the homepage, so some basic setup:
		// show that we are on the first item
		$(".rotator_numbers a:first").addClass("selected");
		// setup the timer
		rotatorInterval = setInterval("nextRotatorItem(777);", 15000);

		$("#rotator_prev").click(function() {
			clearInterval(rotatorInterval);
			prevRotatorItem(777);
			rotatorInterval = setInterval("nextRotatorItem(777);", 15000);
		});
		// setup rollover img
		$("#rotator_prev").hover(function() {
			// over
			$(this).attr("src", "sites/Proventys/Resources/images/left_arrow_over.gif");
		}, function() {
			// off
			$(this).attr("src", "sites/Proventys/Resources/images/left_arrow.gif");
		});
	}
	// next button
	if ($("#rotator_next").length) {
		$("#rotator_next").click(function() {
			clearInterval(rotatorInterval);
			nextRotatorItem(777);
			rotatorInterval = setInterval("nextRotatorItem(777);", 15000);
		});
		// setup rollover img
		$("#rotator_next").hover(function() {
			// over
			$(this).attr("src", "sites/Proventys/Resources/images/right_arrow_over.gif");
		}, function() {
			// off
			$(this).attr("src", "sites/Proventys/Resources/images/right_arrow.gif");
		});
	}
	// links
	if ($(".rotator_numbers a").length) {
		$(".rotator_numbers a").each(function(i) {
			$(this).click(function() {
				clearInterval(rotatorInterval);
				setRotatorItem(i);
				rotatorInterval = setInterval("nextRotatorItem(777);", 15000);
			});
		});
	}

	// setup homepage ticker
	lengthTickerText = $(".ticker_item").length;
	if (lengthTickerText > 0) {
		$(".ticker_item").hide();
		$(".ticker_item:first").show();
		tickerInterval = setInterval("nextTickerItem();", 7000);
		if ($("#btnPrevTickerItem").length) {
			$("#btnPrevTickerItem").click(function() {
				clearInterval(tickerInterval);
				prevTickerItem();
				tickerInterval = setInterval("nextTickerItem();", 7000);
				return false;
			});
			$("#btnPrevTickerItem").hover(function() {
				// over
				$(this).attr("src", "sites/Proventys/Resources/images/Previous_btn_over.gif");
			}, function() {
				// off
				$(this).attr("src", "sites/Proventys/Resources/images/previous_btn.gif");
			});
		}
		if ($("#btnPauseTicker").length) {
			$("#btnPauseTicker").click(function() {
				togglePauseTicker();
				return false;
			});
			$("#btnPauseTicker").hover(function() {
				// over
				$(this).attr("src", "sites/Proventys/Resources/images/Pause_btn_over.gif");
			}, function() {
				// off
				if (!paused) {
					$(this).attr("src", "sites/Proventys/Resources/images/pause_btn.gif");
				}
			});
		}
		if ($("#btnNextTickerItem").length) {
			$("#btnNextTickerItem").click(function() {
				clearInterval(tickerInterval);
				nextTickerItem();
				tickerInterval = setInterval("nextTickerItem();", 7000);
				return false;
			});
			$("#btnNextTickerItem").hover(function() {
				// over
				$(this).attr("src", "sites/Proventys/Resources/images/Next_btn_over.gif");
			}, function() {
				// off
				$(this).attr("src", "sites/Proventys/Resources/images/next_btn.gif");
			});
		}
	}
});

function nextRotatorItem(spd){
	$(".rotator_numbers a:eq(" + curRotatorText + ")").removeClass("selected");
	curRotatorText = ++curRotatorText % rotatorTextLeftPositions.length;
	$("#homepage_rotator_inner").animate({ marginLeft: rotatorTextLeftPositions[curRotatorText] + "px" }, spd);
	$(".rotator_numbers a:eq(" + curRotatorText + ")").addClass("selected");
}
function prevRotatorItem(spd){
	$(".rotator_numbers a:eq(" + curRotatorText + ")").removeClass("selected");
	curRotatorText--;
	if (curRotatorText < 0) {
		curRotatorText = rotatorTextLeftPositions.length - 1;
	}
	$("#homepage_rotator_inner").animate({ marginLeft: rotatorTextLeftPositions[curRotatorText] + "px" }, spd);
	$(".rotator_numbers a:eq(" + curRotatorText + ")").addClass("selected");
}
function setRotatorItem(i){
	$(".rotator_numbers a:eq(" + curRotatorText + ")").removeClass("selected");
	curRotatorText = i;
	$("#homepage_rotator_inner").animate({ marginLeft: rotatorTextLeftPositions[curRotatorText] + "px" }, "normal");
	$(".rotator_numbers a:eq(" + curRotatorText + ")").addClass("selected");
}
var srcPauseButton = "sites/Proventys/Resources/images/pause_btn.gif";
var srcUnPauseButton = "sites/Proventys/Resources/images/pause_btn_over.gif";
var paused = false;
function nextTickerItem() {
	$("#btnPauseTicker").attr("src", srcPauseButton);
	paused = false;
	$(".ticker_item:eq(" + curTickerText + ")").fadeOut("fast", function() {
		curTickerText++;
		curTickerText = curTickerText % lengthTickerText;
		$(".ticker_item:eq(" + curTickerText + ")").fadeIn("fast", null);
	});
}
function prevTickerItem() {
	$("#btnPauseTicker").attr("src", srcPauseButton);
	paused = false;
	$(".ticker_item:eq(" + curTickerText + ")").fadeOut("fast", function() {
		curTickerText--;
		if (curTickerText < 0) curTickerText = lengthTickerText - 1;
		$(".ticker_item:eq(" + curTickerText + ")").fadeIn("fast", null);
	});
}
function togglePauseTicker() {
	if (!paused) {
		$("#btnPauseTicker").attr("src", srcUnPauseButton);
		clearInterval(tickerInterval);
		paused = true;
	} else {
		$("#btnPauseTicker").attr("src", srcPauseButton);
		nextTickerItem();
		paused = false;
	}
}