var decadePos_Arr = new Object();
var zoomS = "80%"; 
var zoomM = "100%"; 
var zoomL = "120%";
var zoomLevel = "zoomM";
var heroFadeSpeed = 500;
var activeHomeHero = "1";
var dropDownConfig = {
	sensitivity: 3, 
	interval: 100, 
	over: displayDD,
	timeout: 200,
	out: hideDD
};
var tipConfig = {
	sensitivity: 3, 
	interval: 100, 
	over: displayTip,
	timeout: 200,
	out: hideTip
};
var videoTipConfig = {
	sensitivity: 3, 
	interval: 100, 
	over: displayVideoTip,
	timeout: 200,
	out: hideVideoTip
}
var tipReportConfig = {
	sensitivity: 3, 
	interval: 100,
	over: displayReportTip,
	timeout: 200,
	out: hideTip
}
var overTip = false;
var currDecade = "";
//	Multiple Assessments vars
var currTnTab1 = "tabTnOverview";
var currTnTab2 = "tabTnArticles";
//var currTnTab1 = "tabTnReports";
//	Sample Reports
var reportContentsURL = "/img/reports/sampleReport.html";
var currReportAudience = "audience1";
var currReportType = "reportType1";
var reportsLBReqStatus;
$(document).ready(function() {
	//	remove outline to clicked links
	$("a").mouseup(function(){$(this).blur();});
	//	Text sizing tool
	//------------------------------------------
	$("#zoomS").click(function(){
		setZoom("zoomS");
	});
	$("#zoomM").click(function(){
		setZoom("zoomM");
	});
	$("#zoomL").click(function(){
		setZoom("zoomL");
	});
	//	=searchBox
	//------------------------------------------
	$("#searchTerm").focus(function() {
		if ($(this).attr("value") == "Enter keyword") $(this).attr("value", "");
	});
	$("#searchTerm").blur(function() {		
		if ($(this).attr("value").length==0 ) $(this).attr("value", "Enter keyword");		
	});
	
	$(".textZoom img").hover(textZoomOver, textZoomLeave);
	
	//	Add/remove _over to src of image (must be .gif and enclosed in a.rollOverbtn)
	//------------------------------------------
	var isset = false;
	$("a.rollOverBtn").mouseover(function(){
		try {
			src = ($("img",$(this)).attr("src")).split(".gif");
			$("img",$(this)).attr("src",src[0] + "_over.gif");
		}catch(e){}
	}).mouseout(function(){
		try {
			src = ($("img",$(this)).attr("src")).split("_over.gif");
			if(src.length == 2) { 
				// this fix is for IE, after page refresh, on mouse out no image was being displayed
				$("img",$(this)).attr("src",src[0] + ".gif");
			}
		}catch(e){}
	});

	//	DropDowns
	$(".dropDown, .dropDownL2").hoverIntent( dropDownConfig );
	if($.browser.msie) {
		$("li[rel^='overlay']").hover(function() { $(".grayTabsContent").css("position", "static"); });
		$("li[rel^='overlay']").hoverIntent( dropDownConfig ).live("mouseout", function() { $(".grayTabsContent").css("position", "relative"); });
	}
	
	//	Generic dropDown
	var dropListTO;
	$(".dropListContainer").click(function() {
		$(".dropList",$(this)).toggle();
		reportNoConflict('on');
		dropListTO = setTimeout(dropListHide, 2000);
	});
	$(".dropList").hover(function() {
			clearTimeout(dropListTO);
		},
		function(){
			dropListTO = setTimeout(dropListHide, 500);
		}
	);
	//	Multiple Assessments
	//------------------------------------------
	//	Display default audience's set of report thumbs
	updateReportThumbs();
	$("#reportAudience1 ul li a").click(updateCurrAudience);
	$("#reportType ul").hide();
	$("#reportType ul#" + currReportAudience + "_opt").show();	//	show default linked set of options in right dropdown
	$(".maSelection").each(function() {
		var container = $(this).parent();
		$("ul li a", $(this)).click(function(){
			$(".selected", container).html($(this).html());
			if ($(container).attr("id") == "reportType") {
				//	save selected id to global currReportType
				currReportType = $(this).attr("id");
			}
			dropListHide();
			return false;
		});
	});
	//	Load remote sample reports and show PopUp
	if ($.blockUI) {
		//	run this only if blockUI plugin has been loaded
		$.blockUI.defaults.css = {}; 
		$('.reportThumbs a').click(function() {
			currReportType = getBaseId($(this).attr("id"), "_1");
			initReportsLB();
		});
		$("#btnCloseLB").live("click",closeReportsLB);
	}
	$("#btnCloseWait").live("click",cancelReportsLB);
	//	Linked dropDowns in MA PopUp
	$("#reportAudience2 ul li a").click(function() {
		//	update second dropDown
		$("#reportType ul").hide();
		$("#reportType #" + $(this).attr("id") + "_opt").show();
		//	auto select linked dropdown's first opt
		$("#reportType .selected").html($("#reportType #" + $(this).attr("id") + "_opt li:first a").html());
		currReportType = $("#reportType #" + $(this).attr("id") + "_opt li:first a").attr("id");
	});
	$(".btnGetReport").click(updateReportView);

	//	Video Carousel
	//------------------------------------------
	$('#videoCarousel').serialScroll({
		items:'li',
		prev:'#btnPrev img',
		next:'#btnNext img',
		start:0,
		duration:300,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		exclude:3,
		onBefore:updateTimelineArrows
	});
	//$("#videoCarousel ul li img").hoverIntent(videoTipConfig);	
	$("#listOfVideo a img").hoverIntent(videoTipConfig);
	//Adding onclick event for video details
	$("#listOfVideo a").click(function(e){ displayVideoDetails($(this).attr("id"));});
	$(".videoTip").hover(tipOver, tipOut);
	

	//	=home
	//------------------------------------------
	//	Hero image swap
	$("#heroTab1").hover(heroOver, heroOut).click(function() {
		//	select hero 1
		$("#homeFeature").css("background-position", "bottom left");
		if (activeHomeHero!=1) {
			$("#homeHero1").show();
			$("#homeHero" + activeHomeHero).fadeOut(heroFadeSpeed);
		}
		activeHomeHero = 1;
		updHeroTabStates();
	});
	$("#heroTab2").hover(heroOver, heroOut).click(function() {
		//	select hero 2
		$("#homeFeature").css("background-position", "bottom center");
		if (activeHomeHero==1) {
			$("#homeHero2").fadeIn(heroFadeSpeed, function(){$("#homeHero1").hide();});
		}
		else if (activeHomeHero==3) {
			$("#homeHero2").show();
			$("#homeHero3").fadeOut(heroFadeSpeed);
		}
		activeHomeHero = 2;
		updHeroTabStates();
	});
	$("#heroTab3").hover(heroOver, heroOut).click(function() {
		//	select hero 3
		$("#homeFeature").css("background-position", "bottom right")
		if (activeHomeHero!=3) {
			$("#homeHero3").fadeIn(heroFadeSpeed, function(){$("#homeHero1, #homeHero2").hide();})
		}
		activeHomeHero = 3;
		updHeroTabStates();
	});
	$(".productTabIcons #byName").show();	
	$(".productTabIcons #slidesContainer").show();
	$(".productTabIcons .slide").show();
	$(".productTabIcons #slideInner").show();
	//	Update tabs and icons in products tab
	//------------------------------------------
	$("ul.productTabs li").click(function() {
		$("ul.productTabs li").each(function() {$(this).removeClass("selected");});
		$(this).addClass("selected");
		var newTab_Arr = ($(this).attr("id").split("show_"));
		$(".productTabIcons div").hide();
		$(".productTabIcons #" + newTab_Arr[1]).show();
		if(newTab_Arr[1] == 'byName')
		{
			$(".productTabIcons #byName").show();
			$(".productTabIcons #slidesContainer").show();
			$(".productTabIcons .slide").show();
			$(".productTabIcons #slideInner").show();
		}
	});
	//	Tips over logos
	$(".logoMore").hoverIntent(tipConfig);
	$(".productTip").hover(tipOver, tipOut);
	//	See All Products

	//	tabs
	//------------------------------------------
	$("#grayTabs1Container .grayTabs a").click(function() {grayTabClick(this,"grayTabs1Container");return false;});
	$("#grayTabs2Container .grayTabs a").click(function() {grayTabClick(this,"grayTabs2Container");return false;});
	$("#grayTabs3Container .grayTabs a").click(function() {grayTabClick(this,"grayTabs3Container");return false;});
	$("#"+currTnTab1).show();
	$("#"+currTnTab2).show();

	//	=article
	//------------------------------------------
	$("#btnShare, #shareDropDown").mouseover(function(){		
		$(".articleUtility").css("position","relative");//	needs this for positioning
		$("#shareDropDown").show();
	}).mouseout(function(){
		$("#shareDropDown").hide();
		$(".articleUtility").css("position","static");// in IE6/7 position relative makes this div cover the subnav dropdown
	});

	//	=heritage Timeline
	//------------------------------------------
	$('#timeline').serialScroll({
		items:'li',
		prev:'#btnPrev img',
		next:'#btnNext img',
		start:0,
		duration:300,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		exclude:3,
		onBefore:updateTimelineArrows,
		onAfter:updateDecades
	});
	//	Timeline jump between decades (positions dictionary)
	//------------------------------------------
	$("#timeline ul li").each(function(i) {
		if ($(this).hasClass("decade")) {
			decadePos_Arr['goto' + $(this).attr("id")] = i;
		}
	});
	$("#decadeNav div").click(function(){
		$("#timeline").trigger( 'goto', decadePos_Arr[$(this).attr("id")] );
		setDecadeBtn($(this).attr("id"));
	});
	//	Article update
	//------------------------------------------
	$("#btnCloseFeature").live("click",defaultHeritageArticle);
	$(".updateArticle").click(function() {
		$("#defaultArticle").hide();
		$(".dynamicArticle").hide();
		$("div." + $(this).attr("id")).show();
	});
});
function defaultHeritageArticle() {
	$(".dynamicArticle").hide();
	$("#defaultArticle").show();
}
function displayDD() {
	$(this).toggleClass("over");
	$(this).children("ul, div").show();
}
function hideDD() {
	$(this).toggleClass("over");
	$(this).children("ul, div").hide();
}
function setZoom(size) {
	$("body").css("font-size",eval(size));
	$("#zoomS").attr("src", "/ctb.com/images/img/btnTextZoomSmall.gif");
	$("#zoomM").attr("src", "/ctb.com/images/img/btnTextZoomMed.gif");
	$("#zoomL").attr("src", "/ctb.com/images/img/btnTextZoomLarge.gif");
	src = ($("#" + size).attr("src")).split(".gif");
	$("#" + size).attr("src",src[0] + "_sel.gif");
	zoomLevel = size;
}
function textZoomOver() {
	if ($(this).attr("id") != zoomLevel) {
		src = ($(this).attr("src")).split(".gif");
		$(this).attr("src",src[0] + "_sel.gif");
	}
}
function textZoomLeave () {
	if ($(this).attr("id") != zoomLevel) {
		src = ($(this).attr("src")).split("_sel");
		$(this).attr("src",src[0] + ".gif");
	}
}
function updateTimelineArrows( e, elem, $pane, $items, pos ) {
	if (pos == 0) {	$("#btnPrev img").attr("src", "/ctb.com/images/img/btnPrev_dis.gif"); }
	else { $("#btnPrev img").attr("src", "/ctb.com/images/img/btnPrev.gif"); }
	if (pos >= $items.length - 4) {	$("#btnNext img").attr("src", "/ctb.com/images/img/btnNext_dis.gif"); }
	else { $("#btnNext img").attr("src", "/ctb.com/images/img/btnNext.gif"); }
	//	update decades so the current gets highlighted
	$.each(decadePos_Arr, function(key,val) {
		if (pos == val) {
			currDecade = key;
			if (key == "gotod1930") {currDecade = "gotod1920"}
		}
	});
}
function updateDecades() {
	if (currDecade !== "") {
		setDecadeBtn(currDecade);
	}
	currDecade = "";
}
function setDecadeBtn(decade) {
	$(".decadeTab").removeClass("selected");
	$("#" + decade).addClass("selected");
}
function displayReportTip(ev) {
	var target = $("#" + $(this).attr("id") + "_more");
	target.show();
}
function displayTip(ev) {
	var target = $("#" + $(this).attr("id") + "_more");
	$(".productTip").hide();
	var pointerWidth = 12;
	//get position and width of the trigger element
	var triggerPos = $(this).offset();  
	var triggerWidth = $(this).width();
	if ($(this).hasClass("leftMost")) {
		//	If first logo, position left edge with left edge
		target.css( { "left": (triggerPos.left - 5) + "px", "top":(triggerPos.top - 130) + "px" } );
		$(".tipPointer", target).css("margin-left", "32px");
	}
	else if ($(this).hasClass("rightMost")) {
		//	If last logo, position right edge with right edge
		target.css( { "left": (triggerPos.left + (triggerWidth) - 27 - target.width()) + "px", "top":(triggerPos.top - 130) + "px" } );
		var targetWidth = target.width() + 10;
		var pointerLeft = targetWidth  - pointerWidth - 10;
		$(".tipPointer", target).css("margin-left",pointerLeft + "px");
	}
	else {
		//	position centered relative to logo
		var targetWidth = target.width() + 25;
		target.css( { "left": (triggerPos.left - (targetWidth - triggerWidth)/2) + "px", "top":(triggerPos.top - 130) + "px" } );
		var pointerLeft = targetWidth / 2 - pointerWidth;
		$(".tipPointer", target).css("margin-left",pointerLeft + "px");
	}
	target.show();
}
function hideTip() {
	if (!overTip) {
		var target = $("#" + $(this).attr("id") + "_more");
		target.hide();
	}
}
function tipOver() {
	overTip = true;
}
function tipOut() {
	overTip = false;
	$(this).hide();
}
function dropListHide() {
	reportNoConflict('off');
	$(".dropList").hide();
}
function heroOver() {
	$("img", this).fadeIn("fast");
}
function heroOut() {
	$("img", this).fadeOut("fast");
}
function updHeroTabStates() {
	$("#heroTab1, #heroTab2, #heroTab3").addClass("active");
	$("#heroTab" + activeHomeHero).removeClass("active");
}
function grayTabClick(_this, containerId){
	$("#" + containerId + " .grayTabs li").removeClass("selected");
	$(_this).parent().addClass("selected");
	$("#" + containerId + " .grayTabsContent").hide();
	$("#" + containerId + " .grayTabsNarrowContent").hide();
	var newTab = $(_this).attr("id").split("_link");
	$("#"+newTab[0]).show();
	return false;
}
function initReportsLB() {
	reportsLBReqStatus = true;
	$.blockUI({ message: null,
				overlayCSS: { cursor: 'default' }
	});
	$().scrollTop(0);
	$('#reportsLightbox').hide();	//hide until ajax calls back
	$('#wait').show();
	$('#remoteReports').load(reportContentsURL,{id: 1}, remoteReportsReady);
}
function cancelReportsLB() {
	reportsLBReqStatus = false;
	closeReportsLB();
	$('#wait').hide();
}
function remoteReportsReady() {
//	setTimeout(displayReportsUI, 2000);
	displayReportsUI();
}
function displayReportsUI() {
	if (reportsLBReqStatus) {
		//	Auto select curr audience
		$("#reportAudience2 .selected").html($("#" + currReportAudience).html());
		$('#wait').hide();
		updateReportView();
		$('#reportsLightbox').show();
	}
	return;
}
function updateReportView() {
	$("div[id$='reportContent']").hide();
	$("#" + currReportType + "_reportContent").show();
}
function updateReportThumbs() {	
	$(".reportThumbs").hide();
	$("#" + currReportAudience + "ReportThumbs").show();
}
function closeReportsLB() {
	$('#reportsLightbox').hide();
	$.unblockUI();
}
function getBaseId(fullId, sufix) {
	//	expects: "something_1", "_1"
	//	returns: "something"
	var tmp_Arr = (fullId.split(sufix));
	return tmp_Arr[0];
}
function updateCurrAudience() {
	currReportAudience = getBaseId($(this).attr("id"), "_1");//	remove "_1" that makes unique ids between first page's control and the popup's one
	updateReportThumbs();
}
function reportNoConflict(state) {
	/*
	Change position of reportPicture to static when it's above dropDown is expanded
	otherwise picture overlaps list
	*/
	if (state=='on') {
		$(".reportPicture").css("position","static");
		$(".reportSpot").hide();
	}
	else {
		$(".reportPicture").css("position","relative");
		$(".reportSpot").show();
	}
}
function displayVideoTip(ev) {
	var id = $(this).attr("id");
	var tip = $("#" + id.replace("_thumb", "_tip"));
	//get position and width of the trigger element
	var triggerPos = $(this).offset();  
	var triggerWidth = $(this).width();
	tip.css({'top': triggerPos.top - 85 ,'left':triggerPos.left + triggerWidth/2 - 30});
	tip.show();
}
function hideVideoTip() {
	if (!overTip) {
		$('.videoTip').hide();
	}
}

function displayVideoDetails(id) {
	var spans=$("#"+id+" span");
	$("#videoDetails h2").text($(spans[0]).text());	
	$("#videoDetails p").html($(spans[1]).html());	
}

function hrefWithCurrentURL(url){	
	var modifiedURL=url;
	var currentURL=location.href;
	if(modifiedURL){
		if(modifiedURL.indexOf("currentUrl=")){			
			//assuming that currentUrl is the last param
			currentURL=currentURL.replace(/\?currentUrl=.*/,"");			
			currentURL=currentURL.replace(/&currentUrl=.*/,"");			
		}
	}
	if(modifiedURL.indexOf("?")==-1){
		modifiedURL=modifiedURL+"?currentUrl="+escape(currentURL);
	}else {
		modifiedURL=modifiedURL+"&currentUrl="+escape(currentURL);
	}	
	location.href=modifiedURL;
}

function getHREFWithCurrentURL(url){	
	var modifiedURL=url;
	var currentURL=location.href;
	var urls = url.split('?');
	var url0="";
	var url1="";
	if(urls[0] != null)
		url0 = urls[0];
		
	if(urls[1] != null)
		url1 = urls[1];
		
	if(modifiedURL){
		if(modifiedURL.indexOf("currentUrl=")){			
			//assuming that currentUrl is the last param
			currentURL=currentURL.replace(/\?currentUrl=.*/,"");			
			currentURL=currentURL.replace(/&currentUrl=.*/,"");			
		}
	}
	/*if(modifiedURL.indexOf("?")==-1){
		modifiedURL=modifiedURL+"?currentUrl="+escape(currentURL);
	}else {
		modifiedURL=modifiedURL+"&currentUrl="+escape(currentURL);
	}*/
	if(url1 != null && url1 != '')
		modifiedURL = url0 + "?currentUrl="+escape(currentURL) + "&" + url1;
	else
		modifiedURL = url0 + "?currentUrl="+escape(currentURL);
	
	return modifiedURL;
}

function nextStepUrlButtonChange(){
  		
	var nextButton=$("#externalUrl");
	if(nextButton.length==0)
		return;
	
	var width=nextButton.attr("textWidth");
	if(width!=""){
		width=width.split("_");
		if(width.length == 2){
			nextButton.css({'display':'block','margin':'0px auto','width':width[0]+"px"});
			nextButton.parent().css("width",width[1]+"px");
		}
	}
	if(nextButton.attr("newWindow")=="Y")
		nextButton.attr("target","_blank");
		
	nextButton.mouseover(function(){
		$(this).children("img").attr("src","showMediaViewAction?mediaId="+nextButton.attr("onImgId"));
	});
	
	nextButton.mouseout(function(){
		$(this).children("img").attr("src","showMediaViewAction?mediaId="+nextButton.attr("offImgId"));
	});  		
}

function clearImageOnLeft(){	
	var nextStepDiv=$("#nextStepDiv");
	if(nextStepDiv.length==0)
		return;
	var parentWidth=nextStepDiv.width();	
	var childDivs=$("#nextStepDiv").children("div");	
	var totlaWidth=0;
	var childDiv;
	var width=0;
	for(var i=0;i<childDivs.length;i++){
		childDiv=childDivs.eq(i);
		width=childDiv.width();
		totlaWidth+=width;	
		if(totlaWidth > parentWidth){
			childDivs.eq(i).css("clear","left");
			totlaWidth=width;
		}
	}
}
