// For IE hover flicker bug
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// Hello jQuery!
$(document).ready(function(){
// Add class to body to tell the stylesheet that JavaScript is turned on
	$("body").removeClass("jsOff").addClass("jsOn");
	inputClear(".clearInput");

//	Identify the first item
	$("#facultyListing li.facultyBio:first").addClass("first");
	$("table th:first").addClass("first");

//	Identify the last item
	$("table td:last-child").addClass("last");
	$("#local .active li:last-child").addClass("last");
	$(".tweets li:last").addClass("last");
	$("table th:last").addClass("last");
	$(".shareContent li:last-child").addClass("last");

//	News & Event Mod 
    $("#newsEvent h2").click(function () {
		$("#newsEvent div").removeClass("active");
		$(this).parent("div").addClass("active");
	});
//	Share Mod 
    $("#shareConnect span").click(function () {
		$("#shareConnect").toggleClass("open");
	});

// zebra tables
	$("#content table").each(function(){
		$("tr:nth-child(odd)").addClass("odd");
	});
/*
 * Clear out the default text in a specified input when it gains focus
 * If focus is lost and field is empty replace with default text
 *
 * Takes an optional parameter which is a jQuery string selector "#search-form input", "form .focusInput", etc
 * By default it targets ALL text inputs on a page
 */
function inputClear(target) {
	var target = target || "input";
	
	$(target).each(function() {
		if($(this).attr("type") == "text") {
			var value = $(this).val();

			$(this).focus(function() {
				if($(this).val() == value) {
					$(this).val("");
				}
			});

			$(this).blur(function() {
				if($(this).val() == "") {
					$(this).val(value);
				}
			});
		}
	});
}

//	Aggregator Highlight Area
	$("#highlightNav li span").hover(
		function(){
			$(this).parent().addClass("over");
		},
		function(){
			$(this).parent().removeClass("over");
		}
	);
	var highlightDefaultImg = $("#highlight .image span").html();

	$("#highlightNav li span").click(function () {
		$("#highlight a.close").show();
		$("#highlightNav li").removeClass("active");
		$(this).parent("li").addClass("active");
		$("#highlight h1").html();

		$("#highlight h2").text( $(this).text() );
		$("#highlight .default").hide();
		$("#highlight .panel").html( $(this).next(".content").html() ).show();
		$("#highlight .image span").html( $(this).next().next(".image").html() )
   });

	$("#highlight a.close").click(function(){
		$(this).hide();
		$("#highlightNav li").removeClass("active");
		$("#highlight h1").html();
		$("#highlight h2").empty();
		$("#highlight .default").show();
		$("#highlight .panel").hide();
		$("#highlight .image span").html(highlightDefaultImg);
		return false;
	})








//	Inline Gallery
	var numImgs = $("#galleryInline a").length;
	var hasNextPage = false;

	if( $("#galleryInline").hasClass("three") ){
		$("#galleryInline li:lt(3)").addClass("vis");
		$("#galleryInline li:gt(2) img").each(function(){
			$(this).attr("path", $(this).attr("src"));
			$(this).removeAttr("src");
		});
		hasNextPage = numImgs > 3;

	} else if( $("#galleryInline").hasClass("four") ) {
		$("#galleryInline li:lt(4)").addClass("vis");
		$("#galleryInline li:gt(3) img").each(function(){
			$(this).attr("path", $(this).attr("src"));
			$(this).removeAttr("src");
		});
		hasNextPage = numImgs > 4;
	}
	$('<a class="prev inactiveP" href="#">&laquo;&nbsp;Previous</a>').insertAfter("#galleryInline h2");
	$('<a class="next" href="#">Next&nbsp;&raquo;</a>').insertAfter("#galleryInline ul");
	$("#galleryInline a.next").click(function(){
		galleryNextClick();
		return false;
	});
	$("#galleryInline a.prev").click(function(){
		galleryPrevClick();
		return false;
	});
	
	//AC:  added to fix bug with galleries with less than 1 screen of images
	if (!hasNextPage) {
		$("#galleryInline a.next").addClass("inactiveN");
	}

//	Profile
	$("#profile.multi").prepend('<a class="prev inactiveP" href="#">&laquo;&nbsp;Previous</a>').append('<a class="next" href="#">Next&nbsp;&raquo;</a>');
	$("#profile.multi .item").hide();
	$("#profile.multi .item:first").addClass("active").show();
	$("#profile.multi a.next").click(function(){
		profileNextClick();
		return false;
	});
	$("#profile.multi a.prev").click(function(){
		profilePrevClick();
		return false;
	});

//	Layers
$("body").append('<div id="overlay"><a class="close" href="#">close</a></div>');
	$("#galleryListing a:not(.out), #galleryInline ul a:not(.out)").click(function(){
		var galleryURL = $(this).attr("href");
		$("#overlay").prepend('<div id="galleryFrame"><iframe src="'+galleryURL+'" width="100%" height="100%" scrolling="no" frameborder="0"><p>Your browser does not support iframes.</p></iframe></div><div id="overlayBG"></div>');

		if ( $("body").hasClass("photo") || $(this).parent().hasClass("vis") ){
			$("#galleryFrame").addClass("photo");
			$("#overlay a.close").addClass("photoClose");

		} else if ( $("body").hasClass("video") ){
			$("#galleryFrame").addClass("video");
			$("#overlay a.close").addClass("videoClose");

		} else if ( $("body").hasClass("audio") ){
			$("#galleryFrame").addClass("audio");
			$("#overlay a.close").addClass("audioClose");
		}
		window.scrollTo(0, 0);
		$("#overlay").show();
		return false;
	});

	$("#overlay .close").click(function(){
		$("#overlay").hide();
		$("#overlay div").remove();
		$("#overlay").removeClass();
		return false;
	})

});

function galleryNextClick(){
	if( !$("#galleryInline a.next").hasClass("inactiveN") ){
		$("#galleryInline a.prev").removeClass("inactiveP");
		$("#galleryInline li.vis").addClass("purg");
		if( $("#galleryInline").hasClass("three") ){
			$("#galleryInline li.vis:last").next().addClass("vis").next().addClass("vis").next().addClass("vis");
		} else if ( $("#galleryInline").hasClass("four") ){
			$("#galleryInline li.vis:last").next().addClass("vis").next().addClass("vis").next().addClass("vis").next().addClass("vis");
		}
		$("#galleryInline li.purg").removeClass("vis").removeClass("purg");
		$("#galleryInline li.vis img").each(function(){
			$(this).attr("src", $(this).attr("path"));
		})
		if ( $("#galleryInline li.vis:last").next("li").length < 1 ){
			$("#galleryInline a.next").addClass("inactiveN");
		}
	}
}

function galleryPrevClick(){
	if( !$("#galleryInline a.prev").hasClass("inactiveP") ){
		$("#galleryInline a.next").removeClass("inactiveN");
		$("#galleryInline li.vis").addClass("purg");
		if( $("#galleryInline").hasClass("three") ){
			$("#galleryInline li.vis:first").prev().addClass("vis").prev().addClass("vis").prev().addClass("vis");
		} else if ( $("#galleryInline").hasClass("four") ){
			$("#galleryInline li.vis:first").prev().addClass("vis").prev().addClass("vis").prev().addClass("vis").prev().addClass("vis");
		}
		$("#galleryInline li.purg").removeClass("vis").removeClass("purg");
		if ( $("#galleryInline li.vis:first").prev("li").length < 1 ){
			$("#galleryInline a.prev").addClass("inactiveP");
		}
	}
}

function profileNextClick(){
	if ( !$("#profile a.next").hasClass("inactiveN") ){
		$("#profile a.prev").removeClass("inactiveP");
		$("#profile .active").removeClass("active").hide().next(".item").addClass("active").show();
		if ( $("#profile .active").next(".item").length < 1 ){
			$("#profile a.next").addClass("inactiveN");
		}
	}
}
function profilePrevClick(){
	if ( !$("#profile a.prev").hasClass("inactiveP") ){
		$("#profile a.next").removeClass("inactiveN");
		$("#profile .active").removeClass("active").hide().prev(".item").addClass("active").show();
		if ( $("#profile .active").prev(".item").length < 1 ){
			$("#profile a.prev").addClass("inactiveP");
		}
	}
}

function emailPopup(emailURL, thisPage) {
	var windowStatus = 'toolbar=0,status=0,height=436,width=646,scrollbars=yes,resizable=yes';
 	var win = window.open(emailURL + "?link=" + thisPage, 'Email A Friend', windowStatus);
}

function pageOptionPopup(site, title) {

    var url = self.location.href;
    var facebook = 'http://www.facebook.com/sharer.php?u=';
    var delicious = 'http://del.icio.us/post?v=4&partner=nyt&noui&jump=close&url=';
    var digg = 'http://digg.com/submit?phase=2&url=';
    var myspace = 'http://www.myspace.com/index.cfm?fuseaction=postto&u=';
    var twitter = 'http://twitter.com/home/?status=';
	var linkedin = 'https://www.linkedin.com/secure/login?trk=hb_signin';
	var blogger = 'https://www.blogger.com/start?pli=1';
	var stumbleupon = 'http://www.stumbleupon.com/sign_up.php?pre2=login';
	var technorati = 'http://technorati.com/account/signup/';
	
    var windowStatus = 'toolbar=0,status=0,height=436,width=646,scrollbars=yes,resizable=yes';
 

    if(site=='facebook') {
       var win = window.open(facebook + url, site, windowStatus);
    }
    else if(site=='delicious')
    {
       var win = window.open(delicious + url, site, windowStatus);
    }
    else if(site=='digg')
    {
       var win = window.open(digg + url, site, windowStatus);
    }
    else if(site=='myspace')
    {
       var win = window.open(myspace + url, site, windowStatus);
    }    
    else if(site == 'twitter')
    {
       var win = window.open(twitter + url, site, windowStatus);
    }
	else if(site == 'linkedin')
    {
       var win = window.open(linkedin + url, site, windowStatus);
    }
	else if(site == 'blogger')
    {
       var win = window.open(blogger + url, site, windowStatus);
    }
	else if(site == 'stumbleupon')
    {
       var win = window.open(stumbleupon + url, site, windowStatus);
    }
	else if(site == 'technorati')
    {
       var win = window.open(technorati + url, site, windowStatus);
    }
}

