// IMAGE PRELOADS

function newImage(arg) 
{
  if (document.images) 
  {
    rslt = new Image();
    rslt.src = arg;
    return rslt;
  }
}

function changeImages() 
{
  if (document.images && (preloadFlag == true)) 
  {
    for (var i=0; i<changeImages.arguments.length; i+=2) 
    {
      panel     = document.getElementById(changeImages.arguments[i]);
      container = document.getElementById("nav_container");
      if (changeImages.arguments[i+1] == "On")
      {
      	panel.style.background = panel_background;
//      	panel.style.background = "#C26F54";
      	panel.style.color      = container.style.background;
      }
      else
      {
      	panel.style.background = container.style.background;
//      	panel.style.color      = "#C26F54";
      	panel.style.color      = panel_background;
      }
    }
  }
}

var preloadFlag = false;
function preloadImages() 
{
  if (document.images) {
    preloadFlag = true;
  }
}




// SHOW/HIDE LAYERS

var w3c = document.getElementById? true : false;
var ns4 = document.layers? true : false;
if (w3c) {
  layerPre="document.getElementById(";
  layerPst=").style";
  showRef="block";
  hideRef="none";
  posPst="px";
} else if (ns4) {
  layerPre="document.layers[";
  layerPst="]";
  showRef="block";
  hideRef="none";
  posPst="";
} else {
  layerPre="document.all[";
  layerPst="].style";
  showRef="block";
  hideRef="none";
  posPst="";
}    
function showLayer(theLayer) {
      eval(layerPre + '"' + theLayer +'"' + layerPst + '.display = showRef');
}
function hideLayer(theLayer) {
      eval(layerPre + '"' + theLayer +'"' + layerPst + '.display = hideRef');
}



// global functions
var browser = {};
browser.ua = navigator.userAgent.toLowerCase();
browser.win = (browser.ua.indexOf("windows") > 0);
browser.mac = (browser.ua.indexOf("mac") > 0);
browser.opera = (browser.ua.indexOf("opera") > 0);
if (!browser.opera) 
{
	browser.ie = (browser.ua.indexOf("msie") > 0);
	browser.ie5_5 = (browser.ua.indexOf("msie 5.5") > 0 || browser.ua.indexOf("msie 5.6") > 0);
	browser.ie5 = (browser.ua.indexOf("msie 5") > 0);
}
browser.gecko = (browser.ua.indexOf("gecko") > 0);
browser.ns4 = (document.layers);

function getPageOffsetLeft(el) 
{
	var total = el.offsetLeft;
	for(var parent = el.parentNode;(parent = parent.offsetParent) != null;total += parent.offsetLeft);
	return total;
}

function getPageOffsetTop(el) 
{
	var total = el.offsetTop;
	for(var parent = el.parentNode;(parent = parent.offsetParent) != null;total += parent.offsetTop);
	return total;
}


// DHTML MENU
// Gall&Gall mouseover images
function turnMenuOn(menuitem) 
{
  changeImages('nav_menu'+menuitem,"On");
}
function turnMenuOff(menuitem) 
{
  changeImages('nav_menu'+menuitem,"Off");
}


// GallPage namespace with embedded functionality
var GallPage = function() {
	var page = this; // needed for nested functions
	
	this.navigation = {
		items: [],
		active: null,
		timerId: 0,
	
		show: function(el) {
			if (typeof el == "string") el = document.getElementById(el);

			//addad image swaps for Gall & gall
			//alert (el.id.substr(8,9));
			turnMenuOn(el.id.substr(8,9));

			if (el != page.navigation.active) return false;
			if (el.panel.hideTimerId != 0) {
				clearTimeout(el.panel.hideTimerId);
				el.panel.hideTimerId = 0;
			}
			if (el.panel.showTimerId == 0) {
				el.panel.style.clip = "rect("+ el.panel.height +"px auto auto auto)";
				el.panel.style.visibility = "visible";
			}
			el.panel.top = Math.min(el.panel.top + 15, el.panel.maxTop);
			el.panel.style.top = el.panel.top + "px";
			el.panel.style.clip = "rect("+ (el.panel.maxTop - el.panel.top) +"px auto auto auto)";
			if (el.panel.top != el.panel.maxTop)
				el.panel.showTimerId = setTimeout('page.navigation.show("'+ el.id +'");', 30);
		},
		
		hide: function(el) 
		{
			if (typeof el == "string")
				el = document.getElementById(el);

			//addad image swaps for Gall & gall
			//alert (el.id.substr(8,9));
			turnMenuOff(el.id.substr(8,9));

			if (page.navigation.active == el) page.navigation.active = null;
			if (el.panel.showTimerId != 0) 
			{
				clearTimeout(el.panel.showTimerId);
				el.panel.showTimerId = 0;
			}
			if (el.panel.top == el.panel.maxTop - el.panel.height) 
			{
				el.panel.style.visibility = "hidden";
				//el.panel.style.left = "-300px";
			}
			else {
				el.panel.top = Math.max(el.panel.top - 15, el.panel.maxTop - el.panel.height);
				el.panel.style.top = el.panel.top + "px";
				el.panel.style.clip = "rect("+ (el.panel.maxTop - el.panel.top) +"px auto auto auto)";
				el.panel.hideTimerId = setTimeout('page.navigation.hide("'+ el.id +'");', 30);
			}
		},
		
		/**
			Searches the divs with the dropdowns (nav_panel) and adds properties and functionality 
			to them and to the corresponding firstlevel nav-divs (nav_item).
		*/
		init: function() {
			var nav = page.navigation;
			var divs = document.getElementsByTagName("div");
			for (var i = 0; i < divs.length; i++) {
				var el = divs.item(i);
				if (el.id.indexOf("nav_panel") == -1) 
					continue;
				var j = nav.items.length;
				var item = document.getElementById("nav_menu" + j);
				if (item == null) 
					continue;
				nav.items[j] = item;
      		logo = document.getElementById("nav_bar");

//	var scherm_breedte 	= screen.availWidth;
	var scherm_breedte 	= document.body.offsetWidth ;
	var scherm_rest		= (scherm_breedte - 772) / 2;
	var scherm_links	= scherm_rest+225;			//tel lengte logo erbij op

				item.style.left = (scherm_links+(j*100))+"px";
				
				item.panel = el;
				if (browser.mac && browser.ie5) 
				{
					item.panel.width = 200;
					item.panel.style.width = item.panel.width + "px";
				}
				else
				{
					item.panel.width = item.panel.offsetWidth;
				}	
				item.panel.style.left = item.style.left;
				
				item.panel.height = item.panel.offsetHeight;
				item.panel.maxTop = 76;
				item.panel.top = item.panel.maxTop - item.panel.height;
				item.panel.style.top = item.panel.top + "px";
				item.panel.showTimerId = 0;
				item.panel.hideTimerId = 0;
				
				item.onmouseover = function() 
				{
					if (nav.active != null) 
					{
						clearTimeout(nav.timerId);
						if (nav.active == this)
							return;
						nav.hide(nav.active);
					}
					nav.active = this;
					//this.panel.style.left = Math.min(getPageOffsetLeft(this) - 0, document.body.clientWidth - this.panel.width) + "px";
					nav.show(this);
				};
				item.onmouseout = function() 
				{
					nav.timerId = setTimeout('page.navigation.hide(page.navigation.active);', 200);
				}
				item.panel.onmouseover = function() 
				{
					clearTimeout(nav.timerId);
				};
				item.panel.onmouseout = function() 
				{
					if (this.hideTimerId == 0)
						nav.timerId = setTimeout('page.navigation.hide(page.navigation.active);', 200);
				};
			}
		}
		
	}; // end navigation
	
} // end GallPage



// INITIALISATION

// window load and init
// Gebruik deze functie ipv je functies in onload te zetten;
// Bv. addInit(toonRandomWereld);

var inits = [];
function addInit(fn) {
	if (typeof fn == "function") inits[inits.length] = fn;
}
var oldLoad = (window.onload) ? window.onload : new Function();
window.onload = function() {
	oldLoad();
	for (var i in inits) inits[i]();
};

// Startup the Gall namespace.
var page = new GallPage();
addInit(preloadImages);
// Navigatin.init() is done in the page content (so you don't have to wait for images
// to have been loaded before the mainnav works).
//addInit(page.navigation.init);

