
var init_executed = false;
function init(){
	if (!init_executed) {
    	hiliteAnchor();
    	listmagic();
    	hoverText();
    	/*bindPopUp();*/

    	navbarObj = new NavBar();
    	
    	initMouseEvents();
    	initMouseOut();
    	if (navbarObj.selectedtab){
    		navbarObj.selectedtab.className = 'hilited'+navbarObj.selectedtab.baseClassName;
    	}
    	MM_preloadImages('/media/menu/tabs_hover.gif', '/media/menu/tabs_hilite.gif', '/media/menu/submenu_Autobedrijven.gif', '/media/menu/submenu_Autonieuws.gif', '/media/menu/submenu_Contact.gif', '/media/menu/submenu_Home.gif', '/media/menu/submenu_Info.gif', '/media/menu/submenu_Nieuw.gif', '/media/menu/submenu_Occasions.gif');


    	init_executed = true;
	}
    	
}

function hiliteAnchor(){
	var anchorLocation;

	if(document.URL.match(/#/))
	{
		anchorLocation = document.URL;
		blink_Article(anchorLocation.replace(/^.*#/, ''));
	}
	
    addBlinkToAnchorLinks();
	
}

function bindFunc(node, tagName, attributeName, matchExp, eventName, func){
	var i;
	if (node.tagName.toUpperCase() == tagName.toUpperCase()){
		var nodeAttribute = node.getAttribute(attributeName);
		if (nodeAttribute != null){
			if (nodeAttribute.match(matchExp)){
				if (eventName == 'onclick'){
					//bindcount[bindcount.length]=node.tagName;
					node.onclick = func;
				}
			}
		}
	}
	for(var i=0; i<node.childNodes.length; i++){
		if (node.childNodes[i].nodeType == 1){
			var delayFunction = getDelayBind(node.childNodes[i], tagName, attributeName, matchExp, eventName, func);

			setTimeout(delayFunction, 100);
		}
	}
}

function getDelayBind(node, tagName, attributeName, matchExp, eventName, func){
	var delayFunction = function(){
		var childNode = node;
		var childTagName = tagName;
		var childAttributeName = attributeName;
		var childMatchExp = matchExp;
		var childEventName = eventName;
		var childFunc = func;
		bindFunc(childNode, childTagName, childAttributeName, childMatchExp, childEventName, childFunc);
	}
	return delayFunction;
}

function addBlinkToAnchorLinks(){
	var body = document.getElementsByTagName('body')[0];
	body.onclick = blinkAnchorLink;
}

function blinkAnchorLink(e){
	var evt = e || window.event;
	var tgt = evt.target || evt.srcElement;
	var tag = getElementName(tgt);
   	if (tag == 'a') {
   	    var href = tgt.href || '';
       	if (href.match(/#.+/)) {
       	    startBlink(href);
        }
    }
}

function getElementName(el) {
	var elName = el.type || el.nodeName || 'window';
	return elName.toLowerCase();
}

function startBlink(href){
	var anchorLocation = href.replace(/^.*#/, '');
	blink_Article(anchorLocation);
}



function blink_Article(article)
{
	var articleColor, HiliteColor;
	var i;

	id_article = 'id_'+article;
	articleElement = document.getElementById(id_article);
	if (articleElement){
		articleColor = articleElement.style.backgroundColor;

		//colors = Array('#ffff00', '#ffff7f', '#ffffcc', '#fffff2', '#ffffff');

		n=60;  //aantal stappen
		d=20; //stapdelay
		start = 1/3;
		end = 1
		for (i=0; i<n; i++)
		{
			color = Math.round(start*255+(end-start)*255/n*i);
			setTimeout('setBackground("'+id_article+'", "rgb(255, 255, '+ color +')")', d*i);
		}
		setTimeout('setBackground("'+id_article+'", "'+articleColor+'")', n*d+d);
	}
}


function setBackground(item, color)
{
	item = document.getElementById(item);
	item.style.backgroundColor = color;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");

		for (i=0; i<navRoot.childNodes.length; i++) {

			node = navRoot.childNodes[i];

			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

function hoverText(){
	if (document.all&&document.getElementById) {
		footertext = document.getElementById("footerbox");
		if (footertext){
			footertext.onmouseover=function() {
				this.className="over";
			}
			footertext.onmouseout=function() {
				this.className="";
			}
		}
	}
}

ieHover_js=1;


//popup
var popUpWin = null;
function popUpWindow(URLStr, left, top, width, height){
	if(popUpWin){
		popUpWin.close();
	}
	var opties = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbar=no, resizable=no, copyhistory=yes,';
	opties += 'width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top;

	popUpWin = open(URLStr, 'popUpWin', opties);
}


function popUpWindow2(URLStr, left, top, width, height){
	if (popUpWin) {
		popUpWin.close();
	}
	var opties = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes,';
	var opties = 'scrollbars=yes, resizable=yes, copyhistory=yes,';
	opties += 'width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top;

	popUpWin = window.open(URLStr, 'popUpWin', opties);
	popUpWin.focus();
}


function showNLExample(URL){
	var left = 100;
	var top = 100;
	var width = 640;
	var height = 480;
	var opties = '';
	opties = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=yes,';
	opties += 'width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top;
	example = window.open(URL, 'example', opties);
}


function adButton(){
	examplebody = window.example.document.getElementsByTagName('body')[0];
	if (examplebody){
		buttondiv = document.createElement('div');
		buttondiv.setAttribute('class', 'sluitvensterdiv');

		buttonlink = document.createElement('a');
		buttonlink.setAttribute('href', 'javascript:window.close()');
		buttonlink.innerHTML='Sluit venster';

		buttondiv.appendChild(buttonlink);
		examplebody.appendChild(buttondiv);
	}
}

var popup_js = 1;

function bindPopUp(){
	var imageIds;
	var idsPerSubdir = new Array;

	idsPerSubdir['tests']=10;
	idsPerSubdir['thumbnails']=10;
	idsPerSubdir['evenementen']=10;
	idsPerSubdir['pers']=10;
	idsPerSubdir['merkgarantie']=10;
	idsPerSubdir['merk']=10;
	idsPerSubdir['columns']=10;
	idsPerSubdir['columnist']=10;
	idsPerSubdir['acties']=10;
	idsPerSubdir['pers']=10;

	images = getElementsByClassName('popup');

	for (i=0; i<images.length; i++){
		images[i].onclick = function(){
			imageid = this.id;
			width = 534;//imageid.replace(/W(\d+)H\d+.*/,"$1");
			height = 414;//imageid.replace(/W\d+H(\d+).*/,"$1");
			circuit = imageid.replace(/W\d+H\d+C([a-z]*)F.*/,"$1");
			image = imageid.replace(/.*F(.*)/, "$1");
			id  = image.replace(/(\d+)_.*/,"$1");
			aant = idsPerSubdir[circuit];
			url = '/afbeeldingen/'+circuit+'/'+Math.floor(id/aant)*aant+'/'+image;
			popUpWindow2('/popup/index.html?image='+url, 10, 10, width, height);
		}
	}
}

function listmagic(){
	// gooit class 'iehover' op lijstitems.
	var lijstitems;
	lijstitems = getElementsByClassName('lijstitem');

	for (var i=0; i<lijstitems.length; i++){
		lijstitems[i].onmouseover=function() {
			this.className+=" iehover";
		}
		lijstitems[i].onmouseout=function() {
			this.className=this.className.replace(" iehover", "");
		}
	}
}

function getElementsByClassName(className){
	var arr = new Array();
	var elems = document.getElementsByTagName("*");
	for(var i = 0; i < elems.length; i++){
		var elem = elems[i];
		var id = elem.id;
		var cls = elem.className;
		if(cls.match(className)){
			arr[arr.length] = elem;
		}
	}
	return arr;
}

function setHilite(tabid){
	tab = document.getElementById(tabid);
	hilited = navbarObj.hilited;
	selected = navbarObj.selectedtab;
	if (tab != hilited){
		if (tab == selected){

		} else {

		}
		tab.className = 'hilited'+tab.baseClassName;
		setTimeout('hilited.className = hilited.baseClassName;', 10);
		navbarObj.hilited = tab;
	}
}

function resetMenu(tabid){
	tab = document.getElementById(tabid);
	hilited = navbarObj.hilited;
	if (tab != hilited){
		tab.className = 'hilited'+tab.baseClassName;
		hilited.className = hilited.baseClassName;
		navbarObj.hilited = tab;
	}
}

function NavBar(){
	this.menutabs = getElementsByClassName('menutab');
	this.selectedtab = getElementsByClassName('selected')[0];
	this.navbar = getElementsByClassName('nav')[0];
	this.hiliteTimer = 0;
	this.mouseOutTimer = 0;

	this.mouseOverDelay = 300;
	this.mouseOutDelay = 1000;

	this.hilited = this.selectedtab;
}

function mouseEvents(tab){
	tab.baseClassName = tab.className;// voor de wisseltruc //
	tab.onmouseover = function(){
		clearTimeout(navbarObj.mouseOutTimer);
		navbarObj.hiliteTimer = setTimeout('setHilite("'+tab.id+'")', navbarObj.mouseOverDelay);
	}
	tab.onmouseout = function(){
		clearTimeout(navbarObj.hiliteTimer);
	}
}

function initMouseEvents(){
	for (i=0; i<navbarObj.menutabs.length; i++){
		tab = navbarObj.menutabs[i];
		mouseEvents(tab);
	}
	if (navbarObj.selectedtab){
		mouseEvents(navbarObj.selectedtab);
	}
}

function initMouseOut(){
	localnavbar = navbarObj.navbar;
	if (localnavbar){
		localnavbar.onmouseout = function(){
			navbarObj.mouseOutTimer = setTimeout('resetMenu("'+navbarObj.selectedtab.id+'")', navbarObj.mouseOutDelay);
		}
		localnavbar.onmouseover = function(){
			clearTimeout(navbarObj.mouseOutTimer);
		}
	}
}



window.onload=init;

// function om forms met maar één veld niet te versturen:
function checkEnter(e, evalscript){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		if (evalscript != ''){
			eval(evalscript);
		}
		return false;
	}else{
		return true;
	}
}


function hideHelpdiv(helpDivId){
	helpDivId = 'popoverplacer';
	// show ads
	var adplacer = document.getElementById('ad_placer');
	if (adplacer) {
		adplacer.style.visibility = 'visible';
	}
	unfade();

	// kh 23-12-2005 Remove popover content after unfade
	myChild  = document.getElementById('popover_content').firstChild;
	document.getElementById('popover_content').removeChild(myChild);

	helpDiv = document.getElementById(helpDivId);
	helpDiv.style.display="none";
}

function showHelpdiv(helpDivId){
	helpDivId = 'popoverplacer';
	fade();
	// remove ads
	var adplacer = document.getElementById('ad_placer');
	if (adplacer) {
		adplacer.style.visibility = 'hidden';
	}
	helpDiv = document.getElementById(helpDivId);
	helpDiv.style.display="block";
}

function fade()
{
	obj = document.getElementById('fader');
	obj.style.display = 'block';

	obj.style.height = '100%';

	browser = 'ns6';
	if (document.all)
	{
		browser = 'ie5';
	}

	if(browser == 'ns6'){
		obj.style.position = 'fixed';
	}

	curropacity = 35;
	opacity = 50;
	steps = 8;

	setOpac('fader', curropacity, steps, opacity);
}

function setOpac(objId, currOpac, steps, endOpac){
	obj = document.getElementById(objId);

	newOpac = (Number(endOpac) - Number(currOpac)) / Number(steps) + Number(currOpac);

	obj.style.opacity = newOpac/100;
	obj.style.filter = 'alpha(opacity=' + newOpac + ')';
	steps = steps-1;
	if (steps > 0)
	{
		temp = setTimeout('setOpac("'+objId+'",'+newOpac+','+steps+','+endOpac+')',20);
	}
}

function unfade()
{
	obj = document.getElementById('fader');
	setTimeout("obj.style.display = 'none'", 120);

	curropacity = 20;
	opacity = 0;
	steps = 5;
	//=ie5
	setOpac('fader', curropacity, steps, opacity);
}

var theTop = 0;
var old = theTop;
function movefader()
{
	obj = document.getElementById('fader');
	if (window.innerHeight)
	{
		  pos = window.pageYOffset
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop
	}
	else if (document.body)
	{
		  pos = document.body.scrollTop
	}
	if (pos < theTop) pos = theTop;

	if (pos == old)
	{
		obj.style.top = pos;
	}
	old = pos;

}

function setPopoverContent(content_id)
{
	contentcontainer = document.getElementById(content_id);
	var newcontent = contentcontainer.cloneNode(true);
	newcontent.removeAttribute('id');
	document.getElementById('popover_content').appendChild(newcontent);
}