var cScrollStep          = 5;
var cScrollSpeed         = 20;
var ScrollTimeOut;
var TimeScrolling        = 0;
var strBrowser           = "";
var BreadCrumbOffset     = -23;
var TopDivOffset         = 16;
var BottomDivOffset      = -8;
var NewsDivHeightOffset  = 10;
var MinimumMenuDivHeight = 308;
var PageImageMaxHeight   = 165;
var AlbumImageShowing    = -1;

var theDiv;
var theND;

var KKP_ImageArray = new Array(
"/style library/koreinkinderplein/button-blauw.gif","/style library/koreinkinderplein/button-groen.gif",
"/style library/koreinkinderplein/button-oranje.gif","/style library/koreinkinderplein/button-roze.gif"
);

function KoreinKinderpleinOnLoad()
{
	// Gridview van FAQ webpart inklappen
	if (theGV = document.getElementById('FAQgview'))
		CloseGroupedGridView(theGV);
	
	// Browser bepalen
	GetBrowser();
	
	theDiv = document.getElementById("KoreinContent");
	theND = document.getElementById("NewsDiv");
	
	var theCD = document.getElementById("ContentDiv");
	var theTD = document.getElementById("TopDiv");
	var theCR = document.getElementById("ConsoleDiv");
	var theMR = document.getElementById("MenuRow");
	var theBC = document.getElementById("BreadcrumbDiv");
	var theMD = document.getElementById('MenuDiv');
	var thePD = document.getElementById('ProductDiv');
	var theRBD = document.getElementById("RightButtonsDiv");
	
	// Mouse scroll wheel event aan content div hangen
	if (theDiv)
		hookEvent(theDiv, "mousewheel", DoMouseWheel);
	
	if (theND)
		hookEvent(theND , "mousewheel", DoMouseWheelNews);
	
	// Positie van breadcrumb goed zetten
	if (theTD && theCR && theMR && theBC)
	{
		theBC.style.top = theTD.clientHeight + theCR.clientHeight + theMR.clientHeight + BreadCrumbOffset;
	}
	
	// PNG images aanpassen voor IE6
	if (strBrowser == "MSIE6")
		correctPNG();
	
	// Top marge van right buttons div aanpassen, afhankelijk van de inhoud van de nieuws div
	if (theND && theRBD)
		theND.style.height = theCD.clientHeight + NewsDivHeightOffset - theRBD.offsetHeight;
	
 	// Menu DIV groter maken als de inhoud te groot is
	if (theMD)
		if (theMD.offsetHeight < MinimumMenuDivHeight)
			theMD.style.height = MinimumMenuDivHeight + 'px';
	
	// Producten DIV zichtbaar maken
	if (thePD)
		thePD.style.display = 'block';
		
	// Als er page images zijn, dan de hoogte controleren en als ze te hoog zijn, dan de afmeting aanpassen
	if (PI = document.getElementById('PageImage1'))
	{
		var theImage = PI.getElementsByTagName('IMG')[0];
		if (theImage)
		{
			if (theImage.clientHeight > PageImageMaxHeight)
			{
				theImage.style.width = 'auto';
				theImage.style.height = PageImageMaxHeight + 'px';
			}
		}
		PI.style.visibility = 'visible';
	}
	if (PI = document.getElementById('PageImage2'))
	{
		var theImage = PI.getElementsByTagName('IMG')[0];
		if (theImage)
		{
			if (theImage.clientHeight > PageImageMaxHeight)
			{
				theImage.style.width = 'auto';
				theImage.style.height = PageImageMaxHeight + 'px';
			}
		}
		PI.style.visibility = 'visible';
	}
	
	// Als de console balk wordt weergegeven, dan ronde hoekje verbergen
	if (theCR)
	{
		var theCT = theCR.getElementsByTagName("table");
		if (theCT.length > 0)
		{
			var theTD = document.getElementById("ContentTopCorners");
			var theBD = document.getElementById("ContentBottomCorners");
			
			if (theTD)
				theTD.style.display = "none";
			
			if (theBD)
				theBD.style.display = "none";
				
			var VestigingsBody = document.getElementById('VestigingsBody');
			if (VestigingsBody)
			{
				theCT.style.display = 'block';
			}
		}
		
		// Als in edit mode, dan content areas laten groeien met de inhoud
		if (document.forms[0].MSOLayout_InDesignMode.value == '1')
		{
			var VestigingsBody = document.getElementById('VestigingsBody');
			if (VestigingsBody)
			{
				if (theCD)
				{
					VestigingsBody.style.overflow = 'scroll';
					theCD.style.backgroundImage = 'none';
					theCD.style.backgroundColor = '#ffffff';
					theCD.childNodes[0].style.overflow = 'visible';
					theCD.childNodes[1].style.overflow = 'visible';
					theCD.childNodes[1].style.position = 'absolute';
					
					if (tmp = document.getElementById('KoreinContent'))
					{
						tmp.style.overflow = 'visible';
						tmp.style.margin = '0px';
						tmp.style.padding = '0px';
					}
						
					if (tmp = document.getElementById('NewsDiv'))
					{
						tmp.style.overflow = 'visible';
						tmp.style.margin = '0px';
					}
						
					if (tmp = document.getElementById('BottomImage'))
						tmp .style.display = 'none';
						
					if (tmp = document.getElementById('Scroller3'))
						tmp .style.display = 'none';
						
					if (tmp = document.getElementById('Scroller4'))
						tmp .style.display = 'none';
				}	
			}
			else
			{
				if (theCD)
				{
					theCD.style.overflow = 'visible';
					
					if (tmp = document.getElementById('OverflowStop1'))
						tmp.style.overflow = 'visible';
						
					if (tmp = document.getElementById('KoreinContent'))
						tmp.style.overflow = 'visible';
						
					if (tmp = document.getElementById('PPIO'))
						tmp.style.display = 'none';
				}
			}
		}
	}
		
	// Images preloaden
	if (KKP_ImageArray.length > 0)
	{
		for (var i = 0; i < KKP_ImageArray.length; i++)
		{
			var PreLoad = new Image();
			if (KKP_ImageArray[i].length > 0)
			{
				PreLoad.src = KKP_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 StartNewsScrollUp()
{
	ScrollTimeOut = window.setTimeout("ScrollNews(" + (cScrollStep * -1) + ", true)", cScrollSpeed);
}
function StartNewsScrollDown()
{
	ScrollTimeOut = window.setTimeout("ScrollNews(" + 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 DoMouseWheelNews(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;
	
	ScrollNews(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 ScrollNews(step, timeout)
{
	if (theND)
	{
		if (theND.clientHeight < theND.scrollHeight)
		{
			var newPos = theND.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 > theND.scrollHeight)
				newPos = theND.scrollHeight;
				
			theND.scrollTop = newPos
			
			if (newPos >= (newPos + theND.clientHeight))
				timeout = false;
			
			if (timeout)
				ScrollTimeOut = window.setTimeout("ScrollNews(" + step + ", true)", cScrollSpeed);
		}
	}
}

function HoverLeftNav(obj)
{
	if (obj.className.indexOf("_hover") > -1)
	{
		obj.className = obj.className.replace("_hover","");
	}
	else
	{
		obj.className = obj.className + "_hover";
	}
}

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 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 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 ChangeCurrentImage(objID, imgPath, imgWidth, imgHeight)
{
	var theImg = document.getElementById(objID)
	if (theImg)
	{
        if (imgWidth < imgHeight)
        {
        	var max = 280;
        	var ratio = imgHeight / max;
            theImg.style.height = max + 'px';
            theImg.style.width = (imgWidth / ratio) + 'px';
        }
        else
        {
        	var max = 350;
        	var ratio = imgWidth / max;
            theImg.style.width = max + 'px';
            theImg.style.height = (imgHeight/ ratio) + 'px';
        }
        
		theImg.src = imgPath;
	}
}
function ShowAlbumImage(imgPath, imgWidth, imgHeight, index)
{
	var ipu = document.getElementById('ImagePopUp');
	
	if (ipu)
	{
		// Als er een index beschikbaar is, dan data van image uit array halen (nieuwe webpart sinds (10-7-2008)
		if (index != 'undefined')
		{
			// De huidige index onthouden
			AlbumImageShowing = index;
			
			imgPath = arrImages[index][0];
			imgWidth = arrImages[index][2];
			imgHeight = arrImages[index][3];
		}

		var theImg = ipu.getElementsByTagName('IMG')[0];
		if (theImg)
		{
			var newWidth;
			var newHeight;
			
	        if (imgWidth < imgHeight)
	        {
	        	var max = 280;
	        	var ratio = imgHeight / max;
	        	newHeight = max;
	        	newWidth = imgWidth / ratio;
	        }
	        else
	        {
	        	var max = 350;
	        	var ratio = imgWidth / max;
	        	newHeight = imgHeight / ratio;
	        	newWidth = max;
	        }
	        
            theImg.style.height = Math.round(newHeight) + 'px';
            theImg.style.width = Math.round(newWidth)  + 'px';
            theImg.style.marginLeft = Math.round((362 - newWidth) / 2);
			theImg.src = imgPath;
		}
		
		if (theDiv)
		{
			ipu.style.marginTop = theDiv.scrollTop + 'px';
		}
			
		ipu.style.display = 'block';
	}
}
function HideAlbumImage(ipu)
{
	if (ipu)
	{
		var theImg = ipu.childNodes[0];
		if (theImg)
		{
            theImg.height = '0px';
            theImg.width = '0px';
			theImg.src = '/_layouts/images/blank.gif';
		}
		
		ipu.style.marginTop = '0px';
		ipu.style.display = 'none';
	}
}
function ShowPrevAlbumImage()
{
	AlbumImageShowing--;
	
	if (AlbumImageShowing < -1)
	{
		return;
	}
	
	if (AlbumImageShowing < 0)
	{
		AlbumImageShowing = arrImages.length - 1;
	}
	
	ShowAlbumImage('', 0, 0, AlbumImageShowing);
}
function ShowNextAlbumImage()
{
	AlbumImageShowing++;
	
	if (AlbumImageShowing < 1)
	{
		return;
	}
	
	if (AlbumImageShowing > arrImages.length - 1)
	{
		AlbumImageShowing = 0;
	}
	
	ShowAlbumImage('', 0, 0, AlbumImageShowing);
}
function DownloadAlbumImage(siteurl)
{
	if (AlbumImageShowing == -1)
	{
		return false;
	}
	else
	{
		var SourceUrl = arrImages[AlbumImageShowing][1];
		var Source = document.location;
		
		var loc = '/download.aspx?SourceUrl=' + encodeURIComponent(SourceUrl) + '&Source=' + encodeURIComponent(Source);
		
		document.location = loc;
	}
}
function OpenSiteWindow(url)
{
	if (url != 'undefined')
	{
		var popupHeight = 530;
		var popupWidth = 807;
		
		var popupTop = (screen.height - popupHeight) / 2;
		var popupLeft = (screen.width - popupWidth ) / 2;
		
		//var popupOptions = 'channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,';
		var popupOptions = '';
		
		if (window.location.href.indexOf('beheer.kinderplein.korein.nl') != -1)
			popupOptions = 'location=yes,menubar=yes,resizable=yes,';
		
		popupOptions = popupOptions + 'width=' + popupWidth + ',height=' + popupHeight + ',';
		popupOptions = popupOptions + 'top=' + popupTop + ',left=' + popupLeft ;
		
		window.open(url, '_blank', popupOptions);
	}
	return false;
}
function CloseSiteWindow()
{
	window.close();
}
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", KoreinKinderpleinOnLoad);

// Overrule standard sharepoint scripts
function ProcessImn() { }



