// slideshow type scrollers
var bodyHeight = "";
var bodyWidth = "";

function animateTitle(){
	$("#cmapGOTO2040Title").addClass("animate");
}

$(document).ready(function(){
		setTimeout("animateTitle()",750);
		
		checkScrollSlideShowWidth();
		
		$("a#previous").bind("click", function(e){ this.blur();});
		$("a#next").bind("click", function(e){ this.blur();});
		
		$("map").bind("hover",function(e){alert("hi");})

		$(".go-to-top a").bind("click", function(e){e.preventDefault(); window.scrollTo(0,0);});
		curBack=-1;
		$("a[href^='#']").bind("click", function(e){
				if(!$(".go-to-top a")){								 
					try{
						curBack=curBack-1;
						
						$("a.moving-forward-gb-top").attr("href","java"+"script:history.go("+curBack+")");
						$(".go-back a").attr("href","java"+"script:history.go("+curBack+")");
						}catch(e){
							//stiffle errors if any.
							}
				}
				
			
				
		});
		

		
		// set up images for lightbox gallery
		var galImgs = new Array("img_id=235256&t=1286214096210", "img_id=235260&t=1286214096250", "img_id=235264&t=1286214096255", "img_id=235268&t=1286214096261", "img_id=235272&t=1286214096267", "img_id=235276&t=1286214096273", "img_id=235280&t=1286214096279", "img_id=235284&t=1286214096285", "img_id=235288&t=1286214096290");
		// images for spanish gallery
		var spanishImgs = new Array("img_id=288366&t=1298399890463", "img_id=288370&t=1298399912339", "img_id=288374&t=1298399929675", "img_id=288382&t=1298399967758", "img_id=288386&t=1298399967767", "img_id=288390&t=1298399967777", "img_id=288394&t=1298399967787", "img_id=288398&t=1298399967796", "img_id=288378&t=1298399967748");

		var animate = "null";
		// Show lightbox											
		jQuery("a.showLB").click(function() {
			if($(this).hasClass('spanishLB')) {
				var imgToShow = spanishImgs[0];
				var lbContent = "<div id='cmap-lightbox'>&nbsp;</div><div class='cmap-lightboxImg spanish-lightboxImg'><div id='cmap-lightbox-close'><img src='/cmap-theme/images/color_schemes/cmap/newimages/close_lightbox.gif' alt='close' align='right' /></div><div class='cmap-lightboxImgContent'><img id='lightboxImg' src='/image/image_gallery?" + imgToShow + "' /><div id='cmap-lightbox-nav'><div class='cmap-lightbox-nav-wrapper'><div id='cmap-lightbox-nav-prev'>&nbsp;</div><div id='cmap-lightbox-nav-next'>&nbsp;</div></div></div></div></div>";
			}
			else {
				var imgToShow = galImgs[0];
				var lbContent = "<div id='cmap-lightbox'>&nbsp;</div><div class='cmap-lightboxImg default-lightboxImg'><div id='cmap-lightbox-close'><img src='/cmap-theme/images/color_schemes/cmap/newimages/close_lightbox.gif' alt='close' align='right' /></div><div class='cmap-lightboxImgContent'><img id='lightboxImg' src='/image/image_gallery?" + imgToShow + "' /><div id='cmap-lightbox-nav'><div class='cmap-lightbox-nav-wrapper'><div id='cmap-lightbox-nav-prev'>&nbsp;</div><div id='cmap-lightbox-nav-next'>&nbsp;</div></div></div></div></div>";
			}
			getBodySize();
			animate = "yes";
			jQuery(lbContent).appendTo(document.body);
			
			jQuery("#cmap-lightbox-nav-prev").addClass('inactive');
			var imgSrc = jQuery(imgToShow).attr('src');

			jQuery('#cmap-lightbox').height(bodyHeight).width(bodyWidth).show();
			jQuery('.cmap-lightboxImg img#lightboxImg').attr('src', imgSrc)
			jQuery('.cmap-lightboxImg img#lightboxImg').load(function() {
				var imgWidth = jQuery('.cmap-lightboxImg img#lightboxImg').width();
				var imgHeight = jQuery('.cmap-lightboxImg img#lightboxImg').height();
												
				setOptions(imgWidth, imgHeight, animate);
			});

		});

		// Close lightbox
		jQuery("#cmap-lightbox-close").live('click', function() {
			jQuery('.cmap-lightboxImg').animate({height:0}, 400, function() {
				jQuery('.cmap-lightboxImg').remove();
				jQuery('#cmap-lightbox').remove();
			});
		});
		// Display next/previous image in lightbox
		jQuery(".default-lightboxImg .cmap-lightbox-nav-wrapper div").live('click', function() {
			animate = 'no';
			
			var currImg = jQuery('img#lightboxImg').attr('src');
			var currImgId = currImg.replace('/image/image_gallery?', '');
			var numImgs = galImgs.length-1;
			
			if(jQuery(this).attr('id') == 'cmap-lightbox-nav-prev') {
				var nextImg = jQuery.inArray(currImgId, galImgs) - 1;
			}
			else {
				var nextImg = jQuery.inArray(currImgId, galImgs) + 1;
			}
			
			var nextImgId = galImgs[nextImg];
			var nextImgSrc = '/image/image_gallery?' + nextImgId;

			if(nextImg >= 0 && nextImg < galImgs.length) {
				jQuery('img#lightboxImg').css({'opacity': 0});
				jQuery('.cmap-lightboxImg img#lightboxImg').attr('src', nextImgSrc).load(function() {
					jQuery("#cmap-lightbox-nav-prev").removeClass('inactive');
					jQuery("#cmap-lightbox-nav-next").removeClass('inactive');
					var imgWidth = jQuery(this).width();
					var imgHeight = jQuery(this).height();
					setOptions(imgWidth, imgHeight, animate);
					if(nextImg == 0) {
						jQuery("#cmap-lightbox-nav-prev").addClass('inactive');
					}
					else if(nextImg == numImgs)  {
						jQuery("#cmap-lightbox-nav-next").addClass('inactive');
					}
				});
			}
		});

		jQuery(".spanish-lightboxImg .cmap-lightbox-nav-wrapper div").live('click', function() {
			animate = 'no';
			
			var currImg = jQuery('img#lightboxImg').attr('src');
			var currImgId = currImg.replace('/image/image_gallery?', '');
			var numImgs = spanishImgs.length-1;
			
			if(jQuery(this).attr('id') == 'cmap-lightbox-nav-prev') {
				var nextImg = jQuery.inArray(currImgId, spanishImgs) - 1;
			}
			else {
				var nextImg = jQuery.inArray(currImgId, spanishImgs) + 1;
			}
			
			var nextImgId = spanishImgs[nextImg];
			var nextImgSrc = '/image/image_gallery?' + nextImgId;

			if(nextImg >= 0 && nextImg < spanishImgs.length) {
				jQuery('img#lightboxImg').css({'opacity': 0});
				jQuery('.cmap-lightboxImg img#lightboxImg').attr('src', nextImgSrc).load(function() {
					jQuery("#cmap-lightbox-nav-prev").removeClass('inactive');
					jQuery("#cmap-lightbox-nav-next").removeClass('inactive');
					var imgWidth = jQuery(this).width();
					var imgHeight = jQuery(this).height();
					setOptions(imgWidth, imgHeight, animate);
					if(nextImg == 0) {
						jQuery("#cmap-lightbox-nav-prev").addClass('inactive');
					}
					else if(nextImg == numImgs)  {
						jQuery("#cmap-lightbox-nav-next").addClass('inactive');
					}
				});
			}
		});
});


// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){
	
	
	//for news slider
	$('#newsSlideContainer').serialScroll({
		items:'li',
		prev:'a#previousNews',
		next:'a#nextNews',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1200,
		force:true,
		step:1,
		stop:true,
		lock:false,
		cycle:true, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: false, //click on the images to scroll to them
		interval:5000
	});
	$('#newsSlideContainer').trigger( 'start' );
	//for facts slider
	$('#slideContainer').serialScroll({
		items:'li',
		prev:'a#previous',
		next:'a#next',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1200,
		force:true,
		stop:true,
		lock:false,
		cycle:true, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: true, //click on the images to scroll to them
		interval:5000
	});
	$('#slideContainer').trigger( 'start' );
	
});

function launchPops(ids){
		var windowW=$(window).width();
		var windowH=$(window).height();
		var scrollLoc=$(window).scrollTop()-10;
		$("#coverAlls").css({display:"block", width:windowW+"px", height:windowH+"px", opacity:.5});
		popsContent=$("#"+ids).html();
		$("#popupsDiv .popsContent").html(popsContent);
		$("#popupsDiv").css({visibility:"hidden", display:"block"});
		timg=popsContent
		
		
			contWidth=$("#popupsDiv .popsContent img").width()+30;
			
		
		contHeight=$("#popupsDiv  .popsContent").height()+100;
		contsCent=(windowW/2)-(contWidth/2);
		
		$("#popupsDiv").css({width:contWidth+"px", visibility:"visible", margin:scrollLoc+"px auto "+contHeight+"px "+contsCent+"px", position:"absolute"});
		
		window.onresize=function(){recenter(ids)};
	}
	
	function recenter(ids){
		windowW=$(window).width();
		windowH=$(window).height();
		$("#coverAlls").css({width:windowW+"px", height:windowH+"px"});
		contWidth=$("#"+ids+" .popsContent").width();
		contsCent=(windowW/2)-(contWidth/2);
		$("#"+ids).css({margin:"0px auto 0px "+contsCent+"px"});
	}
	
	function closePopsUp(){
		$("#coverAlls").css({display:"none"});
		$("#popupsDiv").css({display:"none"});
		window.onresize="";
		}

// temp js for title images of sections


//setting cases

var urlEnd=window.location;
urlEnd=urlEnd.toString();
urlEnd=urlEnd.split("/");
urlEnd=urlEnd[urlEnd.length-1];

var titleImgfromUrl;
var breadyTitleImg;

switch(urlEnd)
{
case 'about': titleImgfromUrl="cmapMainTitle.png" ;
break;
case 'updates': titleImgfromUrl="cmapUpdates.png";
break;
case 'goto-2040': titleImgfromUrl="cmapGoto2040.png";
break;
case 'strategy-papers': titleImgfromUrl="cmapGoto2040.png";
break;
case 'moving-forward': titleImgfromUrl="cmapMovingForward.png";
break;
case 'publications': titleImgfromUrl="cmapPublications.png";
break;
case 'data': titleImgfromUrl="cmapData.png";
break;
case 'get-involved': titleImgfromUrl="cmapGetInvolved.png";
break;
case 'news': titleImgfromUrl="cmapNews.png";
break;
case 'events': titleImgfromUrl="cmapEvents.png";
break;
case 'for-the-media': titleImgfromUrl="cmapForMedia.png";
break;
case 'moving-forward-new': titleImgfromUrl="cmapMovingForward.png";
break;

default:titleImgfromUrl="nomatch";
}




$(document).ready(function(){
	if(titleImgfromUrl=="nomatch"){
			breadyTitleImg=$("#breadcrumbs li").eq(1).text();
			breadyTitleImg=breadyTitleImg.toLowerCase();
			switch(breadyTitleImg){
		
			case 'about': titleImgfromUrl="cmapMainTitle.png" ;
			break;
			case 'updates': titleImgfromUrl="cmapUpdates.png";
			break;
			case 'goto-2040': titleImgfromUrl="cmapGoto2040.png";
			break;
			case 'strategy-papers': titleImgfromUrl="cmapGoto2040.png";
			break;
			case 'strategy papers': titleImgfromUrl="cmapGoto2040.png";
			break;
			case 'go to 2040': titleImgfromUrl="cmapGoto2040.png";
			break;
			case 'moving-forward': titleImgfromUrl="cmapMovingForward.png";
			break;
			case 'moving forward': titleImgfromUrl="cmapMovingForward.png";
			break;
			case 'publications': titleImgfromUrl="cmapPublications.png";
			break;
			case 'data': titleImgfromUrl="cmapData.png";
			break;
			case 'get-involved': titleImgfromUrl="cmapGetInvolved.png";
			break;
			case 'get involved': titleImgfromUrl="cmapGetInvolved.png";
			break;
			case 'news': titleImgfromUrl="cmapNews.png";
			break;
			case 'events': titleImgfromUrl="cmapEvents.png";
			break;
			case 'for-the-media': titleImgfromUrl="cmapForMedia.png";
			break;
			case 'for the media': titleImgfromUrl="cmapForMedia.png";
			break;
			
			default:titleImgfromUrl="nomatch";
		}
		
		
	
	}
	setTimeout("setIE7_21Layout()", 100);
	//SetTitleImage();
	/*if(titleImgfromUrl=="cmapMovingForward.png"){
	$("#titleImg").addClass("moveFordward");}*/
});

// fixes ie7 issues in the 2-1 layout
function setIE7_21Layout(){
	$(".ie7 .columns-2-1 table:last-child").addClass("ieCorrectTableSpace");
}

function SetTitleImage(){
	//alert(titleImgfromUrl);
	if(titleImgfromUrl!="nomatch" && titleImgfromUrl!="cmapMainTitle.png"){
		locationImgDirectory="/cmap-theme/images/color_schemes/cmap/newimages/sectionHeaders/";
		classSplit=titleImgfromUrl.split(".");
		$("#cmapGOTO2040Title").animate({opacity:0}, 500);
		
		setTimeout("SetTitleImage2()",500);
		if(classSplit[0]=="cmapMovingForward"){
			updateMainNav()
		}
		
	}else{
		if($("#titleImg").attr("class")=="moveForward"){
			locationImgDirectory="/cmap-theme/images/color_schemes/cmap/newimages/sectionHeaders/";
			pclassFile="cmapMovingForward.png";
			classSplit=pclassFile.split(".");
			titleImgfromUrl="cmapMovingForward.png";
			$("#cmapGOTO2040Title").animate({opacity:0}, 500);
			setTimeout("SetTitleImage2()",500);
			updateMainNav()
			}
		}
	
}

function SetTitleImage2(){
		$("#cmapGOTO2040Title").css({top:"88px"})
		$("#cmapGOTO2040Title a").addClass(classSplit[0]);
		$("#cmapGOTO2040Title a").css({background:"url("+locationImgDirectory+titleImgfromUrl+") no-repeat"});
		$("#cmapGOTO2040Title a img").attr("src",locationImgDirectory+titleImgfromUrl);
		$("#cmapGOTO2040Title").animate({opacity:1}, 500);
}

function updateMainNav(){
	$("#navigation ul").addClass("altHoverColor");
}


$(document).ready(function(){
	var getHref;
	var setSubRss=$(".subscribe").length;
	var setSubRssPrev=$(".entry-navigation a.previous").length;
	var setSubRssNext=$(".entry-navigation a.next").length;
	
	if(setSubRss==1){
		setTimeout("runRSSLinker()", 500);
	}	
	
	if(setSubRssPrev!=0 || setSubRssNext!=0){
		if(setSubRssPrev!=0 && setSubRssNext!=0){
			setTimeout("runRSSLinkerSecondary(1)", 500);
		}
		
		if(setSubRssPrev!=0 && setSubRssNext==0){
			setTimeout("runRSSLinkerSecondary(1)", 500);
		}
		
		if(setSubRssPrev==0 && setSubRssNext!=0){
			setTimeout("runRSSLinkerSecondary(2)", 500);
		}
	}
});

function runRSSLinker(){
		rssLoc=$(".subscribe a").eq(0).attr("href");
		
		$("#cmapRSSFeedsLink").attr("href",rssLoc);
		$("#cmapRSSFeedsLink").attr("title","Subscribe to: "+title+" Feed");
		$("#cmapRSSFeeds").css({opacity:0,display:"block"});
		$("#cmapRSSFeeds").animate({opacity:1}, 500);	
	}
	
function runRSSLinkerSecondary(nn){
	if(nn==1){
		getHref=$(".entry-navigation a.previous").attr("href");
		//getHref=getHref.toString();
	}else{
		getHref=$(".entry-navigation a.next").attr("href");
		//getHref=getHref.toString();
	}
	getHrefSplit=getHref.split("/");
	getSplitL=getHrefSplit.length-1;
	newHref="";
	for(i=0;i<getSplitL;i++){
		newHref+=getHrefSplit[i]+"/"
		
	}
	newHref+="rss";
	
		//html for prepend
		prependText='<span id="cmapRSSFeeds"><a id="cmapRSSFeedsLink" href="" target="_blank"><span>RSS Feed</span></a></span>';
		//prepend title
		$("#taberTitle").prepend(prependText);
		newTitle=$("#taberTitle h1").text();
		$("#cmapRSSFeedsLink").attr("href",newHref);
		$("#cmapRSSFeedsLink").attr("title","Subscribe to: "+newTitle+" Feed");
		$("#cmapRSSFeeds").css({opacity:0,display:"block"});
		$("#cmapRSSFeeds").animate({opacity:1}, 500);
}

/* to fix enlarger script on IE in the top left position, bottom left positions does not need for some reason. */

$(document).ready(function(){
		
		if(document.all){
			
			$(".whiteBG .enlargerHolder").bind("click",function(){											
					IEEnlargeWorkaround(this.useMap);
					
			});
			
			$(".whiteBG .enlargerHolder").css({cursor:"pointer"});
			
		}				
});

function IEEnlargeWorkaround(ieDumDum){
		
			$("#"+ieDumDum+" area").click();
}

// Set up for lightbox window
	function setOptions(imgWidth, imgHeight, animate) {
		var lightboxWidth = imgWidth + 40;
		var lightboxHeight = imgHeight + 40;
		var bodyWidth = jQuery('body').width();
		var lbLeft = Math.round((bodyWidth - lightboxWidth)/2);
		jQuery('.cmap-lightboxImg').css({'width': lightboxWidth, 'left': lbLeft})
		if(animate == 'yes') {
			jQuery('.cmap-lightboxImg').stop().animate({'height':lightboxHeight});
		}
		else {
			jQuery('.cmap-lightboxImg').css({'height': lightboxHeight});
		}
		jQuery('img#lightboxImg').stop().animate({opacity:1}, 700);
	}
	
	function getBodySize() {
		bodyHeight = jQuery(document).height();
		bodyWidth = jQuery(document).width();
	}

jQuery(window).resize(function(){
	getBodySize();
	jQuery('#cmap-lightbox').css({'height': bodyHeight, 'width': bodyWidth});
});


/* homepage slider take two */

var sliderNomNum=1;
var prevsliderNomNum=0;
var sliderNomTimer=10000;
var nomTimerP;

$(document).ready(function(){//alert(title)
									 titlestring=title.toString();
									 //titlestring=titlestring.toLowerCase()
									 //titlestring=titlestring.split(" ");
								//alert(titlestring)	 
						   if(titlestring=="A Pivotal Moment: GO TO 2040"){
						   nomTimerP=setTimeout("chngSlideToAuto("+sliderNomNum+")",sliderNomTimer);
						   }
						   });

function chngSlideTo(slideNom){
	
	$(".hslide").removeClass("hslideActive");
	$(".hslide").eq(slideNom).addClass("hslideActive");
	
	clearTimeout(nomTimerP)
	
}
	
function chngSlideToAuto(slideNom){
	//alert(slideNom);
	$(".hslide").removeClass("hslideActive");
	$(".hslide").eq(slideNom).addClass("hslideActive");
	
	if(sliderNomNum < 3){
		
		sliderNomNum++;
        
	}else{
		
		sliderNomNum=0;
	}
	 nomTimerP=setTimeout("chngSlideToAuto("+sliderNomNum+")",sliderNomTimer);
}

function preventBackIssues(){
	
}

function checkScrollSlideShowWidth(){
	var HPFact=$("div[id^='slideContainer'] ul .fact").length;	 
	var HPNews=$("div[id^='newsSlideContainer'] ul .fact").length;	
	var MFFacts=$("div[id^='movingFowardSlideContainer'] ul .movingForwardFact").length;
		
	var scrollWidthMFF=MFFacts*315;
	var scrollWidthHPF=HPFact*315;
	var scrollWidthHFN=HPNews*315;
	
	if(MFFacts>0){
		$("div[id^='movingFowardSlideContainer'] ul").css({width:scrollWidthMFF+"px"})
	}
	
	if(HPFact>0){
		$("div[id^='slideContainer'] ul").css({width:scrollWidthHPF+"px"})
	}
	
	if(HPNews>0){
		$("div[id^='newsSlideContainer'] ul").css({width:scrollWidthHFN+"px"})
	}
	
}
