var cntDefault = 1;
var cntOther   = 1;
var topic      = window.location.href.substring(window.location.href.lastIndexOf('/')+1, window.location.href.length -4);
var mode       = 'default'
var to         = null;
var picCont    = null;
var picGallery = null;
var slideshowPic = null;

// all pages now use the same pictures, except for facility
var pictureArr = new Array();
pictureArr[0] = "image001.jpg";
pictureArr[1] = "image002.jpg";
pictureArr[2] = "image003.jpg";
pictureArr[3] = "image004.jpg";
pictureArr[4] = "image005.jpg";
pictureArr[5] = "image006.jpg";
pictureArr[6] = "image007.jpg";
pictureArr[7] = "image008.jpg";
pictureArr[8] = "image009.jpg";
pictureArr[9] = "image010.jpg";
pictureArr[10] = "image011.jpg";
pictureArr[11] = "image012.jpg";


jQuery(document).ready(function() {  
  //opening and closing of extra  info for building, also initiates separate slideshow for facility.net 
	if(document.getElementById('building') != null && document.getElementById('building').tagName.toLowerCase() == 'div')
	{
		var sim = new subInfoManager();
		sim.hideAll();
	}
	
	//hide the default slideshow picture and load the gallery instead
	slideshowPic = jQuery('#picture').hide();
	picCont = jQuery('#picture-container').css({border : 'none',
	                                            backgroundColor : '#444'});
	picGallery = jQuery('<div id="picture-gallery">').appendTo(picCont).load('/gallery.php', enableGallery);
  
  //opening and closing of extra technical info 
  if(window.location.href.indexOf('technical') > -1) {
   var foo = new toggleTechnicalUl();
   foo.hideAll();
  }
});

//init colorbox for the picture gallery
function enableGallery() {
  jQuery('#picture-container').find('a').colorbox({opacity: 0.4,
                                                  current: '{current} / {total}'});
}


function picChange()
{
	window.clearTimeout(to);

  this.imgObj  = document.getElementById('picture');
  this.imgPath = this.imgObj.src.substr(0, this.imgObj.src.lastIndexOf('/') + 1);
  this.imgName = this.imgObj.src.replace(this.imgPath, '');
  
	if(mode == 'default')
	{
    this.imgPath = '../pics/slideshow/'
    this.imgObj.src = this.imgPath + pictureArr[cntDefault]
    cntDefault = cntDefault < pictureArr.length -1 ? cntDefault + 1 : 0;
	}
  else
	{
    this.imgPath = '../pics/' + mode + '/'
    this.imgObj.src = this.imgPath + otherArr[cntOther]
    cntOther = cntOther < otherArr.length -1 ? cntOther + 1 : 0;
	}
  to = window.setTimeout("picChange()", 3000);
}


function toggleUl(sender)
{
  var childUl = sender.getElementsByTagName('ul')[0];
  childUl.style.display = childUl.style.display == 'block' ? 'none' : 'block';
}

function subInfoManager()
{
	var i = 0;
	var thisObj   = this;
	this.aArr     = document.getElementById('contents').getElementsByTagName('a')
	for(i = 0; i < this.aArr.length; i++)
	{
		this.aArr[i].onclick = function()
		{
			var tgt = document.getElementById(this.className);
			var currState = tgt.style.display == 'none' ? false : true;
			thisObj.hideAll();
	    tgt.style.display = currState ? 'none' : 'block';
	    if(this.className == 'facility-net')
	    {
	      //hide the gallery and show the slideshow pic
	      picGallery.hide();
	      
	    	mode = 'facility';
	    	picChange();
	    		      
	      slideshowPic.show();
	    }
	    else
	  	{
	  	  //hide the picture and show the gallery
	      slideshowPic.hide();
	      picGallery.show();
	  	}
		}
	}
	
	this.infoArr = new Array();
	this.infoArr[this.infoArr.length] = document.getElementById('administrative');
	this.infoArr[this.infoArr.length] = document.getElementById('technical');
	this.infoArr[this.infoArr.length] = document.getElementById('gestion');
	this.infoArr[this.infoArr.length] = document.getElementById('facility-net');
	
	this.hideAll = function ()
	{
	  for(i = 0; i < this.infoArr.length; i++)
	  {
  		this.infoArr[i].style.display = 'none' 
	  }
  }
}


function toggleTechnicalUl()
{
  this.liArr  = document.getElementsByTagName('li');
  this.ulArr  = new Array();
  var selfRef = this;
  for(var i = 0; i < this.liArr.length; i++) {
    if(this.liArr[i].className == 'toggleUl') {
      this.liArr[i].ul = this.liArr[i].getElementsByTagName('ul')[0];
      this.liArr[i].ul.style.display = 'none';
      
      this.liArr[i].onclick    = function() {
        selfRef.hideAll();
        this.ul.style.display = 'block';
      }
      
      this.liArr[i].onmouseover    = function() {
      }
      
      this.liArr[i].onmouseout    = function() {
      }
        
      this.ulArr.push(this.liArr[i].ul);
    }
  }
        
  this.hideAll  = function() {
    for(var i = 0; i < this.ulArr.length; i++) {
      this.ulArr[i].style.display = 'none';
    }
  }
}



function launchDemo(lang_id)
{
  var l = Math.ceil((screen.width - 780) / 2);
  var myWindow = window.open('demo/index.htm','facilityhelp','width=780,height=580,left=' + l + ',top=20');
  myWindow.focus();
  return false;
}