var cScrollStep = 5;
var cScrollSpeed = 20;
var ScrollTimeOut;
var TimeScrolling = 0;
var strBrowser = "";

var theDiv;

var Korein_ImageArray = new Array(
"'/style%20library/korein/tn-buttonhover.gif'"
);

function KoreinOnLoad()
{
	// Gridview van FAQ webpart inklappen
	if (theGV = document.getElementById('FAQgview'))
		CloseGroupedGridView(theGV);
	
	// Browser bepalen
	GetBrowser();
	
	theDiv = document.getElementById("KoreinContent");
	
	// Mouse scroll wheel event aan content div hangen
	if (theDiv)
		hookEvent(theDiv, "mousewheel", DoMouseWheel);
	
	// PNG images aanpassen voor IE6
	if (strBrowser == "MSIE6")
		correctPNG();
	
	// Images preloaden
	if (Korein_ImageArray.length > 0)
	{
		for (var i = 0; i < Korein_ImageArray.length; i++)
		{
			var PreLoad = new Image();
			if (Korein_ImageArray[i].length > 0)
			{
				PreLoad.src = Korein_ImageArray[i];
			}
		}
	}
}
function CloseGroupedGridView(theGV)
{
	var theRows = theGV.getElementsByTagName('TR');
	for (i = 0; i < theRows.length; i++)
	{
		if (theRows[i].className == 'ms-gb')
		{
			if (theLink = theRows[i].childNodes[0].childNodes[0])
			{
				if (document.dispatchEvent)
				{
				    var oEvent = document.createEvent('MouseEvents');
				    oEvent.initMouseEvent("click", true, true,window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
				    theLink.dispatchEvent(oEvent);
				}
				else
				{
					theLink.fireEvent('onclick');
				}
			}
		}
	}
}
function StartScrollUp()
{
	ScrollTimeOut = window.setTimeout("Scroll(" + (cScrollStep * -1) + ", true)", cScrollSpeed);
}
function StartScrollDown()
{
	ScrollTimeOut = window.setTimeout("Scroll(" + cScrollStep + ", true)", cScrollSpeed);
}
function StopScroll()
{
	clearInterval(ScrollTimeOut);
	TimeScrolling = 0;
}
function DoMouseWheel(e)
{
	e = e ? e : window.event;
	var raw = e.detail ? e.detail : e.wheelDelta;
	var normal = e.detail ? e.detail : e.wheelDelta / 40;
	
	var theStep;
	
	if (raw == normal)
		theStep = raw * 4;
	else
		theStep = raw * -1 / 10;
	
	Scroll(theStep, false);
	TimeScrolling = 0;
	
	return CancelEvent(e);
}
function Scroll(step, timeout)
{
	if (theDiv)
	{
		if (theDiv.clientHeight < theDiv.scrollHeight)
		{
			var newPos = theDiv.scrollTop;
			
			newPos = newPos + step;
			TimeScrolling = TimeScrolling + cScrollSpeed;
			
			if ((TimeScrolling >= 1000) && ((step == (cScrollStep * 1)) || (step == (cScrollStep * -1))))
					step = step * 2;
			else if ((TimeScrolling >= 2000) && ((step == (cScrollStep * 2)) || (step == (cScrollStep * -2))))
					step = step * 2;
			
			if (newPos < 0)
				newPos = 0;
			
			if (newPos > theDiv.scrollHeight)
				newPos = theDiv.scrollHeight;
				
			theDiv.scrollTop = newPos
			
			if (newPos >= (newPos + theDiv.clientHeight))
				timeout = false;
			
			if (timeout)
				ScrollTimeOut = window.setTimeout("Scroll(" + step + ", true)", cScrollSpeed);
		}
	}
}
function SearchBoxKeyPress(k)
{
	var unicode = k.keyCode? k.keyCode : k.charCode;
	
	if (unicode == 13 || unicode == 10)
	{
		k.returnValue = false;
		DoSearch();
		return false;
	}
	else
	{
		return true;
	}
}
function DoSearch()
{
	var sb = document.getElementById("searchbox");
		
	if (sb)
	{
		if (sb.value != "")
			window.location = "/pages/results.aspx?k=" + encodeURIComponent(sb.value);
		else
			alert('U heeft geen zoekwoord opgegeven');
	}
}

function NavigationBarOnClick(obj)
{
	var theLink = obj.childNodes[0].childNodes[0];
	
	if (theLink)
	{
		if (theLink.target == "_self")
		{
			document.location = theLink.href;
		}
		else
		{
			window.open(theLink.href);
		}
	}
	
	return false;
}

function GetBrowser()
{
	strBrowser = navigator.appVersion;
	if (strBrowser.indexOf('MSIE') > 0)
		strBrowser = "MSIE" + parseFloat(strBrowser.substring(strBrowser.indexOf('MSIE') + 5));
	else
		strBrowser = navigator.appName + parseInt(strBrowser);
}
function correctPNG() 
{
	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		
		if (imgName.substring(imgName.length - 3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			
			if (img.align == "left")
				imgStyle = "float:left;" + imgStyle;
				
			if (img.align == "right")
				imgStyle = "float:right;" + imgStyle;
			
			if (img.parentElement.href)
				imgStyle = "cursor:hand;" + imgStyle;
			
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			
			img.outerHTML = strNewHTML;
			i = i - 1;
		}
	}
}
function CancelEvent(e)
{
	e = e ? e : window.event;
	
	if(e.stopPropagation)
		e.stopPropagation();
	
	if(e.preventDefault)
		e.preventDefault();
	
	e.cancelBubble = true;
	e.cancel = true;
	e.returnValue = false;
	
	return false;
}
function hookEvent(element, eventName, callback)
{
	if(element.addEventListener)
	{
		if(eventName == 'mousewheel')
		{
			element.addEventListener('DOMMouseScroll', callback, false);  
		}
		else
		{
			element.addEventListener(eventName, callback, false);
		}
	}
	else if(element.attachEvent)
	{
		element.attachEvent("on" + eventName, callback);
	}
	else
	{
		// Older browsers
		var currentEventHandler = elementObj['on' + eventName];
		if (currentEventHandler == null)
			elementObj['on' + eventName] = eventHandlerFunctionName;
		else
			elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }
	}
}

// Bind scripts to events
hookEvent(window, "load", KoreinOnLoad);

// Overrule standard sharepoint scripts
function ProcessImn() { }

