var initMessage="اضغط على الرابط كي تحفظ من مقالة الى عشر مقالات";
var myStoryList=new Array();

var tools="<div id=\"ss_options\"><a href=\"javascript:void(0)\" onclick=\"clearList();\">احذف جميع المقالات</a><a href=\"javascript:void(0);\" onclick=\"clearRead();\"> احذف المقالات المقروءة</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;


}

function addMyStory(storyId,url,hed) {
  if ((l=myStoryList.length)==10)
  {
     alert("فقط يمكنك حفظ عشرة مقالات, يمكنك الإزالة من الموجود.");
     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() {
  document.onclick=checkMenu;
  readCookieList();
}

//Cookie Management
function getSSCookie() {
  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=getSSCookie())
   {
      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+"');\">&#187 "+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():"";
  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;


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);
}
function getCookies()
{
var allcookies = document.cookie;
var pos = allcookies.indexOf("testvalue1");
}

