/* Author: 

*/

$(document).ready(function() {
	// new MBP.fastButton(document.getElementsByClassName("ad-next")[0], function() {
	// 	gallery[0].showImage(gallery[0].nextIndex());
	// });
	
	var gallery;
	try {
		gallery = $('.ad-gallery').adGallery({
			description_wrapper: $('#image-description'),
			display_back_and_forward: true,
			loader_image: '/theme/mweiss/images/loader.gif',
			thumb_opacity:0.5,
			slideshow: {
			    enable: false
			}
		});
	} catch(e) {}
	
	// disable menu items for preview page
/*
	$('#main-menu a').click(function() {
		return false;
	});*/

	
	
	
	// handle touch events + privitive swipe
	var touching, dPrevious, dCurrent, dNext, oX, scrollDelta;
	// Whether or not the finger is touching the screen
	touching = false;
	// Original X-coordinate
	oX = 0;
	// Initial page numbers
	dPrevious = 0;
	dCurrent = 1;
	dNext = 2;

	// number of pixels to move before triggering the animation
	scrollDelta = 20;

	// Apple iPhone Touch API events
	try {
		document.getElementById("gallery").addEventListener('touchstart', touchHandler, false);
		document.getElementById("gallery").addEventListener('touchmove', touchHandler, false);
		document.getElementById("gallery").addEventListener('touchend', touchHandler, false);
		document.getElementById("gallery").addEventListener('touchcancel', touchHandler, false);
	} catch(e) {}

	// The handler for all Apple iPhone Touch API events
	function touchHandler(e) {
		// If the user has started a touch event
		if (e.type == "touchstart") {
			touching = true;
			// If there's only one finger touching
			if (e.touches.length == 1) {
				var touch = e.touches[0];
				// If they user tries clicking on a link
				if(touch.target.onclick) {
					touch.target.onclick();
				}
				// The originating X-coord (point where finger first touched the screen)
				oX = touch.pageX;
				// Reset default values for current X-coord and scroll distance
				nX = 0;
				scrollX = 0;
			}
		}
		// If the user has touched the screen and moved the finger
		else if (e.type == "touchmove") {
			// Prevent the default scrolling behaviour (notice: This disables vertical scrolling as well)
			e.preventDefault(); 

			// If there's only one finger touching
			if (e.touches.length == 1) {
				var touch = e.touches[0];
				// The current X-coord of the users finger
				var nX = touch.pageX;

				// If the user moved the finger from the right to the left
				if (oX > nX) {
					// Find the scrolling distance
					var scrollX = oX-nX;
					// If the user scrolled more than 100 pixels
					if (scrollX > scrollDelta) {
						gallery[0].showImage(gallery[0].nextIndex());
					} // not yet reached scrollDelta
					else {
						// document.getElementById('scrollX').innerHTML = document.getElementById('Div'+dCurrent).style.left;
						// document.getElementById('Div'+dCurrent).style.left = -scrollX +10  +"px";
					}
				// If the user moved the finger from the left to the right
				} else {
					// Find the scrolling distance
					var scrollX = nX-oX;
					// If the user scrolled more than 100 pixels
					if (scrollX > scrollDelta) {
						gallery[0].showImage(gallery[0].prevIndex());
					}
				}
			}
		}
		// If the user has removed the finger from the screen
		else if (e.type == "touchend" || e.type == "touchcancel") {
			// Defines the finger as not touching
			touching = false;
		}
	}
});



