/*
  Global variables
 */

var
	searchPrefix = "http://pesquisa.sapo.ao/sHP?channel=noticias&barra=noticias&t=&q=",
	selectedChartView = "day",
	commentNews,
	ajaxOpts;


/*
  Include some external stuff
 */

document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/browser.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/page.js\"></script>");



/*
  Common functions
 */

function goHome ( ) {
	location.href = '/';
}

function minWidth ( width ) {
	return this.width < width ? width : this.width;
}

function minHeight ( height ) {
	return this.height < height ? height : this.height;
}

function maxWidth ( width ) {
	return this.width > width ? width : this.width;
}

function maxHeight ( height ) {
	return this.height > height ? height : this.height;
}

function getAndShowFileOn ( url, elementId, onCompleteHandler ) {

	return new Ajax.Request ( url, {
		method: 'get',
		parameters: null,
		encoding: 'UTF-8',
		onComplete: function (response) {
			document.getElementById(elementId).innerHTML = response.responseText;
			if ( onCompleteHandler != null )
				onCompleteHandler();
		}
	} );

}



/*
  Search tabs
 */


// Swap a tab

function searchTagSwap ( tab, noFocus ) {

	var
		prefixes = {
			"tabnot": "http://pesquisa.sapo.ao/sHP?channel=noticias&barra=noticias&t=&q=",
			"tabweb": "http://pesquisa.sapo.ao/search?st=&barra=resumo&q=",
			"tabimg": "http://pesquisa.sapo.ao/search?st=.pt&barra=imagens&q=",
			"tabprd": "http://pesquisa.sapo.ao/shopping?q=",
			"tablog": "http://pesquisa.sapo.ao/sHP?barra=blogs&q=",
			"tabpai": "http://pesquisa.sapo.ao/sHP?location=ao&channel=pai&barra=pai&t=&q=",
			"tabdir": "http://pesquisa.sapo.ao/search?barra=directorio&channel=directorio&location=ao&q="
		},
		lis = document.getElementsByTagName("li"),
		li;

	for ( tabName in prefixes ) {
		li = document.getElementById(tabName);
		if ( (li != null) && (tabName != tab) )
			li.className = "";
	}

	document.getElementById(tab).className = "active";
	searchPrefix = prefixes[tab];
	if ( !noFocus )
		document.getElementById("q").focus();


}


// Perform search

function performSearch ( ) {

	var
		qTextbox = document.getElementById('q');

	location.href = searchPrefix + qTextbox.value;

	return false;	// Skip submit

}


// Initialize tabs

function initTabs ( ) {

	// Defaults to tabnot

	searchTagSwap("tabnot",1);

}


/*
  Simulate a site interaction
 */

function siteInteraction ( ) {

	var
		mrec;

	// Identify MREC

	if ( ((mrec = document.getElementById('mrec')) == null) && ((mrec = document.getElementById('pub300x250')) == null) )
		return true;
	if ( mrec.nodeName.toLowerCase() == 'div' )
		mrec = (mrec.getElementsByTagName("IFRAME"))[0];

	// Refresh MREC

	mrec.src = mrec.src;

	// Hit Netscope

	if ( typeof(w_counter) != 'undefined' )
		w_counter.count();
        
	return true;
 
}


/*
  Show MREC
*/

function showMREC() { siteInteraction(); return setMRECVisibility('visible'); }
function hideMREC() { return setMRECVisibility('hidden'); }
function setMRECVisibility(value) {

	var 
		mrec = null;

	// Identify MREC

	if ( ((mrec = document.getElementById('mrec')) == null) && ((mrec = document.getElementById('pub300x250')) == null) )
		return true;

	// Set visibility

	mrec.style.visibility = value;

}


/*
  Newspapers functions
 */

function loadNewspapers ( ) {

	return getAndShowFileOn("/banca/__column_left.html","contentBancaLeft") && getAndShowFileOn("/banca/__column_right.html","contentBancaRight");

}

function np ( file, title ) {

	lw('/banca/'+file+'.html',(title?'<a href="/banca/" onClick="myLightWindow.inactivate(); return(false)">Banca de Jornais</a> &gt; '+title:''),'lightwindow_width=670,lightwindow_height=490');
	return false;

}



/*
  Local functions
 */


// Go to a specific municipality (called from flash)

function gotoConcelho ( code ) {

	location.href = "/local/"+code+"/";

}


// Get municipality widget

function showMunList ( ) {

	var
		widget = document.getElementById('boxConcelho'),
		commands = locChildWithClass(widget,'commands_sapo') || locChildWithClass(widget,'commands'),
		widDiv = locChildWithClass(widget,'formlocal');

	widDiv.id = "concelho_formlocal";
	return getAndShowFileOn("/xml/municipalities.html",'concelho_formlocal',function() { commands.style.display = "none"; widDiv.style.display = "block"; } );

}


// Hide municipalitites list

function hideMunList ( ) {

	var
		widget = document.getElementById('boxConcelho'),
		commands = locChildWithClass(widget,'commands_sapo') || locChildWithClass(widget,'commands'),
		widDiv = locChildWithClass(widget,'formlocal');

	widDiv.innerHTML = "";
	commands.style.display = "block";
	widDiv.style.display = "none";

}


// Set municipality

function setMunicipality ( ) {

	var
		obj = (document.getElementsByName("pickconcelho"))[0];

	if ( obj == null )
		return null;

	location.href = '/local/'+obj.value+'/';

}



/*
  Weather functions
 */

function _showWeatherLocals ( code ) {

	var
		widget = document.getElementById('boxWeather'),
		widDiv = locChildWithClass(widget,'footer');

	widDiv.innerHTML = code;

}

function showWeatherLocals ( ) {

	return getMunicipalities(_showWeatherLocals);

}




/*
  Reuters quotes
 */


// Change quote view

function chQuoteView ( quote, type ) {

	var
		curChart = document.getElementById(selectedChartView+'Chart'),
		newChart = document.getElementById(type+'Chart'),
		curChartSel = document.getElementById(selectedChartView+'ChartSel'),
		newChartSel = document.getElementById(type+'ChartSel');

	if ( curChart == null || newChart == null )
		return;

	curChart.style.display = "none";
	newChart.style.display = "block";
	curChartSel.className = "";
	newChartSel.className = "active";

	selectedChartView = type;

}




/*
  Newsletter
 */


// Subscribe newsletter

function subscribeNL ( ) {

	var
		addrBox = (document.getElementsByName("mailaddress"))[0],
		widDiv = locParWithClass(addrBox,"snBoxContent"),
		synd = new SAPO.Communication.Syndication(),
		nlSubId;


	// Subscribe to newsletter via syndication

	nlSubId = synd.push('/xml/newsletter.html?addr='+addrBox.value, {
		timeout: 	10,

		onComplete:	function (nl,widDiv) {
			if ( nl.result == 'ok' )
				widDiv.innerHTML = "<p id=\"ok\">Obrigado por ter subscrito a nossa newsletter.</p>";
			else
				document.getElementById('nlerror').innerHTML = "Erro: "+nl.message+".";
		},
		optOnComplete:	widDiv,

		onTimeout:	function (widDiv) { document.getElementById('nlerror').innerHTML = "Erro: O pedido excedeu o tempo."; },
		optOnTimeout:	widDiv
	} );
	synd.run(nlSubId);

}



/*
  Initialize site
 */

function init () {

	// Tabs

	initTabs();

	// Correct PNG's for IE

	if ( browser.isIE() )
		correctPNG();

}



/*
  Include some external stuff
 */

document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/elements.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/pngfix.js\"></script>");

document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://js.sapo.pt/SAPO/0.1/\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://js.sapo.pt/SAPO/Exception/0.1/\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://js.sapo.pt/SAPO/Utility/Url/0.1/\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://js.sapo.pt/SAPO/Utility/Crypto/0.1/\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://js.sapo.pt/SAPO/Communication/Syndication/0.1/\"></script>");

document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://js.sapo.pt/Prototype/1.8/\"></script>");

document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/utils.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/swfobject.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/vlw.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/sapocarrossel.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASAO/comments.js\"></script>");

