var menuCollection = new Array(["associacao","/associacao/"],
								["associados","/associados/"],
								["galerias","/galerias/"],
								["noticias","/noticias/"],
								["contactos","/contactos/"],
								["facebook","sdfbsdfbsfdbsfdgbnfgnsfgn"]);
var selectedmenu;

function selectMenu(){
	var link = String(window.location);
	for(var i = 0; i < menuCollection.length; i++){
		if(link.indexOf(menuCollection[i][1]) != -1){
			//alert(subMenuCollection[i][j][0]+"   I:"+i+"    j:"+j+"");
			var obj = document.getElementById(menuCollection[i][0]);
			selectedmenu = obj;
			obj.src = "/images/menu/m_"+obj.id+"_on.png";
		}
	}
}

function overMenu(obj){
	obj.src = "/images/menu/m_"+obj.id+"_on.png";
}

function outMenu(obj){
	if(obj == selectedmenu){return;}
	obj.src = "/images/menu/m_"+obj.id+"_off.png";
}


var subMenuCollection = new Array(
[["missao","/associacao/default.aspx"],["historia","/associacao/historia"],["orgaos","/associacao/orgaos.aspx"],["programa","/associacao/programa.aspx"],["parcerias","/associacao/parcerias.aspx"]],
[["noticiasSub","/noticias/default.aspx"],["loyola","/noticias/loyola.aspx"]],
[["directorio","/associados/default.aspx"],["directorio","/associados/pesquisa"],["login","/associados/login.aspx"],["obter_acesso","/associados/registo.aspx"],["inscricao","/associados/inscricao.aspx"],["actualizar","/associados/actualizar.aspx"],["directorio","/associados/ficha.aspx"],["directorio","/associados/ficha_a.aspx"]],
[["galeriaSubM","/galerias/default.aspx"],["galeriaSubM","/galerias/tema.aspx"],["recordacoes","/galerias/recordacoes"],["pesquisar","/galerias/pesquisa"]]
);

function selectSubMenu(){
	var link = String(window.location);
	for(var i = 0; i < subMenuCollection.length; i++){
		for(var j = 0; j < subMenuCollection[i].length; j++){
			//alert(subMenuCollection[i][j][0]+"   I:"+i+"    j:"+j+"");
			if(link.indexOf(subMenuCollection[i][j][1]) != -1){
				//alert(subMenuCollection[i][j][0]+"   I:"+i+"    j:"+j+"");
				document.getElementById(subMenuCollection[i][j][0]).className = "txtSubMenuSelected";
				document.getElementById(subMenuCollection[i][j][0]).innerHTML = "<div class='smSelectedIcon'></div>" + document.getElementById(subMenuCollection[i][j][0]).innerHTML;
			}
		}
	}
}


function flashCanto(side, link){
	AC_FL_RunContent(
	'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
	'width', '76',
	'height', '66',
	'src', '/flash/corner',
	'quality', 'best',
	'pluginspage', 'http://www.adobe.com/go/getflashplayer_br',
	'align', 'middle',
	'play', 'true',
	'loop', 'false',
	'scale', 'showall',
	'wmode', 'transparent',
	'devicefont', 'false',
	'id', 'canto',
	'bgcolor', '#ffffff',
	'name', 'canto',
	'menu', 'false',
	'FlashVars', 'side='+side+'&link='+link,
	'allowFullScreen', 'false',
	'allowScriptAccess','sameDomain',
	'movie', '/flash/corner',
	'salign', ''
	); //end AC code
}


/* HISTORIA FLASH */
	function historiaDataProvider(xmlPath){
		var timestamp = new Date().getTime();
		(xmlPath.indexOf("?") < 0 ? xmlPath += "?timestamp=" + timestamp : xmlPath += "&timestamp=" + timestamp ) 
	
		AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
		'width', '1',
		'height', '1',
		'src', '/flash/xmlPhraser',
		'quality', 'best',
		'pluginspage', 'http://www.adobe.com/go/getflashplayer_br',
		'align', 'middle',
		'play', 'true',
		'loop', 'false',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'dataProvider',
		'bgcolor', '#ffffff',
		'name', 'dataProvider',
		'menu', 'false',
		'FlashVars', 'xmlPath='+xmlPath,
		'allowFullScreen', 'false',
		'allowScriptAccess','sameDomain',
		'movie', '/flash/xmlPhraser',
		'salign', ''
		); //end AC code
	}
	function nextPage(){
		document["dataProvider"].nextPage();
	}
	function prevPage(){
		document["dataProvider"].prevPage();
	}
/* FIM HISTORIA FLASH */


/* GALERIA FLASH */
	function galeriaTemaDataProvider(xmlPath){
		AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
		'width', '1',
		'height', '1',
		'src', '/flash/galeriaXmlPhraser',
		'quality', 'best',
		'pluginspage', 'http://www.adobe.com/go/getflashplayer_br',
		'align', 'middle',
		'play', 'true',
		'loop', 'false',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'dataProvider',
		'bgcolor', '#ffffff',
		'name', 'dataProvider',
		'menu', 'false',
		'FlashVars', 'xmlPath='+xmlPath,
		'allowFullScreen', 'false',
		'allowScriptAccess','sameDomain',
		'movie', '/flash/galeriaXmlPhraser',
		'salign', ''
		); //end AC code
	}
	function historiaTemaDataProvider(xmlPath){
		AC_FL_RunContent(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
		'width', '1',
		'height', '1',
		'src', '/flash/historiasXmlPhraser',
		'quality', 'best',
		'pluginspage', 'http://www.adobe.com/go/getflashplayer_br',
		'align', 'middle',
		'play', 'true',
		'loop', 'false',
		'scale', 'showall',
		'wmode', 'transparent',
		'devicefont', 'false',
		'id', 'dataProvider',
		'bgcolor', '#ffffff',
		'name', 'dataProvider',
		'menu', 'false',
		'FlashVars', 'xmlPath='+xmlPath,
		'allowFullScreen', 'false',
		'allowScriptAccess','sameDomain',
		'movie', '/flash/historiasXmlPhraser',
		'salign', ''
		); //end AC code
	}
/* FIM GALERIA FLASH */



/*FRAME FUNCTIONS*/
	function showFrameSmall(imgSrc){
		document.write("<img src='"+imgSrc+"' width='146' height='108' />");
	}
	function showFrameSmall2(imgSrc){
		if(imgSrc == ""){imgSrc = "/images/common/no_pic_small.png"}
		document.write("<img src='"+imgSrc+"' width='80' height='100' />");
	}
	function showFrameBig(imgSrc){
		if(imgSrc == ""){imgSrc = "/images/common/no_pic_big.png"}
		document.write("<img src='"+imgSrc+"' width='167' height='208' />");
	}
/* END FRAME FUNCTIONS */


/* CUSTOM COMBOS FUNCTIONS */
var COMBO_ID;
var TEXTBOX_ID;
var HIDDEN_ID;
var comboActiveItem;
var openedCombo;

function showCombo(cboId, divId, hdnId){
	COMBO_ID = cboId;
	TEXTBOX_ID = divId;
	HIDDEN_ID = hdnId;
	
	//show combo
	var container = document.getElementById(COMBO_ID);
	var textbox = document.getElementById(TEXTBOX_ID);
	
	if(container.style.display == 'block'){container.style.display = 'none';openedCombo = null;return;}
	if(openedCombo != null){openedCombo.style.display = 'none';}
	
	container.style.left = getPosition(textbox).x;
	container.style.display = 'block';
	openedCombo = container;
	container.focus();
}

function sendToTextbox(selectedOption){
	var combo = document.getElementById(COMBO_ID);
	var textbox = document.getElementById(TEXTBOX_ID);
	var hidden = document.getElementById(HIDDEN_ID);

	textbox.innerHTML = selectedOption[selectedOption.selectedIndex].text;
	hidden.value = selectedOption[selectedOption.selectedIndex].value;
	combo.style.display='none';	
}

//function used in the COMBO
function keyupCombo(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	var combo = document.getElementById(COMBO_ID);
	var textbox = document.getElementById(TEXTBOX_ID);
	if ((code==38) && (comboActiveItem==0)) { //up arrow pressed
		combo.selectedIndex = -1; //remove highlight from combo
	}
	
	if ((code==13) && (combo.selectedIndex > -1)) {
		textbox.innerHTML = combo.options[combo.selectedIndex].value;
		combo.style.display='none';
	}
}

//function used in the COMBO
function keydownCombo(obj) {
	comboActiveItem=obj.selectedIndex;
}

		/* GET TEXTFIELD POSITION ADDED BY EJR 19/05/2010*/
  			function comboPositionHandler(){
		  		var textbox = document.getElementById(TEXTBOX_ID);
		  		var container = document.getElementById(COMBO_ID);
		  		
		  		//container.style.left = getPosition(textbox).x; //old version
		  		if (container!=null) container.style.left = getPosition(textbox).x; //new version
		
		  	}
		  	
  			function getPosition(obj){   
				var left = 0;
				var top  = 0;
			
				while (obj.offsetParent){   
					left += obj.offsetLeft;
					top  += obj.offsetTop;
					obj     = obj.offsetParent;
				}   
			
				left += obj.offsetLeft - 4;   
				top  += obj.offsetTop;   
				return {x:left, y:top};   
			 }
			 
			 window.onresize = comboPositionHandler;
  		/* END GET TEXTFIELD POSITION */



/* NOTICIAS AJAX */

/* AJAX SCRIPT RUNNER */
var codeToRun;
var scriptRunnerTimerId;

function runScript(){
	clearTimeout(scriptRunnerTimerId);
	eval(codeToRun);
}
function scriptFilter(htmlString){

	if(String(htmlString).indexOf("<script>") != -1 && String(htmlString).indexOf("</script>") != -1){
		var initPointer = String(htmlString).indexOf("<script>");
		var endPointer = String(htmlString).indexOf("</script>");
		
		var scriptCode = String(htmlString).substr(initPointer + 8, endPointer);
		scriptCode = String(scriptCode).substr(0, String(scriptCode).indexOf("</script>"));
		
		codeToRun = scriptCode;
		scriptRunnerTimerId = setTimeout('runScript()', 50);
		
		var finalHtml = String(htmlString).substr(0, initPointer)+""+String(htmlString).substr(endPointer+9, String(htmlString).length);
		//alert(finalHtml +"   CODE: "+scriptCode);
		return finalHtml;
	}
	
	return htmlString;
}
/* END AJAX SCRIPT RUNNER */

var layerContentContainer;

function ajaxGetContents(url,container){
	layerContentContainer = document.getElementById(container);
	
	if(url.indexOf("?") != -1){url = url+"&v="+Math.random();}
	else{url = url+"?v="+Math.random();}
		
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET",url,true);
		req.send(null);
	}
		// Procura por uma versão ActiveX (IE)
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req)
		{
			req.onreadystatechange = processReqChange;
			req.open("GET",url,true);
			req.send();
		}
	}
}

function processReqChange(){
	// apenas quando o estado for "completado"
	if (req.readyState == 4)
	{
		// apenas se o servidor retornar "OK"
		if (req.status ==200)
		{
			// procura pela div id="pagina" e insere o conteudo
			// retornado nela, como texto HTML
			layerContentContainer.innerHTML = scriptFilter(req.responseText);
		}
		else
		{
			layerContentContainer.innerHTML = "<div class='txtNoticiasFicha' style='width:100%;height:300;text-align:center;'>Ocorreu um erro pedimos desculpa pelo incómodo causado.</div>";
		}
	}
	if(req.readyState == 1){
		layerContentContainer.innerHTML = "<div style='width:100%;height:300;text-align:center;padding:100 0 0 0;'><img src='/images/common/ajaxLoading.gif' alt='A Carregar'/></div>";
	}
}

// returns value of checked radio button
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
/*
ESTA A DAR CONFLITOS COM O SELECTITEM ver o que se passa.
*/
function getFichaNoticiaUrl(){
	var stringMatch = String("idficha=");
	var id;
	var index = String(window.location).indexOf(stringMatch);
	
	if (index != -1) {
        id = String(window.location).substring(index + stringMatch.length, String(window.location).length);
        
        ajaxGetContents('ficha.aspx?id='+id,'fichaNoticia');
        selectItem(id);
    }
}
