/** Local Variable settings **/

var curLocation = location.host;


/** creates an extra stylesheet **/
function addStyles(){

	//** Build js rendered stylesheet that css validators fail on **
	var jstyle = document.getElementsByTagName("head")[0];
	var lk = document.createElement("link");
	jstyle.appendChild(lk);
	lk.setAttribute("rel", "stylesheet");
	lk.setAttribute("type", "text/css");
	lk.setAttribute("href", "/js-vl.css");
}

function siteSearch(formObj){

	sQuery = formObj.search.value;

	if(sQuery.replace(/ /gi,"") != '')
	{
		top.location = "/"+sQuery.replace(/ /gi,"%20");
	}

	return false;
}


function countSearch(){

	if(document.getElementById("sCount"))
	{
		searchRes = document.getElementById("errpage").getElementsByTagName("p").length-1;
		document.getElementById("sCount").innerHTML = "<strong>"+searchRes+"</strong>";
	}
}


function getMap(smap){

	if(smap == 'fibracon')
	{
		mapHtml = '<iframe width="447" height="344" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Fibracon+Twin+Ltd+6+Bowden+Hey+Road&amp;sll=53.328694,-1.908788&amp;sspn=0.073712,0.154324&amp;ie=UTF8&amp;hq=Fibracon+Twin+Ltd+6+Bowden+Hey+Road&amp;hnear=&amp;cid=8107026240902714980&amp;ll=53.342353,-1.902695&amp;spn=0.035871,0.072956&amp;z=13&amp;iwloc=A&amp;output=embed"></iframe>';
	}
	else if(smap == 'insoll')
	{
		mapHtml = '<iframe width="447" height="344" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Insoll+Components+Ltd&amp;sll=53.800651,-4.064941&amp;sspn=19.385722,39.506836&amp;ie=UTF8&amp;hq=Insoll+Components+Ltd&amp;hnear=&amp;cid=2240438683728669829&amp;ll=51.979488,-0.266933&amp;spn=0.037007,0.072956&amp;z=13&amp;iwloc=A&amp;output=embed"></iframe>';
	}
	
	if(!document.getElementById("popinfo"))
	{
		var parentBody = document.getElementsByTagName("body")[0];
		var popdiv = document.createElement("div");
		var popinner = document.createElement("div");
		var popclose = document.createElement("a");
		parentBody.appendChild(popdiv);
		popdiv.appendChild(popclose);
		popdiv.appendChild(popinner);
		popdiv.setAttribute("id", "popinfo");
		popinner.setAttribute("id", "popcontent");
		popclose.setAttribute("id", "popclose");
		popclose.setAttribute("title", "Close pop-up info box - click here");
		popclose.setAttribute("href", "javascript:closeMap()");
	}
	
	document.getElementById("popinfo").style.display = "block";
	document.getElementById("popcontent").innerHTML= mapHtml;
}

function closeMap(){

	document.getElementById('popinfo').style.display = 'none';
	document.getElementById('popcontent').innerHTML = '';
}

var sFadeA;
var sFadeB;

/** loop function for fading and moving objects - start **/
function valueCounter(id, opacStart, opacEnd, tFunction, millisec) { 

clearTimeout(sFadeA);
clearTimeout(sFadeB);

var speed = Math.round(millisec / 100); 
var timer = 10; 

if(opacStart > opacEnd) { 
 for(i = opacStart; i >= opacEnd; i--) { 
     sFadeA = setTimeout("changeValues(" + i + ",'" + id + "','" + tFunction + "')",(timer * speed)); 
     timer++; 
 } 
} else if(opacStart < opacEnd) { 
 for(i = opacStart; i <= opacEnd; i++) 
 { 
     sFadeB =setTimeout("changeValues(" + i + ",'" + id + "','" + tFunction + "')",(timer * speed)); 
     timer++; 
 } 
} 
} 

function changeValues(nValue, id, tFunction) { 
	
	if(tFunction == "opacity")
	{
		var object = document.getElementById(id).style; 
		object.filter = "alpha(opacity=" + nValue + ")"; 
		object.opacity = (nValue / 100); 
		object.MozOpacity = (nValue / 100); 
		object.KhtmlOpacity = (nValue / 100); 
	}
	else if(tFunction == "top")
	{
		document.getElementById(id).style.top = "-"+nValue+"px";
	}
}

var nxtTimer;

function flashHome(curLoop,curDiv){


	clearTimeout(nxtTimer);
	fDiv = document.getElementById("photostrip").getElementsByTagName("div");
	fImage = document.getElementById("photostrip").getElementsByTagName("img");
	fImages = fImage.length;
	
	if(curLoop>=fImages){curLoop=0;}
	if(curDiv>=fDiv.length){curDiv=0;}
	nxtDiv = curDiv+1;
	
	for(d=0;d<fDiv.length;d++)
	{
		curFDiv = fDiv[d];
		curFDiv.style.zIndex = "1";
	}
	
	fDiv[curDiv].style.zIndex = "2";
	
	strLoop = curLoop;
	curLoop=curLoop+1;

	for(g=0;g<fImage.length;g++)
	{
		curNImg = fImage[g];
		curNImg.style.opacity = "1";
		curNImg.style.filter = "alpha(opacity=100)";
		
		newImgId = "FlashImgId"+g;
		if(!document.getElementById(newImgId)){curNImg.setAttribute("id",newImgId);}
	}
	
	fimgidStr = "FlashImgId"+(curLoop+7);
	document.getElementById(fimgidStr).style.opacity = "1";
	document.getElementById(fimgidStr).style.filter = "alpha(opacity=100)";
	
	
	cid=strLoop;
	imgTimerA = setTimeout("$('#FlashImgId'+cid).fadeTo(1000,0, function() {})",1500);
	cida=strLoop+1;
	imgTimerB = setTimeout("$('#FlashImgId'+cida).fadeTo(1000,0, function() {})",2000);
	cidb=strLoop+2;
	imgTimerC = setTimeout("$('#FlashImgId'+cidb).fadeTo(1000,0, function() {})",2500);
	cidc=strLoop+3;
	imgTimerD = setTimeout("$('#FlashImgId'+cidc).fadeTo(1000,0, function() {})",3000);
	cidd=strLoop+4;
	imgTimerE = setTimeout("$('#FlashImgId'+cidd).fadeTo(1000,0, function() {})",3500);
	cide=strLoop+5;
	imgTimerF = setTimeout("$('#FlashImgId'+cide).fadeTo(1000,0, function() {})",4000);
	cidf=strLoop+6;
	imgTimerG = setTimeout("$('#FlashImgId'+cidf).fadeTo(1000,0, function() {})",4500);
	cidg=strLoop+7;
	imgTimerH = setTimeout("$('#FlashImgId'+cidg).fadeTo(1000,0, function() {})",5000);
	cidh=strLoop+8;
	imgTimerI = setTimeout("$('#FlashImgId'+cidh).fadeTo(1000,0, function() {})",5500);
	
	nxtLoop = curLoop+8;
	nxtTimer = setTimeout("flashHome("+nxtLoop+","+nxtDiv+")",6700);
}


var newsTimer;

function rotateNews(newsNum){

	if(document.getElementById("newscontenth"))
	{
		clearTimeout(newsTimer);
		hpNews = document.getElementById("newscontenth").getElementsByTagName("p").length;

		if(newsNum >= hpNews){newsNum=0;}
		fadecontentviewer.init("newscontenth", "hnitem", "newschp", newsNum, 600);

		nxtNews = newsNum+1;
		newsTimer = setTimeout("rotateNews("+nxtNews+")",4000);
	}
}

var latestTimer;

function rotateLatest(newsNum){

	if(document.getElementById("latestevents"))
	{
		clearTimeout(latestTimer);
		hpNews = document.getElementById("latestevents").getElementsByTagName("div").length;

		if(hpNews>1)
		{
			if(newsNum >= hpNews){newsNum=0;}
			fadecontentviewer.init("latestevents", "latestc", "latesteventsm", newsNum, 600);

			nxtNews = newsNum+1;
			latestTimer = setTimeout("rotateLatest("+nxtNews+")",5000);
		}
	}
}

var rba;
var rbb;
var rbc;
var rbd;

function homeBullets(newsNumb){

	newsNum=1;
	if(location.pathname == '/')
	{
		clearTimeout(rba);
		clearTimeout(rbb);
		clearTimeout(rbc);
		clearTimeout(rbd);
		
		document.getElementById("homebulletscont").getElementsByTagName("div")[0].style.visibility = 'visible';
		document.getElementById("homebulletscont").getElementsByTagName("div")[0].style.zIndex = '100';
		document.getElementById("homebulletscontb").getElementsByTagName("div")[0].style.visibility = 'visible';
		document.getElementById("homebulletscontb").getElementsByTagName("div")[0].style.zIndex = '100';
		document.getElementById("homebulletscontc").getElementsByTagName("div")[0].style.visibility = 'visible';
		document.getElementById("homebulletscontc").getElementsByTagName("div")[0].style.zIndex = '100';
		document.getElementById("homebulletscontd").getElementsByTagName("div")[0].style.visibility = 'visible';
		document.getElementById("homebulletscontd").getElementsByTagName("div")[0].style.zIndex = '100';
		
		rba = setTimeout("homeBulletsA("+newsNum+")",2000);
		rbb = setTimeout("homeBulletsB("+newsNum+")",4000);
		rbc = setTimeout("homeBulletsC("+newsNum+")",6000);
		rbd = setTimeout("homeBulletsD("+newsNum+")",8000);
	}
}

var bulletAtimer;
var bulletBtimer;
var bulletCtimer;
var bulletDtimer;

function homeBulletsA(newsNum){

	if(document.getElementById("homebulletscont"))
	{
		clearTimeout(bulletAtimer);
		hpNews = document.getElementById("homebulletscont").getElementsByTagName("div").length;

		if(hpNews>1)
		{
			if(newsNum >= hpNews){newsNum=0;}
			fadecontentviewer.init("homebulletscont", "homebullets", "homebulletslink", newsNum, 600);

			if(location.pathname == '/')
			{
				nxtNews = newsNum+1;
				bulletAtimer = setTimeout("homeBulletsA("+nxtNews+")",8000);
			}
		}
	}
}

function homeBulletsB(newsNum){

	if(document.getElementById("homebulletscontb"))
	{
		clearTimeout(bulletBtimer);
		hpNews = document.getElementById("homebulletscontb").getElementsByTagName("div").length;

		if(hpNews>1)
		{
			if(newsNum >= hpNews){newsNum=0;}
			fadecontentviewer.init("homebulletscontb", "homebullets", "homebulletslinkb", newsNum, 600);

			if(location.pathname == '/')
			{
				nxtNews = newsNum+1;
				bulletBtimer = setTimeout("homeBulletsB("+nxtNews+")",8000);
			}
		}
	}
}

function homeBulletsC(newsNum){

	if(document.getElementById("homebulletscontc"))
	{
		clearTimeout(bulletCtimer);
		hpNews = document.getElementById("homebulletscontc").getElementsByTagName("div").length;

		if(hpNews>1)
		{
			if(newsNum >= hpNews){newsNum=0;}
			fadecontentviewer.init("homebulletscontc", "homebullets", "homebulletslinkc", newsNum, 600);

			if(location.pathname == '/')
			{
				nxtNews = newsNum+1;
				bulletCtimer = setTimeout("homeBulletsC("+nxtNews+")",8000);
			}
		}
	}
}

function homeBulletsD(newsNum){

	if(document.getElementById("homebulletscontd"))
	{
		clearTimeout(bulletDtimer);
		hpNews = document.getElementById("homebulletscontd").getElementsByTagName("div").length;

		if(hpNews>1)
		{
			if(newsNum >= hpNews){newsNum=0;}
			fadecontentviewer.init("homebulletscontd", "homebullets", "homebulletslinkd", newsNum, 600);

			if(location.pathname == '/')
			{
				nxtNews = newsNum+1;
				bulletDtimer = setTimeout("homeBulletsD("+nxtNews+")",8000);
			}
		}
	}
}



function startChildren()
{
	if(document.getElementById("startchildren"))
	{
		childDivs = document.getElementById("childrencontent").getElementsByTagName("div");
		tcDiv = childDivs.length-1;

		fstHeight = childDivs[0].offsetHeight;
		fstMargin = fstHeight-(fstHeight*2);
		
		pheight=fstMargin;
		for(i=0;i<childDivs.length;i++)
		{
			childDiv = childDivs[i];
			divClass = childDiv.getAttribute("class");

			if(divClass == "childpage")
			{
				thisHeight = childDiv.offsetHeight;
				childDivs.item(i).style.minHeight=thisHeight+"px";
				//childDivs.item(i).style.top="-"+pheight+"px";
				pheight=pheight+thisHeight;
			}
		}
		
		document.getElementById("childrencontent").style.minHeight = fstHeight+"px";
		//childDivs[0].style.marginBottom = fstMargin+"px";

		fadecontentviewer.init("childrencontent", "childpage", "childnav", 0, 300);
	}
}


function buildXt(){

	//expanding lists / highlight q&a
	ulTags = document.getElementsByTagName("ul");
	for(h=0;h<ulTags.length;h++)
	{
		ulTag = ulTags[h];
		ulClass = ulTag.getAttribute("class");
		if(ulClass == null){ulClass = ulTag.getAttribute("className");}
		
		if(ulClass == "expandlist" || ulClass == "expandlistred")
		{
			nTempId = "expandListTemp"+h;
			ulTag.setAttribute("id",nTempId);
			ulChild = ulTag.getElementsByTagName("li");

			ulChild[0].onclick = function(e)
			{
				target = e?e.target:window.event?window.event.srcElement:null;
				if(target) setExpandHide("objshow",target.parentNode.getAttribute("id"));
				target.setAttribute("class","openlist");
			}
			
			hTitle = ulChild[0].innerHTML;
			hTitle = 'View information on '+hTitle+' - click here';
			ulChild[0].setAttribute("title",hTitle);
		}
		
		if(ulClass == "qnahighlight")
		{
			qnahightID = "qnahightlightid";
			ulTag.setAttribute("id",qnahightID);

			setQNAhighlight();
		}
	}
	//end expand
	
	if(document.getElementById("hidechildcontent"))
	{
		childDivs = document.getElementById("rightcontent").getElementsByTagName("div");
		
		for(c=1;c<childDivs.length;c++)
		{
			childDiv = childDivs[c];
			
			childId = childDiv.getAttribute("id");
			childClass = childDiv.getAttribute("class");
			if(childClass==null){childClass = childDiv.getAttribute("className");}
			
			if(childClass == 'childcontent' && childId != "parentcontent")
			{
				hLink = childDiv.getElementsByTagName("h1")[0];
				chLink = childDiv.getElementsByTagName("div")[0].getElementsByTagName("h1")[0];
				
				getHeaderTxt = childDivs.item(c).getElementsByTagName("div")[0].getElementsByTagName("h1")[0].innerHTML;
				hLink.innerHTML = getHeaderTxt;
				
				hLink.onclick = function(e)
				{
					target = e?e.target:window.event?window.event.srcElement:null;
					if(target){
						siblDiv = target.parentNode.getElementsByTagName("div")[0];
						target.style.display= 'none';siblDiv.style.display = 'block';
					}
				}
				
				chLink.onclick = function(e)
				{
					target = e?e.target:window.event?window.event.srcElement:null;
					if(target){
						thisParent = target.parentNode;
						siblDiv = thisParent.parentNode.getElementsByTagName("h1")[0];
						siblDiv.style.display= 'block';target.parentNode.style.display= 'none';
					}
				}
			}
		}
	}
}

function setQNAhighlight(){

	qlists = document.getElementById("qnahightlightid").getElementsByTagName("li");
	
	for(i=0;i<qlists.length;i++)
	{
		qlist = qlists[i];
		
		qName = qlist.getElementsByTagName("a")[0].getAttribute("href");
		qName = qName.substr(1);
		
		qlist.getElementsByTagName("a")[0].setAttribute("href","javascript:qnaHighlight('"+qName+"')");
	}
}

function qnaHighlight(qTag){

	tQuestions = document.getElementsByTagName("h4");
	
	for(i=0;i<tQuestions.length;i++)
	{
		tQuestion = tQuestions[i];
		
		if(qName = tQuestion.getElementsByTagName("a")[0].getAttribute("name"))
		{
			tQuestion.style.color = '#000000';

			goQtag = "#"+qTag;

			if(qName == qTag)
			{
				tQuestions.item(i).style.color = '#DF3733';
			}

			top.location = goQtag;
		}
	}
}


function setExpandHide(objType,id){

	fObj = document.getElementById(id);
	fObjs = fObj.getElementsByTagName("li");
	
	if(objType == "objhide")
	{
		showHide = "none";
		
		fObjs[0].onclick = function(e)
		{
			target = e?e.target:window.event?window.event.srcElement:null;
			if(target) setExpandHide("objshow",id);
			fObjs[0].setAttribute("class","openlist");
		}
		
		hTitle = fObjs[0].innerHTML;
		hTitle = 'View information on '+hTitle+' - click here';

	}
	else if(objType == "objshow")
	{
		showHide = "list-item";
		
		fObjs[0].onclick = function(e)
		{
			target = e?e.target:window.event?window.event.srcElement:null;
			if(target) setExpandHide("objhide",id);
			fObjs[0].setAttribute("class","");
		}
		
		hTitle = fObjs[0].innerHTML;
		hTitle = 'Hide information on '+hTitle+' - click here';
	}
	
	for(t=0;t<fObjs.length;t++)
	{
		fObjs[t].style.display = showHide;
	}
	
	fObjs[0].style.display = "list-item";
	fObjs[0].setAttribute("title",hTitle);
}


function elementHide(cElement){

	elemArr = cElement.split(',');
	
	styleHTML='';
	for(i=0;i<elemArr.length;i++)
	{
		if(elemArr[i].substr(0,1)=="." || elemArr[i].substr(0,1)=="#")
		{
			styleHTML += elemArr[i]+"{display:none;}\n";
		}
	}
	
	var jxstyle = document.getElementsByTagName("head")[0];
	var xlk = document.createElement("style");
	var fstyles = document.createTextNode(styleHTML);
	xlk.setAttribute("type", "text/css");

	if(xlk.styleSheet){xlk.styleSheet.cssText = fstyles.nodeValue;}
	else{ xlk.appendChild(fstyles);}
	jxstyle.appendChild(xlk);
}


function printContent(){

myWindow = window.open('printPage.mfp','_blank');
myWindow.focus();

}


function checkWidePrint(){

	if(opener.document.getElementById("rightcontent"))
	{
		rDiv = opener.document.getElementById("rightcontent");
		rClass = rDiv.getAttribute("class");
		
		if(rClass == "wide")
		{
			document.getElementById("previewcontent").setAttribute("class","wide");
		}
	}
}


function removeLink(linkDet){

	links = document.getElementsByTagName("a");

	for(i=0;i<links.length;i++)
	{
		link = links[i];
		linkHref = link.getAttribute("href");

		if(linkHref == linkDet)
		{
			links.item(i).innerHTML = '';
			links.item(i).setAttribute("href","#");
		}
	}
}


function mail(){

	if(curLocation.search(/insoll/i) >= 0)
	{
		top.location = 'mailto:sales@insoll.com';
	}
	else
	{
		top.location = 'mailto:sales@fibracon.com';
	}
}

function imail(){

	if(curLocation.search(/insoll/i) >= 0)
	{
		top.location = 'mailto:sales@fibracon.com';
	}
	else
	{
		top.location = 'mailto:sales@insoll.com';
	}
}

function loginLinks(){
	
	if(document.getElementById("logoutlink"))
	{
		document.getElementById("logoutlink").href = 'javascript:loginJS()';
	}
}

/** creates an inline popup **/
function loginJS(){

	if(!document.getElementById("popinfo"))
	{
		var parentBody = document.getElementsByTagName("body")[0];
		var popdiv = document.createElement("div");
		var popinner = document.createElement("div");
		var popclose = document.createElement("a");
		parentBody.appendChild(popdiv);
		popdiv.appendChild(popclose);
		popdiv.appendChild(popinner);
		popdiv.setAttribute("id", "popinfo");
		popinner.setAttribute("id", "popcontent");
		popclose.setAttribute("id", "popclose");
		popclose.setAttribute("title", "Close pop-up info box - click here");
		popclose.setAttribute("href", "javascript:loginClose()");
	}
	
	if(bName == "Explorer")
	{
		//document.getElementById("popinfo").style.backgroundImage = "none";
	}
	
	document.getElementById("popinfo").style.display = "block";
	//valueCounter('popinfo',0,100,'opacity',200);
	
	document.getElementById("popcontent").innerHTML= '<iframe name="login" id="loginframe" src="/LoginJs.mfp" scrolling="yes" frameborder="0" width="100%" height="360"></iframe>';	

	if(bName == "Explorer")
	{
		//var RemFilTimer = setTimeout("document.getElementById('popinfo').style.filter = ''",300);
		//var AddBGTimer = setTimeout("document.getElementById('popinfo').style.background = 'transparent url(/gfx/info-pop.png) no-repeat center center'",400);
	}

}


function popInfoClose(){

	document.getElementById('rightcontent').style.display = 'none';
	//parent.document.getElementById('popcontent').innerHTML = '';
	parent.location.reload();
}

function loginClose(){

	document.getElementById('popinfo').style.display = 'none';
	parent.document.getElementById('popcontent').innerHTML = '';
}

/** http://www.quirksmode.org/js/detect.html           **/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

var bName = BrowserDetect.browser;
var bVer = BrowserDetect.version;


window.onload = function(){rotateNews(0);rotateLatest(0);homeBullets(0);buildXt();elementHide('.expandlist li,.expandlistred li');startChildren();loginLinks();}

