///////////////////////////////////////////////////////////////////////////////////////////////////
// INITIALIZE VARIABLES

var currentSection = null;
var open = new Array();
var pageWidth = screen.width;
if (pageWidth < 480) pageWidth = 480;
var menuText = parent.document.URL.indexOf("lang=ES") == -1 ? "Menu" : "Menú";
var closeText = menuText == "Menu" ? "Close" : "Cerrar";

//////////////////////////////////////////////////////////////////////////////////////////////////////
// ANIMATION FUNCTIONS

function setLeft(section, value) {
	if (cssTrans) {
		section.style.left = value + "px";
	} else {
		currentSection = section;
		value == 0 ? slide(pageWidth, -1) : slide(0, 1);
	}
}

function setHeight(section, fromValue, toValue) {
	if (cssTrans) {
		section.style.height = toValue + "px";
	} else {
		currentSection = section;
		toValue == 0 ? drawer(fromValue, 0, -1) : drawer(0, toValue, 1);
	}
}

function slide(currentValue, direction) {
	currentValue = currentValue + (direction * 60);
	if (currentValue < 0) {
		currentSection.style.left = "0px";
		currentSection = null;
	} else if (currentValue >= pageWidth) {
		currentSection.style.left = pageWidth + "px";
		currentSection = null;
	} else {
		currentSection.style.left =  currentValue + "px";
		setTimeout('slide(' + currentValue + ', ' + direction + ')', 1);
	}
}

function drawer(currentValue, maxValue, direction) {
	currentValue = currentValue + (direction * 50);
	if(currentValue < 0) {
		currentSection.style.height = "0px";
		currentSection = null;
	} else if(currentValue >= maxValue && direction == 1) {
		currentSection.style.height = maxValue + "px";
		currentSection = null;
	} else {
		currentSection.style.height =  currentValue + "px";
		setTimeout('drawer(' + currentValue + ', ' + maxValue + ', ' + direction + ')', 1);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// DRAWERS

function toggleDrawer(content) {
	if (!currentSection) {
		var arrow = document.getElementById(content.id + 'arrow');
		var row = document.getElementById(content.id + 'row');
		var expand = arrow.style.opacity == "0.99" || content.style.display == "none";
		if(animate) {
			var max_height = content.getAttribute("max_height");
			if (!max_height) {
				content.style.display = "block";
				var max_height = content.offsetHeight;
				content.setAttribute("max_height", max_height);
				if (!expand) {
					content.style.height = max_height + "px";
				}
			}
			setHeight(content, expand ? 0 : max_height, expand ? max_height : 0);
		} else {		
			content.style.display = expand ? "block" : "none";			
		}
		toggleArrow(arrow, expand);
	}			
}

function toggleArrow(arrow, up) {
	if (up) {
		cssTrans ? arrow.style.webkitTransform = 'rotate(-180deg)' : arrow.src = arrow.src.replace(/down/,"up");
		arrow.style.opacity = "0.6";
	} else {
		cssTrans ? arrow.style.webkitTransform = 'rotate(0deg)' : arrow.src = arrow.src.replace(/up/,"down");
		arrow.style.opacity = "0.99";	
	}
	arrow.width = 12
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// MENU FUNCTIONS

function toggleMenu() {
	if (!currentSection) {
		var menuButton = document.getElementById('menuButton');
		var menu = document.getElementById('menu');
		var max_height = menu.getAttribute("max_height");
		var cover = document.getElementById('cover');
		if (!max_height) {
			menu.style.display = "block";
			max_height = menu.offsetHeight
			menu.setAttribute("max_height", max_height);
			var menuTop = menuButton.offsetTop + menuButton.offsetHeight + 9;
			menu.style.top = menuTop + "px";
		}

		if (menu.style.height == "" || menu.style.height == "0px") {
			setHeight(menu, 0, max_height);
			menuButton.innerHTML = closeText;
			if (!cover.style.height) {
				cover.style.height = (document.getElementById('wrapper').offsetHeight - menuTop) + "px";
				cover.style.top = menuTop + "px";
			}
			cover.style.left = "0px";
			cover.style.width = "100%";
			cover.style.opacity = "0.4";
		} else {	
			resetMenu();
			setHeight(menu, max_height, 0);
		}
	}
}

function closeMenu() {
	if( document.getElementById('menu') ){
		document.getElementById('menu').style.height = "0px";
		resetMenu();
	}
}

function resetMenu() {
	for (i=0; i<open.length; i++) {
		open[i].style.display = "none";
		open[i].style.left = "100%";
	}
	document.getElementById('menuButton').innerHTML = menuText;
	document.getElementById('cover').style.width = "0px";		
	document.getElementById('cover').style.opacity = "0";
	for (i=0; i<open.length; i++) {
		open[i].style.display = "block";
	}
	open = new Array();
}

function goTo(obj) {
	if (!currentSection) {
		if (obj.parentNode) {
			section = obj.parentNode.childNodes[3];
			open[open.length] = section;
			document.getElementById('menu').style.height = section.offsetHeight + "px";
			setLeft(section, 0);
		} else {
			closeMenu();
			location = obj;
		}
	}
}

function goBack(obj) {
	if (!currentSection) {
		document.getElementById('menu').style.height = document.getElementById('menu').getAttribute("max_height") + "px";
		setLeft(obj.parentNode, pageWidth);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// TABS

function setTabs(group, number, obj) {
	for(i=1; i<=number; i++) {
		if(document.getElementById(group+i)) {
			document.getElementById(group+i).className = "tab";
		}
	}
	if (obj) {
		obj.className = "tab on";
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// AUTO DROPDOWNS

function gotoDropdown(dropdown) {
	var url = dropdown.options[dropdown.selectedIndex].value;
	if (url != '#') location = url;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// HIDE URL BAR ON IPHONE

window.onload = function() {	
	window.scrollTo(0, 1);
}