// 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("#btnPageTool1 ul li").not("#btnPageTool5 ul li").hover(
		function() {
		    var self = $j(this);
		    //	If it's a special function button, don't fade the button
		    if (self.attr("id") == "btnPageTool5" || self.attr("id") == "btnPageTool1") {
		        self.addClass("over");
		        //	If it's currently being animated, stop the animation and restart it
		    } else if ($j("a").is(":animated")) {
		        self.children("a").stop().fadeTo(menuFadeSpeed, 0);
		        //	Default action
		    } else {
		        self.children("a").fadeTo(menuFadeSpeed, 0);
		    }
		},
		function() {
		    var self = $j(this);
		    //	If it's a special function button, don't fade the button
		    if (self.attr("id") == "btnPageTool5" || self.attr("id") == "btnPageTool1") {
		        self.removeClass("over");
		        //	If it's currently being animated, stop the animation and restart it
		    } else if ($j("a").is(":animated")) {
		        self.children("a").stop().fadeTo(menuFadeSpeed, 1);
		        //	Default action
		    } else {
		        self.children("a").fadeTo(menuFadeSpeed, 1);
		    }
		}
	);
    //	Text Size module hover action
    // iPad textsize fix

    var textSize = $j("#btnPageTool1");
    textSize.bind('click', function() {
        if (textSize.hasClass("over")) {
            textSize.removeClass("over");
            $j("#textSize").css("left", "-9999em");
        }
        else {
            textSize.addClass("over");
            $j("#textSize").css("left", "0px");
        }
    });
    textSize.bind('mouseenter', function() {
        $j("#textSize").css("left", "0px");
    });
    textSize.bind('mouseleave', function() {
        $j("#textSize").css("left", "-9999em");
    });

    //	Share and Connect module hover action
    var shareConnect = $j("#btnPageTool5");
    shareConnect.bind('click', function() {
        if (shareConnect.hasClass("over")) {
            shareConnect.removeClass("over");
            $j("#danburyConnect").css("left", "-9999em");
        }
        else {
            shareConnect.addClass("over");
            $j("#danburyConnect").css("left", "0px");
        }
    });
    $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, #details-contact a.directions").click(function(e) {
        e.preventDefault();
        $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>');
    })
    //	Close the layer and remove the iframe content
    $j("#overlay .close").click(function(e) {
    e.preventDefault();
        $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");

    //Web Forms for Marketers - event date field
    var event_date_field = $j("div.event_date_field > div > input");
    var date = getQSVarByKey("date");
    event_date_field.val(date);

    //Web Forms for Marketers - form fields
    var webForm = $j("div.scfSectionContent");
    webForm.find("div").append('<div class="clear" />');
    webForm.css("visibility", "visible");

    //Web Forms for Marketers - submit button
    var btnWrapper = $j("div.scfSubmitButtonBorder");
    btnWrapper.find("input").val(" ");
    btnWrapper.append('<div class="clear" />');
    btnWrapper.css("visibility", "visible");
});          // Bye-bye jQuery!

function getQSVarByKey(key) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");

	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");

		if (pair[0] === key) {
			return pair[1];
		}
	}
}
