/*
  Global variables
 */

var
	searchPrefix = "http://pesquisa.sapo.mz/sHP?barra=noticias&t=&q=",
	selectedChartView = "day",
	commentNews,
	ajaxOpts,
	curmenu = null,
	mrecTag = null;


/*
  Include some external stuff
 */

document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/browser.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/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();
		}
	} );

}



/*
  Submenus
 */

function showpm ( el ) {

	var
		subel;

	for ( var x = 0 ; x < el.childNodes.length ; x++ )
		if ( (subel = el.childNodes[x]).className == "popup" )
			_showpm(el,subel);

}

function _showpm ( pel, el ) {

	pel.onmouseout = function(){_showpm(pel,null)}
	if ( curmenu )
		curmenu.style.visibility = 'hidden';

	if ( el ) {
		el.style.visibility = 'visible';
		curmenu = el;
	}
 
}


/*
  Simulate a site interaction
 */

function siteInteraction ( affiliate ) {

        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

        if ( mrec ) {
                if ( mrecTag == null ) mrecTag = mrec.src;
                if ( affiliate == null )
                        mrec.src = mrecTag;
                else
                        mrec.src = mrecTag+"&affiliate="+affiliate;
        }
   
        // Hit Netscope
   
        if ( typeof(w_counter) != 'undefined' )
                w_counter.count();
        if ( typeof(nsAccounts) != 'undefined' ) {  
                for ( var x = 0 ; x < nsAccounts.length ; x++ ) {
                        if ( nsAccounts[x].obj ) nsAccounts[x].obj.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;

}



/*
  Search tabs
 */


// Swap a tab

function searchTagSwap ( tab, noFocus ) {

	var
		prefixes = {
			"tabnot": "http://pesquisa.sapo.mz/sHP?channel=noticias&barra=noticias&t=&q=",
			"tabweb": "http://pesquisa.sapo.mz/search?st=&barra=resumo&q=",
			"tabimg": "http://pesquisa.sapo.mz/search?st=.pt&barra=imagens&q=",
			"tabprd": "http://pesquisa.sapo.mz/shopping?q=",
			"tablog": "http://pesquisa.sapo.mz/sHP?barra=blogs&q=",
			"tabpai": "http://pesquisa.sapo.mz/sHP?location=cv&channel=pai&barra=pai&t=&q=",
			"tabdir": "http://pesquisa.sapo.mz/search?barra=directorio&channel=directorio&location=cv&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);

}



/*
  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+"/";

}



// Select a municipality on the municipality widget

function selectMun ( mun ) {

	var
		box;

	// Get municipality select box

	if ( (box = (document.getElementsByName('pickconcelho'))[0]) == null )
		return;

	for ( var x = 0 ; box.options[x] ; x++ ) {
		if ( box.options[x].value == mun ) {
			box.options[x].selected = true;
			break;
		}
	}

}

// Get municipality widget

function showMunList ( selectmun ) {

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

	widDiv.id = "concelho_formlocal";

	// Show municipalities on list

	getAndShowFileOn("/xml/municipalities.html",'concelho_formlocal',function() { if(selectmun) selectMun(selectmun); 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/NOTICIASMZ/2.0.9/elements.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/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/NOTICIASMZ/2.0.9/utils.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/swfobject.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/vlw.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/sapocarrossel.js\"></script>");
document.write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://imgs.sapo.pt/js/NOTICIASMZ/2.0.9/comments.js\"></script>");

