/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Framework for ULB pages
//
// Cellule web, ULB, 2008
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var slides = null;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loading a page
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function frameworkLoad()
{
//alert("cookie INTRAULB: "+getCookie("INTRAULB")+", cookie EXTRAULB: "+getCookie("EXTRAULB"));
setupSearchForm();
if (slides!=null) setupSlideshows();
}

function setupSearchForm()
{
var extraulb = getCookie("EXTRAULB");
if (extraulb.indexOf("SRCH=")==-1) return;

var tmp = extraulb.substr(extraulb.indexOf("SRCH=")+5, 2);
if (tmp!="PH" && tmp!="SU" && tmp!="SG") return;

var form = document.getElementById("pageHeaderSearch");
if (form==null) return false;

if (tmp=='PH') form.type[1].checked = true;
else form.type[0].checked;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Cookies management
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getCookie(name)
{
if (document.cookie.length>0)
  {
  var start=document.cookie.indexOf(name + "=");
  if (start!=-1)
    {
    start=start + name.length+1;
    var end=document.cookie.indexOf(";",start);
    if (end==-1) end=document.cookie.length;
    return unescape(document.cookie.substring(start, end));
    }
  }
return "";
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Search keyword/phonebook
//
// Cellule web, ULB, 2008
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//----------
// search activated
//
function search()
   {
   //get the form object
   var form = document.getElementById("pageHeaderSearch");
   if (form==null) return false;
   
   //read keyword and trim it
   var keyword = form.keyword.value;
   while (keyword.charAt(0)==' ') keyword=keyword.substring(1);
   while (keyword.length>0 && keyword.charAt(keyword.length)==' ') keyword=keyword.substring(0, keyword.length - 1);
   form.keyword.value = keyword;
   if (keyword.length==0) return false;

   if (form.type[0].checked) 
      {
      // form.q.value=keyword;
      // form.action = "http://www.google.be/univ/ulb";
      form.action = "http://newsite.ulb.ac.be:8080/commons/sitesearch?mode=site-search";
      form.method = "get";
      }
   else if (form.type[1].checked) 
      {
      form.action = "http://newsite.ulb.ac.be:8080/commons/phonebook?mode=site-search";
      form.method = "get";
      }

   return true;
   }

function annuaireSearch()
   {
   //get the form object
   var form = document.getElementById("annuaireSearch");
   if (form==null) return false;
   
   //read keyword and trim it
   var keyword = form.keyword.value;
   while (keyword.charAt(0)==' ') keyword=keyword.substring(1);
   while (keyword.length>0 && keyword.charAt(keyword.length)==' ') keyword=keyword.substring(0, keyword.length - 1);
   form.keyword.value = keyword;
   if (keyword.length==0) return false;

   form.action = "http://newsite.ulb.ac.be:8080/commons/phonebook?mode=site-search";
   form.method = "get";
   return true;
   }


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Menubar object
//
// Cellule web, ULB, 2008
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//----------
// object's constructor
//
function MenuBarConstructor(id, imgId, imgOn, imgOff)
   {
   this.id = id;
   this.menuImg = null;
   this.menuImgId = imgId;
   this.menuImgOnUrl = imgOn;
   this.menuImgOffUrl = imgOff;

   this.menuImg = null;
   this.menuImgUrl = null;
   this.mainmenuTimer = -1;
   this.menuOn = new Array();
   this.menuLastId = "";

   this.menuBarOn = MenuBarMenuBarOn;
   this.menuBarOff = MenuBarMenuBarOff;
   this.mainMenuOn = MenuBarMainMenuOn;
   this.mainMenuKeep = MenuBarMainMenuKeep;
   this.mainMenuOff = MenuBarMainMenuOff;
   this.mainMenuGo = MenuBarMainMenuGo;
   this.displayMenuOn = MenuBarDisplayMenuOn;
   this.displayMenuOff = MenuBarDisplayMenuOff;
   }

			
//----------
// select an item on the menu bar (onmouseover)
//
function MenuBarMenuBarOn(id, imgUrl, color)
   {
   if (imgUrl!=null)
      {
      var img = document.getElementById(this.id+"_"+id);
      if (img==null) return;
      img.src=imgUrl;
      }
   else
      {
/*
      var div = document.getElementById(this.id+"_"+id+"_link");
      if (div==null) return;
      div.style.borderColor=color;
*/
      }
   }

//----------
// unselect an item on the menu bar (onmouseout)
//
function MenuBarMenuBarOff(id, imgUrl)
   {
   if (imgUrl!=null)
      {
      var img = document.getElementById(this.id+"_"+id);
      if (img==null) return;
      img.src=imgUrl;
      }
   else
      {
/*
      var div = document.getElementById(this.id+"_"+id+"_link");
      if (div==null) return;
      div.style.borderColor="";
*/
      }
   }

//----------
// display a menu pane (onmouseover on a menu bar item or a menu item)
//
function MenuBarMainMenuOn(id)
   {
   this.displayMenuOn(id);
   }

//----------
// keep current menu panes displayed
//
function MenuBarMainMenuKeep()
   {
   clearTimeout(this.mainmenuTimer);
   }

//----------
// hide all menu panes after a timeout (allowing to enable them again with another onmouseover without display flicks)
//
function MenuBarMainMenuOff()
   {
   this.mainmenuTimer = setTimeout(this.id+".displayMenuOff()",400);
   }

//----------
// called when the user click on a link: turn menu panes display off
//
function MenuBarMainMenuGo()
   {
   this.displayMenuOff();
   return true;
   }

//----------
// display menu panes: do the job
//
function MenuBarDisplayMenuOn(id)
   {
   if (this.menuImgId!=null)
      {
      if (this.menuImg==null) 
         {
         this.menuImg = document.getElementById(this.id+"_"+this.menuImgId);
         if (this.menuImg!=null)
            {
            this.menuImgUrl = this.menuImg.src;
            if (this.menuImgUrl.indexOf("/")!=-1) this.menuImgUrl= this.menuImgUrl.substring(0,this.menuImgUrl.lastIndexOf("/")+1);
            else if (this.menuImgUrl.indexOf("\\")!=-1) this.menuImgUrl= this.menuImgUrl.substring(0,this.menuImgUrl.lastIndexOf("\\")+1);
            else this.menuImgUrl="";
            }
         }
      if (this.menuImg!=null)
         {
         this.menuImg.src = this.menuImgUrl+this.menuImgOffUrl;
         }
      }

   if (id.indexOf(".")==-1)
      {
      var tmp = id;
      if (tmp.indexOf(":")!=-1) tmp = tmp.substring(0,tmp.indexOf(":"));
      if (this.menuLastId.indexOf("."+tmp+".")!=-1)
         {
         id = this.menuLastId.substring(0, this.menuLastId.lastIndexOf("."+tmp+"."))+"."+id;
         }
      }

   clearTimeout(this.mainmenuTimer);
   var menus = id.split(".");
   for (var i=0; i<menus.length; i++) menus[i] = menus[i].split(":");
   
   while (this.menuOn.length > 0)
       {
       var div = this.menuOn.pop();
       var ok = false;
       for (var i=0; i<menus.length; i++) if (menus[i][0] == div.id) ok = true;

       if (!ok) div.style.display="none"; 
       }

   this.menuLastId = "";

   for (var i=0; i<menus.length; i++) 
       {
       var div = document.getElementById(this.id+"_"+menus[i][0]);
       if (div!=null) 
          {
          if (menus[i].length>1) div.style.top  = menus[i][1]+"px";
          if (menus[i].length>2) div.style.left = menus[i][2]+"px";
          if (menus[i].length>3) div.style.width= menus[i][3]+"px"; 
    
          div.style.display="block"; 
          this.menuOn[this.menuOn.length]=div;
          this.menuLastId += menus[i][0]+".";
          }
       }
   }


//----------
// hide menu panes: do the job
//
function MenuBarDisplayMenuOff()
   {
   clearTimeout(this.mainmenuTimer);
   while (this.menuOn.length > 0)
       {
       var div = this.menuOn.pop();
       div.style.display="none"; 
       }

   if (this.menuImg!=null)
      {
      this.menuImg.src = this.menuImgUrl+this.menuImgOnUrl;
      }
   }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Add to bookmark
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 function addBookmark(url, title)
    {
    if (window.sidebar) window.sidebar.addPanel(title, url,""); // Mozilla Firefox
    else if( window.external) window.external.AddFavorite( url, title); // IE
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Display/Hide masked content on screen
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var maskBackground = null;
var maskContent = null;
var maskContentTop = 0;
var maskContentWidth = 0;
var maskContentScroll = -1;
var maskTimer = -1;

function displayMask(maskId, imgId)
   {
   var maskImg = document.getElementById(imgId);
   displayPopupWindow(maskId, maskImg.width);
   }

function displayMaskPopup(maskId, maskWidth)
   {
   displayPopupWindow("popup:"+maskId, 600);
   }

function displayPopupWindow(maskId, maskWidth)
   {
   clearTimeout(maskTimer);

   // get DIV objects
   maskBackground = document.getElementById('maskBackground');
   maskContent = document.getElementById(maskId);

   // get scroll
   var xScroll, yScroll;	
   if (window.innerHeight && window.scrollMaxY) 
      {	
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
	}
   else if (document.body.scrollHeight > document.body.offsetHeight)
      {
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
	}
   else
      {
	xScroll = document.body.offsetWidth;
	yScroll = document.body.offsetHeight;
	}
	
   // get page and window widh and height
   var windowWidth, windowHeight;
   if (self.innerHeight) 
      {
	windowWidth = self.innerWidth;  // not IE
	windowHeight = self.innerHeight;
	}
   else if (document.documentElement && document.documentElement.clientHeight) 
      {
	windowWidth = document.documentElement.clientWidth; // IE 6
	windowHeight = document.documentElement.clientHeight;
	}
   else if (document.body) 
      {
      windowWidth = document.body.clientWidth; // other IE
	windowHeight = document.body.clientHeight;
	}	
   if (yScroll < windowHeight) pageHeight = windowHeight;
   else pageHeight = yScroll;
   if(xScroll < windowWidth) pageWidth = windowWidth;
   else pageWidth = xScroll;

   // set height of maskBackground DIV to page's height
   maskBackground.style.height = (pageHeight + 'px');
   maskContent.style.width = (maskWidth + 2*18)+"px";


   //shows the two DIV 
   maskBackground.style.display = 'block';
   maskContent.style.display = 'block';
   maskContent.style.width = maskWidth + 2*18; //adjust the width to the displayed image

   //gets maskContent DIV width and height
   contentWidth = maskContent.offsetWidth;
   contentHeight = maskContent.offsetHeight;

   //places maskContent DIV in the window's center
   maskContentTop = ((windowHeight - contentHeight) / 2);
   if (maskContentTop<0) maskContentTop = 0;

   maskContentLeft = ((pageWidth - contentWidth) / 2);
   if (maskContentLeft<0) maskContentLeft = 0;	

   maskContent.style.left = maskContentLeft + "px";

   maskContentScroll = -1;

   moveMask();
   }

function hideMask()
   {
   if (maskContent==null) return;
   clearTimeout(maskTimer);
   maskBackground.style.display = 'none';
   maskContent.style.display = 'none';
   maskBackground = null;
   maskContent = null;
   }

function moveMask()
   {
   if (maskContent==null) return;

   var yPageScroll;

   if (self.pageYOffset) yPageScroll= self.pageYOffset;
   else if (document.documentElement && document.documentElement.scrollTop) yPageScroll= document.documentElement.scrollTop; //IE 6
   else if (document.body) yPageScroll= document.body.scrollTop; //IE

   var topScroll = maskContentTop + yPageScroll;
   if (topScroll<0) topScroll = 0;

   if (topScroll == maskContentScroll) 
      {
      maskTimer = setTimeout(moveMask, 100);
      return;
      }

   maskBackground.style.top = yPageScroll + "px";
   maskContent.style.top = topScroll + "px";
   maskContentScroll = topScroll;
   maskTimer = setTimeout(moveMask, 100);
   }
