(function($j){
/* equal col heights */
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $j(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

$j(document).ready(function(){
	/*
	 * GLOBAL
	 */

    $j('#primary-nav').droppy();//dropdown

	// $j("#primary-nav li:last-child ul").css({
	// 		'width': 125
	// });

	$j("#secondary-nav li:first-child").addClass('first');

	$j("#search").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});

	$j("#landing-cols .col a").hover(function() {
		$j(this).parents('.col').addClass('hover');
	}, function() {
		$j(this).parents('.col').removeClass('hover');
	});

/* ======= BEGIN GLOBAL cookie-based POPUP ======= */
useCookie = true;
thePopupElement = $j("#global-popup-wrapper");
if (useCookie) {
	thePopupElement.hide();
	openPopUp();
}
thePopupElement.find("a.close").click(function() {
	closePopUp();
});

function closePopUp() {
	// Cookie setter
	if (useCookie) {
		var expireDate = new Date();
		expireDate.setHours(expireDate.getHours() + 6);
		document.cookie = "hasClicked=True;expires=" + expireDate.toGMTString() + ";path=/;";
	}
	thePopupElement.hide();
}
// Cookie getter
function getCookieValue() {
	if (useCookie) {
		var cookieName = "hasClicked";
		var cookieValue = document.cookie;
		var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
		if (cookieStartsAt == -1) {
			cookieStartsAt = cookieValue.indexOf(cookieName + "=");
		}
		if (cookieStartsAt == -1) {
			cookieValue = null;
		}
		else {
			cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
			var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
			if (cookieEndsAt == -1) {
				cookieEndsAt = cookieValue.length;
			}
			cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
		}
		return cookieValue;
	}
}

// Survey Psuedo-Popup
function openPopUp() {
	var hasClicked = getCookieValue();
	if (hasClicked === null || (document.referrer.indexOf(document.location.hostname) == -1)) {
		thePopupElement.show();
	}
}
/* ======= END GLOBAL cookie-based POPUP ======= */




	//open link in new window
	$j('a[href$="2008-grants/"], a[href*="pdf"]').attr('target','_blank');

	$j(window).load(function () {

		/*make columns same height (after everything has loaded) */

		equalHeight($j("#landing-cols .col"));

		if($j("#right-col").hasClass('border')) {
			var r = $j("#right-col").height();
			var l = $j("#left-col").height();
			if (r < l)
				$j("#right-col").height(l);
			else
				$j("#left-col").height(r);
		}

	});
});

	//popups
	$j("a.popup").click(function() {
                properties = eval("properties = {" + $j(this).attr('rel') + "}");

                if(!properties.height)
                    properties.height = 570;

                if(!properties.width)
                    properties.width = 800;

                if(!properties.name)
                    properties.name = "default";

                newwindow = window.open( properties.href,properties.name,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + properties.width + ',height=' + properties.height );
		if (window.focus) {
		    newwindow.focus();
		}
		return false;
	})
})(jQuery);
