﻿
var activeBannerId = "";
var activeBannerControllerId = "";
var activeTipId = "";
var rotate = true;
var myBannerNumber = 1;
var activeBannerNumber = myBannerNumber;
var bannerItemsLength = $(".banner-item").length;
var bannerTimeout;
var useToolTips = false;

/*
 * 	Config to taste
 */
var bannerFadeIn = 750;
var bannerFadeOut = 50;
var bannerControlsFadeIn = 2000;
var bannerRotateSpeed = 6000;

/*
 * 	End config
 */


function displayBanner(myBannerId){
	if (activeBannerId !== myBannerId) {
		if(activeBannerId !== ""){
			hideBanner(activeBannerId);
		}
		activeBannerId = myBannerId;
		if(useToolTips === true){
			activeTipId = document.getElementById(myBannerId).tip.id;
			hideTooltip(activeTipId);
		}
		activeBannerControllerId = document.getElementById(myBannerId).controller.id;
		$("#" + activeBannerControllerId).addClass("selected").click(function(e){
			e.preventDefault();
		});
		$('#' + myBannerId).fadeIn(bannerFadeIn, function(){
				myBannerNumber++;
				bannerTimeout = setTimeout("rotateBanner(myBannerNumber)", bannerRotateSpeed);
		});
	}
	else{
		return false;
	}
}

function hideBanner(myBannerId){
	activeBannerId = "";
	 $("#"+activeBannerControllerId).removeClass("selected").click(function(){
	 	return true;
	 });
	  $('#'+myBannerId).fadeOut(bannerFadeOut, function() {
	   	return true;
	  });
}


/*
 * Tooltip for banner controls
 * <div id="banner-controls-tip"><div id="bct-text" class="rounded-5px-all">PKI for Enterprise</div><div id="bct-bottom"></div></div>
*/





	function hideTooltip(tipId){
		myToolTip = document.getElementById(tipId);
		myToolTip.style.display = "none";
	}

	function showTooltip(tipId){
		myToolTip = document.getElementById(tipId);
		myToolTip.style.display = "block";
	}

function rotateBanner(){
	$('#banner-controls').click(function(){
		return true;
	})
	if (!rotate) {
		clearTimeout(bannerTimeout);
		return false;
	}
	else {
		if (myBannerNumber > bannerItemsLength) {
			myBannerNumber = 1;
			rotate = false;
		}
		myBannerId = "banner" + myBannerNumber;
		displayBanner(myBannerId);
	}
}

function initializeRotator(toolTipFlag){ // set flag to true if you have tool tips -- default is true

	if(typeof(toolTipFlag) == "undefined"){
		useToolTips = true;
	}
	else if(typeof(isIe6) != "undefined"){ // this var is set in shared assets preload
		useToolTips = false;
	}
	else{
		useToolTips = toolTipFlag;
	}

	$('#banner-controls').click(function(){
		return false;
	})

	if (useToolTips === true) {
		var toolTipShown = false;

		$(".banner-control").hover(function(){

			//console.log("OVER - " + $(this).attr("id"));
			if (!$(this).hasClass("selected")) {
				showTooltip(this.banner.tip.id);
			}
		}, function(){
			//console.log("OUT - " + $(this).attr("id"));
			if (!$(this).hasClass("selected")) {
				hideTooltip(this.banner.tip.id);
			}
		})

		$(".banner-control").click(function(){
			//console.log(this.banner.tip.id);
			hideTooltip(this.banner.tip.id);
			//	}
		});
	}

	$(".banner-item").each(function(i){
		myId = this.id;
		myControllerNumber = i+1;
		bannerControlId = "bc"+myControllerNumber;
		this.$controller = $("#"+bannerControlId);
		this.controller = document.getElementById(bannerControlId);
		this.controllerCenter = (this.controller.offsetLeft+15);
		if (useToolTips === true) {
			tipId = "tip" + myControllerNumber;
			this.$tip = $("#" + tipId);
			this.tip = document.getElementById(tipId);
			this.tip.banner = this;
			this.tip.offset = -(this.$tip.width()-20);
			this.tip.style.left = this.controllerCenter+this.tip.offset+"px";
		}
		this.controller.banner = this;
		this.controller.bannerId = this.id;
		this.$controller.click(function(){
			displayBanner(this.bannerId);
			rotate = false;
		});
	});

	$('#banner-controls').fadeIn(bannerControlsFadeIn, function(){
				rotateBanner();
	});

}

//Added initialization code
//initializeRotator();
