/* 
// @@@
// @@ Author: Kevin Versteeg
// @@ Date: 2007-07-24
// @@ Last change: 2007-09-10
// @@ Version: 1
// @@ Revision: 1
// @@ Copyright: Ecommany B.V.
// @@ Description: Diverse sitefuncties
// @@-->	1.1 - scrollToHospital() opgenomen.
// @@$
*/
// scroll object
var scroll;
// references naar css regels in stylesheet
var aCssRule = new Array('body', 'container', 'content', 'container_body', 'container_content', 'wide_css_container_content');
// timeoutid voor resize event
var iResizeTimeoutId = -1;
// timeoutid voor scroll
var iKeepScrollingTimeoutId = -1;

var carouselIncrementer = 0;
function detectBrowser()
{ 
	var BO = new Object(); 
	BO["ie"]        = false /*@cc_on || true @*/; 
	BO["ie4"]       = BO["ie"] && (document.getElementById == null); 
	BO["ie5"]       = BO["ie"] && (document.namespaces == null) && (!BO["ie4"]); 
	BO["ie6"]       = BO["ie"] && (document.implementation != null) && (document.implementation.hasFeature != null); 
	BO["ie55"]      = BO["ie"] && (document.namespaces != null) && (!BO["ie6"]); 
	/*@cc_on
	BO["ie7"]       = @_jscript_version == '5.7';
	@*/ 
	BO["ns4"]       = !BO["ie"] &&  (document.layers != null) &&  (window.confirm != null) && (document.createElement == null); 
	BO["opera"]     = (self.opera != null); 
	BO["gecko"]     = (document.getBoxObjectFor != null); 
	BO["khtml"]     = (navigator.vendor == "KDE"); 
	BO["konq"]      = ((navigator.vendor == 'KDE') || (document.childNodes) && (!document.all) && (!navigator.taintEnabled)); 
	BO["safari"]    = (document.childNodes) && (!document.all) && (!navigator.taintEnabled) && (!navigator.accentColorName); 
	BO["safari1.2"] = (parseInt(0).toFixed == null) && (BO["safari"] && (window.XMLHttpRequest != null)); 
	BO["safari2.0"] = (parseInt(0).toFixed != null) && BO["safari"] && !BO["safari1.2"]; 
	BO["safari1.1"] = BO["safari"] && !BO["safari1.2"] && !BO["safari2.0"]; 
	return BO; 
} 
var BO = new detectBrowser();
function getInnerHeight(win)
{
	h = 0;
	if (win.innerHeight)
	{
		h = win.innerHeight;
	} else if (win.document.documentElement && win.document.documentElement.clientHeight) 
	{
		h = win.document.documentElement.clientHeight;
	} else if (win.document.body) 
	{
		h = win.document.body.clientHeight;
	}
	return h;
}
function getInnerWidth(win)
{
 	w=0;
 	if (win.innerWidth)
	{
		w = win.innerWidth;
	} else if (win.document.documentElement && win.document.documentElement.clientWidth)
	{
		w = win.document.documentElement.clientWidth;
	} else if (win.document.body)
	{
		w = win.document.body.clientWidth;
	}
	return w;
}

// 27-04-2009, Peter Vorstenbosch
// sOnderdeel toegevoegd om verschillende links aan te maken voor ika of ikno
function gotoCal(sOnderdeel, id)
{
	if (typeof id != 'undefined')
	{
		window.location = '/'+sOnderdeel+'/roadshow/index.php?id='+id;
	}
	return true;
}
// extra class voor calendar divs in Safari browser, zodat font size bijgesteld kan worden
function fixSafari()
{
	aJsObj = new Array('calendar_1','calendar_2', 'calendar_3', 'calendar_4','calendar_5');
	for (i = 0; i < aJsObj.length; i++)
	{
		jsObj = document.getElementById(aJsObj[i]);
		if (!jsObj) continue;
		for (x = 0; x < jsObj.childNodes.length; x++)
		{
			if (jsObj.childNodes[x].nodeType != 1) continue;
			if (jsObj.childNodes[x].getAttribute('class').indexOf('calendar_date') != -1)
			{
				jsObj.childNodes[x].setAttribute('class', 'calendar_date calendar_date_safari');
			}
		}
	}
	// Pijl staat te hoog, dus margin-top voor correctie.
	//$($('menu_left_more_footer_arrow_container').setStyle({'margin-top': '5px'}));
	return;
}
// container om de selectbox voor IE, omdat IE geen border kan toepassen op 't select element.
function fixIE()
{
	// Object slecht zichtbaar in IE6, via deze methode niet.
	if (BO["ie6"])
	{
		//Position.absolutize($($('tree')));
		//Position.absolutize($($('tree_click')));
		//$($('tree')).setStyle({'height' : parseInt($($('tree')).getHeight()+3) });
		//$($('tree_click')).setStyle({'height' : parseInt($($('tree_click')).getHeight()+3) });
	}
	jsObj = document.getElementById('container_hospital_selection');
	if (!jsObj) return;
	jsObj.style.height = '18px';
	jsObj.style.visibility = 'visible';
	window.focus();
	if (document.getElementsByName('hospital').length == 0)
	{
		jsObj.style.display = 'none';
	}
	return;
}
function initScroll()
{
	// Controleer of we voldoende content hebben voor het aanbieden van een scroll, zie handleScrollUpate() voor 
	// deltaH.
	deltaH = ($($('content').getHeight()) - $($('container_content_body').getHeight()) + $($('container_content_margin').getHeight()));
	// Als niet voldoende dan 'handle' verbergen
	bDisabled = (parseInt(deltaH) >= 1) ? false : true;
	if (bDisabled == true)
	{
		$(('container_scroll')).style.visibility = 'hidden';
	} else
	{
		$(('container_scroll')).style.visibility = 'visible';
	}
	// Als scroll object al aanwezig is, dan alleen trackLength bijstellen (we worden aangeroepen na resize event)
	if (typeof scroll == 'object')
	{
		// Handle weer bovenaan plaatsen, voor het geval 'ie er buiten valt
		$($('scroll_handle').setStyle({'top': '0px'}));
		//$($('content').setStyle({'top': '0px'}));
		aCssRule['content'].style.top = '0px';
		scroll.trackLength = scroll.maximumOffset() - scroll.minimumOffset();
	} else
	{
		// Scroll initialiseren, al dan niet enabled
		scroll = new Control.Slider('scroll_handle','scroll_track', {axis:'vertical', alignX: 0, alignY: -7, disabled: bDisabled});
		scroll.options.onChange = function(value)
		{
			handleScrollUpdate(value);
		};
		scroll.options.onSlide = function(value)
		{                                  
			handleScrollUpdate(value);
		};
	}
	return;
}
function handleScrollUpdate(i)
{
	// Reken (nieuwe) tophoogte van contentdiv uit
	// Hoogte van 'container_content_body' - de hoogte van 'content' + de hoogte van de 'container_content_margin' die
	// fungeert als een "top-margin"
	deltaH = ($($('content').getHeight()) - $($('container_content_body').getHeight()) + $($('container_content_margin').getHeight()));
	// Als daar een positief getalletje uitkomt dan dit toekennen aan content.top
	if (deltaH > 0)
	{
		// Verander 'top' hoogte van content div.
		//$($('content')).setStyle({'top': '-' + (parseInt(i * deltaH)+ 'px')});
		aCssRule['content'].style.top = '-' +(parseInt(i * deltaH))+ 'px';
	}
	return;
}
function calcContentHeight()
{
	// Y positie van container tov. top window
	aY = Position.cumulativeOffset($('container_content_body'));
	y = aY[1] +6;	// +2 maal 3px h. background images
	// wat witruimte tussen onderkant window en onderkant container
	whiteSpace = 50;
	y += whiteSpace;
	wH = getInnerHeight(window);
	// hoeveel ruimte hebben we nog over in het window vanaf de Y positie
	dH = wH - y;
//	cH = 422; // hoogte van container_content_body
	cH = 384;
	// als overgebleven ruimte meer is dan 422 (min. height) dan deze returnen, anders 422.
	return dH > cH ? dH : cH;
}
function resizeContent()
{
	// div met content resizen zodat ze net niet onderkant scherm raakt.
	h = calcContentHeight();
	$($('container_content_body')).setStyle({'height': h+'px'});
	// correctie uitvoeren op de hoogte van de scroll track (diverse margins)
	x = parseInt($($('container_content_margin')).getHeight() + 6 + 30); // 6 = hoogte van header/footer content images (hoekjes)
	$($('scroll_track')).setStyle({'height': (h-x)+'px'});
	// reken width uit
	w = getInnerWidth(window);
	cw = w - (424 - deltaContentWidth);
	if (w >= 1105)
	{
		if (cw > (714 + deltaContentWidth))
		{
			cw = 714 + deltaContentWidth;
		}
	} else
	{
		if (cw < (600 + deltaContentWidth))
		{
			cw = 600 + deltaContentWidth;
		}
	}

	aCssRule['container_content'].style.width = parseInt(cw)+'px';
	$($('content_footer_mid')).setStyle({'width': parseInt(cw-6)+'px'});
	$($('content_header_mid')).setStyle({'width': parseInt(cw-6)+'px'});
	if (deltaContentWidth != 0) {
		nw = $($('container_content_body')).getWidth() + 128 + 9;
	} else {
		nw = $($('container_menu_left')).getWidth() + $($('container_content_body')).getWidth() + 128 + 20;
	}
	
	//$($('container_body')).setStyle({'width': parseInt(nw)+'px'});
	aCssRule['container_body'].style.width = parseInt(nw)+'px';
	// Body resized ook niet altijd lekker mee...
	windowWidth = getInnerWidth(window);
	if (nw <= 996)
	{
		dnw = parseInt(windowWidth-nw);
		if (windowWidth < 996)
		{
			dnw = 0;
		}
		document.cookie = 'tnk_b_width='+dnw;
		aCssRule['body'].style.width = parseInt(nw+dnw)+'px';
	// In FF is dit een apart geval, tussen deze breedtes moet de body
	// een andere size krijgen.
	} else if(windowWidth >= 1005 && windowWidth <= 1025)
	{
		dnw = parseInt(windowWidth-nw-16);
		document.cookie = 'tnk_b_width='+dnw;
		aCssRule['body'].style.width = parseInt(nw+dnw)+'px';
	}	else
	{
		aCssRule['body'].style.width = '100%';
	}
	// klein browser scherm, move roadshow naar links, resize content nogmaals
	if (windowWidth <= 1024)
	{
		if (deltaContentWidth != 0) {
			aCssRule['container_content'].style.marginLeft = '5px';
			if (aCssRule['wide_css_container_content']) {
				aCssRule['wide_css_container_content'].style.marginLeft = '5px';
			}
		} else {
			$($('container_menu_left')).setStyle({'margin-left': '5px'});
		}		
		document.cookie = 'tnk_cml=yes';
		cw = cw+123; // 123 is margin left van linker menu
		document.cookie = 'tnk_cc_width='+cw;
		aCssRule['container_content'].style.width = parseInt(cw)+'px';
		$($('content_header_mid')).setStyle({'width': parseInt(cw-6)+'px'});
		$($('content_footer_mid')).setStyle({'width': parseInt(cw-6)+'px'});
		windowHeight = getInnerHeight(window);
		if (windowHeight <= 768)
		{
		// div met content resizen zodat ze net niet onderkant scherm raakt.
			h = calcContentHeight();
			// schrijf deze waarde naar een cookie
			document.cookie = 'tnk_ccb_height='+h;
			$($('container_content_body')).setStyle({'height': h+'px'});
			// correctie uitvoeren op de hoogte van de scroll track (diverse margins)
			x = parseInt($($('container_content_margin')).getHeight() + 6 +30); // 6 = hoogte van header/footer content images (hoekjes)
			$($('scroll_track')).setStyle({'height': (h-x)+'px'});
		}
	} else
	{
		if (deltaContentWidth != 0) {
			aCssRule['container_content'].style.marginLeft = '128px';
			if (aCssRule['wide_css_container_content']) {
				aCssRule['wide_css_container_content'].style.marginLeft = '128px';
			}
		} else {
			$($('container_menu_left')).setStyle({'margin-left' : ''});
		}
		document.cookie = 'tnk_cml=no';
	}
	return;
}

// handelt scroll m.b.v. scroll wiel af
function handleWheelUpdate(i) 
{
	// zie handleScrollUpdate(), zelfde hoogte wordt hier berekend.
	h = ($($('content').getHeight()) - $($('container_content_body').getHeight()) + $($('container_content_margin').getHeight()));
	// we rekenen 60 pixels per 'scroll' actie
	// 60 / h levert getal tussen 0 en 1 op
	// zodat we dit aan setValueBy() kunnen geven.
	dh = 60 / h;
	// scroll down
	if (i <= -1)
	{
		// i kan ook -2 of minder zijn, dan scrollt gebruiker wat 'sneller' waardoor 
		// er bv. 2x gescroll wordt.
		// maar moet wel een positief getal uitrollen, want we scrollen omlaag dus dh moet toenemen, daarom '-i'
		dh = -i * dh;
		scroll.setValueBy(dh);
	// scroll up
	} else
	{
		dh = i * dh;
		scroll.setValueBy(-dh);	
	}
	return;
}
// rekent wheel delta waarde uit 
function handleWheel(event)
{
	var delta = 0;
	if (event.wheelDelta)
	{
		delta = event.wheelDelta/120; 
	} else if (event.detail)
	{
		delta = -event.detail/3;
	}
	if (delta)
	{
		handleWheelUpdate(delta);
	}
  if (event.preventDefault)
	{
    event.preventDefault();
	}
  event.returnValue = false;
}

// sommige divs resizen we afhankelijk van window size
// als we dit echter 'inline' doen, kunnen we dat niet meer in de print styling overrulen
// dus editen we de css rules: deze functie zoekt de rule refs op (niet alle resized divs worden op deze 
// wijze aangepast!)
function findCssRules()
{
	s = document.styleSheets[0].cssRules ? document.styleSheets[0].cssRules : document.styleSheets[0].rules;
	for (i = 0; i < s.length; i++)
	{
		if (s[i].selectorText == '#content')
		{
			aCssRule['content'] = s[i];
		}
		if (s[i].selectorText == '#container_content')
		{
			aCssRule['container_content'] = s[i];
		}
		if (s[i].selectorText == '#container_body')
		{
			aCssRule['container_body'] = s[i];
		}
		if (s[i].selectorText == '#container')
		{
			aCssRule['container'] = s[i];
		}
		if (s[i].selectorText == 'body' || s[i].selectorText == 'BODY')
		{
			aCssRule['body'] = s[i];
		}
	}
	s = document.styleSheets[1].cssRules ? document.styleSheets[1].cssRules : document.styleSheets[1].rules;
	for (i = 0, len = s.length; i < len; i++)
	{
		if (s[i].selectorText == '#container_content')
		{
			aCssRule['wide_css_container_content'] = s[i];
			break;
		}
	}
	return;
}

// list-items van menu button underlinen, button background image alteren
function handleMenuMouseOver(event, jsObj)
{
	if (event.type == 'mouseover')
	{
		try
		{
			jsObj.childNodes[1].style.textDecoration = 'underline';
		} catch(e) 
		{
			try
			{
				jsObj.childNodes[0].style.textDecoration = 'underline';
			} catch (e) {};
		};
		// Als class 'selected', dan geen background mouseover, anders wel
		if (jsObj.className.indexOf("_selected") == -1)
		{
			jsObj.className += '_hover';
		}
	} else if (event.type == 'mouseout')
	{
		try
		{
			jsObj.childNodes[1].style.textDecoration = '';
		} catch(e)
		{
			try
			{
				jsObj.childNodes[0].style.textDecoration = '';
			} catch (e) {};
		};
		// Als class 'selected', dan geen background mouseover, anders wel
		if (jsObj.className.indexOf("_selected") == -1)
		{
			jsObj.className = jsObj.className.replace('_hover', '');
		}
	}
	return;
}
// Meet positie van ziekenhuis select container, en scrollt daar naar toe indien aangeroepen
function scrollToHospital()
{
	aY = Position.positionedOffset($($('container_hospital')));
	y = aY[1];
	// We simuleren een scroll actie, die afgehandeld wordt door handleWheelUpdate(), daar is ook uitleg over 
	// de /60 te vinden
	handleWheelUpdate('-'+parseInt(y/60));
	return;
}
function onLoad()
{

	if (BO.safari)
	{
		fixSafari();
	}
	if (BO.ie)
	{
		// maho toevoeging 24-08-2007:
		// IE heeft de nare eigenschap om background images bij scrollen opnieuw op te blijven halen
		// dit verzorgt een soort van flikkering in het scherm. De volgende regel code zorgt er voor dat
		// IE niet meer gaat checken voor een nieuwere versie op de server en het plaatje uit de cache gebruikt.
		// functie blijft werken voor de gehele sessie!
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}

		fixIE();
	}
	findCssRules();
	resizeContent();
	initScroll();
	Event.observe(window, 'resize', function() { clearTimeout(iResizeTimeoutId); iResizeTimeoutId = setTimeout(function() {resizeContent(); initScroll();}, 100) });
//	Event.observe('content', 'DOMMouseScroll', handleWheel);
//	Event.observe('content', 'mousewheel', handleWheel);
	Event.observe(window, 'DOMMouseScroll', handleWheel);
	Event.observe('container', 'mousewheel', handleWheel);
	Event.observe('scroll_track_axis_top', 'click', function() { handleWheelUpdate(1); });
	Event.observe('scroll_track_axis_bottom', 'click', function() { handleWheelUpdate(-1); });
	
	if (window.location.href.indexOf('hospital') != '-1'
		&& window.location.hash == '') {
		window.location.hash = '#tab2';
	}
	hash = window.location.hash;
	if ('' != hash && hash.indexOf('tab') != '-1') {
		showCard(hash.replace('#tab', ''));
	}
	if (bJsScroll)
	{
		scrollToHospital();
	}
	return;
}
function initCarouselThemesHp()
{
	carouselThemesHp();
	setTimeout(initCarouselThemesHp, 6000);
}
function carouselThemesHp()
{
	divs		= $('themeContainer').getElementsByClassName('themesetContainer');
	min			= 0;
	max			= 9;
	classHidden	= ' hidden';
	if (carouselIncrementer < min || carouselIncrementer > max) carouselIncrementer = min;
	
	current		= carouselIncrementer;
	for (x = 0, len = divs.length; x < len; x++) {
		if (divs[x].className.indexOf('hidden') == -1) {
			divs[x].className	= divs[x].className 
								+ classHidden;
		}
	}	
	divs[current].className	= divs[current].className.replace(classHidden, '');
	++carouselIncrementer;
}
function switchContentHolder(nr)
{
	divs 		= $('content').getElementsByClassName('contentHolder');
	classHidden	= ' contentHolderHidden';
	
	for (x = 0, len = divs.length; x < len; x++) {
		if (divs[x].className.indexOf(classHidden) == -1) {
			divs[x].className	= divs[x].className 
								+ classHidden;
		}
	}
	visibleContentHolder			= $('contentHolder' + nr);
	visibleContentHolder.className	= visibleContentHolder.className.replace(classHidden, ''); 
}
function switchTab(nr)
{
	tabs			= $('container_content').getElementsByClassName('contentTab');
	classActive		= ' contentTabActive';
	classInactive	= ' contentTabInactive'
	for (x = 0, len = tabs.length; x < len; x++) {
		if (tabs[x].className.indexOf(classActive) != -1) {
			tabs[x].className	= tabs[x].className.replace(classActive, classInactive); 
		}
	}
	visibleTab				= $('tab' + nr);
	visibleTab.className	= visibleTab.className.replace(classInactive, classActive);
}
function showCard(nr)
{
	switchContentHolder(nr);
	switchTab(nr);
	initScroll();
}
