/*****************************************************

 TERMS OF USE

 You may freely use, distribute, and modify this code
 for any purpose, so long as you attribute the code as
 follows:

 Author:                              Matthew Congrove
 More Information:               mydailyphoto.com/blog

******************************************************/

/*****************************************************

 NOTICE:
 
 Vertical scrolling is disabled using the Touch API
 (Line 154: e.preventDefault). This means that it is
 impossible for a user to access the window location
 bar on the iPhone, so please be kind and provide some
 sort of 'Close' button, or modify the script to suit
 your needs, as well as those of your users.
 
 NOTICE:
 
 All debugging code is clearly marked as such. Just
 search for 'Debug'.
 
 MORE INFORMATION:
 
 For a complete tutorial on how this code works,
 please visit the myDailyPhoto blog entry --
 
 mydailyphoto.com/blog/iphone-safari-flick-navigation

******************************************************/

var touching, dPrevious, dCurrent, dNext, oX;

// 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;

// Apple iPhone Touch API events
document.addEventListener('touchstart', touchHandler, false);
document.addEventListener('touchmove', touchHandler, false);
document.addEventListener('touchend', touchHandler, false);
document.addEventListener('touchcancel', touchHandler, false);

// 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 > 100) {
					
					// If the next DIV exists then continue
					if (document.getElementById('Div'+dNext)) {
						// If this is still from the original touch
						if (touching == true)
						{
							// End the current touch
							touching = false;
							// Move in the next DIV
							switchNext(dCurrent,dNext);
							// Recalculate the pages
							dPrevious = dCurrent;
							dCurrent = dNext;
							dNext = dNext+1;
							document.getElementById('divOF').innerHTML = (dCurrent-1)+' of ' + slideCount;
							
						}
					}
				}
			// 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 > 100) {
					
					// If the previous page isn't 0, in other words there's a previous page to the left
					if (dPrevious != 1) {
						// If this is still from the original touch
						if (touching == true) {
							// End the current touch
							touching = false;
							// Move in the previous DIV
							switchPrevious(dCurrent,dPrevious);
							// Recalculate the pages
							dNext = dCurrent;
							dCurrent = dPrevious;
							dPrevious = dPrevious-1;
							document.getElementById('divOF').innerHTML = (dCurrent-1)+' of 4';
						
							
						}
					}
				}
			}
		}
	}
	// 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;
	}
}
// If the user requests the page to the right of the screen ('next' DIV)
function switchNext(divOut,divIn) {
	
	// Show the DIV to the right
	document.getElementById('Div'+divIn).style.display = 'block';
	// Move the currently displaying DIV from Center to Left
	document.getElementById('Div'+divOut).className = 'divCtL';
	// Move the requested DIV from the Right to Center
	document.getElementById('Div'+divIn).className = 'divRtC';
	// For some reason the animation doesn't stick after exiting this function, so force the off-screen location
	document.getElementById('Div'+divOut).style.left = '-490px';
	// For some reason the animation doesn't stick after exiting this function, so force the on-screen location
	document.getElementById('Div'+divIn).style.left = '10px';
	
}
// If the user requests the page to the left of the screen ('previous' DIV)
function switchPrevious(divOut,divIn) {
	// Show the DIV to the left
	document.getElementById('Div'+divIn).style.display = 'block';
	// Move the currently displaying DIV from Center to Right
	document.getElementById('Div'+divOut).className = 'divCtR';
	// Move the requested DIV from the Left to Center
	document.getElementById('Div'+divIn).className = 'divLtC';
	// For some reason the animation doesn't stick after exiting this function, so force the off-screen location
	document.getElementById('Div'+divOut).style.left = '490px';
	// For some reason the animation doesn't stick after exiting this function, so force the on-screen location
	document.getElementById('Div'+divIn).style.left = '10px';
}
/* Debug Results Display DIV */
function numberOF() {
	document.getElementById('divOF').innerHTML =  (dCurrent-1)+' of ' + slideCount ;
}
 

function slideIn(){

	if (dCurrent == 1) {	
		switchNext(dCurrent,dNext);
		// Recalculate the pages
		dPrevious = dCurrent;
		dCurrent = dNext;
		dNext = dNext+1;
		if (document.getElementById("divOF")){
			document.getElementById('divOF').innerHTML = (dCurrent-1)+' of ' + slideCount;
		}
	}
  
}



setTimeout(slideIn, 4500);



