var OFFSET_X = -134;
var OFFSET_Y = -164;
var page;
var pages = new Array("como-queda-terminado", "que-hacemos", "como-contactarse" , "como-lo-hacemos");
var totPage = 4;
var IE;
var client;

function handleNavClick(e) {
	e.preventDefault();
	window.location.hash = $(this).attr("href");
	moveContent(450);
}

function handleFooterClick(e) {
	e.preventDefault();
}

function getLocation(h) {
	var x = 0;
	var y = 0;
	
	var x1 = -513;
	var x2 = -1531;
	
	var y1 = 0;
	var y2 = -768;
	
	var px = 0;
	var py = 0;
	var pr = 0;
	
	switch(h) {
		case "#"+pages[1]:
			page = 1;
			x = x2;
			y = y1;
		break;
		case "#"+pages[2]:
			page = 2;
			x = x1;
			y = y2;
		break;
		case "#"+pages[3]:
			page = 3;
			x = x2;
			y = y2;
		break;
		default:
			page = 0;
			x = x1;
			y = y1;
	}
	return {x:x,y:y};
}

function moveContent(d) {
	var loc = getLocation(window.location.hash);
	if(d != 0) {
		var cx = Number($("#mainContent").css("left").replace(/px/, ''));
		var cy = Number($("#mainContent").css("top").replace(/px/, ''));
		if((cx - loc.x) != 0 || (cy - loc.y) != 0) resetIntro();
	}
	$("#mainContent").animate({
		left: loc.x,
		top: loc.y
	}, d, function(){
		// Animation Complete
	});
	$("#ballCanvas").animate({
		left: loc.x + OFFSET_X,
		top: loc.y + OFFSET_Y
	}, d);
}

function replaceHeaders() {
	$(".sectionTitle").each(function() {
		var bgImg = $(this).css("background-image");
		$(this).css("background-image", bgImg.replace(/png/, 'int.png'));
	});
}

function touchCycle(right) {
	if(right) page--;
	else page++;
	if(page < 0) page = pages.length - 1;
	else if(page >= pages.length) page = 0;
	window.location.hash = pages[page];
	moveContent(450);
}

function findClient() {
	//alert(navigator.userAgent);
	if(navigator.userAgent.match(/iPhone/i)) client = "iPhone";
	else if(navigator.userAgent.match(/iPad/i)) client = "iPad";
}

function setOrientation() {
	var initScale;
	var scalable = true;
	switch(client) {
		case "iPhone":
			initScale = (window.innerWidth == 480) ? .3 : .3;
		break;
		case "iPad":
			initScale = (window.innerWidth == 1024) ? 1 : .8;
		break;
		default:
			initScale = 1;
	}
	$("head meta[name=viewport]").attr('content', 'initial-scale='+String(initScale)+', user-scalable='+((scalable) ? "yes" : "no"));
}

$(document).ready(function() {
	findClient();
	setOrientation();
	
	$("#mainNav a").bind("click", handleNavClick);
	$("#footer a").bind("click", handleFooterClick);
	
	IE = document.all ? true : false;
	if(!IE) document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = setMousePos;
	// detect mouse swipe
	document.onmousedown = startMouseCapture;
	document.onmouseup = getMousePos;
	
	moveContent(0);
	window.onresize = function() {
		resizeCanvas();
	}
	resizeCanvas();
	try {
		if(client == "iPad") {
			$("#pageWrapper").touchwipe({
				wipeLeft: function() { touchCycle(); },
				wipeRight: function() { touchCycle(true); }
			});
		}
		document.body.onorientationchange = function() {
			//setOrientation();
			//resizeCanvas();
			window.location = "/"+window.location.hash.replace(/#/,'');
		}
		replaceHeaders();
		window.onload = function() {
			initBalls();
		}
	} catch(r) {}
	
});
