var dbg = 0;
var nextIdVal = 0;
nextId = function()
{
	return ++nextIdVal;	
}
$(document).ready(function(){
		// #bodyCanvas is used to center the main block with auto margins on left/right
	//$("body").wrapInner("<div id='bodyCanvas'></div>");

	// We have two left floats: leftNav and pageContents then we need a clear after pageContents
	//$('#pageContents').after("<div class='clear'></div>");

	// find <div id='pageMain'> then wrap it with 1px white borders
	//$('#pageMain').wrap("<div id='borders'></div>");
	
	// Insert bottom border image
	$('#borders').after("<img id='bkgd_footer' src='images/bkgd_footer.gif' />");

	// Add .inner for repeat-y image
	//$('#pageMain').wrapInner("<div class='inner'></div>");
	
	// Add icon for mobile device link
	//$('#mobileDevice a').before("<img src='images/icon_mobile.gif' />&nbsp;");
	
	// Add pdf icon for pdf downloads
	$('a.pdf').not(".no-auto").before("<img src='images/icon_pdf.gif' />&nbsp;");
	
	// Add news Flash icon
	$('a.newsFlash').before("<img src='images/icon_news_wh.gif' />&nbsp;");
	
	// Add video icon
	$('div.videoCaption').before("<img src='images/icon_video.gif' />&nbsp;");
	$('a.video').before("<img src='images/icon_video.gif' />&nbsp;");
	
	$('div#searchBox').after("<div class='clear'></div>");

	// make ALL links do nothing
	//$('a').attr("href","javascript:alert('Links do not work during prototype phase')");

		// LeftNav padding
	//$('div#leftNav').wrapInner("<div class='padding'></div>");

	// mainCol padding
	//$('div#pageContents').wrapInner("<div class='padding'></div>");
	//$('div#pageContents').wrapInner("<div id='page_"+pagename+"'></div>");
	//$('div#pageContents .padding').wrapInner("<div id='print_content'></div>");

	// Modal "popups"
	$.nyroModalSettings = {
		closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="close">Close</a>'
	}
	// Left Nav Page Highlighting
	$("ul#navPageOn > li > a[name]").each(function(){
		if ($(this).attr('name') == pagename) {
			$(this).addClass("strong");
		}
	});
	
	//add dashed line between items
	$(".section ul.items > li").not(":first").addClass("topDash");
	$(".section ul.items > li").find("div.title").addClass("strong");
	$(".section ul.itemsLong > li").find("div.title").addClass("strong");
	$(".section ul.itemsLong > li").find("p.red").addClass("strong");
	$(".section ul.sectionRightSidePdf > li").find("div.title").addClass("strong");
	$(".section ul.items > li").find("p.title").addClass("strong");
	$(".section").find("p.title span").addClass("strong");
	$(".section").find("span.brown").addClass("strong");
	
	// remove the border from the first section
	$('.section:first').css("border-top", 0);

	// add an empty blank section, so the 'top' can be insterted at the bottom of all sections (or just one)
	$('.section:last').append("<div class='section'></div>");

	//find <div class='section'>then insert a div.top after its closing </div>:
	$('.section').not(":first").prepend("<div align='center'><div class='top'><a href='#top'>top</a></div></div>");
	$('.section:last').find("div.top a").css("font-size", "110%");
	
	//add offsite link indicator
	//$('a[target=_blank]:not(.pdf||.pdf1||.noIcon)').after("<img class='externalURL' src='images/icon_externalURL.gif' />");
	$('a:not(.noIcon,.pdf,.pdf1)[target=_blank]').after("<img class='externalURL' src='images/icon_externalURL.gif' />");
	//$('a[href$=.pdf]') // selects are <a> elements that possess an href attribute that ends in .pdf

	$('.photoCredit').find('.externalURL').remove();
	
	//Add classes to image floats
	$("img.addContentImageFloat")
		.wrap("<div class='sectionImageFloat'></div>")
		.wrap("<div class='alpha-shadow'></div>")
		.wrap("<div>");
	
	$("div.addContentImageCaptionFloat").find("div.caption").before("<div class='clear'></div>");
	
	//Add classes to Image/Caption floats
	$("div.addContentImageCaptionFloat").wrapInner("<div class='sectionImageFloat'></div>");
	$("div.addContentImageCaptionFloat").find("img:first")
		.wrap("<div class='alpha-shadow'></div>")
		.wrap("<div>")
	;
	
	//Grab caption from <div class='caption'>caption here</div>, and insert it into the
	//<a class='fancybox' title="caption here">
	// If the <A> tag has a title, it is used and not the caption from the associated div
	$("div.addContentImageCaptionFloat").find("a.fancybox + div.caption").each(function(){
		var a = $(this).prev();
		if ($(a).attr("title")=="") {
			$(a).attr("title", $(this).html());
		}
	});

	// If two divs are one immediately after the other, put in a clear
	$("div.addContentImageCaptionFloat").each(function(){
		if ( $(this).next().attr('class')=="addContentImageCaptionFloat") {
			$(this).after("<div class='clear'></div>");
		}
	});

	// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
	// Add Zoom Larger Image Link
	// FIXME! this can be done w/jQuery event functions
	$("div.addContentImageCaptionFloat").each(function(){
		//var prevA =$(this).children("a.fancybox:first");
		var aTag = $(this).contents().find("img").parent().parent().parent();
		if ( $(aTag).attr('class') == 'fancybox' ) {
			var id = nextId();
			$(aTag).attr('fancyClick',id);
			$(this).find("div:last")
				.after("<div class='zoomImage'><img src='images/icon_zoom.gif' />&nbsp;<a class='clickFancyBox' rel='"+id+"' href='javascript:void(0);'>VIEW LARGER IMAGE</a></div>");
		}
	});
	/**/

	//Add classes to Image/Caption floats
	$("ul.sectionRightSide li").find("img:first")
		.wrap("<div class='alpha-shadow'></div>")
		.wrap("<div>")
	;
	
	//Grab caption from <div class='caption'>caption here</div>, and insert it into the
	//<a class='fancybox' title="caption here">
	// If the <A> tag has a title, it is used and not the caption from the associated div
	$("ul.sectionRightSide").find("a.fancybox + div.caption").each(function(){
		var a = $(this).prev();
		if ($(a).attr("title")=="") {
			$(a).attr("title", $(this).html());
		}
	});

	// If two divs are one immediately after the other, put in a clear
	$("ul.sectionRightSide").each(function(){
		if ( $(this).next().attr('class')=="sectionRightSide") {
			$(this).after("<div class='clear'></div>");
		}
	});

	$("ul.sectionRightSide li").each(function(){
		//var prevA =$(this).children("a.fancybox:first");
		var aTag = $(this).contents().find("img").parent().parent().parent();
		if ( $(aTag).attr('class') == 'fancybox' ) {
			var id = nextId();
			$(aTag).attr('fancyClick',id);
			$(this).find("div:last")
				.after("<div class='zoomImage'><img src='images/icon_zoom.gif' />&nbsp;<a class='clickFancyBox' rel='"+id+"' href='javascript:void(0);'>VIEW LARGER IMAGE</a></div>");
		}
	});
	/**/
	$("a.clickFancyBox").click(function(){
		var id = "a[fancyClick="+ $(this).attr("rel") + "]";
		$(id).click();
	});

	$("ul.sectionRightSide").find("div.caption").before("<div class='clear'></div>");

	//Add FancyBox
	$("a.fancybox").colorbox();

	$("a[href^=#]").each(function(){
		$(this).attr('href', window.location.pathname+ $(this).attr('href'));
	});
	/**/
	$("#printSection").click(function(){
		print_contents();
	});

	//$("#cse-search-box input[name=q]").val("Enter your search here");
	$("#cse-search-box input[name=q]").bind("focus",function(){
		if ($(this).data('visited') != 1) {
			$(this).val("");
		}
		$(this).data('visited',1);
	});
	$("#cse-search-button").bind("click", function(){
		$("#cse-search-box").submit();
	});


	/*
	LEAVE THIS AT BOTTOM  - do not add JS code after this
	* /
	{
		$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body')
			.animate({scrollTop: targetOffset}, 1000);
			return false;
			}
		}
		});
	}
	/**/
});


