// Used for initial load from cookies.
var LoadingFavoriteBoothList = false;

// Viewer Frame Number.
var vfn = 2;

// Add option to option list.
function move(fbox, tbox) 
{
   var no = new Option();
   no.value = fbox;
   fbox = fbox.split("\\").join("");
   no.text = fbox;
   tbox.options[tbox.options.length] = no;
}



// Removes currently selected booth from option list and cookies.
function remove(box) 
{

   if(box.length <= 1)
   {
      alert("No booths on favorites list. To add a booth to the list, right click on the booth and select \"Add to Favorites\".");
      return;
   }

   var BoothNumber;

   if(box.selectedIndex == 0)
      return;

   for(var i=1; i < box.options.length; i++) 
   {
      if(box.options[i].selected && box.options[i] != "") 
      {
         BoothNumber = box.options[i].text;
         DelFavoriteBoothFromCookies(BoothNumber.substring(BoothNumber.indexOf("#") + 1, BoothNumber.indexOf(")")));
         box.options[i].value = "";
         box.options[i].text = "";
      }
   }

   BumpUp(box);

   document.tb.favorites.selectedIndex = 0;
} 



// Removes all booths from option list and cookies.
function removeAll(box) 
{
   if(box.length <= 1)
   {
      alert("No booths on favorites list. To add a booth to the list, right click on the booth and select \"Add to Favorites\".");
      return;
   }

   for(var i=1; i< box.options.length; i++) 
   {
      box.options[i].value = "";
      box.options[i].text = "";
   }
 
   BumpUp(box);

   setCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"), "", 1000);

   document.tb.favorites.selectedIndex = 0;
}



// Bumps up all values on option list after option is deleted.
function BumpUp(box) 
{
   var ln;

   for(var i=0; i < box.options.length; i++) 
   { 
      if(box.options[i].value == "")  
      {
         for(var j=i; j < box.options.length - 1; j++)  
         {
            box.options[j].value = box.options[j + 1].value;
            box.options[j].text = box.options[j + 1].text;
         }
         
         ln = i;
         break;
      }
   }

   if(ln < box.options.length)
   {
      box.options.length -= 1;
      BumpUp(box);
   }
}



// Loads stored view names onto option list.
function LoadStoredViewsList()
{ 
   if(document.tb.views.length > 1)
   {
      for(var i=1; i < document.tb.views.length; i++) 
      {
         document.tb.views.options[i].value = "";
         document.tb.views.options[i].text = "";
      }
   }
 
   BumpUp(document.tb.views);

   for(var i=0; i < parent.frames[vfn].StoredViews.length; i++)
      document.tb.views[document.tb.views.length] = new Option(parent.frames[vfn].StoredViews[i], i+1);
}



// Adds booth to option list and cookies.
function AddBoothToFavorites(BoothNumber)
{
   var Exhibitor = "";

   // Make sure booth number still exists in show.
   for(var i = 0; i < parent.frames[vfn].BoothNumbers.length; i++)
   {
      if(parent.frames[vfn].BoothNumbers[i] == BoothNumber)
         break;

      if(i == parent.frames[vfn].BoothNumbers.length - 1)
	return;
   }


   // Find exhibitor name for booth number.
   for(var i = 0; i < parent.frames[vfn].ExhibBoothNumbers.length; i++)
   {
      if(parent.frames[vfn].ExhibBoothNumbers[i] == BoothNumber)
      {
         Exhibitor = parent.frames[vfn].ExhibitorNames[i] + " (#" + BoothNumber + ")";
         break;
      }
   }
 
   // Prevent duplicates on list.
   for(var i = 0; i < document.tb.favorites.length; i++)
   {
      if(document.tb.favorites[i].text == Exhibitor.split("\\").join(""))
         return;	
   }

   // Find exhibitor name for booth number.
   for(var i = 0; i < parent.frames[vfn].ExhibBoothNumbers.length; i++)
   {
      if(parent.frames[vfn].ExhibBoothNumbers[i] == BoothNumber)
      {
         move(Exhibitor, document.tb.favorites);
         document.tb.favorites.selectedIndex = document.tb.favorites.length - 1;

         if (LoadingFavoriteBoothList == false)
            AddFavoriteBoothToCookies(BoothNumber);

         return;
      }
   }

   // Prevent duplicates on list.
   for(var i = 0; i < document.tb.favorites.length; i++)
   {
      if(document.tb.favorites[i].text == ("Available (#" + BoothNumber + ")"))
         return;	
   }

   move("Available (#" + BoothNumber + ")", document.tb.favorites)
   document.tb.favorites.selectedIndex = document.tb.favorites.length - 1;
	
   if(LoadingFavoriteBoothList == false)
      AddFavoriteBoothToCookies(BoothNumber);
}



// Adds a booth number to cookies.
function AddFavoriteBoothToCookies(BoothNumber)
{
   var FavoriteBoothList = "";
   var TheCookie = "";
   var OurCookie = "";
   var BeginCookie = "";
   var EndCookie = "";

   FavoriteBoothList = getCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"))

   if(FavoriteBoothList == "0")
      setCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"), BoothNumber, 1000);
   else
   {	
      FavoriteBoothList = FavoriteBoothList + "|" + BoothNumber;
      setCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"), FavoriteBoothList, 1000);
   }
}



// Deletes a booth number from cookies.
function DelFavoriteBoothFromCookies(BoothNumber)
{
   var FavoriteBoothList = "";
   var FavoriteBoothListArray = new Array();

   FavoriteBoothList = getCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"));

   if(FavoriteBoothList == "0" || FavoriteBoothList.indexOf("|") < 0)
   {
      setCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"), "", 1000);
      document.cookie = "";
   }
   else
   {
      FavoriteBoothListArray = FavoriteBoothList.split("|");
      FavoriteBoothList = "";

      for(var i=0; i < FavoriteBoothListArray.length; i++)
      {
         if(FavoriteBoothListArray[i] == BoothNumber)
            continue;
	 else
	 {
            if(i == (FavoriteBoothListArray.length - 1))
               FavoriteBoothList += FavoriteBoothListArray[i];
	    else
               FavoriteBoothList += (FavoriteBoothListArray[i] + "|");
         }
      }

      setCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"), FavoriteBoothList, 1000);
   }
}



// Retrieve a cookie from the browser.
function getCookie(NameOfCookie)
{  
   if (document.cookie.length > 0) 
   {   
      var begin = document.cookie.indexOf(NameOfCookie+"="); 

      if (begin != -1)   
         begin += NameOfCookie.length+1; 
      else
         return "0";

      var end = document.cookie.indexOf(";", begin);

      if(end == -1) 
         end = document.cookie.length;

      return unescape(document.cookie.substring(begin, end));       
   } 

   return "0"; 
}



// Set the value of a cookie.
function setCookie(NameOfCookie, value, expiredays) 
{
   var ExpireDate = new Date ();
   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
   document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}



// Adds favorite booths to option list from cookies.
function LoadFavoriteBoothList()
{
   LoadingFavoriteBoothList = true;
   var FavoriteBoothList;
   var FavoriteBoothList = new Array();
   var BoothNumber = "";

   FavoriteBoothList = getCookie((parent.frames[vfn].EventName + "FavoriteExhibitors"))
   FavoriteBoothListArray = FavoriteBoothList.split("|");
   

   if(FavoriteBoothList == "0")
   {
         LoadingFavoriteBoothList = false;
	 return;
   }
   else
   {
      for(var i=0; i < FavoriteBoothListArray.length; i++)
            AddBoothToFavorites(FavoriteBoothListArray[i]);
   }
   
   LoadingFavoriteBoothList = false;
}



// Causes floorplan to zoom to the selected pre-defined view.
function ZoomView()
{
   parent.frames[vfn].ZoomView(document.tb.views.options[document.tb.views.selectedIndex].text);
   document.tb.views.selectedIndex = 0;
}



// Causes floorplan to zoom to the selected favorite booth.
function ZoomFavorite()
{
   var BoothNumber = document.tb.favorites.options[document.tb.favorites.selectedIndex].text;
   BoothNumber = BoothNumber.substring(BoothNumber.indexOf("#") + 1, BoothNumber.indexOf(")"));
   parent.frames[vfn].zoomBooth(BoothNumber);
}



// Performs multi-zoom on favorite booths.
function multiZoomView(box)
{
   var MultiZoomBooths = "";
   var BoothNumber;
   var ExhibitorName;

   if (box.length > 2)
   {
      for(var i=1; i < box.length; i++)
      {
         ExhibitorName = box.options[i].text;
         BoothNumber = ExhibitorName.substring(ExhibitorName.indexOf("#") + 1, ExhibitorName.indexOf(")"));
         MultiZoomBooths = MultiZoomBooths + BoothNumber + " ";
      }
      
      MultiZoomBooths = MultiZoomBooths.substring(0, MultiZoomBooths.lastIndexOf(" "));

      parent.frames[vfn].zoomBooth(MultiZoomBooths);

   }
   else if(box.length > 1)
   {
      ExhibitorName = box.options[1].text;
      BoothNumber = ExhibitorName.substring(ExhibitorName.indexOf("#") + 1, ExhibitorName.indexOf(")"));
      parent.frames[vfn].zoomBooth(BoothNumber);
   }
   else
      alert("No booths on favorites list. To add a booth to the list, right click on the booth and select \"Add to Favorites\".");

}