var initMessage="<p>Haga click en un ícono de Grabar Artículo para agregar hasta 10 artículos aquí para fácil referencia.</p>";
var myStoryList=new Array();

var tools="<div id=\"ss_options\"><a href=\"javascript:void(0)\" onclick=\"clearList();\">Borrar todos los artículos</a><a href=\"javascript:void(0);\" onclick=\"clearRead();\">Borrar artículos leidos</a></div>";

function myStory(storyData) {
   storyInfo=storyData.split("::");
   this.sid=storyInfo[0];
   this.url=storyInfo[1];
   this.hed=storyInfo[2];
   this.read=(storyInfo[3]=='t')?true:false;
}

function shiftMyStoryList() {
  l=myStoryList.length;
  
  for (i=l;i>0;i--)
    myStoryList[i]=myStoryList[i-1];
}

function swapSaveGif(storyID,state) {
  gifID="add"+storyID;
  if (!(savedImg=document.getElementById(gifID)))
     return;
  
  if ((savedImg.width==101) || (savedImg.width==101))
  {
     if (state=="saved")
        savedImg.src="../imgmx/saved2.gif";
     else if (state=="save")
        savedImg.src="../imgmx/save2.gif";
  }

  else if (savedImg.width==12)
  {
     if (state=="saved")
        savedImg.src="../imgmx/saved_icon.gif";
     else if (state=="save")
        savedImg.src="../imgmx/save_icon.gif";
  }
}

function addMyStory(storyId,url,hed) {
  if ((l=myStoryList.length)==15)
  {
     alert("Sólo 15 artículos pueden ser grabados. Para guardar un nuevo artículo, por favor borre uno de su carpeta.");
     return;
  }

  inlist=false;
  
  if (l)
    for (i=0;i<l;i++)
      if (myStoryList[i].sid==storyId)
	{
	  inlist=true;
	  break;
	}
  
  if (!(inlist))
    {
      storyData=storyId+"::"+url+"::"+hed;

      shiftMyStoryList();
      myStoryList[0]=new myStory(storyData);

      setCookieList();
      changeList();
      swapSaveGif(storyId,'saved');
    }
}

// Initialize menu
function startupSaved() {  //checks for mysavedstories cookie and tells document bacground to watch for clicks
  document.onclick=checkMenu;
  readCookieList();
}

//Cookie Management
function getCookie() {
  var dc = document.cookie;
  var prefix = "mynewsstories=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function readCookieList() {
   if (cookstring=getCookie())
   {
      storyList=cookstring.split("**");
      for (i=0;i<storyList.length;i++)
	  {
	    myStoryList[i]=new myStory(storyList[i]);
	    if (document.getElementById('add'+myStoryList[i].sid))
	       swapSaveGif(myStoryList[i].sid,'saved');
	  }
      changeList();
   }
   else document.getElementById("ss_headlines").innerHTML=initMessage;
}

function setCookieList() {

   l=myStoryList.length;
   cookstring='';
   for (i=0;i<l;i++)
   {
     readtf=(myStoryList[i].read)?'::t':'::f'
     if (i>0) cookstring+="**";
     cookstring+=myStoryList[i].sid+"::"+myStoryList[i].url+"::"+myStoryList[i].hed+readtf; //took out the extra colon
   }
   var expdate = new Date();
   expdate.setTime(expdate.getTime()+31536000000);
   document.cookie="mynewsstories="+escape(cookstring)+"; expires="+expdate.toGMTString()+"; path=/;";
}


// Menu management

function buildPullDown() {

  l=myStoryList.length;
  myStoryHTML="";
  
  for (i=0;i<l;i++)
  {
   readtest=(myStoryList[i].read)?' class=\"read\"':'';
   myStoryHTML+="<a href=\""+myStoryList[i].url+"\""+readtest+" onclick=\"readThis('"+myStoryList[i].sid+"');\">"+myStoryList[i].hed+"</a>"
  }
  myStoryHTML+=tools;
  return myStoryHTML;
}

function changeNums() {
   l=myStoryList.length;
   document.getElementById('ss_count').innerHTML=l;
}

function changeList() {
  p=document.getElementById("ss_headlines");
  p.innerHTML=(myStoryList.length)?buildPullDown():"<span class=\"mla\"><table width=\"100%\" cellpadding=3 cellspacing=0 border=0><tr valign=\"top\"><td><img src=\"../imgmx/save_icon.gif\" width=12 height=8 border=0></td><td class=\"mla\">Haga click en el botón de MAS debajo para agregar artículos aquí.</td></tr></table></p><div align=\"right\"><a href=\"javascript:void(0)\">Cerrar</a></span></span>";
  changeNums();
}

function clearList() {
   for (i=0;i<myStoryList.length;i++)
       swapSaveGif(myStoryList[i].sid,'save');

   document.cookie="mynewsstories=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/;";
   myStoryList=new Array();
   changeNums();
   document.getElementById("ss_headlines").innerHTML=initMessage;
}

function clearRead() {
   newStoryList=new Array();
   for (i=0;i<myStoryList.length;i++)
   {
     if (!(myStoryList[i].read))
        newStoryList[newStoryList.length]=myStoryList[i];
     else
        swapSaveGif(myStoryList[i].sid,'save');
   }
   myStoryList=newStoryList;
   changeList();
   setCookieList();
}

function readThis(storyid) {
  for (i=0;i<myStoryList.length;i++)
    if (myStoryList[i].sid==storyid)
    {
       myStoryList[i].read=true;
       break;
    }
  setCookieList();
}

// Menu Show/Hide Functions
var openMenu=null;
    // variable to determine which pulldown is open - needed for closing menus by clicking on background

function toggleSS(which) {

   obj=document.getElementById(which).style.visibility;
   if ((obj=="hidden") || (obj==""))
      { showSS(which); }
   else { hideSS(which); }
}

function hideSS(which) {
   document.getElementById(which).style.visibility="hidden";
   openMenu=null;
}

function showSS(which) {
   document.getElementById(which).style.visibility="visible";
   openMenu=which;
}

function checkMenu() {
  if (openMenu != null)
     hideSS(openMenu);
}