var g_counter =0;
var g_cycle_flag=true;
var g_active_cycler='cycler_1';
var g_timeout;

function changeBackground(elementId,color){
	var baseTd = document.getElementById('header_'+elementId);
	if(baseTd !=null){
		var cellIndex = baseTd.cellIndex;
		var previousTd = baseTd.parentNode.cells[cellIndex-1];
		var nextTd = baseTd.parentNode.cells[cellIndex+1];
		baseTd.style.backgroundColor =color;
		previousTd.style.backgroundColor =color;
		nextTd.style.backgroundColor =color;
	}
}

function setHeaderBackground(elementId,color,currentCategory){
	if(currentCategory!=elementId){
		changeBackground(elementId,color);
	}
}
//Nav box control
function cycleNavBoxCustomer(divId,op,nPicture){
	var div =  document.getElementById(divId);
	var visibleLogo = document.getElementById('visible_logos');
	var imgList = document.getElementById('hidden_customer_logo');
	var imgs = imgList.getElementsByTagName('IMG');
	var index = 0;
	var imgId = visibleLogo.getElementsByTagName('IMG')[0].id;
	for(var i=0;i<imgs.length;i++){
		if(imgs[i].id == imgId){
			index = eval(i+op+nPicture);
			break;
		}
	}
	visibleLogo.innerHTML='';
	for(var j=0;j<nPicture;j++){
		if(index>=imgs.length || index<0){
			index = setIndexFirst(op,imgs.length,nPicture);
		}
		visibleLogo.appendChild(imgs[index].cloneNode(false));
		index++;
	}
}

function setIndexFirst(operator,maxLength,picCount){
	if(operator=='+'){
		return 0;
	}
	if(operator=='-'){
		return maxLength-picCount;
	}
}
//end navbox control
function launchSearch(id){
	window.location.href='s.nl?sc=53&category=&search='+escape(document.getElementById(id).value);
}


//Home picture cycler functions
function cycleCycler(){
	var cycler = document.getElementById(g_active_cycler);
	var foundNextDiv = false;
	while(!foundNextDiv){
		cycler=cycler.nextSibling;
		if(cycler==null){
			//g_cycle_flag=false; //Block infinite loop
			g_active_cycler='cycler_1'; //reset the cycler
			return;
		}
		if(cycler.tagName=='DIV'){
			foundNextDiv = true;
		}
	}
	resetNav(g_active_cycler.split('_')[1]);
	//hideAllActiveDivs(g_active_cycler);
	g_active_cycler = cycler.id;
	g_counter=0;
	cyclePicture();
}
function cyclePicture(){
	var delay = 3000;
	var imgCycler = document.getElementById(g_active_cycler);
	var imgCyclerChilds = imgCycler.childNodes;
	imgCyclerChilds = filterNodes(imgCyclerChilds,'div');
	var divCount = imgCyclerChilds.length;
	if(g_counter-1==-1){
		imgCyclerChilds[divCount-1].style.visibility='hidden';
	}else{
		imgCyclerChilds[g_counter-1].style.visibility='hidden';
	}
	imgCyclerChilds[g_counter].style.visibility='visible';
	setCyclePicture(imgCyclerChilds[g_counter].parentNode.id,g_counter);
	g_counter++;
	if(g_counter!=divCount){
		g_timeout=setTimeout(cyclePicture,delay);
	}else{
		if(g_cycle_flag){
			delay==12000;
			g_timeout=setTimeout(cycleCycler,delay);
		}
	}
}

function hideAllActiveDivs(cyclerId){
	var cycler = document.getElementById(cyclerId);
	var divs = cycler.childNodes;
	divs = filterNodes(divs,'div');
	for(var i=0;i<divs.length;i++){
			divs[i].style.visibility='hidden';
	}
}

function resetNav(navNumber){
	var div = document.getElementById('cycler_nav_'+navNumber);
	var img = div.getElementsByTagName('img');
	img[0].src="/erpguru/image/rotating/cycler_selector_empty.png";
}

function filterNodes(elems,tagName){
	var nodes =[];
	if(elems!=null){
		for(var i=0;i<elems.length;i++){
			if(elems[i].tagName==tagName.toUpperCase()){
				nodes.push(elems[i]);
			}	
		}
	}
	return nodes;
}
function setCyclePicture(parentId,counter){
	if(parentId==null || parentId==''){
		return;
	}
	var id = parentId.split('_')[1];
	var div = document.getElementById('cycler_nav_'+id);
	var img = div.getElementsByTagName('img');
	img[0].src="/erpguru/image/rotating/cycler_selector_"+(counter*1+1)+'.png';
}

function onclick_setCycler(nav_number){
	var wrapper = document.getElementById('img_cycler');
	var wrapperChilds = wrapper.childNodes;
	wrapperChilds = filterNodes(wrapperChilds,'div');
	for(var i=0;i<wrapperChilds.length;i++){
		var idNumber = wrapperChilds[i].id.split('_')[1];
		hideAllActiveDivs('cycler_'+idNumber);
		resetNav(idNumber);
	}
	g_cycle_flag=false;
	g_counter=0;
	g_active_cycler="cycler_"+nav_number;
	clearTimeout(g_timeout);
	cyclePicture();
}
// End of cycler function 

//Tab navigation

function onclick_setActiveTab(elemId){
	setTabsBackground('tab_'+elemId);
	setContentVisibility('item_'+elemId);
}

function setTabsBackground(contentId){
	var wrapper = document.getElementById('tab_wrapper');
	var childs = wrapper.childNodes;
	var divs = filterNodes(childs,'DIV');
	for(var i=0;i<divs.length;i++){
		var tabNodes = divs[i].getElementsByTagName('DIV');
		if(divs[i].id==contentId){
			setActiveBackground(tabNodes,true);
		}else{
			setActiveBackground(tabNodes,false);
		}
	}
}

function setActiveBackground(nodes,active){
	var backgrounds = [];
	backgrounds[0] = 'url(http://preview.erpguru.com/erpguru/image/item_inactivetab_left.png)';
	backgrounds[1] = 'url(http://preview.erpguru.com/erpguru/image/item_inactivetab_center.png)';
	backgrounds[2] = 'url(http://preview.erpguru.com/erpguru/image/item_inactivetab_right.png)';
	if(active){
		backgrounds[0] = 'url(http://preview.erpguru.com/erpguru/image/item_activetab_left.png)';
		backgrounds[1] = 'url(http://preview.erpguru.com/erpguru/image/item_activetab_center.png)';
		backgrounds[2] = 'url(http://preview.erpguru.com/erpguru/image/item_activetab_right.png)';
	}
	for(var i=0;i<backgrounds.length;i++){
		if(nodes[i]!=null){
			nodes[i].style.backgroundImage=backgrounds[i];
		}
	}
}

function setContentVisibility(contentId){
	var wrapper = document.getElementById('item_content_wrapper');
	var childs = wrapper.childNodes;
	var divs = filterNodes(childs,'DIV');
	for(var i=0;i<divs.length;i++){
		if(divs[i].id==contentId){
			divs[i].style.visibility='visible';
		}else{
			divs[i].style.visibility='hidden';
		}
	}
}

function setDivVisibility(divId,visibility){
	var div=document.getElementById(divId);
	var divBody = document.getElementById('div__body');
	if(div!=null){
		div.style.visibility=visibility;
		if(visibility=='visible'){
			div.style.height='400px';
			if(typeof (window.external.AddToFavoritesBar) != "undefined"){ ///verify IE8
				divBody.innerHTML = divBody.innerHTML;
			}
		}else{
			div.style.height='25px';
			if(typeof (window.external.AddToFavoritesBar) != "undefined"){///verify IE8
				divBody.innerHTML = divBody.innerHTML;
			}
		}
	}
}
// End Tab Navigation control

//Blocking div

function createBlockingElements(){
	var blockingDiv = document.createElement('DIV');
	blockingDiv.id='blocking_div';
	blockingDiv.style.display='none';
	
	var textDiv = document.createElement('DIV');
	textDiv.id='blocking_text_div_wrapper';
	textDiv.style.display='none';
	
	
	var blockingDivExit = document.createElement('DIV');
	blockingDivExit.id = 'blocking_div_exit';
	blockingDivExit.style.textAlign='right';
	blockingDivExit.style.width ='100%';
	
	var blockingDivExitInput = document.createElement('INPUT');
	blockingDivExitInput.type='image';
	blockingDivExitInput.src = '/erpguru/image/black_box_x.png ';
	blockingDivExitInput.onclick = resetBlockingElements;
	
	var blockingText = document.getElementById('blocking_text_div');
	
	blockingDivExit.appendChild(blockingDivExitInput);
	textDiv.appendChild(blockingDivExit);
	textDiv.appendChild(blockingText);
	
	document.body.appendChild(blockingDiv);
	document.body.appendChild(textDiv);
	
}

function setBlockingElements(){
	var blockingDiv = document.getElementById('blocking_div');
	var textDiv = document.getElementById('blocking_text_div_wrapper');
	
	var scrolling = document.documentElement.scrollTop;
	blockingDiv.innerHTML= '&nbsp;'
	blockingDiv.style.width ="100%";
	blockingDiv.style.height ="100%";
	blockingDiv.style.backgroundColor ="#9D9D9D";
	blockingDiv.style.opacity ="0.8";
	blockingDiv.style.filter ="alpha(opacity=80)";
	blockingDiv.style.position ="absolute";
	blockingDiv.style.left ="0px";
	blockingDiv.style.top =scrolling+'px';
	blockingDiv.style.zIndex ="100";
	blockingDiv.style.display='block';
	
	var textDivWidth = 900;
	var textDivHeight =450;
	textDiv.style.backgroundColor='white';
	textDiv.style.border ='3px solid #BF1218';
	textDiv.style.position ='absolute';
	textDiv.style.left='0px';
	textDiv.style.width= textDivWidth+'px';
	textDiv.style.height= textDivHeight+'px';
	textDiv.style.top = parseInt(scrolling + ((document.documentElement.clientHeight-textDivHeight)/2))+'px';
	textDiv.style.left = parseInt((document.documentElement.clientWidth-textDivWidth)/2)+'px';
	textDiv.style.display='block';
	textDiv.style.zIndex='200';
	
	document.body.style.overflow ='hidden';
}

function resetBlockingElements(){
	var blockingDiv = document.getElementById('blocking_div');
	var textDiv = document.getElementById('blocking_text_div_wrapper');
	
	blockingDiv.style.display='none';
	textDiv.style.display='none';
	
	document.body.style.overflow='visible';
}

function modifyGoogleSearchLayout(){
	if(document.getElementById('search_input').firstChild!=null){
		var submitButton = document.getElementById('search_input').firstChild.firstChild.firstChild.firstChild.childNodes[1].firstChild;
	}else{
		return;
	}
	var newInput = document.createElement('INPUT');
	newInput.className = 'gsc-search-button';
	newInput.type = 'image';
	newInput.value = 'Submit';
	newInput.title = submitButton.title;
	newInput.onclick = setBlockingElements;
	newInput.src = '/erpguru/image/loupe.png';
	
	submitButton.parentNode.appendChild(newInput);
	submitButton.parentNode.removeChild(submitButton);
	
}

function makeDoubleDelegate(function1, function2) {
    return function() {
        if (function1)
            function1();
        if (function2)
            function2();
    }
}
