var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function menu_DoFSCommand(command, args) {
	var myFlashObj = InternetExplorer ? menu : document.menu;
	if (command == "getDepth") {
		getDepth(myFlashObj);
	} else if (command == "newPage") {
		newPage(args);
	}
}

function topo_DoFSCommand(command, args) {
	var myFlashObj = InternetExplorer ? topo : document.topo;
	if (command == "getDepth") {
		getDepth(myFlashObj);
	} else if (command == "newPage") {
		newPage(args);
	}
}

function flash_DoFSCommand(command, args) {
	if (args) {
		eval(command+"('"+args+"')");
	} else {
		eval(command+"()");
	}
}

function getDepth(obj){
	/*var param = self.location.href.substr(self.location.href.indexOf("?")+1,4);
	if (param == "inpg"){
		obj.SetVariable("innerpage", 1);
	}*/
	var param = self.location.href.indexOf("?");
	if (param){
		obj.SetVariable("innerpage", 1);
	}
}

function newPage(args) {
	var nextFile = args.split(",")[0];
	var lang = args.split(",")[1];
	if (!lang) lang = "pt";
	var thisFile = findThisFileName ();
	if ((nextFile == thisFile) && (self.location.href.indexOf(lang)>0)) return;
	if (nextFile.indexOf(".pdf") >0 || nextFile.indexOf(".jpg")>0) window.open(nextFile);
	else self.location.href="/htmls/"+lang+"/"+nextFile+"?inpg";
}

if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
  document.write('<SCRIPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  document.write('Sub menu_FSCommand(ByVal command, ByVal args)\n');
  document.write(' call menu_DoFSCommand(command, args)\n');
  document.write('end sub\n');
  document.write('Sub topo_FSCommand(ByVal command, ByVal args)\n');
  document.write(' call topo_DoFSCommand(command, args)\n');
  document.write('end sub\n');
  document.write('Sub premios_FSCommand(ByVal command, ByVal args)\n');
  document.write(' call flash_DoFSCommand(command, args)\n');
  document.write('end sub\n');
  document.write('Sub flash_FSCommand(ByVal command, ByVal args)\n');
  document.write(' call flash_DoFSCommand(command, args)\n');
  document.write('end sub\n');
  document.write('</SCRIPT\> \n');
} 

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

var fotoWindow;
function popImg(filename) {
	if (fotoWindow){
		fotoWindow.close();
	}
	fotoWindow=window.open("", "fotoWindow", "height=150,width=150,resize=yes,scrollbars=yes,status=yes");
	fotoWindow.document.write("<HTML>");
	fotoWindow.document.write("<HEAD>");
	fotoWindow.document.write("<SCRIPT LANGUAGE=\"JavaScript\">function resizeMe(picRef) {	MM_showHideLayers('tempLayer','','hide'); 		if (window.screen){ 		var Wlimit = screen.width-70; 		var Hlimit = screen.height-80; 	} 	var picW = picRef.width+50; 	var picH = picRef.height+70; 	var targetW = (Wlimit) ? (picW > Wlimit) ? Wlimit : picW : picW; 	var targetH = (Hlimit) ? (picH > Hlimit) ? Hlimit : picH : picH; 	window.resizeTo(targetW,targetH); 	var window_left = (window.screen.width/2) - ((targetW/2) + 5); 	var window_top = (window.screen.height/2) - ((targetH/2) + 22); 	window.moveTo(window_left,window_top);}</SCRIPT>");
	fotoWindow.document.write('<script language="JavaScript" type="text/JavaScript">function MM_reloadPage(init) {  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();}MM_reloadPage(true);function MM_findObj(n, d) { var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);if(!x && d.getElementById) x=d.getElementById(n); return x;}function MM_showHideLayers() { var i,p,v,obj,args=MM_showHideLayers.arguments;for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];if (obj.style) { obj=obj.style; v=(v==\'show\')?\'visible\':(v==\'hide\')?\'hidden\':v; }obj.visibility=v; }}</script>');
	fotoWindow.document.write("<TITLE>CL.pt : Imagem</TITLE>");
	fotoWindow.document.write("<HEAD>");
	fotoWindow.document.write("<BO"+"DY bgcolor=#0E4F24 leftmargin=10 topmargin=10 marginwidth=10 marginheight=10>");
	fotoWindow.document.write('<div id="tempLayer" style="position:absolute; left:0; top:0; width:130; height:130; z-index:10; background-color: #0E4F24; layer-background-color: #0E4F24; border: 1px none #000000; visibility: visible; overflow: visible;"><table width="130" height="130"  border="0" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><b><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif">Por favor aguarde.</font></b></td></tr></table></div>');
	fotoWindow.document.write("<A href=javascript:self.close()>");
	fotoWindow.document.write("<IMG name=foto id=foto src="+filename+" onLoad=resizeMe(this) border=0 alt=\"clique para fechar esta imagem\"></a>");
	fotoWindow.document.write("</HTML>");
}

var infoWindow;
function popinfo(file,w,h) {
	if (infoWindow){
		infoWindow.close();
	}
	infoWindow=window.open(file, "infoWindow", "height="+h+",width="+w+",resize=no,scrollbars=yes,status=yes");
}

function popvinho(args) {
	var ref = (args.indexOf(",") > 0) ? args.split(",")[0] : args;
	var lang = (args.indexOf(",") > 0) ? args.split(",")[1] : findLang();
	lang = (lang == "en") ? lang = "_en" : "";
	popinfo("/cgi-bin/bo/viewnews.cgi?id="+ref+"&tmpl=ficha_vinho&style=ficha_vinho"+lang,740,590);
}

//VINHOS ['ano','concurso','premio','vinho','linkvinho'],
function buidPrizeList(){
		var output = '<ul style="text-align: center; margin: 0 30px;">';
		var thisYear = 0;
		var linkString;
		if (typeof(prizeList[0])!= "string"){
			if (arguments.length){//só os do ano especificado
				if (arguments[0].indexOf(",")) arguments = arguments[0].split(",");
				for (var i in prizeList) {
					for (x=0; x<arguments.length; ++x){
						if (prizeList[i][0] == arguments[x]) {
							linkString = (prizeList[i][4]) ? '<a href="javascript: popvinho(\''+prizeList[i][4]+'\')">'+prizeList[i][3]+'</a>' : prizeList[i][3];
							output += '<li><b>'+prizeList[i][1]+', '+prizeList[i][0]+'</b><br>'+linkString+'<br>• '+prizeList[i][2]+'<br><br>';
						}
					}
				}
			} else {//os 5 últimos ordenados cronológicamente do mais recente ao mais antigo
				var y = 0;
				if (prizeList.length) {
					output += "</ul></ul><ul style='margin:0;'><font size='+1' style='line-height:30px; margin:0;'>";
					output += (findLang() == 'en') ? "The most recent awards" : "Os prémios mais recentes";
					output += "</font><ul>";
				}
				while (y < 5 && prizeList[y]) {
					linkString = (prizeList[y][4]) ? '<a href="javascript: popvinho(\''+prizeList[y][4]+'\')">'+prizeList[y][3]+'</a>' : prizeList[y][3];
					output += '<li><b>'+prizeList[y][1]+', '+prizeList[y][0]+'</b><br>'+linkString+'<br>• '+prizeList[y][2]+'<br><br>';
					++y;
				}
			}
		}
		fillLayer('LayerList', (output == '<ul style="text-align: center; margin: 0 30px;">') ? (findLang() == 'en') ? '<p style="padding-left:30; margin: 0 30px;">There is no information available</p>' : '<p style="padding-left:30; margin: 0 30px;">Não existem informações disponíveis' : output);
}

//COUDELARIA 0:categoria, 1:destaque, 2:data, 3:nome do prémio, 4:prova, 5:modalidade, 6:classe, 7:classificação, 8:animal
function buidPrizeListC(categoria,anos){
		var output = '';
		var thisYear = 0;
		var linkString;
		var titulo = '';
		if (typeof(prizeList[0])!="string"){
			if (arguments.length > 1){//só os do ano especificado
				for (var i in prizeList) {
					for (x=1; x<arguments.length; ++x){//desactivado porque só há um ano e não uma array de anos
						if ((prizeList[i][2].indexOf(arguments[x]) == 0) && categoria == prizeList[i][0]) {//é deste ano e desta categoria
							output += '<li><b>'+prizeList[i][8]+'</b><br>'+prizeList[i][4]+', '+prizeList[i][5]+' ('+prizeList[i][6]+')<br>• '+prizeList[i][3]+' - '+prizeList[i][7]+'</li><br>';
						}
					}
				}
			} else {//os 6 últimos destaques da categoria escolhida
				var y = 0;
				var i = 0;
				if (prizeList.length) titulo = "<font size='+1' style='line-height: 30px;'>Destaques de "+categoria+"</font></ul><ul>"
				while (y < 6 && prizeList[i]) {// até 6
					if (prizeList[i][1] && categoria == prizeList[i][0]) {//é destaque e desta categoria
						output += '<li><b>'+prizeList[i][8]+'</b> - '+prizeList[i][7]+'<br>• '+prizeList[i][3]+'<br>&nbsp;&nbsp;&nbsp;'+prizeList[i][4]+', '+prizeList[i][5]+' ('+prizeList[i][6]+')<br>&nbsp;&nbsp;&nbsp;'+prizeList[i][2]+'</li>';
						++y;
					}
					++i;
				}
			}
		}

		;

		fillLayer('LayerList', (output == '') ? '<p style="padding-left:30;">Não existem informações disponíveis.</p>' : '<ul style="text-align: center">'+titulo+output+'</ul>');
}

//progs ['tipo','nome','ficheiro','url'],
function showProgs(){
		progs.pop();
		progs.sort();
		var output = "<ul>";
		var thisTipo = "";
		if (typeof(progs[0])!="string"){
			for (var x in progs) {
				if (progs[x][0] != thisTipo) {
					thisTipo = progs[x][0];
					output += "</ul></ul><ul><li><font size='+1' style='line-height: 30px;'>"+thisTipo+"</font></li><ul>";
				}
				output += '<li><b>'+progs[x][1]+'</b><br><a href="'+progs[x][3]+'" target="_blank">Faça o download do seguinte documento: '+progs[x][2]+'</a><br><br>';
			}
		}
		document.write((output == "<ul>") ? '<p>Não existem programas disponíveis.</p>' : output);
}

//packages ['categoria(ref)','titulo','datas','texto resumido','imagem1','newsID'],
function showPackages(ref){
		packages.pop();
		packages.sort();
		var output = "";
		var imagem;
		var datas;
		if (typeof(packages[0])!="string"){
			for (var y in packages) {
				if (packages[y][0] == ref) {
					thisTipo = packages[y][0];
					imagem = (packages[y][4]) ? '<img src="'+packages[y][4]+'" border="1" align="left">' : '';
					datas = (packages[y][2]) ? '<br><font size="-2">('+packages[y][2]+')</font>' : '';
					output += '<table><tr><td>'+imagem+' <a href="javascript: popinfo(\'/cgi-bin/bo/viewnews.cgi?id='+packages[y][5]+'&tmpl=ficha_vinho&style=pacote_ag\',740,590)"><b><font size="+1" style="line-height=18px;">'+packages[y][1]+'</font></b>'+datas+'<br><br>'+packages[y][3]+'</a></td></tr></table><br><br>';
				}
			}
		}
		document.write((output == "") ? '<p>Não existem informações disponíveis.</p>' : output);
}

function showFlag(){
	/*if (window.location.href.indexOf('/pt/') != -1 ) {
		document.write('<a href="javascript:changeLang(\'pt\',\'en\')"><img src="../../imgs/band_en.jpg" width="19" height="14" align="absmiddle" border="0"></a>');
	} else if (location.href.indexOf('/en/') != -1 ) {
		document.write('<a href="javascript:changeLang(\'en\',\'pt\')"><img src="../../imgs/band_pt.jpg" width="19" height="14" align="absmiddle" border="0"></a>');
	}*/
}

function changeLang(nowLang,newLang){
	var prePath = self.location.href.substr(0,location.href.indexOf('/'+nowLang+'/'));//ONLY FOR HD NAVIGATION
	var postPath = self.location.href.substr(location.href.indexOf('/'+nowLang+'/')+4);
	self.location.href = prePath+"/"+newLang+"/"+postPath;
}

function findLang(){
	return ((location.href.indexOf('htmls/en/') > 0) || (location.href.indexOf('_en') > 0 && location.href.indexOf('viewnews.cgi') > 0)) ? "en" : "pt";
}

var beforeTit = '<span class="txt_titulo">';
var afterTit = '</span>';
//var ignoreThese = ' pequenacl_passeios.html pequenacl_animacao.html ';
var ignoreThese = ' pequenacl_passeios.html ';
var thisFile;

function buildMenu(ref,lang) {
	var thisTree = (lang) ? eval("tree_"+lang) : tree;
	var menuCols = "";
	var thisPage;
	for (i=0; i<thisTree.length; ++i){
		for (x=1; x<thisTree[i].length; ++x){
			if (thisTree[i][x][0] == ref) {//é este menu
				for (z=0; z<thisTree[i][x][2].length; ++z){
					if (findThisFileName().indexOf(thisTree[i][x][2][z+1]) != -1) {//é esta página
						thisPage=thisTree[i][x][2][z];
						thisFile=thisTree[i][x][2][z+1];
						menuCols += '<td><table width="50" height="52" border="0" cellpadding="0" cellspacing="0"><tr align="left" valign="top"><td width="10" height="52" align="right"><img src="../../imgs/subm_e.gif" width="10" height="52"></td><td height="52" align="center" valign="middle" nowrap background="../../imgs/subm_m.gif" class="submenuOff"><img src="../../imgs/st-2.gif"> '+thisTree[i][x][2][z]+'</td><td width="10" height="52"><img src="../../imgs/subm_d.gif" width="10" height="52"></td></tr></table></td>';
					} else {
						menuCols += '<td><table width="50" height="52" border="0" cellpadding="0" cellspacing="0"><tr align="left" valign="top"><td width="10" height="52" align="right"><img src="../../imgs/subm_e.gif" width="10" height="52"></td><td height="52" align="center" valign="middle" nowrap background="../../imgs/subm_m.gif" class="submenu"><img src="../../imgs/st-2.gif"> <a href="javascript:newPage(\''+thisTree[i][x][2][z+1]+','+findLang()+'\');">'+thisTree[i][x][2][z]+'</a></td><td width="10" height="52"><img src="../../imgs/subm_d.gif" width="10" height="52"></td></tr></table></td>';
					}
					++z;
				}
				i=100;//parar o FOR anterior
				break;//parar este FOR;
			}
		}
	}
	document.write('<table border="0" cellspacing="0" cellpadding="0"><tr>');
	document.write( (menuCols != "") ? menuCols : noMenu(lang) );
	document.write('</tr></table>');
	if (ignoreThese.indexOf(thisFile) == -1){
		(thisPage) ? document.write(beforeTit+thisPage+afterTit) : findTit(lang);
	}
	document.write('</span>');
}

function noMenu(lang) {
	var thisTree = (lang) ? eval("tree_"+lang) : tree;
	var output = "";
	for (i=0; i<thisTree.length; ++i){
		for (x=1; x<thisTree[i].length; ++x){
			for (z=0; z<thisTree[i][x][2].length; ++z){
				if (findThisFileName().indexOf(thisTree[i][x][2][z+1]) != -1) {//é este grupo
					for (y=1; y < thisTree[i].length; ++y){
						if (y == x) {//é esta página
							thisFile=thisTree[i][x][2][z+1];
							output += '<td><table width="50" height="52" border="0" cellpadding="0" cellspacing="0"><tr align="left" valign="top"><td width="10" height="52" align="right"><img src="../../imgs/subm_e.gif" width="10" height="52"></td><td height="52" align="center" valign="middle" nowrap background="../../imgs/subm_m.gif" class="submenuOff"><img src="../../imgs/st-2.gif"> '+thisTree[i][y][2][0]+'</td><td width="10" height="52"><img src="../../imgs/subm_d.gif" width="10" height="52"></td></tr></table></td>';
						} else {
							output += '<td><table width="50" height="52" border="0" cellpadding="0" cellspacing="0"><tr align="left" valign="top"><td width="10" height="52" align="right"><img src="../../imgs/subm_e.gif" width="10" height="52"></td><td height="52" align="center" valign="middle" nowrap background="../../imgs/subm_m.gif" class="submenu"><img src="../../imgs/st-2.gif"> <a href="javascript:newPage(\''+thisTree[i][y][2][1]+','+findLang()+'\');">'+thisTree[i][y][2][0]+'</a></td><td width="10" height="52"><img src="../../imgs/subm_d.gif" width="10" height="52"></td></tr></table></td>';
						}
					}
					return (output);
					i=100;//parar o FOR anterior anterior
					x=100;//parar o FOR anterior
					z=100;//parar o FOR 
				}
				++z;
			}
		}
	}
}

function findTit(lang) {
	var thisTree = (lang) ? eval("tree_"+lang) : tree;
	for (i=0; i<thisTree.length; ++i){
		for (x=1; x<thisTree[i].length; ++x){
			for (z=0; z<thisTree[i][x][2].length; ++z){
				if (findThisFileName().indexOf(thisTree[i][x][2][z+1]) != -1) {//é esta página
					document.write(beforeTit+thisTree[i][x][2][z]+afterTit);
					return;
					i=100;//parar o FOR anterior anterior
					x=100;//parar o FOR anterior
					z=100;//parar o FOR 
				}
				++z;
			}
		}
	}
}

function buildTree(lang) {
	var thisTree = (lang) ? eval("tree_"+lang) : tree;
	lang = (lang) ? lang : "pt";
	var outPut = '';
	for (i=0; i<thisTree.length; ++i){
		if (thisTree[i][0].indexOf("|") > 0) {
			outPut += '<ul><li><span  class="txt_titulo">'+thisTree[i][0].split("|")[1]+'</span></li>';
		} else {
			outPut += '<ul><li><span  class="txt_titulo">'+thisTree[i][0]+'</span></li>';
		}
		for (x=1; x<thisTree[i].length; ++x){
			outPut += '<ul>';
			if (thisTree[i][x][0]){
				outPut += '<li><span class="txt_titulosub">'+thisTree[i][x][1]+'</span></li>';
				for (z=0; z<thisTree[i][x][2].length; ++z){
					outPut += '<ul><li><a href="javascript:newPage(\''+thisTree[i][x][2][z+1]+','+lang+'\');">'+thisTree[i][x][2][z++]+'</a></li></ul>';
				}
			} else {
				outPut += '<li><a href="javascript:newPage(\''+thisTree[i][x][2][1]+','+lang+'\');">'+thisTree[i][x][1]+'</a></li>';
			}
			outPut += '</ul>';
		}
		outPut += '</ul>';
	}
	document.write(outPut);
}

function findThisFileName () {
	var thisFile = self.location.href.substr(self.location.href.lastIndexOf("/")+1);
	if (thisFile.indexOf("?") >= 0){
		thisFile = thisFile.substr(0,thisFile.indexOf("?"));
	}
	return thisFile;
}

function findFamily (ref) {
	var output = '<p align="center" class="txt_titulosub">';
	for (var i in vinhosList){
		if (ref == vinhosList[i][0]){
//			var lang = (findLang() == "en") ? "_en" : "";
			output += '<a href="javascript: popvinho(\''+vinhosList[i][2]+'\')">'+vinhosList[i][1]+'</a><br>';
		}
	}
	output += '</p>';
	fillLayer('LayerList',output);
}

function fillLayer(objName,newText) { 
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

function findPrizes(newsId){
	output = "";
	for (var i in prizeList){
		if (prizeList[i][4] == newsId){
			output += '<tr><td colspan=2 valign=top align=left>'+prizeList[i][1]+', '+prizeList[i][0]+' - <b>'+prizeList[i][2]+'</b></td></tr>';
		}
	}
	if (output != "") {
		document.write ('<tr><th colspan=2><p>');
		document.write ((findLang() == "en") ? 'Awards' : 'Prémios');
		document.write ('</p></th></tr>'+output+'</tr>');
	}
}

function getBottomLine () {
	document.write ('<p align="center"><a href="javascript:window.print();">');
	document.write ((findLang() == "en") ? 'print' : 'imprimir');
	document.write ('</a> • <a href="javascript:window.top.close()">');
	document.write ((findLang() == "en") ? 'close' : 'fechar');
	document.write ('</a></p>');
}

function showhidelayer( whichLayer ){
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

