/* INITS */

var site = "";
var previous = "";

var currLocationName = "";

var animating = "";
var animatingId = "";
var animateTo = 0;

function init() {
	var path = location.href;
	var selectedSection = path.substring(path.lastIndexOf('?')+1, path.length-path.substring(path.indexOf("&"), path.length).length );

	switch (selectedSection) {
		case "who_we_are":
		case "what_we_do":
		case "portfolio":
		case "clients":
		case "who_we_love": 
		case "contact":
			goto(selectedSection);
			site = selectedSection;
			break;
		case "NYC":
			showLocations();
			gotoNYC();
			site = "NYC";
			break;
		case "Brazil":
			showLocations();
			gotoBrazil();
			site = "Brazil";
			break;
		case "home":
		default:
			fill("php_sites/home.php?language=" + language, "content_data");
			site = "home";
			break;
	}
	
}

function changeLocation(id) {
	document.location.href = "home.php?" + id + getLanguageLocation();
}

function changeLanguage(lang) {
	language = lang;
	changeLocation(site);
}

function getLanguageLocation() {
	return "&language=" + language;
}

function getLanguageSuffix() {
	return suffix;
}

/* Buttons */

function goto(id) {
	element("home_link").style.color = "#a7a7a7";
	
	deselectPreviousButton();
	selectButton(id);
	fill("php_sites/" + id + ".php?language=" + language, "content_data");
}

function markLocations() {
	element("home_link").style.color = "#a7a7a7";
	
	deselectPreviousButton();
	selectButton("locations");
}

function gotoNYC() {
	markLocations();
	element("nyc").src = "images/nyc" + getLanguageSuffix() + "_bold.gif";
	element("brazil").src = "images/brazil" + getLanguageSuffix() + ".gif";
	fill("php_sites/nyc.php", "content_data");
}

function gotoBrazil() {
	markLocations();
	element("nyc").src = "images/nyc" + getLanguageSuffix() + ".gif";
	element("brazil").src = "images/brazil" + getLanguageSuffix() + "_bold.gif";
	fill("php_sites/brazil.php?language=" + language , "content_data");
}

function gotoPortfolio(page) {
	fill("php_sites/portfolio.php?Page=" + page + getLanguageLocation(), "content_data");
}

/* Specific API */

function deselectButton(id) {
	element(id).src = "images/" + id + getLanguageSuffix() + ".gif";
}

function deselectPreviousButton() {
	if (previous != "") {
		deselectButton(previous);
	}
}

function selectButton(id) {
	element(id).src = "images/" + id + getLanguageSuffix() + "_bold.gif";
	previous = id;
}

function showLocations() {
	if (site != "NYC" && site != "Brazil") {
		element("nyc").src = "images/nyc" + getLanguageSuffix() + ".gif";
		element("brazil").src = "images/brazil" + getLanguageSuffix() + ".gif";
		element("nyc").style.visibility = "visible";
		element("brazil").style.visibility = "visible";
	}
}

function hideLocations() {
	if (site != "NYC" && site != "Brazil") {
		element("nyc").src = "images/nyc" + getLanguageSuffix() + ".gif";
		element("brazil").src = "images/brazil" + getLanguageSuffix() + ".gif";
		element("nyc").style.visibility = "hidden";
		element("brazil").style.visibility = "hidden";
	}
}


function alocationTitle(location) {
	if (location == "") {
		location = "locations";
	}
	element("place").src = "images/" + location + "_title.png";
}

function alocations(location) {
	currLocationName = location;
	gotoLocation(location, 1);
}

function gotoLocation(location, page) {
	fill("php_sites/brazil_photos.php?Page=" + page + getLanguageLocation() + "&Location=" + location, "photo_container");
}

/* General API */

function element(id) {
	return document.getElementById(id);
}

function ajaxObject() {
	var request = null;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		document.getElementById("content").innerHTML = "Lo sentimos, su browser no soporta ajax.  Por favor descargue un browser mas actualizado.  <br />Gracias";
	}
	return request;
}

function fill(file, place) {

	var request = ajaxObject();
	
	request.onreadystatechange = function() {
		if(request.readyState == 4) {
			document.getElementById(place).innerHTML = request.responseText;
			document.body.style.cursor = 'auto';
		}
	}
	document.body.style.cursor = 'wait';
	request.open ("GET", file, true);
	request.send (null);
}

function getHeightFor(id) {
	var height = element(id).style.height;
	
	if (height == "") {
		height = 0;
	} else {
		height = height.substring(0, height.length - 2) * 1;
	}
	
	return height;
}

function animate() {
	var currentHeight = getHeightFor(animating);
	
	if (animateTo == 0) {
		currentHeight -= 40;
	} else {
		currentHeight += 40;
	}
	
	element(animating).style.height = currentHeight + "px";
	
	if ((animateTo == 0 && currentHeight <= animateTo) || (animateTo > 0 && currentHeight >= animateTo)) {
		clearInterval(animatingId);
		
		element(animating).style.height = animateTo + "px";
		
		animating = "";
		animatingId = "";
		animateTo = 0;
	}
}

function animateElement(id, maxSize) {
	if (animating != "") {
		return;
	}
	
	var currentHeight = getHeightFor(id);
	
	if (currentHeight == 0) {
		animateTo = maxSize;
	} else {
		animateTo = 0;
	}
	
	animating = id;
	animatingId = setInterval("animate();", 1);
	
	animate();	
}

function showElement(id) {
	var display = element(id).style.display;
	if (display == "" || display == "none") {
		display = "block";
	} else {
		display = "none";
	}
	element(id).style.display = display;
}

function showLinks() {
	//animateElement("link", 270);
	element("bookings").style.display = "none";
	element("travel").style.display = "none";
	showElement("link");
}

function showBookings() {
	//animateElement("bookings", 110);
	element("link").style.display = "none";
	element("travel").style.display = "none";
	showElement("bookings");
}

function showTravel() {
	element("link").style.display = "none";
	element("bookings").style.display = "none";
	showElement("travel");
}

function autoSetHeight() {
    parent.document.getElementById('blog_frame').height = document['body'].offsetHeight;
}
