var rotationIntervalId = null;

function mastheadRotator() {
	if($('div#mastheadBanners').size() != 0) {
		$('div#mastheadBanners ul#banner-images li').css({opacity: 0.0});//Set all images opacity to 0
		$('div#mastheadBanners ul#banner-images li:first').css({opacity: 1.0});//Set the first image opacity to 100%
		rotateDoNextButtonTimeoutImage();
		rotationIntervalId = setInterval('rotate()',5000);//Run the rotate function for 5 seconds	
	}
}

function rotateGetCurrent() {
	return ($('div#mastheadBanners ul#banner-images li.show')?  $('div#mastheadBanners ul#banner-images li.show') : $('div#mastheadBanners ul#banner-images li:first'));//Get the first image
}

function rotateGetNext(current) {
	return ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#mastheadBanners ul#banner-images li:first') :current.next()) : $('div#mastheadBanners ul#banner-images li:first'));//Get next image, when it reaches the end, rotate it back to the first image
}

function rotateGetCurrentMHButton() {
	var currentMHButton = (($('div#mastheadBanners ul#banner-buttons li.activeButton').size() != 0) ? $('div#mastheadBanners ul#banner-buttons li.activeButton') : $('div#mastheadBanners ul#banner-buttons li:first'));
	if(currentMHButton.attr('id') == "banner-pause") {
		return currentMHButton.next();
	}
	return currentMHButton;
	
}

function rotateGetNextMHButton() {
	var currentMHButton = rotateGetCurrentMHButton();
	var nextMHButton = ((currentMHButton.next().length) ?  currentMHButton.next() : $('div#mastheadBanners ul#banner-buttons li:first'));
	
	if(nextMHButton.attr('id') == "banner-pause") {
		return nextMHButton.next();
	}
	return nextMHButton;
}

function rotateRemoveButtonTimeoutImage() {
	var currentMHButton = rotateGetCurrentMHButton();

	currentMHButton.removeClass('activeButton');
	var currentMHButtonImg = currentMHButton.find('a img');
	var currentMHButtonImgSrc = currentMHButtonImg.attr("src");
	if(currentMHButtonImgSrc.indexOf("1") > -1) {
		currentMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/1_MHbutton.png");
	} else if(currentMHButtonImgSrc.indexOf("2") > -1) {
		currentMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/2_MHbutton.png");
	} else if(currentMHButtonImgSrc.indexOf("3") > -1) {
		currentMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/3_MHbutton.png");
	} else if(currentMHButtonImgSrc.indexOf("4") > -1) {
		currentMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/4_MHbutton.png");
	} else if(currentMHButtonImgSrc.indexOf("5") > -1) {
		currentMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/5_MHbutton.png");
	} else if(currentMHButtonImgSrc.indexOf("6") > -1) {
		currentMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/6_MHbutton.png");
	}
}

function rotateDoNextButtonTimeoutImage() {
	var currentMHButton = rotateGetCurrentMHButton();
	var nextMHButton = rotateGetNextMHButton();
	
	rotateRemoveButtonTimeoutImage();
		
	nextMHButton.addClass('activeButton');
	var nextMHButtonImg = nextMHButton.find('a img');
	var nextMHButtonImgSrc = nextMHButtonImg.attr("src");
	if(nextMHButtonImgSrc.indexOf("1") > -1) {
		nextMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/1active_MHbutton.png");
	} else if(nextMHButtonImgSrc.indexOf("2") > -1) {
		nextMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/2active_MHbutton.png");
	} else if(nextMHButtonImgSrc.indexOf("3") > -1) {
		nextMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/3active_MHbutton.png");
	} else if(nextMHButtonImgSrc.indexOf("4") > -1) {
		nextMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/4active_MHbutton.png");
	} else if(nextMHButtonImgSrc.indexOf("5") > -1) {
		nextMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/5active_MHbutton.png");
	} else if(nextMHButtonImgSrc.indexOf("6") > -1) {
		nextMHButtonImg.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/6active_MHbutton.png");
	} 
				
}

function rotate() {	
	rotateDoNextButtonTimeoutImage();

	var current = rotateGetCurrent();
	var next = rotateGetNext(current);
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);//Fade in the next image, the show class has higher z-index
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');//Hide the current image
};
	
function showBannerThumb(thumbNumber) {
	var thumb = $('li#banner-thumb-'+thumbNumber);
	
	var button = $('li#banner-button-'+thumbNumber);
	var buttonParent = button.parent();
	var buttonParentPos = buttonParent.position();
	buttonParentPos.top -= 50+4;
	buttonParentPos.left += (16+7+((2+16)*thumbNumber)-235)-2;
	thumb.css(buttonParentPos);
	
	thumb.hide();
	thumb.css('visibility','visible');
	thumb.slideDown();
}

function hideBannerThumb(thumbNumber) {
	var thumb = $('li#banner-thumb-'+thumbNumber);
	thumb.css('visibility','hidden');
	thumb.hide();
}

function stopRotation() {
	clearInterval(rotationIntervalId);
	rotationIntervalId = null;
}

function startRotation() {
	rotationIntervalId = setInterval('rotate()',5000);
}

// pause the rotation and update the play/pause button to the correct state/image
function pauseRotation() {
	var playPauseButton = $('#banner-button-playpause');
	if(rotationIntervalId == null) {
		startRotation();
		playPauseButton.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/pause_MHbutton.png");
	} else {
		stopRotation();
		playPauseButton.attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/play_MHbutton.png");
	}
}

// jump to any banner - index starts at 1
function jumpToBanner(bannerNumber) {
	// fetch first banner
	var first = $('div#mastheadBanners ul#banner-images li:first');
	
	// fetch current banner, remove the indicator class, and hide it
	var current = rotateGetCurrent();
	current.removeClass('show');
	current.css({opacity: 0.0})
	
	// set target to the banner that we want to display - count up from the first banner
	var target = first;
	for(var i=1;i<bannerNumber;i++) {
		target = target.next();
	}
	
	// kill the rotation timer that is currently registered
	stopRotation();
	
	// just in case we were in a 'paused' state, reset the 'pause' button to the correct icon
	$('#banner-button-playpause').attr("src","https://imagelibrary.securian.com/corporate/vcm/buttons/pause_MHbutton.png");
	
	// whatever button was active, reset it to be the plain number button
	rotateRemoveButtonTimeoutImage();
	
	// make the correct number button active
	var targetButton = $('ul#banner-buttons li#banner-button-'+bannerNumber);
	targetButton.addClass("activeButton");
	var targetButtonImg = targetButton.find('a img');
	var targetButtonImgSrc = targetButtonImg.attr("src");
	targetButtonImgSrc = targetButtonImgSrc.replace('_','active_');
	targetButtonImg.attr("src",targetButtonImgSrc);
	
	// show the banner we want to jump to
	target.addClass('show');
	target.animate({opacity: 1.0},1000);
	
	// and register the rotation timer
	startRotation();	
}
