var fSlideLeft, fSlideRight;
var bAutomaticSlide = true;


function fShowSlideNr(iSlideId) {
	var iSteps;

	if (parseInt(iSlideId) >= parseInt(iCurrSlide)) {
		iSteps = (iSlideId - iCurrSlide) + 1;
		strDirection = "right";
	} else {
		iSteps = (iCurrSlide - iSlideId) - 1;
		strDirection = "left";
	}

	fMoveSlideshow(iCurrSlide, iTotalSlides, strDirection, iSteps, 0);
}


window.addEvent('domready', function() {
	// Restrict anchor elements with # in the href attribute from doing anything
	$$('a[href=#]').addEvent('click', fNoAnchor);

	if ($$('ul.overlaynav li a')[0]){
		if ($$('ul.overlaynav li a')[0].hasClass('count')) {
			bAutomaticSlide = false;

			var oHrefCount = $$('ul.overlaynav li a');
			oHrefCount[0].set('class', 'count active');
			for (var iCnt = 0; iCnt < oHrefCount.length; iCnt++) {
				if (oHrefCount[iCnt].hasClass('count')) {
						oHrefCount[iCnt].set('id', iCnt);
						oHrefCount[iCnt].addEvent('mouseover', function() {
							$$('ul.overlaynav li a').set('class', 'count');
							fShowSlideNr(this.id);
							$(this.id).set('class', 'count active');
					});
				}
			}
		}
	}

	// If a slideshow DIV with more than one image is present on the page,
	// attach events to the previous and next handlers
	if ($$('div.slideshow')[0]) {
		if (!$$('ul.overlaynav')[0].hasClass('hide')) {
			// Hide the "previous slide" button on page load as slide 1 will already be shown
			$$('ul.overlaynav li a.prev').getParent('li').setStyle('visibility', 'hidden');

			fSlideLeft = function(event) {
				// When the user uses the slideshow, disable automatic sliding
				if (!(event == null)) { bAutomaticSlide = false; }
				if (!(event == null) || bAutomaticSlide) { fMoveSlideshow(iCurrSlide, iTotalSlides, "left", 1, 300); }
			};
			fSlideRight = function(event) {
				// When the user uses the slideshow, disable automatic sliding
				if (!(event == null)) { bAutomaticSlide = false; }
				if (!(event == null) || bAutomaticSlide) { fMoveSlideshow(iCurrSlide, iTotalSlides, "right", 1, 300); }
			};

			fAddSlideshowNav();
		}

		if ($$('div.slideshow div.images img')[0].getWidth() > 372) {
			// Initiate automated playback
			fSlideRight.delay(4000);
		}
	}
});


// A function which adds event handlers to the slideshow navgiation
function fAddSlideshowNav() {
	$$('ul.overlaynav li a.prev').addEvent('click', fSlideLeft);
	$$('ul.overlaynav li a.next').addEvent('click', fSlideRight);
}

function fActivateSlidesNav() {
	var oHrefCount = $$('ul.overlaynav li a');
	for (var iCnt = 0; iCnt < oHrefCount.length; iCnt++) {

		if (oHrefCount[iCnt].hasClass('count')) {
				oHrefCount[iCnt].set('id', iCnt);
				oHrefCount[iCnt].addEvent('mouseover', function() {
					$$('ul.overlaynav li a').set('class', 'count');
					fShowSlideNr(this.id);
					$(this.id).set('class', 'count active');
			});
		}
	}
}


// A function which moves a slideshow back or forward
// Arguments:
// @iCurr - an integer which describes which slide is currently being viewed
// @iTotal - an integer which describes how many slides the slideshow has
// @sDir - a string which tells in which direction the slideshow should move
function fMoveSlideshow(iCurr, iTotal, sDir, iSteps, iDuration) {
	// Prevent the user from clicking too fast
	var iDelay, iDelay2;

	if (iDuration < 100) {
		iDelay = 100;
		iDelay2 = 40
	} else {
		iDelay2 = iDuration;
		iDelay = iDuration;
	}

	$$('ul.overlaynav li a.prev').removeEvent('click', fSlideLeft);
	$$('ul.overlaynav li a.next').removeEvent('click', fSlideRight);
	fAddSlideshowNav.delay(iDuration);


	$$('ul.overlaynav li a.count').removeEvents('mouseover');
	fActivateSlidesNav.delay(iDelay2);


	var iCurrPos, iNewPos, fxSlideshow;

	iCurrPos = parseInt(($$('div.slideshow div.images img')[0].getStyle('right')).replace(/px/, ""));
	fxSlideshow = new Fx.Tween($$('div.slideshow div.images img')[0], {duration: iDuration});

	switch (sDir) {
		case "left":
			if ((iCurr - 1) != 0) {
				// Calculate the new position for the images
				iNewPos = iCurrPos - (372 * iSteps);

				// Animate the transition
				fxSlideshow.start('right', iCurrPos + 'px', iNewPos + 'px');

				iCurrSlide = (iCurrSlide - iSteps);

				$('hiddenCurrSlide').value = iCurrSlide;

				if ($$('ul.overlaynav li')[0]) {
					var oPrevArrow = $$('ul.overlaynav li')[0];
					var oNextArrow = $$('ul.overlaynav li')[1];

					if (!($$('ul.overlaynav li a')[0].hasClass('count'))) {
						if (iCurrSlide == 1) {
							oPrevArrow.setStyle('visibility', 'hidden');
						} else if (iCurrSlide < iTotal) {
							oNextArrow.setStyle('visibility', 'visible');
						}
					}
				}
			}
			break;

		case "right":
			if (iCurr != iTotal) {

				// Calculate the new position for the images
				iNewPos = iCurrPos + (372 * iSteps);

				// Animate the transition
				fxSlideshow.start('right', iCurrPos + 'px', iNewPos + 'px');

				iCurrSlide = (iCurrSlide + iSteps);

				$('hiddenCurrSlide').value = iCurrSlide;

				if ($$('ul.overlaynav li')[0]) {
					var oPrevArrow = $$('ul.overlaynav li')[0];
					var oNextArrow = $$('ul.overlaynav li')[1];

					if (!($$('ul.overlaynav li a')[0].hasClass('count'))) {
						if (iCurrSlide == iTotal) {
							oNextArrow.setStyle('visibility', 'hidden');
						} else if (iCurrSlide > 1) {
							oPrevArrow.setStyle('visibility', 'visible');
						}
					}
				}
			}
			break;
	}

	// Unless the final slide has been reached (in the right-direction), set up another
	// automatic slide to the right in four seconds
	if ((iCurr != iTotal) && bAutomaticSlide) {
			fSlideRight.delay(4000);

	}
}


function fNoAnchor() {
	return false;
}
