// Abre popups da galeria
function pop( pronde ){
	window.open( pronde , "popup" , "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=468,height=550" ) ;
}

// Função que escreve o código dos contextual ads
function ca_show(){
	var ca_content = "<p class=\"adsKicker\">PUBLICIDADE / LINKS PATROCINADOS<\/p>\n" ;
	ca_content += "<div class=\"contextualAds\">\n" ;

	for( var i = 0 ; i < TR_array.length ; i++ ){
		ca_content += "<p>\n" ;
		ca_content += "<a href=\"" + TR_array[i][0] + "\" target=\"_blank\">" + TR_array[i][2] + "<\/a><br>\n" ;
		ca_content += TR_array[i][3] + "<br>\n" ;
		ca_content += TR_array[i][1] ;
		ca_content += "<\/p>" ;
 	}

	ca_content += "<p class=\"ca1l\">\n" ;
	ca_content += "<a href=\"http://redir.folha.com.br/redir/ads/contextual/*http://br.yahoo.com/info/overture/2/overture_pm_landing_1.html\" target=\"_blank\">Apareça aqui!<\/a>\n" ;
	ca_content += "<\/p>\n" ;

	ca_content += "<\/div>\n" ;

	if( document.getElementById ){
		var x = document.getElementById( "contextualAds" ) ;
		x.innerHTML = ca_content ;
	}
}

// Função usada para "encodar" urls
function url_encode( str ) {
	var encoded = "" ;
	var valid_uri_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.+!*'()," ;
	var hex_chars = "0123456789ABCDEF" ;
	for ( var i = 0 ; i < str.length ; i++ ) {
		if ( valid_uri_chars.indexOf( str.charAt( i ) ) == -1 ){
			encoded += "%" + hex_chars.charAt( str.charCodeAt( i ) >> 4 ) + hex_chars.charAt( str.charCodeAt( i ) & 0xF ) ;
		}
		else{
			encoded += str.charAt( i ) ;
		}
	}
	return encoded ;
}

// Retorna um número randômico de 10 digitos
function rand(){
	var r = new String( Math.round( Math.random()*100000000 ) ) ;
	while( r.length < 10 ){ r = "0" + r ; }
	return r ;
}

// Função que escreve o código das peças publicitárias
var folha_ads_chan = "" ;
function folha_ads_show( site , size , expandable ) {
	var rand = Math.floor( 100000000 * Math.random() ) ;
	var position , width , height ;
	folha_ads_chan = site ;
	if ( expandable != "1" ){ expandable = 0 ; }
	if ( size == "468x60" ){ position = "1" ; width = "468" ; height = "60" ; }
	if ( size == "120x30" ){ position = "1" ; width = "120" ; height = "30" ; }
	if ( size == "120x60" ){ position = "3" ; width = "120" ; height = "60" ; }
	if ( size == "120x600" ){ position = "6" ; width = "120" ; height = "600" ; }
	if ( size == "180x150" ){ position = "7" ; width = "180" ; height = "150" ; }
	if ( size == "300x250" ){ position = "0" ; width = "1" ; height = "1" ; }
	if ( size == "300x40" ){ position = "5" ; width = "300" ; height = "40" ; }
	if ( size == "120x600-2" ){ size = "120x600" ; position = "5" ; width = "120" ; height = "600" ; }
	if ( size == "1x1-2" ){ size = "1x1" ; position = "8" ; width = "1" ; height = "1" ; }
	if ( size == "300x250-2" ){ size = "300x250" ; position = "8" ; width = "1" ; height = "1" ; }
	if ( size == "1x1" ){ position = "11" ; width = "1" ; height = "1" ; }
	document.write( "<scr" + "ipt language=\"javascript1.1\" type=\"text/javascript\" src=\"http://de.uol.com.br/js.ng/site=folha&amp;chan=" + site + "&amp;size=" + size + "&amp;page=" + position + "&amp;expble=" + expandable + "&amp;ord=" + rand + "?\"><\/scr" + "ipt>" ) ;
}

// Chamada à função que escreve o código da publicidade com controle de exposição de uma vez por sessão
function folha_ads_show_once( site , size , expandable ) {
	window.onError = null ;
	var cookie_name = site.replace( "." , "_" ) + "-" + size ;
	var popup = document.cookie.indexOf( cookie_name ) ;
	if( popup == -1 ){
		folha_ads_show( site , size , expandable ) ;
		document.cookie = cookie_name + "=1" ;
	}
}

// Função de verificação do plugin flash
function flashPlugin( plugin_version ){
	var default_version = 5 ;
	var version ;
	if( plugin_version.length == "" ){
		version = new Number( default_version ) ;
	}
	else{
		version = new Number( plugin_version ) ;
	}
	var agent = navigator.userAgent.toLowerCase() ;
	var plugin = new String() ;
	var plugin_description = new String() ;
	if ( navigator.plugins && navigator.plugins.length ){
		for ( var i = 0 ; i < navigator.plugins.length ; i++ ){
			plugin = navigator.plugins[i] ;
			plugin_description = plugin.description.split( " " ) ;
			if ( plugin_description[0] == "Shockwave" && plugin_description[1] == "Flash" && plugin_description[2].split( "." )[0] >= version.valueOf() ){
	            return true ;
	        }
	        else{
		        for ( var j = 0 ; j < plugin.length ; j++ ){
		        	if ( plugin[j].type == "application/x-shockwave-flash"){
		        		return true ;
		        	}
		        }
		    }
		}
		return false ;
	}
	else if ( navigator.mimeTypes && navigator.mimeTypes.length ){
		var detectPlugin = navigator.mimeTypes[ "application/x-shockwave-flash" ] ;
		if( detectPlugin && detectPlugin.enabledPlugin ){
			return true ;
		}
		else{
			return false ;
		}
	}
	else if ( agent.indexOf( "msie" ) != -1 && parseInt( navigator.appVersion , 10 ) >= 4 ){
		document.write( "<scr" + "ipt language=\"VBScript\"> \n") ;
		document.write( "on error resume next \n" ) ;
		document.write( "dim plugin \n" ) ;
		document.write( "dim IEDetect \n" ) ;
		document.write( "set plugin = CreateObject(\"ShockwaveFlash.ShockwaveFlash." + version + "\") \n" ) ;
		document.write( "if IsObject( plugin ) then \n" ) ;
		document.write( "IEDetect = true \n" ) ;
		document.write( "else IEDetect = false \n" ) ;
		document.write( "end if" ) ;
		document.write( "<\/scr" + "ipt> \n") ;
		if( IEDetect ){
			return true ;
		}
	}
	return false ;
}

// Função para inserção local de peças em flash
function folha_flash_object( object_source , width , height , object_href , info , plugin_version ){
	if ( !flashPlugin( plugin_version ) ){ return ; }
	var html = "" ;
	html += "<p style=\"padding: 0; margin: 0 0 10px 0; width: " + width + "; height: " + height + "\">" ;
	if ( object_href.length > 0 ){
		 object_source += "?clicktag=" + object_href ;
	}
	if ( info.length > 0 ){
		html += "<span style=\"font: bold 10px arial;\">" + info + "<\/span><br>" ;
	}
	if ( navigator.appName.indexOf( "Internet Explorer" ) > -1 ){
		html += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=" + plugin_version + ",0,0,0\" width=\"" + width + "\" height=\"" + height + "\">" ;
		html += "<param name=\"movie\" value=\"" + object_source + "\">" ;
		html += "<param name=\"quality\" value=\"high\">" ;
	}
	else{
		html += "<object type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\" data=\"" + object_source + "\">" ;
	}
	html += "<\/object>" ;
	html += "<\/p>" ;
	document.write( html ) ;
}

// Função genérica para navegação que utilize tabs
function show_tab( obj_id ){
	if ( !document.getElementById ){
		return ;
	}
	var i , obj , parent_node ;
	if ( document.getElementById( obj_id ) ){
		obj = document.getElementById( obj_id ) ;
		obj_list = obj.parentNode.getElementsByTagName( obj.tagName ) ;
		for ( i = 0 ; i < obj_list.length ; i++ ){
			obj_list.item(i).style.display = "none" ;
		}
		obj.style.display = "block" ;
	}
}

/* Trecho referente ao controle do player */
var onload_manager_tmp = "" ;
var render_player_before_onload = true ;
var replaced_onload = window.onload ;

if ( flashPlugin( "7" ) && document.getElementById ){
	onload_manager_tmp = onload_manager ;
}
else{
	onload_manager_tmp = replaced_onload ;
}

window.onload = onload_manager_tmp ;

function onload_manager(){
	if ( render_player_before_onload ){
		render_player() ;
	}
	if ( replaced_onload ){
		replaced_onload() ;
	}
	if ( !render_player_before_onload ){
		render_player ;
	}
}

function render_player(){
	var elements_list = document.getElementsByTagName( "a" ) ;
	var player_path = "http://www1.folha.uol.com.br/folha/furniture/3/images/player-2.swf" ;
	var objs = new Array() ;
	for ( var index = 0 , limit = elements_list.length ; index < limit ; index++ ){
		if ( elements_list[index].className == "play" ){
			objs[objs.length] = elements_list[index] ;
		}
	}
	for ( var index = 0 , limit = objs.length ; index < limit ; index++ ){
		var url = objs[index].getAttribute( "href" ) ;
		var text = "" ;
		if ( objs[index].childNodes.length ){
			if ( objs[index].childNodes[0].data ){
				text = objs[index].childNodes[0].data ;
			}
			else if ( objs[index].childNodes[0].innerHTML ){
				text = objs[index].childNodes[0].innerHTML ;
			}
		}
		if ( url != null ){
			var flashvars = "url=" + escape( url ) + "&description=" + decompose( text ) ;
			var tag = "" ;
			if ( navigator.appName.indexOf( "Internet Explorer" ) > -1 ){
				tag += "<object class=\"xpto\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"470\" height=\"74\" id=\"player\" align=\"middle\">" ;
				tag += "<param name=\"movie\" value=\"" + player_path + "\">" ;
				tag += "<param name=\"allowScriptAccess\" value=\"sameDomain\">" ;
				tag += "<param name=\"quality\" value=\"high\">" ;
				tag += "<param name=\"flashvars\" value=\"" + flashvars + "\">" ;
				// IE em mac possui bugs na renderização de tags object; deve-se usar embed nesse caso
				if ( is_ie_mac() ){
					tag += "<embed type=\"application/x-shockwave-flash\" width=\"470\" height=\"74\" id=\"player\" src=\"" + player_path + "\" flashvars=\"" + flashvars + "\"></embed>" ;
				}
			}
			else{
				tag += "<object type=\"application/x-shockwave-flash\" width=\"470\" height=\"74\" data=\"" + player_path + "\" flashvars=\"" + flashvars + "\" id=\"player\">" ;
			}
			tag += "<\/object>" ;
			var parent_node = objs[index].parentNode ;
			var my_element = document.createElement( "div" ) ;
			my_element.className = "player" ;
			my_element.innerHTML = tag ;
			parent_node.replaceChild( my_element , objs[index] ) ;
		}
	}
}

function is_ie_mac(){
	return ( navigator.platform.toLowerCase().indexOf("mac") > -1 ) && ( navigator.appName.indexOf( "Internet Explorer" ) > -1 ) ;
}

/* O flash se atrapalha ao converter texto acentuado por problemas de charset.
O texto enviado ao flash então é enviado como códigos decimais de charset. */
function decompose( s ){
	var str = "" , val ;
	for( var i = 0; i < s.length; ++i ){
		val = s.charCodeAt(i).toString() ;
		while ( val.length < 3 ){
			val = "0" + val ;
		}
		str += val ;
	}
	return str ;
}
/* /Trecho referente ao controle do player */
