﻿var contentSlider = null;
var startYPos = null;
var panelHeight = 180;
var panelCount = null;
var autoChange = null;
var resetChange = null;

Event.observe(window, 'load', function() {
	contentSlider = $("news_container");

	// Idaho Map
	$("idahomap").hide();
	so3.write("idahomap");

	Event.observe("anc_idaho", "click", function() {
		toggleMap();
	});

	// Event News Controls
	Event.observe("btnUp", "click", function() {
		slidePanel(1);
		clearInterval(autoChange);

		clearTimeout(resetChange);
		initResetViewport();

		return false;
	});

	Event.observe("btnDown", "click", function() {
		slidePanel(-1);
		clearInterval(autoChange);

		clearTimeout(resetChange);
		initResetViewport();

		return false;
	});

	// Set the number of panels
	panelCount = $$(".news_reader_img").length;

	if (panelCount <= 1) {
		$("scroll_control").hide();
	}

	startYPos = getPanelYPos();
	callDebug();

	// Start the auto fade elements
	initViewport();
});

function initViewport() {
	autoChange = setInterval(changeViewport, 9000);
}

function initResetViewport() {
	resetChange = setTimeout("initViewport()", 30000);
}

function changeViewport() {
	slidePanel(-1)
}

function toggleMap() {
	$("idahomap").toggle();
}

function slidePanel(direction) {
	var currentPos = getPanelYPos();
	var movePixels = panelHeight;
	var absPosition = startYPos - (currentPos - panelHeight)

	if ((currentPos % 180 == 0) || (currentPos == 0)) {
		// If the up button has been pressed
		if (direction == 1 && (currentPos + panelHeight < startYPos)) {
			movePanel(movePixels);
		}
		else if (direction == 1) {
			movePanel(-(panelHeight * (panelCount - 1)));
		}

		// If the down button has been pressed
		if (direction == -1 && (absPosition < panelHeight * panelCount)) {
			movePixels = -movePixels;
			movePanel(movePixels);
		}
		else if (direction == -1) {
			movePanel(panelHeight * (panelCount - 1));
		}
	}
}

function movePanel(direction) {
	var fadeDuration = .75;

	new Effect.Move(contentSlider, {
		duration: 0,
		x: 0,
		y: direction,
		mode: 'relative',
		afterFinish: function() {
			callDebug();
		}
	});

	/*
	new Effect.Fade(contentSlider, {
		duration: fadeDuration,
		afterFinish: function() {
			new Effect.Move(contentSlider, {
				duration: fadeDuration,
				x: 0,
				y: direction,
				mode: 'relative',
				afterFinish: function() {
					new Effect.Appear(contentSlider, {
						duration: fadeDuration
					});
					callDebug();
				}
			});
		}
	});
	*/
}

function getPanelYPos() {
	var offset = new String(contentSlider.positionedOffset());
	var beginPos = offset.indexOf(",") + 1;
	var y = offset.substring(beginPos, offset.length);

	return y;
}

function callDebug() {
	$("debug").innerHTML = "Start Y: " + startYPos + "<br />" + "Current Y: " + getPanelYPos();
}