﻿/* banner rotator */
/* items to be updated */
var frame = $("#banner-content");
var title = $("#banner-content-strapline");
var text = $("#banner-content-text");
var linkText = $("#banner-content-link");
var displayFor = 10;//second
var fadeTime = 1;//second

/* create array of banners */
var bannersArrayIn;// = [1,2,3,4,5];

/*initial show*/
function showBanner(BannerIdToShow)
{
	var BannerIdToShow = 'banner-1';
	var BannerButtonActive = 'banner-button-' + '1';
	var fadeRate = 2000;//milliseconds
	
	$('#' + BannerIdToShow).show();//set initial banner to not hidden
	$('#' + BannerIdToShow).fadeTo(fadeRate, 1);//fade in initial banner
	$('#' + BannerIdToShow).addClass('active');//set it as active
	
	$('#' + BannerButtonActive).addClass('active');//set button visuals
}
//$(document).ready(showBanner);
//$(document).ready(function() { ShowBannerItem(1); });

function ShowBannerItem(BannerNumberToShow)
{
	//alert(BannerNumberToShow);
	var BannerIdToShow = 'banner-' + BannerNumberToShow;
	var BannerButtonActive = 'banner-button-' + BannerNumberToShow;
	
	var currentActive = $('#activeBanner').val();
	
	if (currentActive != BannerNumberToShow) {
		/* note change behind the scenes */
		$('#activeBanner').val(BannerNumberToShow);
		
		var fadeRate = 2000;//milliseconds
		$('#' + BannerIdToShow).css('z-index', '3');
		$('#banner-' + currentActive).removeClass('active').css('z-index', '2').fadeTo(fadeRate, 0);
		$('#' + BannerIdToShow).show();//set to display
		$('#' + BannerIdToShow).fadeTo(fadeRate, 1);//fade in
		$('#' + BannerIdToShow).addClass('active');//set css to finish off visuals
		
		$('#banner-button-' + currentActive).removeClass('active');//set css on buttons
		$('#' + BannerButtonActive).addClass('active');
	}
	
	//turn off banner rotation
	banners.IsStopped = true;
}

function ShowBannerPrev()
{	
	var activeBanner = $('#activeBanner').val();//find current active banner
	var currentPosition = parseInt(getBannerPosition(activeBanner));
	
	bannerToShow = currentPosition - 1;
	if (bannerToShow < 0) //remember array index starts at zero
		{bannerToShow = bannersArrayIn.length-1;}//we actually want the last one in the list
	
	//then show the previous one
	ShowBannerItem(bannersArrayIn[bannerToShow]);
}

function ShowBannerNext()
{	
	var activeBanner = $('#activeBanner').val();//find current active banner
	var currentPosition = parseInt(getBannerPosition(activeBanner));
	
	bannerToShow = currentPosition + 1;
	if (bannerToShow >= bannersArrayIn.length) //remember array index starts at zero
		{ bannerToShow = 0; }//we actually want the last one in the list
	
	//then show the previous one
	ShowBannerItem(bannersArrayIn[bannerToShow]);
}

function getBannerPosition(currentPositionName) {
	var returnPosition=0;
	for (var i=0; i<bannersArrayIn.length;i++) {
		if (bannersArrayIn[i]==currentPositionName) {
			returnPosition = i;
			break;
		}
	}
	return returnPosition;
}

function BannerRotater(instanceName, bannerContainerId, divisions)
{
	bannersArrayIn = divisions;
	this.Name = instanceName;
	this.IsStopped = false;
	this.IsRandomStart = true;

	//methods
	this.RotateBanners = rotateBanners;
	this.HighlightTab = highlightTab;


	// find out how many items we have
	//$contents = $('#' + bannerContainerId + ' .htmlInner');
	
	
	//if ($contents.length > 1)
	if (divisions.length>1)
	{
		// TODO: Show numbers and prev-next as applicable

		// start banner rotation
		// Stop banner rotation if a button is clicked
		


		//set all banners to opacity zero first
		for (i=0; i< divisions.length; i++)
		{
			$('#' + divisions[i]).fadeTo(0, 0);
		}
	}
	else //only 1 item
	{
		//double check - if zero do nothing
		//TODO: fade in banner
	}

		
	/*
	* Gets all the banners and cycles through them
	*/
	function rotateBanners(currentIndex)
	{
		if (!this.IsStopped)
		{
			//actions:
			//Fade current banner out
			//set to hidden
			//set new banner to not hidden
			//fade in new banner		
			
			var refreshRate = 6000; //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 = divisions[currentIndex];
			bannerIdNew = divisions[nextIndex];

			//$('#' + bannerIdNew).fadeTo(0, 0);
			$('#banner-' + bannerIdNew).css('z-index', '3');
			$('#banner-' + bannerIdOld).css('z-index', '2');	
			$('#banner-' + bannerIdNew).fadeTo(fadeRate, 1).addClass('active');
			$('#banner-' + bannerIdOld).removeClass('active');//set css to finish off visuals
			//$('#' + bannerIdOld).fadeTo(fadeRate, 0);

			//fade out banner after new banner has faded in
			setTimeout('$("#banner-' + bannerIdOld + '").fadeTo(' + (fadeRate/4) + ', 0);', fadeRate);
			
			//line up the next banner
			setTimeout(this.Name + '.RotateBanners(' + nextIndex + ')', refreshRate);
	
			$('#banner-button-' + bannerIdOld).removeClass('active');//set css on buttons
			$('#banner-button-' + bannerIdNew).addClass('active');
			
			/* note change behind the scenes */
			$('#activeBanner').val(bannerIdNew);
		}
	}

	/*	Sets css to produce indicator highlights on homepage LHS tabs, triggered by banner rotator	*/	
	function highlightTab(productline)
	{
		var arrProdLine = [ 'Metrology','Encoders','Medical', 'Spectroscopy']; 
		
		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	
		}
	}
}

/* Controls the central in-line product cat selector*/
/* *********  PNW Version *********** */
(function ($) {
    $.homeLineChooserContent = function (control, options) {
        var c = $(control).parents('#home-line-chooser');
        var l = $(control);
        l.mouseenter(function () {
            if(!l.hasClass('inactive')) { return false; }
            l.removeClass('inactive');              
            c.find('.line-top-level.inactive').stop(true).animate({ width: options.minWidth + 'px' }, 200, 'linear');
                                  
            l.children('.line-top-level-content').hide();
            l.stop(true).animate({ width: options.maxWidth + 'px' }, 200, 'linear', function () { $(this).children('.line-top-level-content').slideDown('fast'); });            
			//if(l.width() < options.maxWidth) {
			//	l.stop(true).animate({ width: options.maxWidthUnits  }, options.expandSpeed, 'linear', function () { $(this).children('.line-top-level-content:not(:animated):not(:visible)').slideDown('fast'); });            			
			//} else {
			//	l.children('.line-top-level-content:not(:animated):not(:visible)').slideDown('fast');
			//}
        });
        l.mouseleave(function(){
			//var x = e.pageX - c[0].offsetLeft;
			//if(((c.width()-x) <= options.defaultWidth) && ((c.width()-x)>=0)) { 
			//	if(l[0] == c.find('.line-top-level:last')[0]) { return; }
			//}
			l.addClass('inactive');
            c.find('.line-top-level').animate({ width: options.defaultWidth + 'px' }, 200, 'linear');
        });
    };
    $.homeLineChooser = function(control, options) {
        var c = $(control);
        c.find('.line-top-level').addClass('inactive');
        c.mouseenter(function() {
            c.find('.line-top-level').filter(function (i) { 
                return $(this).hasClass('.inactive') 
            }).stop(true).animate({ width: options.minWidth + 'px' }, 200, 'linear');
        });
        c.mouseleave(function () {            
            c.find('.line-top-level').stop(true);
            c.find('.line-top-level').addClass('inactive').animate({ width: options.defaultWidth + 'px' }, 200, 'linear');
        });
    };
    $.fn.homeLineChooserContent = function () {
        var options = {};
        if (arguments.length > 0) { $.extend(options, arguments[0]); }
        options.minWidth = options.minWidth || 120;
        options.maxWidth = options.maxWidth || 617;
        options.defaultWidth = options.defaultWidth || 244;
		this.each(function () {
            $.homeLineChooserContent(this, options);
        });
    };
    $.fn.homeLineChooser = function() {
        var options = {};
        if (arguments.length > 0) { $.extend(options, arguments[0]); }
        options.minWidth = options.minWidth || 120;
        options.maxWidth = options.maxWidth || 617;
		options.defaultWidth = options.defaultWidth || 244;
        this.each(function () { $.homeLineChooser(this, options); });
    };
})($);
$(document).ready(function () {
    $("#home-line-chooser .line-top-level").homeLineChooserContent();
    $("#home-line-chooser").homeLineChooser();
});

/*
SEL: this code designed to let us add items ad-hoc easily without a page republish - e.g. urgent notices. 
DO NOT delete this code, if it is not in use simply comment out the function call directly after this code
*/
function AddHomePageNews(){
	if ( (location.hostname == "renishaw.jp") || (location.hostname == "www.renishaw.jp")  || (location.hostname == "www.renishaw.co.jp")  || (location.hostname == "renishaw.co.jp") )
	{
		$("#home-news-feed").after('<ul class="medialist"><li class="medialist-row media-file pdf"><a class="mediaItem" href="/media/shared/Renishaw_sues_Nanophoton_jp.pdf">レニショーがナノフォトンに対して特許侵害で告訴</a></li></ul>')
	}
	if ( (location.hostname == "renishaw.com") || (location.hostname == "www.renishaw.com") )
	{
		$("#home-news-feed").after('<ul class="medialist"><li class="medialist-row media-file pdf"><a class="mediaItem" href="/media/shared/Renishaw_sues_Nanophoton.pdf">Renishaw sues Nanophoton</a></li></ul>')            
	}
}
$(document).ready(AddHomePageNews);
function AddRegulatoryPageUpdate()
{
	$('#ElementMediaList12693 ul.medialist').prepend('<li class="medialist-row media-file pdf"><a class="mediaItem" href="/media/shared/Interim Management Statement Oct 2010.pdf">Interim Management Statement Oct 2010</a></li>');
}
/*$(document).ready(AddRegulatoryPageUpdate);*/


/*
SEL - end ad-hoc publish code
*/

