// JavaScript Document
var oX,oY,nX,nY;
var initDeg=40;
var stopPropagation=false;
var _antigo = undefined;
var selecionado = 0;

//
var touching, dPrevious, dCurrent, dNext, oXp;

// Whether or not the finger is touching the screen
touching = false;

// Initial page numbers
dPrevious = 0;
dCurrent = 1;
dNext = 2;

// Original X-coordinate
oXp = 0;

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}


function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}
	
// Verificar o posicionamento da tela
function updateOrientation()
{
	if(window.orientation != 0)
	{
		alert("Site melhor visualizado verticalmente");
	}
}

document.addEventListener('touchstart',touchBegin,false);
document.addEventListener('touchmove',touchMoved,false);
document.addEventListener('touchend', touchHandler, false);
document.addEventListener('touchcancel', touchHandler, false);

function retirar()
{
	/*document.removeEventListener('touchstart',touchBegin,false);
	document.removeEventListener('touchmove',touchEnd,false);*/
	
	document.removeEventListener('touchstart',touchBegin,false);
	document.removeEventListener('touchmove',touchMoved,false);
	document.removeEventListener('touchend', touchHandler, false);
	document.removeEventListener('touchcancel', touchHandler, false);
	
	// 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;
}
	
function reativar()
{
	/*document.addEventListener('touchstart',touchBegin,false);
	document.addEventListener('touchmove',touchEnd,false);*/
	document.addEventListener('touchstart',touchBegin,false);
	document.addEventListener('touchmove',touchMoved,false);
	document.addEventListener('touchend', touchHandler, false);
	document.addEventListener('touchcancel', touchHandler, false);
}



function touchBegin(e)
{
	if(e.type=="touchmove"||e.type=="touchend"||e.type=="touchcancel")e.preventDefault();
	touch=e.touches[0];
	oX=touch.pageX;
	oY=touch.pageY;
	touchHandler(e);
}

function touchMoved(e)
{
	touchHandler(e);
	
	if(e.type=="touchmove"||e.type=="touchend"||e.type=="touchcancel")
	{
		e.preventDefault();
		touch=e.touches[0];
	}
	
	nX=touch.pageX;
	xY=touch.pageY;
	var diffX;
	
	diffX=nX-oX;
	
	var rotate=initDeg-diffX;
	
	var transform='rotateY('+rotate+'deg)';
	
	$('parent').style.webkitTransform='rotateY('+rotate+'deg)';
	$('parentVinte').style.webkitTransform='rotateY('+rotate+'deg)';
}

function clearClassNames()
{
	$('child').className='node';
}

function showFirst(e)
{
	if(stopPropagation)return;
	
	if($('child').style.opacity==1)return resetMenu(e);
	
	$('child').style.webkitTransform='translateZ(100px)';
	clearClassNames();
	stopEvent(e);
	$('frame').style.webkitPerspective='600';
	$('child').style.opacity=1;
	$('child').className='second_show';
	
	$('these').style.opacity=0;
	$('these').style.marginTop = (-900) + "px";
}

function showSecond(e)
{
	if(stopPropagation)return;
	clearClassNames();
	stopEvent(e);
	$('frame').style.webkitPerspective='1000';
	$('these').style.webkitTransform='translateZ(10px)';
	$('child').style.webkitTransform='translateZ(100px)';
	$('child').style.opacity=1;
	$('these').style.opacity=0;
	$('these').style.marginTop = (-900) + "px";
}

function resetMenu(e)
{
	stopEvent(e);
	clearClassNames();
	$('frame').style.webkitPerspective='500';
	$('child').style.opacity=0;
	$('these').style.opacity=1;
	$('these').style.marginTop = (-250) + "px";
	$('child').className='second_hide';
	$('child').style.webkitTransform='translate3d(0em,0em, -0.1em)';
}

function stopEvent(e)
{
	stopPropagation=false;
	if(e.target&&(e.target==$('photo1')||e.target==$('photo2')||e.target==$('photo3')||e.target==$('photo4')||e.target==$('photo5')||e.target==$('photo6'))){
		stopPropagation=true;return;}console.log(e.nodeType);if(window.event){window.event.cancelBubble=true;}else{e.stopPropagation();
	}
}

function mostrar_sub(e)
{
	if (_antigo != undefined)
	{
		$(_antigo).style.display='none';
	}
	_antigo = e;
	$(_antigo).style.display='block';
}


// 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)
			oXp = touch.pageX;
			// Reset default values for current X-coord and scroll distance
			nX = 0;
			scrollX = 0;
			/* Debugging  document.getElementById('dir').innerHTML = 'Direction: Null';*/
			/* Debugging  document.getElementById('oXp').innerHTML = 'Start X-Coord: '+oXp+'px';*/
		}
	}
	// 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;
			/* Debugging  document.getElementById('nX').innerHTML = 'Current X-Coord: '+nX+'px';*/
			
			// If the user moved the finger from the right to the left
			if (oXp > nX) {
				// Find the scrolling distance
				var scrollX = oXp-nX;
				/* Debugging  document.getElementById('scrollX').innerHTML = 'Scroll Distance: '+scrollX+'px';*/
				// If the user scrolled more than 100 pixels
				if (scrollX > 100) {
					/* Debugging  document.getElementById('dir').innerHTML = 'Direction: Flicked Left';*/
					// 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;
							/* Debugging  alertIt();*/
						}
					}
				}
			// If the user moved the finger from the left to the right
			} else {
				// Find the scrolling distance
				var scrollX = nX-oXp;
				/* Debugging  document.getElementById('scrollX').innerHTML = 'Scroll Distance: '+scrollX+'px';*/
				// If the user scrolled more than 100 pixels
				if (scrollX > 100) {
					/* Debugging  document.getElementById('dir').innerHTML = 'Direction: Flicked Right';*/
					// If the previous page isn't 0, in other words there's a previous page to the left
					if (dPrevious != 0) {
						// 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;
							/* Debugging  alertIt();*/
						}
					}
				}
			}
		}
	}
	// 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 = '0px';
	//
	document.getElementById('bolota'+selecionado).className = 'pos';
	selecionado++
	document.getElementById('bolota'+selecionado).className = 'posMarc';
	//
}
// 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 = '0px';
	//
	document.getElementById('bolota'+selecionado).className = 'pos';
	selecionado--
	document.getElementById('bolota'+selecionado).className = 'posMarc';
	//
}
/* Debug Results Display DIV 
function alertIt() {
	document.getElementById('divPrev').innerHTML = 'Previous Page: '+dPrevious;
	document.getElementById('divCurrent').innerHTML = 'Current Page: '+dCurrent;
	document.getElementById('divNext').innerHTML = 'Next Page: '+dNext;
}*/