// For IE hover flicker bug
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// Hello jQuery!
$j(document).ready(function(){
// Add class to body to tell the stylesheet that JavaScript is turned on
	$j("body").removeClass("jsOff").addClass("jsOn");
//	Print Button (tools nav)
	$j("#btnPageTool2 a").click(function(){
		window.print();
	});
//	Main Menu fade effect
	var menuFadeSpeed = 250;
	$j("#navPrimary li, #navTools li:not(#btnPageTool5 ul li)").hover(
		function() {
			//	If it's a special function button, don't fade the button
			if ( $j(this).attr("id") == "btnPageTool5" || $j(this).attr("id") == "btnPageTool1" ) {
				$j(this).addClass("over");
			//	If it's currently being animated, stop the animation and restart it
			} else if ( $j("a").is(":animated") ) {
				$j(this).children("a").stop().fadeTo(menuFadeSpeed, 0);
			//	Default action
			} else {
				$j(this).children("a").fadeTo(menuFadeSpeed, 0);
			}
		},
		function() {
			//	If it's a special function button, don't fade the button
			if ( $j(this).attr("id") == "btnPageTool5" || $j(this).attr("id") == "btnPageTool1" ) {
				$j(this).removeClass("over");
			//	If it's currently being animated, stop the animation and restart it
			} else if ( $j("a").is(":animated") ) {
				$j(this).children("a").stop().fadeTo(menuFadeSpeed, 1);
			//	Default action
			} else {
				$j(this).children("a").fadeTo(menuFadeSpeed, 1);
			}
		}
	);
//	Text Size module hover action
	$j("#btnPageTool1").hover(
		function() {
			$j("#textSize").css("left", "0px");
		},
		function() {
			$j("#textSize").css("left", "-9999em");
		}
	)
//	Share and Connect module hover action
	$j("#btnPageTool5").hover(
		function() {
			$j("#danburyConnect").css("left", "0px");
		},
		function() {
			$j("#danburyConnect").css("left", "-9999em");
		}
	)

//	Media Gallery
	$j("#mainImg img").wrap('<div class="shad"></div>');
	$j("#mainImg div.shad").append('<span class="shTL"></span><span class="shBR"></span><span class="shTR"></span><span class="shBL"></span>');
	$j("#thumbs a").hover(
		function(){
			$j(this).parent().addClass("over");
		},
		function(){
			$j(this).parent().removeClass("over");			
		}
	);

//	Homepage column link hover action
	$j("#pageHome .cols3 ul a").hover(
		function() {
			$j(this).parent().addClass("over");
		},
		function() {
			$j(this).parent().removeClass("over");
		}
	);
//	Drawer List
	$j("#content .drawerList li.nest").prepend('<strong class="expand">+</strong>');
	$j("#content .drawerList strong.expand").click(function(){
		$j(this).parent().toggleClass("open");
	});
//	Profile Expand control
	$j("#profile .item").append('<span class="expand">&nbsp;</span>');
	$j("#profile .expand").click(function(){
		$j(this).parent().toggleClass("profileOpen");
	});
//	Profile switcher & pagination
	if($j("#profile").length > 0) {
		$j("#profile").append(createTipsNav());
		$j("#profile .item").hide();
		$j("#profile .item:first").show().addClass("activeProfile");
		$j("#profile .numNav:first").addClass("current");
	}
//	Creates the HTML for a paginated list of items with previous and next buttons.
//	The list is created based on the number of items in the profile module.
	function createTipsNav() {
		var numTips = $j("#profile .item").length;
		var el = document.createElement("div");
		$j(el).attr("class", "pagination");
		//	create prev item link	
		var prevItem = document.createElement("a");
		$j(prevItem).attr("class", "prev").attr("href", "#").html("<span>&laquo;</span>");
		addProfileClickEvent(prevItem);
		el.appendChild(prevItem);
		//	create item numbers
		for(var i=1; i<=numTips; i++) {
			var navItem = document.createElement("a");
			//	set initial values for class, href, text
			$j(navItem).attr("class", "numNav").attr("href", "#").html(i);
			addProfileClickEvent(navItem);
			el.appendChild(navItem);
		}
		//	create next item link
		var nextItem = document.createElement("a");
		$j(nextItem).attr("class", "next").attr("href", "#").html("<span>&raquo;</span>");
		addProfileClickEvent(nextItem);
		el.appendChild(nextItem);
		return el;
	}
//	Add an onclick events for each nav item
//	call function setActive nav based on which item is clicked
	function addProfileClickEvent(el) {
		$j(el).click(function() {
			var curActive = $j(this).siblings(".current");
			var newActive;
	
			if($j(this).hasClass("prev")) {
				var previous = curActive.prev(".numNav");
				if(previous.length)
					newActive = previous;
				else
					newActive = $j("#profile .numNav:last");
			}
			else if($j(this).hasClass("next")) {
				var next = curActive.next(".numNav");
				if(next.length)
					newActive = next;
				else
					newActive = $j("#profile .numNav:first");
			}
			else {
				newActive = $j(this);
			}
			setActiveTip(newActive);
			return false;
		});
	}
//	Set the active nav item and tip
	function setActiveTip(el) {
		//	set current nav item
		el.siblings(".current").removeClass("current");
		el.addClass("current");
		//	set current tip
		var activeProfile = el.prevAll().length - 1;
		if(jQuery.browser.version == 6.0 && $j.browser.msie) {
			$j(".activeProfile").hide().removeClass("activeProfile");
			$j(".item:eq("+activeProfile+")").show();
		} else {
			$j(".activeProfile").fadeOut(10).removeClass("activeProfile");
			$j(".item:eq("+activeProfile+")").fadeIn(500);
		}
	
		$j(".item:eq("+activeProfile+")").addClass("activeProfile");
	}

//	Layers
	//	Append the overlay to the end of the page
	$j("body").append('<div id="overlay"><a class="close" href="#">close</a></div>');
//	Email a Friend
	$j("#btnPageTool3 a").click(function(){
		$j("#overlay").prepend('<div id="overlayBG"></div><div id="emailBox"></div>');
		var iframeURL = $j(this).attr("href");
		//	Show the overlay & populate with iframe
		$j("#overlay").show();
		$j("#emailBox").append('<iframe src="'+iframeURL+'" width="100%" height="100%" scrolling="no" frameborder="0"><p>Your browser does not support iframes.</p></iframe>');
		return false;
	})
//	Find a Doctor Directions Layer
	//	Append the overlay to the end of the page
	$j("body#pageFindADoctor.detail p.directions a").click(function(){
		$j("#overlay").prepend('<div id="overlayBG"></div><div id="directionsBox"></div>').addClass("directions");
		var iframeURL = $j(this).attr("href");
		//	Show the overlay & populate with iframe
		$j("#overlay").show();
		$j("#directionsBox").append('<iframe src="'+iframeURL+'" width="100%" height="100%" scrolling="no" frameborder="0"><p>Your browser does not support iframes.</p></iframe>');
		return false;
	})
	//	Close the layer and remove the iframe content
	$j("#overlay .close").click(function(){
		$j("#overlay").hide();
		$j("#overlay div").remove();
		$j("#overlay").removeClass();
		$j("#emailBox").html('');
	})

//	DropList
	$j(".dropList").hover(
		function(){
			$j(this).addClass("dlOver");
		},
		function(){
			$j(this).removeClass("dlOver");
		}
	)

//	Twitter module separator
	$j("#twitter_update_list li:last").addClass("last");

//	Zebra Tables
	$j("table:not(.plain) tr:nth-child(odd)").addClass("odd");
	$j("table tr").find("td:last").addClass("last");
}); // Bye-bye jQuery!
