var lngpath = getCookie("language") + "/";
var viaObjectList=new Array();

var initMessage= "<div style=\"position:relative; top:0; left:0;"
		+"background:#00AD4F; border:1px solid black; border-top: 1px solid white;"
		+"border-left:1px solid white; border-bottom: 1px solid black;padding:1px; padding-left:1px;\">"
		+"<table width=\"100%\"border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
   		+"<tr><td align=left>"
  		+"<table border=\"0\" width=\"100%\" cellspacing=2 cellpadding=2>"
		+"<tr><td><div style=\"font-size:11px;font-family:verdana;color:#ffffff;\">"
		+ mssg_favo_1 + "&nbsp;<img border=0 alt='[button image]' align=absmiddle src=\"" + lngpath + "images/favorites/addtolist-top.gif\">&nbsp;"
		+ mssg_favo_2 + "</div></td></tr>"
		+"<tr><td align=right><a href=\"javascript:void(0)\" style=\"text-decoration:none;color:#ffffff;\">" + mssg_close + "</a></td></tr>"
		+"</table></td></tr>"
		+"</table>"
		+"</td></tr></table></div>";

function viaObject(objectData){
   objectInfo=objectData.split("::");
   this.objectId=objectInfo[0];
   this.objectType=objectInfo[1];
   this.display=objectInfo[2];
   this.image=objectInfo[3];
}

function shiftviaObjectList(){
 	l=viaObjectList.length;
 	for (i=l;i>0;i--)
   		viaObjectList[i]=viaObjectList[i-1];
}

function getObject(objectId, objectType){
	for(i=0;i<viaObjectList.length;i++)	{
		if (viaObjectList[i].objectId==objectId && viaObjectList[i].objectType==objectType)
		obj=viaObjectList[i];
	}
	return obj;
}

function delObject(objectId, objectType){	obj=getObject(objectId, objectType);
	obj.read=true;	clearRead();
}

function addObject(objectId, objectType, display, image){
  if ((l=viaObjectList.length)==10)
  {
     alert(mssg_favo_3);
     return;
  }

  inlist=false;
  
  if (l)
    for (i=0;i<l;i++)
      if (viaObjectList[i].objectId==objectId && viaObjectList[i].objectType==objectType)
	{
	  inlist=true;
	  break;
	}
  
  if (!(inlist))
    {
      objectData=objectId+"::"+objectType+"::"+display+"::"+image;
	  combinedObjectId=objectId+objectType;

      shiftviaObjectList();
      viaObjectList[0]=new viaObject(objectData);

      setCookieList();
      changeList();
      swapSaveGif(combinedObjectId,'saved');
    }
}

// Initialize menu
function startupSaved() {  //checks for mysavedstories cookie and tells document bacground to watch for clicks
  document.onclick=checkMenu;
  readCookieList();
  //checkForSavedImages();
}

function checkForSavedImages(){
	for(i=0;i<viaObjectList.length;i++){
		
		swapSaveGif(combinedObjectId,'saved');
	}
}

//Cookie Management
function getSSCookie() {
  var dc = document.cookie;
  var prefix = "viaobjects=";
  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++)
	  {
	    viaObjectList[i]=new viaObject(storyList[i]);
	    if (document.getElementById('add'+viaObjectList[i].objectId+viaObjectList[i].objectType)){
			combinedObjectId=viaObjectList[i].objectId+viaObjectList[i].objectType;
	       swapSaveGif(combinedObjectId,'saved');
		}
	  }
      changeList();
   }
   else document.getElementById("ss_headlines").innerHTML=initMessage;
}

function setCookieList() {

   l=viaObjectList.length;
   cookstring='';
   for (i=0;i<l;i++)
   {
     if (i>0) cookstring+="**";
     cookstring+=viaObjectList[i].objectId+"::"+viaObjectList[i].objectType+"::"+viaObjectList[i].display+"::"+viaObjectList[i].image; 
   }
   var expdate = new Date();
   expdate.setTime(expdate.getTime()+31536000000);
   document.cookie="viaobjects="+escape(cookstring)+"; expires="+expdate.toGMTString()+"; path=/;";
}

// Image management
function swapSaveGif(combinedObjectId,state) {
  gifID="add"+combinedObjectId;
  if (!(savedImg=document.getElementById(gifID)))
     return;
	if (state=="saved")
		savedImg.src= lngpath + "images/add-to-list-on.gif";
	else if (state=="save")
		savedImg.src= lngpath + "images/add-to-list-off.gif"; 
/*
	if (state=="saved")
	{
		savedImg.className = "button-active";
		savedImg.innerHTML = "In my List";
		savedImg.parentElement.className = "button-on";
	} else if (state=="save")
	{
		savedImg.className = "button-inactive";
		savedImg.innerHTML = "Add To List";
		savedImg.parentElement.className = "button-off";
	}
*/}


// Menu management
function buildPullDown()
{

  l=viaObjectList.length;
  viaObjectHTML="";
  
  for (i=0;i<l;i++)
  {
	oId=viaObjectList[i].objectId;     
	oType=viaObjectList[i].objectType;
	qString= strpath + "detailpage.php?object="+oId;
   
    viaObjectHTML+="<div style=\"position:relative; top:0; left:0;"
		+"background:#00AD4F; border:1px solid black; border-top: 1px solid white;"
		+"border-left:1px solid white;padding:1px; padding-left:1px;\">"
		+"<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
   		+"<tr><td align=left>"
  		+"<table border=\"0\" width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" onMouseover=\"this.style.backgroundColor='#008078';\" onMouseout=\"this.style.backgroundColor='#00AD4F';\">"
		+"<tr>"
		+"<td><a href=\"javascript:location.href='" + qString+ "'\"><img class=thumb width=50 src="+viaObjectList[i].image+" style=\"border:none\"></a></td>"
		+"<td width=125 align=left><a href=\"javascript:location.href='" + qString+ "'\" style=\"text-decoration:none;color:#ffffff;\">"+viaObjectList[i].display+"</a></td>"
		+"<td width=25 align=center><a href=\"javascript:void(0)\" onclick=\"delObject('"+oId+"','"+oType+"');\"><img border=0 src=images/favorites/trash.gif></a></td>"
		+"</tr>"
		+"</table>"
		+"</td></tr></table></div>";
  }

    viaObjectHTML+="<div style=\"position:relative; top:0; left:0;"
		+"background:#00AD4F; border:1px solid black; border-top: 1px solid white;"
		+"border-left:1px solid white; border-bottom: 1px solid black; padding:1px; padding-left:1px;\">"
		+"<table width=\"100%\"border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
   		+"<tr><td align=left>"
  		+"<table border=\"0\" width=\"100%\" cellspacing=\"2\" cellpadding=\"2\">"
		+"<tr><td align=left><a href=\"javascript:void(0)\" onclick=\"clearList();\" style=\"text-decoration:none;color:#ffffff;\">"
		+ mssg_clearall + "</a></td>"
		+"<td align=right><a href=\"javascript:void(0)\" style=\"text-decoration:none;color:#ffffff;\">" + mssg_close + "</a></td></tr>"
		+"</table>"
		+"</td></tr></table></div>";

  return viaObjectHTML;
}

function changeNums() {
   l=viaObjectList.length;
   //document.getElementById('ss_count').innerHTML=l;
}

function changeList() {
  p=document.getElementById("ss_headlines");
  p.innerHTML=(viaObjectList.length)?buildPullDown(): initMessage;
  changeNums();
}

function clearList() {
   for (i=0;i<viaObjectList.length;i++){
		combinedObjectId=viaObjectList[i].objectId+viaObjectList[i].objectType;
		swapSaveGif(combinedObjectId,'save');
   }
   document.cookie="viaobjects=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/;";
   viaObjectList=new Array();
   changeNums();
   document.getElementById("ss_headlines").innerHTML=initMessage;
}

function clearRead() {
   newStoryList=new Array();
   for (i=0;i<viaObjectList.length;i++)
   {     if (!(viaObjectList[i].read))
        newStoryList[newStoryList.length]=viaObjectList[i];
     else{
	 	combinedObjectId=viaObjectList[i].objectId+viaObjectList[i].objectType;
        swapSaveGif(combinedObjectId,'save');
	 }
   }
   viaObjectList=newStoryList;
   changeList();
   setCookieList();
}

function readThis(storyid) {
  for (i=0;i<viaObjectList.length;i++)
    if (viaObjectList[i].objectId==storyid)
    {
       viaObjectList[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";
   document.getElementById(which).style.zIndex=3000; 
   openMenu=which;
}

function checkMenu() {
  if (openMenu != null)
     hideSS(openMenu);
}

/*var oPopup = window.createPopup();
var popold = null;

function showpopup(obj)
{
  var oPopupBody = oPopup.document.body;
  oPopupBody.innerHTML = obj.innerHTML;

  objT = oPopup.show(
	  document.getElementById("outsidefrm").offsetLeft + 16, 
	  125 - document.body.scrollTop, 
	  obj.offsetWidth, obj.offsetHeight, document.body);

  document.body.onmouseup = hiddenpopup;
  popold = obj;
}

function hiddenpopup()
{
  oPopup.hide();
}

function callCloseMenu( strcmd)
{
	if( blnMSIE)
	{
		if(oPopup.isOpen)
			hiddenpopup();
		else
			openpopupswindow(strcmd);
	} else
	{
		var src = document.getElementById(strcmd);
		src.style.left = document.getElementById("outsidefrm").offsetLeft + 16;
		src.style.top = 125 - document.body.scrollTop;
		toggleSS(strcmd);
	}
}

function openpopupswindow( strID)
{
	if(strID != "")
	{
		var src = document.getElementById(strID);
		if( src != null)
		{
			if(!oPopup.isOpen || src != popold)
				showpopup(src);
		}
		else
			hiddenpopup();
	}
	
	return;
}

function closepopupwindow( strID)
{
}

function redirect( loc)
{
	window.location.href = loc;
}

*/
