﻿/* Thanks Renishaw plc. All rights reserved */
/*Requires JQuery*/

function prodLineView(prodLineToView)
{
	//Highlights LHS tabs on the homepage
	document.getElementById('contentHolder').className='contentHolder' + prodLineToView; //set class for content holder
	
	$('#mainContentHome').hide();//ensure home is hidden straight away
	
	var arrProdLine = [ 'APP', 'IND', 'COM', 'LIT', 'QMNG','MET']; 
	//hide all
	for (var i=0; i < (arrProdLine.length); i++) {
	
		//document.getElementById('mainContent' + arrProdLine[i]).style.visibility = 'hidden'; //set all divs as hidden so IE doesn't show them		
		//document.getElementById('tab' + arrProdLine[i]).className = ''; //unset each tab as selected
		if ($('#mainContent' + arrProdLine[i]) != null) 
		{
			$('#mainContent' + arrProdLine[i]).hide();
		}		
		if ($('#tab' + arrProdLine[i]) != null)
		{
			//$('tab' + arrProdLine[i]).removeClassName('productLineTabSelected');//proto
			$('#tab' + arrProdLine[i]).removeClass('productLineTabSelected');//jquery
		}
	}
	
	//show new selected item
	$('#mainContent' + prodLineToView).show(); //then unhide the content we want...
	$('#tab' + prodLineToView).addClass('productLineTabSelected'); //...and set the tab to be selected


	//remove the tab highlight
	for (var j=0; j < (arrProdLine.length); j++) 
	{	
		if ($('#tab' + arrProdLine[j]) != null)
		{
			//unset each tab as selected
			$('#tab' + arrProdLine[j]).removeClass('fauxHighlight');
		}
	}	
	
	
	//turn off banner rotation
	banners.IsStopped = true;
}


function prodLineReveal()
{
	//find the highlighted tab then call prodline view to reveal it
	//determine product line this way as trident and gecko treat 'invisible' banner images differently.
	var prodTabs = document.getElementById('productLineTabs').getElementsByTagName('li');//limit to just the tabs list items

	for (var i=0; i < prodTabs.length; i++)
	{
		if (prodTabs[i].getAttribute('class') == 'fauxHighlight' || prodTabs[i].getAttribute('className') == 'fauxHighlight') //className for IE
		{
			var prodLine = prodTabs[i].id;
			if (prodLine.indexOf('tab') == 0)
			{
				var highlightedDivision = prodLine.substring(3, prodLine.length); //strip the prefix of tab off
				prodLineView(highlightedDivision)
			}
		}
	}
}

function fauxHighlight(prodLine) 
{
	/*
	Sets css to produce indicator highlights on homepage LHS tabs, triggered by banner rotator
	*/
	var arrProdLine = [ 'APP', 'IND', 'COM', 'LIT', 'QMNG','MET'];
	
	for (var i=0; i < (arrProdLine.length); i++) 
	{	
		if ($('#tab' + arrProdLine[i]) != null)
		{
			//unset each tab as selected
			$('#tab' + arrProdLine[i]).removeClass('fauxHighlight');
		}
	}

	$('#tab' + prodLine).className='fauxHighlight'; //set class for tab
}



function BannerRotater(instanceName, divisions)
{
	this.Name = instanceName;
	this.IsStopped = false;
	this.IsRandomStart = true;
	
	//methods
	this.RotateBanners = rotateBanners;
	this.HighlightTab = highlightTab;
	//set all banners to opacity zero first
	for (i=0; i< divisions.length; i++)
	{
		$('#banner' + divisions[i]).fadeTo(0, 0);
	}
	

		
	/*
	* Gets all the banners and cycles through them
	*/
	function rotateBanners(currentIndex)
	{
		if (!this.IsStopped)
		{
			//actions:
			//	fade out current banner
			//	fade in new banner
			//	reset z-index
			//	highlight tab halfway through			
			
			var refreshRate = 5000; //milliseconds
			var fadeRate = 1000; //milliseconds

			nextIndex = 0; //default to first item (reset if end of array
			if ( (currentIndex + 1) < divisions.length )
			{
				nextIndex = currentIndex + 1;
			}

			bannerIdOld = 'banner' + divisions[currentIndex];
			bannerIdNew = 'banner' + divisions[nextIndex];

			//$('#' + bannerIdNew).fadeTo(0, 0);
			$('#' + bannerIdNew).css('z-index', '3');
			$('#' + bannerIdOld).css('z-index', '2');	
			$('#' + bannerIdNew).fadeTo(fadeRate, 1);
			//$('#' + bannerIdOld).fadeTo(fadeRate, 0);

			//fade out banner after new banner has faded in
			setTimeout('$("#' + bannerIdOld + '").fadeTo(' + (fadeRate/4) + ', 0);', fadeRate);
			
			//line up the next banner
			setTimeout(this.Name + '.RotateBanners(' + nextIndex + ')', refreshRate);
			
			//highlight the tab the image relates to
			tabWait = fadeRate / 2;// highlight tab halfway through transition
			setTimeout(this.Name + '.HighlightTab("' + divisions[nextIndex]  + '")', tabWait );
		}
	}

	/*
	Sets css to produce indicator highlights on homepage LHS tabs, triggered by banner rotator
	*/	
	function highlightTab(productline)
	{
		//alert ('prodline: ' + productline);

		var arrProdLine = [ 'APP', 'IND', 'COM', 'LIT', 'QMNG','MET']; 
		
		for (var i=0; i < (arrProdLine.length); i++) 
		{	
			if ($('#tab' + arrProdLine[i]) != null)
			{
				$('#tab' + arrProdLine[i]).removeClass('fauxHighlight');
			}
		}

		if ($('#tab' + productline) != null)
		{
			$('#tab' + productline).addClass('fauxHighlight'); //set class for tab	
		}
	}
}


function ViewProductLine(ProductLineId)
{
	//turn off banner rotation
	banners.IsStopped = true;
	
	//Highlights LHS tabs on the homepage
	$('contentHolder').addClass('contentHolder' + ProductLineId); //set class for content holder
	
	$('#mainContentHome').hide();//ensure home is hidden straight away
	
	var arrProdLine = [ 'APP', 'IND', 'COM', 'LIT', 'QMNG','MET']; 
	//hide all
	for (var i=0; i < (arrProdLine.length); i++) 
	{
		if ($('#mainContent' + arrProdLine[i]) != null) 
		{
			$('#mainContent' + arrProdLine[i]).hide();
		}		
		if ($('#tab' + arrProdLine[i]) != null)
		{
			$('#tab' + arrProdLine[i]).removeClass('productLineTabSelected');
		}
	}
	
	//show new selected item
	$('#mainContent' + ProductLineId).show(); //then unhide the content we want...
	$('#tab' + ProductLineId).addClass('productLineTabSelected'); //...and set the tab to be selected
}


function RevealProductLine(ProductLineId)
{
	//find the highlighted tab then call ViewProductLine to reveal it
	//determine product line this way as trident and gecko treat 'invisible' banner images differently.
	var prodTabs = $('#productLineTabs li');//limit to just the tabs list items

	for (var i=0; i < prodTabs.length; i++)
	{
		if (prodTabs[i].getAttribute('class') == 'fauxHighlight' || prodTabs[i].getAttribute('className') == 'fauxHighlight') //className for IE
		{
			var prodLine = prodTabs[i].id;
			if (prodLine.indexOf('tab') == 0)
			{
				var highlightedDivision = ProductLineId.substring(3, prodLine.length); //strip the prefix of tab off
				ViewProductLine(highlightedDivision)
			}
		}
	}
}
