String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function checkNoData(v)
{
	if (v == null)
		return "<>";
	return v;
}

function getContactsText()
{
	switch(getLanguage())
	{
		case "en":
			return "Contacts";
			break;
		case "fr":
			return "Contacts";
			break;
		default:
			return "Contactos";
			break;
	}
}

function getPressText()
{
	switch(getLanguage())
	{
		case "en":
			return "Media";
			break;
		case "fr":
			return "Media";
			break;
		default:
			return "Comunicação Social";
			break;
	}
}

function getGoogleMapsText()
{
	switch(getLanguage())
	{
		case "en":
			return "Points of interest";
			break;
		case "fr":
			return "Points d'intérêt";
			break;
		default:
			return "Pontos de interesse";
			break;
	}
}

function getSitemapText()
{
	switch(getLanguage())
	{
		case "en":
			return "Sitemap";
			break;
		case "fr":
			return "Plan du site";
			break;
		default:
			return "Mapa do site";
			break;
	}
}

function getLanguage()
{
	
    var path = String(Request.ServerVariables("URL"));
	if (language !== undefined && language !== null) return language;
	if (path.search(/\/en$|\/en\//i) != -1)
	    return "en";
	else if (path.search(/\/fr$|\/fr\//i) != -1)
	    return "fr";
	else
	    return "pt";
	
	try{
		var path = String(Request.ServerVariables("URL"));
	}catch(e){
		var path = location.href;
	}
}

function getParentPath(path, nLevelsBack){
	path = String(path);
	if (nLevelsBack === undefined || nLevelsBack == 0)
		return path;
	var match = path.match(/\//gi);
	while(nLevelsBack > 0){
		path = path.substring(0, match.lastIndex-1);
		match = path.match(/\//gi);
		path = path.substring(0, match.lastIndex);
		nLevelsBack--;
	}
	return path;
}

function getDefaultDescription()
{
	switch(getLanguage())
	{
		case "en":
			return "The Portuguese Vinho Verde Wine is unique, a smooth mix of scent and lightness that makes it as delicious a natural drink can be. Come and taste it with us. Medium in alcohol, Vinho Verde has great digestive properties due to its freshness and special qualities. The CVRVV site contains extensive marketing information covering Vinho Verde, its Region, Main Producers and Brands, Wine Technology, History and Vinho Verde Route.";
			break;
		case "fr":
			return "Le Vinho Verde est un produit unique dans le monde, un mélange d'arôme et de légèreté qui en fait l'une des boissons les plus délicieuses naturelles. Dans la région de Vinho Verde s'étend sur le nord-ouest du Portugal, la région traditionnellement connue sous le nom du site Entre-Douro-e-Minho.Este contient des informations sur la région du Vinho Verde, en particulier sur la Commission du Vin Viticulture Green, fabricants et marques, de la technologie, l'histoire de la région et une bouteille, Stats, Nouvelles de la Région et la Route des Vins vert.";
			break;
		default:
			return "O Vinho Verde é um produto único no mundo, uma mistura de aroma e leveza que o torna numa das mais deliciosas bebidas naturais. A Região Demarcada dos Vinhos Verdes estende-se por todo o Noroeste de Portugal, na zona tradicionalmente conhecida como Entre-Douro-e-Minho.Este site contém informação relativa à Região dos Vinhos Verdes, nomeadamente sobre a Comissão de Viticultura da Região dos Vinhos Verdes, Produtores e Marcas, Tecnologia, História da Região e de uma Garrafa, Estatísticas, Notícias da Região e da Rota do Vinho Verde.";
			break;
	}
}

function getDefaultKeywords()
{
	switch(getLanguage())
	{
		case "en":
			return "Vinho Verde, Wine, Region, Portuguese Wine, Portugal Wine, VQPRD, Wine technology, Wine route, Vinho Verde Region, Wine producers, Wine brands, Grape varieties, Wine history, Viticulture, Natural drink, Wine tourism, Wine trips, vino blanco, white wine, vin blanc";
			break;
		case "fr":
			return "Vinho Verde, vin, Vins du Portugal, Minho, de la Viticulture, VQPRD, région de Vinho Verde, Vinho Verde Route, le nord du Portugal, délimitées, vert, Commission pour la viticulture, CVRVV, oenologie, de l'agriculture, les raisins, les fermes, les agriculteurs, Laboratoire , Maison des vins de qualité";
			break;
		default:
			return "Vinho Verde, Vinho, Vinhos de Portugal, Minho, Viticultura, VQPRD, Região do Vinho Verde, Rota do Vinho Verde, Norte de Portugal, Demarcada, Verde, Comissão de Viticultura, CVRVV, Enologia, Agricultura, Castas,Quintas, Produtores, Laboratório, Turismo rural, Vinhos de Qualidade";
			break;
	}
}

function currentPathObject()
{
	var rootPath = Server.MapPath("/");
	//var filePath = Server.MapPath(Request.ServerVariables("PATH_INFO"));
	var filePath = String(Request.ServerVariables("PATH_INFO"));
	return {root: rootPath, file: filePath, wanted: filePath.replace(rootPath, "").replace(/\\/g, "/")};
}

function searchXMLAttributes(title, wantedPath, root)
{
	var temp;
	var parent;
	var tempReturnObject = null
	if (root.getAttribute("titulo") == title && root.getAttribute("url").toLowerCase() == wantedPath.toLowerCase())
	{
		parent = root;
		while(parent.parentNode != root.ownerDocument.firstChild)
		{
			parent = parent.parentNode;
		}
		return {parent: parent, child: root};
	}
	
	for (var i = 0; i < root.childNodes.length; i++)
	{
		temp = searchXMLAttributes(title, wantedPath, root.childNodes[i])
		if (temp != null)
			return temp;
	}
	return tempReturnObject;
}

function appXMLAttributes(title, wantedPath, root)
{
	var temp;
	var parent;
	if (root.getAttribute("url") != null && root.getAttribute("url").trim() != ""){
		var menuURL = root.getAttribute("url").toLowerCase();
		var match = menuURL.match(/\//g);
		menuURL = menuURL.substring(0, match.lastIndex);
		match = wantedPath.match(/\//g);
		tempWantedPath = wantedPath.substring(0, match.lastIndex);
		if (tempWantedPath.toLowerCase().trim() == menuURL.toLowerCase().trim()){
			parent = root;
			while(parent.parentNode != root.ownerDocument.firstChild){
				parent = parent.parentNode;
		}
		return {parent: parent, child: root};
		}
	}
	for (var i = 0; i < root.childNodes.length; i++){
		temp = appXMLAttributes(title, wantedPath, root.childNodes[i])
		if (temp != null)
			return temp;
	}
	return null;
}

function getBannerInfo()
{
	var returnVariable = [];
	var titulo, frase
		, url, imagem;
	var pathObject = currentPathObject();
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	var bool = xmlDoc.load(pathObject.root + "\\" + getLanguage() + "\\banner.xml");
	if (bool)
	{
		for (var i = 0; i < xmlDoc.documentElement.childNodes.length; i++)
		{
			titulo = "";
			frase = "";
			url = "";
			imagem = "";
			titulo = xmlDoc.documentElement.childNodes[i].getElementsByTagName("titulo")[0].text;
			frase = xmlDoc.documentElement.childNodes[i].getElementsByTagName("frase")[0].text;
			url = xmlDoc.documentElement.childNodes[i].getElementsByTagName("url")[0].text;
			imagem = xmlDoc.documentElement.childNodes[i].getElementsByTagName("imagem")[0].text;
			returnVariable.push({titulo: titulo, frase: frase, url: url, imagem: imagem});
		}
		return returnVariable;
	}
	return [];
}

function getGlobalMenuInfo(node, path)
{
	var menu = "", cssClass = "";
	for (var i = 0; i < node.childNodes.length; i++)
	{
		if (node.childNodes[i].getAttribute("url") == path)
			cssClass = "highlightActive5x40";
		else
			cssClass = "highlight5x40";
		menu += "<li class=\"" + cssClass + "\" ><a href=\"" + (node.childNodes[i].getAttribute("url") == "" ? "javascript://" : node.childNodes[i].getAttribute("url")) + "\">" + (node.childNodes[i].getAttribute("titulo") == "" ? "??" : node.childNodes[i].getAttribute("titulo")) + "</a>";
		if (node.childNodes[i].length != 0)
		{
			menu += "<ul>";
			for(var j = 0; j < node.childNodes[i].childNodes.length; j++)
			{
				menu += "<li><a href=\"" + (node.childNodes[i].childNodes[j].getAttribute("url") == "" ? "javascript://" : node.childNodes[i].childNodes[j].getAttribute("url")) + "\">" + (node.childNodes[i].childNodes[j].getAttribute("titulo") == "" ? "??" : node.childNodes[i].childNodes[j].getAttribute("titulo")) + "</a></li>";
			}
			menu += "</ul>";
			menu += "</li>";
		}
		else
		{
			menu += "</li>";
		}
	}
	return menu;
}

function buildMenu(title, wantedPath, node, allowHref){
	var start = "";
	var end = "";
	var menu = "";
	if (node.childNodes.length != 0)
	{
		if (node.getAttribute("titulo") == title && node.getAttribute("url").toLowerCase() == wantedPath.toLowerCase()){
			menu += "<li class=\"expandivel aberto activo\">"+
				"<a href=\"\" class=\"expandivel\"><img src=\"/images/mais.png\" width=\"14\" height=\"14\" align=\"left\" alt=\"Expandível\"></a>";
			if (allowHref == true)
				menu += "<a class=\"defaultLink\" href=\"" + (node.getAttribute("url") == "" ? "javascript://" : node.getAttribute("url")) + "\">" +  (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo")) + "</a>\n";
			else
				//menu += "<a class=\"specialActivo\" href=\"" + (node.getAttribute("url") == "" ? "javascript://" : node.getAttribute("url")) + "\">" +  (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo")) + "</a>\n";
				menu += "<span class=\"specialActivo\">" +  (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo")) + "</span>\n";
			menu += "<ul class=\"aberto\">";
		}else{
			menu += "<li class=\"expandivel\">"+
				"<a href=\"\" class=\"expandivel\"><img src=\"/images/mais.png\" width=\"14\" height=\"14\" align=\"left\" alt=\"Expandível\"></a>"+
				"<a href=\"" + (node.getAttribute("url") == "" ? "javascript://" : node.getAttribute("url")) + "\">" +  (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo")) + "</a>\n"
			menu +=	"<ul style=\"display:none;\">";
		}
		end = "</ul>\n</li>";
	}else{
		if (node.getAttribute("titulo") == title && node.getAttribute("url").toLowerCase() == wantedPath.toLowerCase()){
			menu += "<li class=\"activo\">"
			if (allowHref == true)
				menu += "<a class=\"defaultLink\" href=\"" + node.getAttribute("url") + "\">" + (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo")) + "</a>";
			else
				menu += (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo"));
		}else{
			menu += "<li><a href=\"" + (node.getAttribute("url") == "" ? "javascript://" : node.getAttribute("url")) + "\">" +  (node.getAttribute("titulo") == "" ? "??" : node.getAttribute("titulo")) + "</a>";
		}
		end = "</li>\n";
	}
	
	for(var i = 0; i < node.childNodes.length; i++)
		menu += buildMenu(title, wantedPath, node.childNodes[i], allowHref);
	menu += end;
	return menu;
}

function processXML()
{
	var menu = "", title = pageTitle;
	if (headerMenuTitle != "")
		title = headerMenuTitle;
	var pathObject = currentPathObject();
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	var bool = xmlDoc.load(pathObject.root + "\\" + getLanguage() + "\\menu.xml");
	if(bool)
	{
		xmlNodeObject = searchXMLAttributes(title, pathObject.wanted, xmlDoc.documentElement);
		if (xmlNodeObject == null)
			xmlNodeObject = appXMLAttributes(title, pathObject.wanted, xmlDoc.documentElement);
		if (xmlNodeObject == null)
			globalMenuInfo.menu = getGlobalMenuInfo(xmlDoc.documentElement, pathObject.wanted);
		else
			globalMenuInfo.menu = getGlobalMenuInfo(xmlDoc.documentElement, xmlNodeObject.parent.getAttribute("url"));
		globalMenuInfo.slogan = checkNoData(xmlDoc.documentElement.getAttribute("slogan"));
		globalMenuInfo.idioma = checkNoData(xmlDoc.documentElement.getAttribute("idioma"));
		globalMenuInfo.pesquisa = checkNoData(xmlDoc.documentElement.getAttribute("pesquisa"));
		globalMenuInfo.pesquisaURL = checkNoData(xmlDoc.documentElement.getAttribute("pesquisaURL"));
		globalMenuInfo.pesquisaTitulo = checkNoData(xmlDoc.documentElement.getAttribute("pesquisaTitulo"));
	}
}
