/* 
 * elementpusher.js
 * Script to move an element left or right within defined boundaries.
 * (Prototype JS and Scriptaculous required)
 */

// alert(sliderTitles);

// id of the moved element
observedFrame = 'slideWrapper';

// variable for the X-dimension
var currentX;

// length to move the element when control button is clicked
var pushLength = 680;

// maximum and minimum X value
var maxX = 0;
var minX = -(sliderTitles * pushLength) + pushLength; // this is a combation of shown bulletins and pushLength value

// functions to execute
Event.observe(window, 'load', function() { elementPusher(observedFrame) });
Event.observe(window, 'load', function() { checkButtons($('pushLeft'), $('pushRight'), $('pushEnd'), $('pushStart')) });

// function to move the chosen element
function elementPusher(movedElement) {

	// id's of the control buttons
	var leftLink = $('pushLeft');
	var rightLink = $('pushRight');
	var endLink = $('pushEnd');
	var startLink = $('pushStart');

	// check the buttons and apply visibility if necessary
	currentX = 0;

	// create a periodical executer to push slides at certain time
	new PeriodicalExecuter(slideByTime, 7);

	// clicking the left button
	Event.observe(leftLink, 'click', function(event) {
		Event.stop(event);
		if(currentX < maxX) {
			currentX = currentX + pushLength;
			if(currentX >= maxX) {
				currentX = maxX;
			}
			slideElement();
			checkButtons(leftLink, rightLink, endLink, startLink);
		}
	});

	// clicking the right button
	Event.observe(rightLink, 'click', function(event) {
		Event.stop(event);
		if(currentX > minX) {
			currentX = currentX - pushLength;
			if(currentX <= minX) {
				currentX = minX;
			}
			slideElement();
			checkButtons(leftLink, rightLink, endLink, startLink);
		}
	});

	// clicking the end button
	Event.observe(endLink, 'click', function(event) {
		Event.stop(event);
		if (currentX != minX) {
			currentX = minX;
		}
		slideElement();
		checkButtons(leftLink, rightLink, endLink, startLink);
	});

	// clicking the start button
	Event.observe(startLink, 'click', function(event) {
		Event.stop(event);
		if (currentX != maxX) {
			currentX = maxX;
		}
		slideElement();
		checkButtons(leftLink, rightLink, endLink, startLink);
	});
}

// button location check and the setting for visibility
// - if there are less than two bulletins, do not show any control buttons
function checkButtons(first, second, end, start) {	
	if(sliderTitles < 2) {
		$(first, second, end, start).invoke('hide');
	} else {
		if ((currentX == minX) || (currentX < minX)) {
			$(first, start).invoke('show');
			$(second, end).invoke('hide');
		} else if ((currentX == maxX) || (currentX > maxX)) {
			$(second, end).invoke('show');
			$(first, start).invoke('hide');
		} else {
			$(first, second).invoke('show');
			$(end, start).invoke('hide');
		}
	}
}

// sliding effect
function slideElement() {
	new Effect.Move (observedFrame,{ duration: 0.6, x: currentX, mode: 'absolute'});
	}

// timed slides
function slideByTime() {
	if (currentX <= minX) {
		currentX = maxX;
	} else {
		currentX = currentX - pushLength;
	}
	checkButtons($('pushLeft'), $('pushRight'), $('pushEnd'), $('pushStart'));
	slideElement();
}
