jQuery.fn.hint = function (blurClass) {
	if (!blurClass) {
		blurClass = 'blur';
	}

	return this.each(function () {
		// get jQuery version of 'this'
		var $input = jQuery(this),

			// capture the rest of the variable to allow for reuse
			title = $input.attr('title'),
			$form = jQuery(this.form),
			$win = jQuery(window);

		function remove() {
			if ($input.val() === title && $input.hasClass(blurClass)) {
				$input.val('').removeClass(blurClass);
			}
		}

		// only apply logic if the element has the attribute
		if (title) {
			// on blur, set value to title attr if text is blank
			$input.blur(
				function () {
					if (this.value === '') {
						$input.val(title).addClass(blurClass);
					}
				}).focus(remove).blur(); // now change all inputs to title

			// clear the pre-defined text when form is submitted
			$form.submit(remove);
			$win.unload(remove); // handles Firefox's autocomplete
		}
	});
};

jQuery(document).ready(function($) {
	
	if ($("td").hasClass("cnwUnderlinedCell")) {
		var financepress = true;
        $("td").css('padding', '2px');
        $("td").css('line-height', '1.5');
        $("td").css('font-size', '11px');
        $(".entry-content").css('width', '89%');
	}
	// Initialize input hinting
	$("input[title]").hint();

	// Add CSS class to nav items with children
	/*$("#nav .sub-menu li").each(function () {
	 if ($(this).find(".sub-menu"))
	 $(this).addClass("parent");
	 });*/
	$("#nav li li > .sub-menu").parent().children("a").addClass("parent");
	$("#sub-nav li li > .sub-menu").parent().children("a").addClass("parent");

	// <sup> to ®, MC, MD
	$('span:contains("®"),a:contains("®"),h1:contains("®"),h2:contains("®"),h3:contains("®"),h4:contains("®"),h5:contains("®"),p:contains("®"),li:contains("®")').each(function() {
		e = $(this);
		html = e.html();
		e.html(html.replace('®', '<sup>®</sup>'));
	});


	if(!financepress) {
		$('h1:contains("MD"),h2:contains("MD"),h3:contains("MD"),h4:contains("MD"),h5:contains("MD"),p:contains("MD"),li:contains("MD"),a:contains("MD")').each(function() {
			e = $(this);
			html = e.html();
			e.html(html.replace('MD', '<sup>MD</sup>'));
		});
	
		$('h1:contains("MC"),h2:contains("MC"),h3:contains("MC"),h4:contains("MC"),h5:contains("MC"),p:contains("MC"),li:contains("MC"),a:contains("MC")').each(function() {
			e = $(this);
			html = e.html();
			e.html(html.replace('MC', '<sup>MC</sup>'));
		});
	}
	
    $('#nav ul li').hover(
            function() {
                $(this).find('ul:first').show();
            },
            function() {
                $(this).find('ul:first').hide();
            }
    );

	$("#nav li li ul.sub-menu").each(function(index) {
		pos = $(this).parent().parent().parent().offset();
		corr = 190*2+$(this).parent().parent().parent().width();
		myleft = pos.left + corr;

		if ($.browser.msie && $.browser.version=="6.0") {
			myleft = $(this).parent().parent().parent().offset().left + corr;
		}

		docwidth = $(document).width();
		parentshtml = $(this).parent().html();
		if (myleft > docwidth) {
			$(this).css('left', '-100%');
		}
	});
	$("#nav li ul.sub-menu").each(function(index) {
		pos = $(this).parent().offset();
		corr = 190 + $(this).parent().width();
		myleft = pos.left + corr;

		if ($.browser.msie && $.browser.version == "6.0") {
			myleft = $(this).parent().offset().left + corr;
		}

		docwidth = $(document).width();
		parentshtml = $(this).parent().html();

		if (myleft > docwidth) {
			$(this).css('left', '-160%');
		}
	});
	$(window).resize(function() {
		$("#nav li li ul.sub-menu").each(function(index) {
			pos = $(this).parent().parent().parent().offset();
			corr = 190*2+$(this).parent().parent().parent().width();
			myleft = pos.left + corr;

			if ($.browser.msie && $.browser.version == "6.0") {
				myleft = $(this).parent().parent().parent().offset().left + corr;
			}

			docwidth = $(document).width();
			parentshtml = $(this).parent().html();
			if (myleft > docwidth) {
				$(this).css('left', '-100%');
			}
		});

		$("#nav li ul.sub-menu").each(function(index) {
			pos = $(this).parent().offset();
			corr = 190 + $(this).parent().width();
			myleft = pos.left + corr;

			if ($.browser.msie && $.browser.version == "6.0") {
				myleft = $(this).parent().offset().left + corr;
			}

			docwidth = $(document).width();

			parentshtml = $(this).parent().html();
			if (myleft > docwidth) {
				$(this).css('left', '-160%');
			}
		});

	});

	// Make sure the footer is fixed to the bottom of the viewport, regardless of the height of the page
	function fixFooter() {
		if ($(document).height() > $("#wrapper-4").height() + 120) {
			$("#wrapper-4").css("height", $(document).height() - 120);
		}
	}

	/**
	 * Accordian Menu Initialize
	 * Added by Steven.
	 */
	function initMenu() {
		$("#sub-nav ul ul ul").hide();
		$("#sub-nav li li.current-menu-ancestor").children("ul").show();
		$("#sub-nav li li.current-menu-item").children("ul").show();

		$("#sub-nav li li li a").click(function() {
			var checkElement = $(this).next();
			if ((checkElement.is("ul")) && (checkElement.is(":visible"))) {
				return false;
			}
			if ((checkElement.is("ul")) && (!checkElement.is(":visible"))) {
				checkElement.slideDown("normal");
				return false;
			}
		});
		$("#sub-nav li li a").click(function() {
			var checkElement = $(this).next();
			if ((checkElement.is("ul")) && (checkElement.is(":visible"))) {
				if ($(this).attr("target") == "_nothing")
					return false; else
					return true;
			}
			if ((checkElement.is("ul")) && (!checkElement.is(":visible"))) {
				$("#sub-nav ul ul ul:visible").slideUp("normal");
				checkElement.slideDown("normal");
				return false;
			}
		});
	}

	/**
	 * Accordian Collapsed Menu Initialize
	 * Added by Steven.
	 */
	function initCollapseMenu() {
		$("#sub-nav ul ul ul").hide();
		$("#sub-nav li li.current-menu-ancestor").children("ul").show();
		$("#sub-nav li li a").click(function() {
			var checkElement = $(this).next();
			if (checkElement.is("ul")) {
				checkElement.slideToggle("normal");
				return false;
			}
		});
	}

	// Initialize and call fixFooter when resizing
	$(window).resize(function() {
		$("#wrapper-4").attr("style", "");
		fixFooter();
	});
	fixFooter();
	initMenu();
	//initCollapseMenu();

	$("a[onclick^='return false']").each(function() {
		$(this).hover(function() {
			$(this).css({'color':'#74838B', 'cursor':'default'})
		})
	})

	// Open all pdf links in new window
	jQuery('a[href$=".pdf"]').attr('target', '_blank');

	// IE 6 Fix
	if ($.browser.msie && $.browser.version == "6.0") {
		$('#nav ul li').mouseover(function () {
			$(this).find('ul:first').show();
		});
		$('#nav ul li').mouseout(function () {
			$(this).find('ul:first').hide();
		});
	}

    // Link Fix for jCarusel
    $('.carousel-skin li h3 a[href$=".pdf"]').each(function () {
        var lnk = $(this).attr('href');
        $(this).parent().parent().find('p:first').find('a.plus').attr('href', lnk);
    });

    /*-- Print CSS Fixes --*/
    // Logo fixes
    var logo = $('#header #logo > h1 > a').css('background-image').replace('url("', '').replace('")', '');
    $('#header #logo > h1').append('<img style="display: none;" src="'+logo+'" id="print-logo" />')
});


