var listaValores=Array();
var contUVL=0;
var listaNoticias=Array();
var contNoticia=0;
var posNoticia=0;
var listaIndice=Array();
var listaDivisa=Array();
var bufferDivisa=Array();
var bufferIndice=Array();
var contDivisa=0;
var contIndice=0;
var alphaNoticia=0;
var timerAlphaNoticia=null;
var alturaNoticias=-100;

var IE = navigator.appName.toUpperCase().indexOf('MICROSOFT') >=0;


function numFormat(num,dec, miles)
{
/*
var signo=3, expr;
var cad = ""+num;
var ceros = "", pos, pdec, i;
for (i=0; i < dec; i++)
ceros += '0';
pos = cad.indexOf('.')
if (pos < 0)
    cad = cad+"."+ceros;
else
    {
    pdec = cad.length - pos -1;
    if (pdec <= dec)
        {
        for (i=0; i< (dec-pdec); i++)
            cad += '0';
        }
    else
        {
        num = num*Math.pow(10, dec);
        num = Math.round(num);
        num = num/Math.pow(10, dec);
        cad = new String(num);
        }
    }
pos = cad.indexOf('.')
if (pos < 0) pos = cad.lentgh
if (cad.substr(0,1)=='-' || cad.substr(0,1) == '+')
       signo = 4;
if (miles && pos > signo)
    do{
*/ 
	num=num.replace(/\./,',');
  var expr = /([+-]?\d)(\d{3}[\.\,]\d*)/
  num.match(expr)
  var cad=num.replace(expr, RegExp.$1+'.'+RegExp.$2)
/*        }
while (cad.indexOf(',') > signo)
    if (dec<0) cad = cad.replace(/\./,'')
        return cad;
*/
return cad;
}


function formatoNumero(valor)
{
	//var $aux=split(
	//return(valor.replace(".",","));
	return(numFormat(valor,2,true));
	
}

function verNoticia(id)
{
	clearInterval(timerNoticia);
//	mostrarCapa('noticia');
//	document.getElementById('cuerponoticias').style.visibility='visible';
//	document.getElementById('cuerponoticias').innerHTML="<center><img src='imagenes/progress.gif'></center>";
//	document.getElementById('divnoticias').style.height='auto';
	for (var i=0;i<listaNoticias.length;i++)
	{
		if (i!=id)
		{
		document.getElementById('titular'+i).style.height='0px';
		document.getElementById('noticia'+i).style.height='0px';
		}
	}
	document.getElementById('noticia'+id).innerHTML="<center><img src='imagenes/progress.gif'></center>";
	llamarasincrono(listaNoticias[id].enlace,'noticia'+id);
	document.getElementById('cuerponoticias').style.height='auto';
	document.getElementById('cuerponoticias').style.minHeight='130px';
	document.getElementById('tablanoticias').style.marginTop='0px';
}

function ocultaNoticia()
{
	document.getElementById('cuerponoticias').style.height='146px';
	showNoticia(contNoticia);
	timerNoticia=setInterval("refrescaNoticia();",100);
	//document.getElementById('divnoticias').style.height='120px';
//	document.getElementById('cuerponoticias').style.visibility='hidden';
//	document.getElementById('cuerponoticias').innerHTML='';
}

function imprimir(capa)
{
	var ventana = window.open("", "", "");
	var contenido = "<html><body onload='window.print();window.close();'>" + document.getElementById(capa).innerHTML + "</body></html>";
	ventana.document.open();
	ventana.document.write(contenido);
	ventana.document.close();
}

function tdivisa(nombre, valor,diario,mensual,tipo)
{
	this.nombre=nombre;
	this.valor=valor;
	this.diario=diario;
	this.mensual=mensual;
	this.tipo=tipo;
}

function tindice(nombre,valor,diario,anual,tipo)
{
	this.nombre=nombre;
	this.valor=valor;
	this.diario=diario;
	this.anual=anual;
	this.tipo=tipo;
}

function addDivisa(nombre, valor,diario,mensual,tipo){
	var aux=new tdivisa(nombre,valor,diario,mensual,tipo);
	listaDivisa.push(aux);
}

function addIndice(nombre, valor,diario,anual,tipo){
	var aux=new tindice(nombre,valor,diario,anual,tipo);
	listaIndice.push(aux);
}

function uvl(valor,nombre,isin,participes,patrimonio,fecha)
{
	this.valor=valor;
	this.nombre=nombre;
	this.isin=isin;
	this.participes=participes;
	this.patrimonio=patrimonio;
	this.fecha=fecha;
	this.show=function(){
	
		document.getElementById('prueba').innerHTML+=listaValores[i]+"<br/>";

	}
}

function noticia(texto,enlace)
{
	this.texto=texto;
	this.enlace=enlace;
}

function addNoticia(texto,enlace)
{
	var a=new noticia(texto,enlace);
	listaNoticias.push(a);
}
/*
function mueveNoticia()
{
	if (posXNoticia<100)
	{
		posXNoticia++;
		document.getElementById('divnoticias').style.margin=posXNoticia+"px";
	}
}
*/
function addValor(valor,nombre,isin,participes,patrimonio,fecha)
{
	var a=new uvl(valor,nombre,isin,participes,patrimonio,fecha);
	listaValores.push(a);
}

function showValor(indice)
{
	document.getElementById('titulouvl').innerHTML=listaValores[indice].nombre;
	document.getElementById('isinuvl').innerHTML=listaValores[indice].isin;
	document.getElementById('valoruvl').innerHTML=listaValores[indice].valor;
	document.getElementById('fechauvl').innerHTML="Actualizado a "+listaValores[indice].fecha;
	document.getElementById('participesuvl').innerHTML=listaValores[indice].participes;
	document.getElementById('patrimoniouvl').innerHTML=listaValores[indice].patrimonio;

}

function showNoticia()
{
var elHTML='';
	for (i=0;i<listaNoticias.length;i++)
	{
	elHTML+="<div id='titular"+i+"' style='height:45px;overflow:hidden;'>"+listaNoticias[i].texto;
	elHTML+="<a href=\"javascript:verNoticia('"+i+"');\">[Leer completo]</a></div>";
	elHTML+="<div id='noticia"+i+"' style='overflow:hidden;'>&nbsp;</div>\n";
	}
//	alert(document.getElementById("divnoticias").style.height);
	document.getElementById("divnoticias").innerHTML=elHTML;
	alturaNoticias=(i+1)*45;
//	document.getElementById('cuerponoticias').innerHTML="<a href=\"javascript:verNoticia('"+listaNoticias[indice].enlace+"');\">[Leer completo]</a>";
	//alert(alturaNoticias);
}

function refrescaNoticia()
{
//mover la noticia actual
//cargar la nueva
//volver a posicionar
//var posact=0;
/*
timerMueve=setInterval("mueveNoticia();",300);

while (posXNoticia<100)
{
//espera
}
clearInterval(timerMueve);
*/
//clearInterval(timerNoticia);
//timerAlphaNoticia=setInterval("cambioNoticia();",10);
document.getElementById("tablanoticias").style.marginTop=posNoticia+"px";
if ((-1*posNoticia)>alturaNoticias)
{
	posNoticia=120;
}
posNoticia--;
}
/*
function cambioNoticia()
{
	alphaNoticia=alphaNoticia+10;
	if (!IE)
	{
	document.getElementById('divnoticias').style.opacity=1-(alphaNoticia/100);
	}else{
	document.getElementById('divnoticias').style.filter="alpha(style=0, opacity="+(100-alphaNoticia)+")";
	}
	if (alphaNoticia>100)
	{
		clearInterval(timerAlphaNoticia);
		alphaNoticia=0;
		if (!IE)
		{
			document.getElementById('divnoticias').style.opacity=1;
		}else{
			document.getElementById('divnoticias').style.filter="alpha(style=0, opacity=100)";
		}
		showNoticia(contNoticia);
		contNoticia++;
		if (contNoticia==listaNoticias.length)
		{
			contNoticia=0;	
		}
		timerNoticia=setInterval("refrescaNoticia();",8000);
	}

}
*/
/*
function refrescaUVL()
{
	showValor(contUVL);
	contUVL++;
	if (contUVL==listaValores.length)
	{
		contUVL=0;	
	}
}
*/
function formateaSubeBaja(valor)
{
var color='';
	if (valor<0)
	{
		color='#FF0000;';
	}else if (valor>0){
		color='#0000FF';
	}else{
		color='#000000';
	}
return("<span style='color:"+color+"';>"+formatoNumero(valor));
}

function refrescaIndice()
{
var ultimoTipo='';
var totalC=0;
for (var i=0;i<listaIndice.length;i++)
{
	totalC+=listaIndice[i].nombre.length;
}
var indiceLongitud=15;
var elHTML="<table id='dentroindice' cellpading='0' cellspacing='0' style='position:relative;height:34px;align:left;top:0px;width:"+parseInt(totalC*indiceLongitud)+"px;' border='0'><tr>";
//rellenar el buffer (inicial)
for (var i=0;i<listaIndice.length;i++)
{
	var aux=listaIndice[i];
	if (aux.tipo!=ultimoTipo)
	{
		ultimoTipo=aux.tipo;
		switch(aux.tipo)
		{
			case 'T':
				var textoTipo="<img src='imagenes/ticker_tipos.gif' border='0' align='absmiddle' alt='TIPOS DE INTER&Eacute;S'>";
			break;
			case 'D':
				var textoTipo="<img src='imagenes/ticker_deuda.gif' border='0' align='absmiddle' alt='DEUDA'>";
			break;
			case 'I':
				var textoTipo="<img src='imagenes/ticker_indices.gif' border='0' align='absmiddle' alt='INDICES'>";
			break;
		}
		elHTML+="<td valign='top' align='right' style='height:30px;width:200px;font-weight:bold;font-size:small;font-family:Arial' align='center'>"+textoTipo+"</td>";
	}
	if (aux.diario>0)
	{
		var img='sube.gif';
		var color='green';
	}else if (aux.diario<0){
		var img='baja.gif';
		var color='red';
	}else{
		var img='igual.gif'
		var color='black';
	}
	elHTML+="<td width='auto' valign='middle'><img src='imagenes/"+img+"' align='left'><span style='text-transform:uppercase;font-weight:bold;font-size:9pt;font-family:Arial;color:#3F7b1c'>"+aux.nombre+"</span> <span style='font-family:Arial;font-weight:bold;color:"+color+";font-size:9pt;'>"+formatoNumero(aux.valor);
	if (aux.tipo!='I')
	{
		elHTML+="&#037;";
	}

	elHTML+="</span><br/><span style='font-size:9pt;'>"+formateaSubeBaja(aux.diario)+"&#037;</span> / "+formateaSubeBaja(aux.anual)+"&#037;</span></span></td>";
}
document.getElementById('divindices').innerHTML=elHTML+"</tr></table>";
}

/*
bufferIndice[0]=bufferIndice[1];
bufferIndice[1]=contIndice;
if (++contIndice==listaIndice.length)
	{
		contIndice=0;
	}


var aux=listaIndice[bufferIndice[0]];
/*	
	for (pos=0;pos<50;pos++)
	{
		document.getElementById('indiceizq').style.marginTop=pos+"px";	
	}
	document.getElementById('indiceizq').style.marginTop="0px";
*/
/*
	if (aux.diario>0)
	{
	var img='sube.gif';
	var color='green';
	}else if (aux.diario==0){
	var img='igual.gif'
	}else{
	var img='baja.gif';
	var color='red';
	}
	document.getElementById('indiceizq').innerHTML="<img src='imagenes/"+img+"' align='left'><strong>"+aux.nombre+"</strong> &nbsp; "+aux.valor+"%<br/><span color='"+color+"'>"+aux.diario+"%<br/>"+aux.anual+"%";	

var aux=listaIndice[bufferIndice[1]];
	if (aux.diario>0)
	{
	var img='sube.gif';
	var color='green';
	}else if (aux.diario==0){
	var img='igual.gif'
	}else{
	var img='baja.gif';
	var color='red';
	}
	document.getElementById('indiceder').innerHTML="<img src='imagenes/"+img+"' align='left'><strong>"+aux.nombre+"</strong> &nbsp; "+aux.valor+"%<br/><span color='"+color+"'>"+aux.diario+"%</span><br/>"+aux.anual+"%";	
}
*/

function refrescaDivisa()
{

document.getElementById('divisa').innerHTML=bufferDivisa[contDivisa];
if (contDivisa==0)
{
	contDivisa++;
}else{
	contDivisa=0;
}
/*
	while (bufferDivisa.length<4)
	{
		bufferDivisa.push(contDivisa);
		contDivisa++;
		//circular
		if (contDivisa>listaDivisa.length)
		{
			contDivisa=0;
		}
	}
//rellenamos el buffer
	for (var i=0;i<4;i++)
	{	
		showDivisa(bufferDivisa[i],i+1);
	}
//ahora hacemos que se mueva el buffer
for (i=0;i<3;i++)
	{
	bufferDivisa[i]=bufferDivisa[i+1];
	}
	bufferDivisa[3]=contDivisa;
	contDivisa++;
	if (contDivisa==listaDivisa.length)
	{
		contDivisa=0;
	}
*/
}

function generaDivisa()
{
//genera los divs para la divisa
bufferDivisa[0]="<table align='center' width='100%' border='0' height='100%'>\n";
bufferDivisa[1]="<table align='center' width='100%' border='0' height='100%'>\n";
var contDIV=0;
var contCOM=0;

for (i=0;i<listaDivisa.length;i++)
{
	var aux=listaDivisa[i];

	if (aux.diario>0)
	{
		var imgDivisa='sube.gif';
		var color="#3F7B1C";
	}else if (aux.diario<0){
		var imgDivisa='baja.gif';
		var color="#FF0000";
	}else{
		var imgDivisa='igual.gif';
		var color="#000000";
	}

if (aux.tipo=='DIV')
{
	if ((contDIV % 2)==0) //el contador es par
	{
	bufferDivisa[0]+="<tr><td><img src='imagenes/"+imgDivisa+"' align='left'><strong>"+aux.nombre+"</strong> &#8226; "+formatoNumero(aux.valor)+"<br/>"+formateaSubeBaja(aux.diario)+"&#037;</span><br/>"+formateaSubeBaja(aux.mensual)+"&#037;</span></td>";
	}else{
	bufferDivisa[0]+="<td><img src='imagenes/"+imgDivisa+"' align='left'><strong>"+aux.nombre+"</strong> &#8226; "+formatoNumero(aux.valor)+"<br/>"+formateaSubeBaja(aux.diario)+"&#037;</span><br/>"+formateaSubeBaja(aux.mensual)+"&#037;</span></td></tr>";
	}
contDIV++;
}else{
	if ((contCOM % 2)==0) //el contador es par
	{
	bufferDivisa[1]+="<tr><td><img src='imagenes/"+imgDivisa+"' align='left'><strong>"+aux.nombre+"</strong> &#8226; "+formatoNumero(aux.valor)+"<br/>"+formateaSubeBaja(aux.diario)+"&#037;</span><br/>"+formateaSubeBaja(aux.mensual)+"&#037;</span></td>";
	}else{
	bufferDivisa[1]+="<td><img src='imagenes/"+imgDivisa+"' align='left'><strong>"+aux.nombre+"</strong> &#8226; "+formatoNumero(aux.valor)+"<br/>"+formateaSubeBaja(aux.diario)+"&#037;</span><br/>"+formateaSubeBaja(aux.mensual)+"&#037;</span></td></tr>";
	}
contCOM++;
}


}
bufferDivisa[0]+="</table>";
bufferDivisa[1]+="</table>";
}
