//Cada noticia será de la forma not_id^titular^seccion^leida

nombre="diariodelpuerto";
ocultar='NO';
noticias=new Array();
cargarNoticias();
imag1=new Image();
imag1.src='/images/iconos/clipping_bajar.gif';
imag2=new Image();
imag2.src='/images/iconos/clipping_subir.gif';
imag3=new Image();
imag3.src='/images/iconos/ico_clipping_estela.gif';
imag4=new Image();
imag4.src='/images/iconos/ico_clipping_on.gif';

function noticia(id,titular,seccion,leida)	{
	this.id = id;
	this.titular = titular;
	this.seccion = seccion;
	this.leida = leida;
}

function cargarNoticias() {
  noticias=new Array();
  var inicio = nombre + "=";
  if (document.cookie.length > 0) {
    i = document.cookie.indexOf(inicio);
    if (i != -1) {
      i += inicio.length;
      j = document.cookie.indexOf(";", i);
      if (j == -1) j = document.cookie.length;       
      not=document.cookie.substring(i,j);
      campos=not.split("^");
      for (k=1;k<campos.length;k+=4){
        nueva_not= new noticia(campos[k],unescape(campos[k+1]),campos[k+2],campos[k+3]);
        noticias[(k-1)/4] = nueva_not; 
      }
    }
  }
 num= document.getElementById("num_noticias");
 if (num!=null) num.src='/images/misnoticias/'+noticias.length+'.gif';
}

function noticiaExiste(id){
    for (k=0;k<noticias.length;k++){
       if (noticias[k].id==id) return true;
    }
    return false;
}
     

function anyadirNoticia(id, titular, seccion, leida) {
  //cargarNoticias();
  if (!noticiaExiste(id) && noticias.length<30){
    nueva= new noticia(id,titular,seccion,leida);
    noticias[noticias.length]=nueva;
    guardarNoticias();
    generarMenu();
  }
}

function borrarTodas(){
    for (k=0;k<noticias.length;k++){ 
        ico = document.getElementById(noticias[k].id);  
        if (ico!=null) ico.src='/images/iconos/ico_clipping.gif'; //ico.style.display='';
    }
    caducidad=new Date();
    caducidad.setTime (caducidad.getTime() + (30 * 24 * 3600000));
    document.cookie = nombre + "=; expires=" + caducidad.toGMTString() + "; path = /";
    generarMenu();
}

function borrarLeidas(){
  valor="";
  caducidad=new Date();
  caducidad.setTime (caducidad.getTime() + (30 * 24 * 3600000));
  for (k=0;k<noticias.length;k++){
    if (noticias[k].leida=='NO'){
        valor+="^"+noticias[k].id+"^"+escape(noticias[k].titular)+"^"+noticias[k].seccion+"^"+noticias[k].leida;
    }else{
       ico = document.getElementById(noticias[k].id);  
       if (ico!=null) ico.src='/images/iconos/ico_clipping.gif'; //ico.style.display='';
    }
  }
    document.cookie = nombre + "=" + valor + "; expires=" + caducidad.toGMTString() + "; path = /";
    generarMenu();
}

function marcarLeida(leida_id){
    cargarNoticias();
    for (k=0;k<noticias.length;k++){
        if (noticias[k].id==leida_id) noticias[k].leida='SI';
    }
    guardarNoticias(); 
}

function guardarNoticias(){
  valor="";
  caducidad=new Date();
  caducidad.setTime (caducidad.getTime() + (30 * 24 * 3600000));
  for (k=0;k<noticias.length;k++){
    valor+="^"+noticias[k].id+"^"+escape(noticias[k].titular)+"^"+noticias[k].seccion+"^"+noticias[k].leida;
  }
    document.cookie = nombre + "=" + valor + "; expires=" + caducidad.toGMTString() + "; path = /";
}

//Genera el menú con las noticias

function generarMenu(){
    cargarNoticias();
    enlace=""; 
    separador="<table cellspacing=\"0\" cellpadding=\"0\"><tr><td><img src=\"/images/trans.gif\" width=\"1\" height=\"5\" /></td></tr></table>";
    if (noticias.length==0) enlace="<tr><td><span class='texto'>Pinche sobre <img src='/images/iconos/ico_clipping_estela.gif' valign='middle' /> adjunto a cada noticia para añadirla a esta sección</span></td></tr>";
    for (k=0;k<noticias.length;k++){ 
        if (noticias[k].seccion=='CE'){
             seccion=' (Comercio Exterior)';
             ruta='/comercioexterior/';
        }else{
             seccion=' (Diario del Puerto)';
             ruta='/';
        }
        if (noticias[k].leida=='SI') estilo='notivisitada'; else estilo='notirelacionada';
        enlace=enlace+"<tr><td><span class='texto'>&middot;<a href='"+ruta+"noticia.php?not_id="+noticias[k].id+"' class='"+estilo+"' onclick='marcarLeida("+noticias[k].id+")'>"+unescape(noticias[k].titular)+"</a><br>"+seccion+"</span><br>"+separador+"</td></tr>";
        }
    tit = document.getElementById("encoger");
    tit.innerHTML = "<table>"+enlace+"</table>";
}

function crearEstela(){
      x=1
 //   for (x=1; x < 4; x++){
           est=document.getElementById("estela"+x);
           if (est==null){ 
	   estela = document.createElement("img");
	   estela.src = "/images/iconos/ico_clipping_estela.gif";
	   estela.setAttribute("id","estela"+x);
	   estela.style.position='absolute';
	   padre = document.getElementById("cuerpo");
	   padre.appendChild(estela);	
           } else {
           }
  //  }
}

function esIE(){
    var agt=navigator.userAgent.toLowerCase();
    return ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
}

function moverIcono(not_id,x,y){
        if (noticias.length<30) {
            desp=(esIE()) ? (document.documentElement.scrollTop + window.document.body.scrollTop) : window.pageYOffset;
            ico = document.getElementById(not_id);  
            //ico.style.display='none';
            ico.src='/images/iconos/ico_clipping_on.gif';
            origenX= x;
            origenY = y+desp;
            q=1;
            crearEstela();
            cli=document.getElementById('clip');
            //destinoX=750;  //(cli.style.left).slice(0,-2);
            //destinoY=300; //(cli.style.top).slice(0,-2); 
            destinoX = findPosX(document.getElementById('num_noticias')) + 100;//pq la imagen tiene de ancho 123
            destinoY = findPosY(document.getElementById('num_noticias'));
            if (esIE()) menu2=document.getElementById('cambio')
                else menu2=document.getElementById('encoger');
            /*if (menu2.style.display!='none') destinoY+=(noticias.length*35); 
                else destinoY = findPosY(document.getElementById('num_noticias'));
                */
            tramoX=Math.round((destinoX-origenX)/15); 
            tramoY=Math.round((origenY-destinoY)/15); 
            intervalo=setInterval("anima()",2);  
       } else alert ('Sólo puede almacenar un máximo de 30 noticias');
}

function anima(){ 
       x=1
  //  for (x=1; x < 4; x++){
        est=document.getElementById("estela"+x);
        if (q<15){                 
            posX=origenX+(tramoX+x)*q;
            est.style.left=posX+"px";  
            posY=origenY-(tramoY+x)*q;
            est.style.top=posY+"px";   
            est.style.display='';        
        }else{
                est=document.getElementById("estela"+x);
                est.style.display='none';
        }  
 //   }  
    if (q>14) clearInterval(intervalo);
    q++;
}

function ocultarMenu(){
    if (esIE()) menu2=document.getElementById('cambio')
        else menu2=document.getElementById('encoger');
   // ico_sub=document.getElementById('icosubir');
   ico_sub=document.images["icosubir"];
    if (ocultar=='NO') {
        ico_sub.src='/images/iconos/clipping_bajar.gif';
        menu2.style.display='none'; 
        ocultar='SI';
    }else {
        menu2.style.display=''; 
        ocultar='NO';
        ico_sub.src='/images/iconos/clipping_subir.gif';
    }
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
