var OverMenu=0;
var MenuDiv=null;
var pos;
var MousePosX;
var MenuBarWidth=0;
var resizeinc=0;
var interval;
var ns4;
var op5;
var op6;
var agt;
var mac;
var ie;
var mac_ie;

sniffBrowsers();

window.onresize = resized;

function stopInterval()
{
  window.clearInterval(interval);
  interval="";
}


function BodyMouseMove(event)
{
  MousePosX = event.clientX;
}

function resized() 
{
var NOptions=0;
var LogoWidth = GetCookie('DocbWeb2LogoWidth')*1;
MenuBarWidth = GetCookie('DocbWeb2MenuBarWidth')*1;

  if (MenuBarWidth > LogoWidth) 
  {
    document.getElementById('MenuLayerDiv').style.overflow = 'visible';
   
    if (document.body.clientWidth-20 > LogoWidth)
    {
      document.getElementById('MenuLayerDiv').style.width = document.body.clientWidth-20;
      document.getElementById('MenuLayerDiv').style.height = 30;
      document.body.style.overflowX = 'hidden';
      document.body.style.overflowY = 'hidden';
      document.body.style.overflowY = 'scroll';
      document.body.style.overflowX = 'scroll';
    }
    else
    {
      document.getElementById('MenuLayerDiv').style.width = LogoWidth;
      document.getElementById('MenuLayerDiv').style.height = 30;
      document.body.style.overflowX = 'scroll';
    }
    
    document.getElementById('MenuLayerDiv').style.overflow = 'hidden';
       

    if (MenuDiv != null)
    {
      MenuDiv.style.left = 0;
      pos = 0;
    }
  }
}


function MainMenuMouseOver(MainMenuDiv)
{
  OverMenu=1;
  MenuDiv=MainMenuDiv;
}


function MainMenuMouseOut()
{
  OverMenu=0;
}


function startInterval()
{
var LogoWidth = GetCookie('DocbWeb2LogoWidth')*1;

  MenuBarWidth = GetCookie('DocbWeb2MenuBarWidth')*1;

  if (MenuBarWidth > LogoWidth) 
  {
    if (document.body.clientWidth-20 > LogoWidth)
      document.getElementById('MenuLayerDiv').style.width = document.body.clientWidth-20;
    else
      document.getElementById('MenuLayerDiv').style.width = LogoWidth;
      
    document.getElementById('MenuLayerDiv').style.height = 33;      
    
    document.getElementById('MenuLayerDiv').style.overflow = 'hidden';
    interval = window.setInterval("tPan()",10);
  }    
} 



function tPan()
{
var LogoWidth = GetCookie('DocbWeb2LogoWidth')*1;

  if (OverMenu)
  {
    if (MousePosX < 50) 
    {
      if (MenuDiv != null)
      {
        pos = MenuDiv.style.left.substring(0,MenuDiv.style.left.length-2)*1;
        if (pos<0)
          pos=pos+5;
        else
          pos = 0;
        MenuDiv.style.left = pos;
        SetCookie('DocbWeb2MenuBarPos',pos,10000,false);
      }      
    }
    if (MousePosX > document.body.clientWidth-70)
    {
     
      if (MenuDiv != null)
      {
        pos = MenuDiv.style.left.substring(0,MenuDiv.style.left.length-2)*1;
        if (document.body.clientWidth-20 > LogoWidth)
        {
          if (pos>document.body.clientWidth- 20 - MenuBarWidth)
            pos=pos-5;
          else
          {
            if (document.body.clientWidth-20<MenuBarWidth) {
              pos= document.body.clientWidth- 20 - MenuBarWidth;
            }
          }
          MenuDiv.style.left = pos;
          SetCookie('DocbWeb2MenuBarPos',pos,10000,false);
        }
        else
        {
          if (pos>LogoWidth - MenuBarWidth)
            pos=pos-5;
          else
            pos=LogoWidth - MenuBarWidth;
          MenuDiv.style.left = pos;
          SetCookie('DocbWeb2MenuBarPos',pos,10000,false);
        }         
      }   
    }
  } 
} 

function SetMenuPos()
{
var LogoWidth = GetCookie('DocbWeb2LogoWidth')*1;
var MenuBarPos = GetCookie('DocbWeb2MenuBarPos')*1;

  MenuBarWidth = GetCookie('DocbWeb2MenuBarWidth')*1;
  
  if (MenuBarWidth > LogoWidth) {
    if (document.body.clientWidth-20 < MenuBarWidth)
      document.getElementById('MainMenuDiv').style.left = MenuBarPos;
  }
}


function GetCookie(cookieName)
{
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName);
if (ind==-1 || cookieName=="") return null;
var ind1=theCookie.indexOf(';',ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function SetCookie(cookieName,cookieValue,nDays,escape)
{
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0)
  {
    if (escape)
      document.cookie = cookieName + "=" +escape(cookieValue) + ";path=/";
    else
      document.cookie = cookieName + "=" +cookieValue + ";path=/";
  }
  else
  {
    expire.setTime(today.getTime() + 3600000*24*nDays);

    if (escape)
      document.cookie = cookieName + "=" +escape(cookieValue) + ";expires="+expire.toGMTString() + ";path=/";
    else
      document.cookie = cookieName + "=" +cookieValue + ";expires="+expire.toGMTString() + ";path=/";
  }
}


function SetDesmarcar(o)
{
var child,i;

    for (i=0;i<o.form.childNodes.length;i++)
    {
      child = o.form.childNodes[i];
      if (child.nodeType == 3)
      {
        child.nodeValue = 'Desmarcar';
        break;
      }
    }
}


function SetMarcar(o)
{
var child,i;

    for (i=0;i<o.form.childNodes.length;i++)
    {
      child = o.form.childNodes[i];
      if (child.nodeType == 3)
      {
        child.nodeValue = 'Marcar';
        break;
      }
    }
}


function RecordSelect(o, cookie_name, mfn)
{
 
    if (GetCookie(cookie_name) == null)
    {
      if (o.checked)
      {
        SetDesmarcar(o);
        SetCookie(cookie_name,mfn + "=checked",10000,false)
      }
      else
      {
        SetMarcar(o);
        SetCookie(cookie_name,mfn + "=unckecked",10000,false)
      }
    }
    else
    {
      cur_cookievalue = GetCookie(cookie_name)
      if (o.checked)
      {
        SetDesmarcar(o);
        SetCookie(cookie_name,cur_cookievalue + "&" + mfn + "=checked",10000,false)
      }
      else
      {
        SetMarcar(o);
        SetCookie(cookie_name,cur_cookievalue + "&" + mfn + "=unchecked",10000,false)
      }
    }
}


function ExportIso2709(BaseName,SessionID,CookieName)
{
  window.open("Iso2709.asp?Base=" + BaseName + "&SID=" + SessionID + "&Cookie=" + CookieName,"_blank","height=150,width=400,top=50,left=50");
}

function showDesc(obj)
{
  if (obj.id == 'simples')
    window.status = 'Pesquisa simples';
  if (obj.id == 'avançada')
    window.status = 'Pesquisa avançada';
  if (obj.id == 'índice')
    window.status = 'Pesquisa por índice';
  if (obj.id == 'thesaurus')
    window.status = 'Pesquisa por thesaurus';
  if (obj.id == 'cdu simples')
    window.status = 'Pesquisa temática simples';
  if (obj.id == 'cdu avançada')
    window.status = 'Pesquisa temática avançada';
  if (obj.id == 'links') 
    window.status = 'Pequisa de links';
  if (obj.id == 'favoritos')
    window.status = 'Favoritos';
  if (obj.id == 'notícias')
    window.status = 'Notícias';
    
  return true;    
}   

function hideDesc()
{
  window.status = '';
}


function ChangeImage(obj, image)
{
  obj.src = image;
}



function CommentWindow(WebMail)
{
  window.open("comment.asp?Email=" + WebMail,"_blank","height=300,width=450,top=50,left=50"); 
}

function doTooltip(e, msg)
{
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, msg);
}

function hideTip()
{
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}


function ToggleViewItems(o, mfn)
{
var divelement,child,i, aelement;

  aelement = document.getElementById("ItemLink" + mfn);
  
  divelement = document.getElementById("SitExemplar" + mfn);
  
  if (divelement != null)
  {
    if (divelement.style.display == "")
    {
      divelement.style.display = "none";
      
      if (aelement.firstChild != null)
        if (aelement.firstChild.firstChild != null)
          aelement.firstChild.firstChild.nodeValue = 'Vêr Informação de Exemplares';
    
    }
    else
    {
      divelement.style.display = "";
     
      if (aelement.firstChild != null)
        if (aelement.firstChild.firstChild != null)
          aelement.firstChild.firstChild.nodeValue = 'Esconder Informação de Exemplares';
      
    }
  }
  
}


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; 
}

function ScrollToElement(theElement)
{ 
var selectedPosX = 0; 
var selectedPosY = 0;

  while(theElement != null)
  { 
    selectedPosX += theElement.offsetLeft; 
    selectedPosY += theElement.offsetTop; 
    theElement = theElement.offsetParent; 
  } 
  
  window.scrollTo(selectedPosX,selectedPosY-document.body.clientHeight); 
}


function getElementHeight(Elem) 
{
var yPos;

  if (ns4) 
  {
    var elem = getObjNN4(document, Elem);
    return elem.clip.height;
  } 
  else 
  {
    if(document.getElementById) 
    {
      var elem = document.getElementById(Elem);
    } 
    else if (document.all)
    {
      var elem = document.all[Elem];
    }
    if (elem!=null) 
    {
      if (op5) 
      { 
        yPos = elem.style.pixelHeight;
      } 
      else 
      {
        yPos = elem.offsetHeight;
      }
    }
    return yPos;
  } 
}


function setElementHeight(Elem,NewHeight) 
{
  if (ns4) 
  {
    var elem = getObjNN4(document, Elem);
    elem.clip.height = NewHeight;
  } 
  else 
  {
    if(document.getElementById) 
    {
      var elem = document.getElementById(Elem);
    } 
    else if (document.all)
    {
      var elem = document.all[Elem];
    }
    if (elem!=null) 
    {
      if (op5) 
      { 
        elem.style.pixelHeight = NewHeight;
      } 
      else 
      {
        if (NewHeight<300) NewHeight=300;
        elem.style.height = NewHeight;
      }
    }
  } 
}

function getElementWidth(Elem) 
{
var xPos = -1;

  if (ns4) {
    var elem = getObjNN4(document, Elem);
    return elem.clip.width;
  } 
  else 
  {
    if (document.getElementById) 
    {
      var elem = document.getElementById(Elem);
    } 
    else if (document.all)
    {
      var elem = document.all[Elem];
    }
    if (elem!=null) 
    {
      if (op5) 
      {
        xPos = elem.style.pixelWidth;    
      } 
      else 
      {
        xPos = elem.offsetWidth;
      }
    }
    return xPos;
  }
}



function sniffBrowsers() 
{ 
  var ns4 = document.layers; 
  var op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) ||(navigator.userAgent.indexOf("Opera/5")!=-1); 
  var op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) ||(navigator.userAgent.indexOf("Opera/6")!=-1); 
  var agt=navigator.userAgent.toLowerCase();
  var mac = (agt.indexOf("mac")!=-1);
  var ie = (agt.indexOf("msie") != -1);
  var mac_ie = mac && ie;
}
