// JavaScript Document

$(document).ready(function(){
	// fix the directory heading issue
	/*$('h1').each(function(){
		if($(this).text()=="Directory: true")
		{
			alert("here");
			$(this).text("Directory");
		}
	});*/
	// end of the fix for the directory issue
	
	// set an equal height for carousel headings
	var carHeadHeight = $("div.carouselBoxContent h2").equalHeights().height();
	/*$('li.carouselBoxWrapper').css("height", height+4);*/
	
	if($('#bodyCopyLeft').length == 0){
		$("#bodyCopyRight").html($("#bodyCopyRight").html() + $("div.directoryContainer").html());
		$("div.directoryContainer").remove();
	}
	
	// control the panels
	hideAllPanels();
	showFirstPanel("center_panels");
	if ($("#non_Accordion1").length == 0){
		$("#non_Accordion1")
		showFirstAccordionPanel(0);
	}
	// set open class on initial tab
	$("div.AccordionPanel:first-child").children(".AccordionPanelTab").addClass("AccordionPanelOpen");
	
	
	/*Right column collapsible pannels */
	//first assign the first tab an active class
	//$(".CollapsiblePanel:first-child").addClass("activeTab");
	$(".CollapsiblePanelTab").click(function(){
		/* test for more than one pannel before allowing collapsible functionality */
		// check to see if the panel clicked on is opened or closed
		if($("div.CollapsiblePanel").length >0)
		{
			//alert($("div.activeTab .CollapsiblePanelContent").html());
			
			
			
			if($(this).parent().hasClass("activeTab"))
			{
				$(this).parent().removeClass("activeTab");
				$(this).siblings(".CollapsiblePanelContent").slideToggle();
			}
			else
			{
				$(this).siblings(".CollapsiblePanelContent").slideToggle();
				$("div.activeTab .CollapsiblePanelContent").slideToggle();
				$("div.activeTab").removeClass("activeTab");
				$(this).parent().addClass("activeTab");
			}	
			
		}
		else
		{
			$(this).siblings(".CollapsiblePanelContent").slideToggle();
										 
		}
	});
	
	// Activate any links that have the hideEmail class to be links
	$('a.hideEmail').click(function(){
		document.location.href="mai"+"lto:"+ $(this).attr("href") + "@" + "post03.curry.edu";
		return false;
	});
	$('h2.hideEmail').css("display","none");

	//if one of the expandable areas is expanded, plus a minus sign in the tab		
	//addSignToPanel();


	//when an expandable tab has been clicked on, change the plus sign to a minus sign
	$("div.CollapsiblePanel div.CollapsiblePanelTab").click(function(){
		//alert($(this).parent().parent().children("div.CollapsiblePanel").children("div.CollapsiblePanelTab").children("span").length);
		if($(this).parent().children('div.CollapsiblePanelContent').height() < 10)
		{
			//$(this).css("background","url(images/interior/global/minus.jpg) left center no-repeat");
			$(this).parent().parent().children("div.CollapsiblePanel").children("div.CollapsiblePanelTab").children("span").html("+");
			$(this).children("span").html("&ndash;");
		}
		else
		{
			//$(this).css("background","url(images/interior/global/plus.jpg) left center no-repeat");	
			$(this).parent().parent().children("div.CollapsiblePanel").children("div.CollapsiblePanelTab").children("span").html("&ndash;");
			$(this).parent().parent().children("div.CollapsiblePanel").children("div.CollapsiblePanelTab").children("span").html("+");
			//$(this).children("span").html("+");
		}
	});
	
	//initialize carousel options
	function mycarousel_initCallback(carousel)
	{
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
		jQuery('a.carouselNext').bind('click', function() {
			carousel.next();
			carousel.stopAuto();
			return false;
		});
	 
		jQuery('a.carouselPrev').bind('click', function() {
			carousel.prev();
			carousel.stopAuto();
			return false;
		});
	};
	
		
	if ($("#non_Accordion1").length == 0){
		$(".AccordionPanelTab").click(function(){
			if($(this).hasClass("AccordionPanelOpen"))
				return false;
			$(".AccordionPanelTab").siblings(".AccordionPanelContent").slideUp();
			$(this).siblings(".AccordionPanelContent").slideDown();
			
			$(".AccordionPanelTab").removeClass("AccordionPanelOpen");
			$(this).addClass("AccordionPanelOpen");
		});
	}


	if ( $('#mainImage.mainImageCycle').is(':visible'))
	{
		$('#mainImage.mainImageCycle').cycle({
			fx:     'scrollHorz',
			speed:   1000,
			//timeout: 0,
			timeout: 15000, 
			next:   'a.mainNext',
			prev:   'a.mainPrev',
			random: 0,
			before: homeMainBefore,
			after: homeMainAfter,
			startingSlide: Math.floor(Math.random()*$('#mainImage.mainImageCycle a img').length)
		});
	}
	
	if($('#randomFactsWrapper').is(':visible') && $('#randomFacts li').length > 1)
	{
		$('#randomFactsWrapper').jCarouselLite({
			speed: 		800,
			auto:   	8000,
			btnPrev:	"a.factPrev",
			btnNext:	"a.factNext",
			visible:    1 
			//afterEnd:   
		});
	}
	else
	{
		$('#randomFactsWrapper').jCarouselLite({
			speed: 		800,
			//auto:   	8000,
			btnPrev:	"a.factPrev",
			btnNext:	"a.factNext",
			visible:    1 
			//afterEnd:   
		});
	}
	
	if ( $('#mainImage.eventsImage').is(':visible'))
	{
		$('#mainImage.eventsImage').cycle({
			fx:     'scrollHorz',
			speed:   800,
			//timeout: 0,
			timeout: 8000, 
			next:   'a.eventsNext',
			prev:   'a.eventsPrev',
			random: 1
			//after: mainImage
		});
	}
	
	if ( $('li.carouselBoxWrapper').is(':visible'))
	{
		var height = $("li.carouselBoxWrapper").equalHeights().height();
		$('li.carouselBoxWrapper').css("height", height+20);
	}
	
	if ( $('#carouselContainer').is(':visible'))
	{	
		// detect if the browser is Safari
		// first see if webkit or safari is true, but chrome is built on webkit as well
		// the section after the && says that it has to be webkit and not chrome
		if(($.browser.webkit || $.browser.safari) &&  navigator.userAgent.toLowerCase().indexOf('chrome') <= -1){
			// run this if as long as there are enough elements to rotate
			if($("#carouselContainer ul li").length > 4){
				$('#mycarousel').jcarousel({
					auto: 15,
					scroll:1,
					animation:800,
					wrap: 'both',
					visible:4,
					buttonNextHTML: null,
					buttonPrevHTML: null,
					initCallback: mycarousel_initCallback,
					itemVisibleInCallback: {
						onAfterAnimation:  newsCarouselAfter
					}
				});
			}
			// run this else if there are not enough elements to rotate
			else
			{
				$('#carouselContainer').jCarouselLite({
					speed: 		800,
					//auto: 		7500,
					//auto:   	5000,
					pause: false,
					btnPrev:	"a.carouselPrev",
					btnNext:	"a.carouselNext",
					visible:    4 
					//afterEnd:   
				});
			}
		}
		// this else corresponds to the Safari detection.  This will run if the user IS NOT on safari
		else
		{
			// run this if as long as there are enough elements to rotate
			if($("#carouselContainer ul li").length > 4){
				$('#mycarousel').jcarousel({
					auto: 15,
					scroll:1,
					animation:800,
					wrap: 'circular',
					visible:4,
					buttonNextHTML: null,
					buttonPrevHTML: null,
					initCallback: mycarousel_initCallback,
					itemVisibleInCallback: {
						onAfterAnimation:  newsCarouselAfter
					}
				});
			}
			// run this else if there are not enough items to rotate
			else
			{
				$('#carouselContainer').jCarouselLite({
					speed: 		800,
					//auto: 		7500,
					//auto:   	5000,
					pause: false,
					btnPrev:	"a.carouselPrev",
					btnNext:	"a.carouselNext",
					visible:    4 
					//afterEnd:   
				});
			}
		}
		// reset the heights of each box to fix the issue with jquery carousel and multiple heights
		$("div.carouselBoxContent h2").equalHeights().height();
	}
	
	

	$("a.loadFancybox").click(function(){
		tempVideo = $(this).parent().parent().children("div.hiddenVideo").html();
		$("div#hiddenVideo").html(tempVideo);
		$("div#hiddenVideo object param").attr("value", $("div#hiddenVideo object param").attr("value") + "&autoplay=1");
		$("div#hiddenVideo object embed").attr("src", $("div#hiddenVideo object embed").attr("src") + "&autoplay=1");
		$("div#hiddenVideo iframe").attr("src", $("div#hiddenVideo iframe").attr("src") + "?autoplay=1");
		return false;
		
	});

	$("a.loadFancybox").fancybox({
		onClosed	:	function() {
		$("#hiddenVideo").html("");
		}
	});
	
	
	
	
/*	$('.newsCarousel').cycle({
		fx: 		'scrollHorz',
		speed: 		800,
		timeout: 	0,
		next:		'a.newsNext',
		prev:		'a.newsPrev', 
		random: 	0,
		after: 	onAfter	
	});*/
	

	
	function newsCarouselAfter(){
		$('.newsCarousel').each(function() {
			var newsNext = $('a.newsNext', this.parentNode);
			var newsPrev = $('a.newsPrev', this.parentNode);
			$(this).cycle({
				fx:     'scrollHorz',
				speed:  'slow',
				timeout: 0,
				next:    newsNext,
				prev: 	 newsPrev,
				after: 	 onAfter
			});
		});
	}
	newsCarouselAfter();

	
	
	$("ul#audienceNav li").hover(function () {
		$('p.audienceText, p.audience0, p.audience1, p.audience2, p.audience3, p.audience4').css("display","none");
		$('p.audience' + $(this).index()).css("display", "block");
	},
	function() {
		$('p.audienceText, p.audience0, p.audience1, p.audience2, p.audience3, p.audience4').css("display","none");
		$('p.audienceText').css("display","block");
	
	});
	
	/**************************************/
	/*         Interior Pages             */
	/**************************************/
		
	$('ul.expandibleContent li.profile a.profileLink').click(function() {
		if($(this).parent().children('ul').is(":visible"))
		{
			$(this).parent().children('ul').css("display","none");
			$(this).parent().css("background-image","url(images/interior/global/plus.jpg)");
			$('ul.expandibleContent li ul li').css("background-image","none");
		}
		else
		{
			$(this).parent().children('ul').css("display","block");
			$(this).parent().css("background-image","url(images/interior/global/minus.jpg)");
			$('ul.expandibleContent li ul li').css("background-image","none");
		}
		return false;
	});
	
	
	
	/*Figure out the height issues on the left*/
	if( ($("#rightCol").height()-$("#leftCol").height()) > 0)
	{
		var totalLeftColHeight = 51 + $("ul#leftNav").height() + $("div.leftColBtm").height() + $("ul#infoFor").height()+ $("div.leftColBtm").height() + $("div#leftColContentWrapper").height();
		
		$("div#leftColContentWrapper").css("height", ( $("div#leftColContentWrapper").height()+ ($("#rightCol").height() - $("#leftCol").height())));
	}
	if($("#leftColContent").html()== '<div style="height:200px;">&nbsp;</div>')
	{
		$("#leftColContentWrapper").css("background-image","none").css("background-color", "transparent");
	}
	
	
	/**************************************/
	/*         IE 6 hacks                 */
	/**************************************/	
	
	if($.browser.msie && $.browser.version=="6.0") 
	{
		$("ul#globalNav li, #leftNav li ul li, ul#infoFor li").hover(
		  function () {
			$(this).addClass("hover");
		  }, 
		  function () {
			$(this).removeClass("hover");
		  }
		);
	}
	
	// sort a table
	$("table").tablesorter({sortList: [[0,0], [1,1], [2,1], [3,1]]}); 
	// *************************************************************
	// now do some crazy junk to the table to move the x's properly
	// *************************************************************
	$('table tr').each(function(){
		// If the title of the first fields of two rows are the same
		
		if($(this).children("td:first-child").html() == $(this).next().children("td:first-child").html()){
			// If the next matching node is a minor
			if($(this).next().children("td:nth-child(3)").html() != "" && $(this).next().children("td:nth-child(3)").html() != " " && $(this).next().children("td:nth-child(3)").html() != null && $(this).next().children("td:nth-child(3)").html() != "&nbsp;"){
				$(this).children("td:nth-child(3)").html($(this).next().children("td:nth-child(3)").html());
				$(this).next().remove();																																			  
			}
			// If the next matching node is a concentration
			if($(this).children("td:first-child").html() == $(this).next().children("td:first-child").html() && $(this).next().children("td:nth-child(4)").html() != "" && $(this).next().children("td:nth-child(4)").html() != " " && $(this).next().children("td:nth-child(4)").html() != null && $(this).next().children("td:nth-child(4)").html() != "&nbsp;"){
				$(this).children("td:nth-child(4)").html($(this).next().children("td:nth-child(4)").html());
				$(this).next().remove();
			}
			
		}
	});
	$('table tr').each(function(){
	// seet colum one's href to the major's href if there is a major checkmark
		if($(this).children("td:nth-child(2)").children("a").attr("href") != "" && $(this).children("td:nth-child(2)").children("a").attr("href") != null)
		{
			$(this).children("td:first-child").children("a").attr("href", $(this).children("td:nth-child(2)").children("a").attr("href"));
		}
		// seet colum one's href to the minors's href if there is a minor checkmark and no major
		else if($(this).children("td:nth-child(3)").children("a").attr("href") != "" && $(this).children("td:nth-child(3)").children("a").attr("href") != null)
		{
			$(this).children("td:first-child").children("a").attr("href", $(this).children("td:nth-child(3)").children("a").attr("href"));
		}
		// seet colum one's href to the concentrations's href if there is a concentrations checkmark and no major and no minor
		else if($(this).children("td:nth-child(4)").children("a").attr("href") != "" && $(this).children("td:nth-child(4)").children("a").attr("href") != null)
		{
			$(this).children("td:first-child").children("a").attr("href", $(this).children("td:nth-child(4)").children("a").attr("href"));
		}
	});

	// code to parse url variables
	var urlsearch = function(){
	var p = window.location.search.substr(1).split(/\&/), l = p.length, kv, r = {};
	while(l--){
	kv = p[l].split(/\=/);
	r[kv[0]] = kv[1] || true; //if no =value just set it as true
	}
	return r;
	}();
	if(urlsearch.selDept != "" && urlsearch.selDept != " " && urlsearch.selDept != null && urlsearch.selDept != "undefined" && urlsearch.selDept != "Undefined")
	{
		if(urlsearch.selDept != true && urlsearch.selDept != "true")
		{
			$("form h1").append(": " + urlsearch.selDept);
		}
	}
	var tempText = $("form h1").text();
	//alert("temp: " + tempText);
	tempText = tempText.replace("+", " ");
	var intIndexOfMatch = tempText.indexOf("+");
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		tempText = tempText.replace( "+", " " )
		// Get the index of any next matching substring.
		intIndexOfMatch = tempText.indexOf("+");
	}
	// remove the ( symbols
	intIndexOfMatch = tempText.indexOf("%28");
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		tempText = tempText.replace( "%28", "(" )
		// Get the index of any next matching substring.
		intIndexOfMatch = tempText.indexOf("%28");
	}
	// remove the ( symbols
	intIndexOfMatch = tempText.indexOf("%29");
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		tempText = tempText.replace( "%29", ")" )
		// Get the index of any next matching substring.
		intIndexOfMatch = tempText.indexOf("%29");
	}
	// remove the ( symbols
	intIndexOfMatch = tempText.indexOf("%2F");
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		tempText = tempText.replace( "%2F", "/" )
		// Get the index of any next matching substring.
		intIndexOfMatch = tempText.indexOf("%2F");
	}
	// remove the ( symbols
	intIndexOfMatch = tempText.indexOf("%26");
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		tempText = tempText.replace( "%26", "&" )
		// Get the index of any next matching substring.
		intIndexOfMatch = tempText.indexOf("%26");
	}
	// remove the ( symbols
	intIndexOfMatch = tempText.indexOf("%27");
	// Loop over the string value replacing out each matching
	// substring.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		tempText = tempText.replace( "%27", "'" )
		// Get the index of any next matching substring.
		intIndexOfMatch = tempText.indexOf("%27");
	}
	
	$("form h1").text(tempText);

});


function homeMainBefore(curr, next, opts)
{
	//alert("before: " + $('#mainImageText p').html());
	//if($('#mainImageText p').html() == "" || $('#mainImageText p').html() == null || $('#mainImageText p').html() == "undefined" || $('#mainImageText p').html() == "Undefined")
	//{
		$('#mainImageText p').css("display","none");
	//}
}


function homeMainAfter(curr, next, opts)
{	
	var index = opts.currSlide;
	//alert("after " + index +": " + $('#mainImageText p'+index).html());
	$('#mainImageText p').css("display","none");
	if($('p.mainImageText' + index).html() !="" && $('p.mainImageText' + index).html() != null && $('p.mainImageText' + index).html() != "undefined" && $('p.mainImageText' + index).html() != "Undefined")
	{
		$('p.mainImageText' + index).css("position", "absolute").css("top","50px").css("left","0px").css("color", "white").css("display","block");
	}
}


/* This displays the count for the News Carousel */
function onAfter(curr, next, opts) 
{
	var index = opts.currSlide;
	$(this).parent().siblings(".carouselBoxContent").children("p.carouselCountText").html(index+1 + " of " + opts.slideCount);
	$(this).parent().siblings(".carouselBoxContent").children(".carouselText").children("p").css("display","none");
	$(this).parent().siblings(".carouselBoxContent").children(".carouselText").children("p.carouselText" + index).css("display","block");
	var newHeight = $("li.carouselBoxWrapper").equalHeights().height();
	$('li.carouselBoxWrapper').css("height", newHeight);
	
}

function showAllPanels(){
	$(".CollapsiblePanelContent").show();
	//alert("show all");
	addSignToPanel();
}

function hideAllPanels(){
	$(".CollapsiblePanelContent").hide();
	//alert("hide all");
	//addSignToPanel();
}


function hideAllAccordions(){
	$(".AccordionPanelContent").each(function(index){
		//$(this).slideUp();
	});
}
/*
function showAllAccordions(){
	$(".AccordionPanelContent").each(function(index){
		$(this).show();
	});
}*/


function addSignToPanel()
{
	/*$('div.CollapsiblePanel.noColor').each(function(index) {
		if($(this).children('div.CollapsiblePanelContent').is(':visible'))
			$(this).children('div.CollapsiblePanelTab').css("background","url(images/interior/global/minus.jpg) left center no-repeat");
		else
			$(this).children('div.CollapsiblePanelTab').css("background","url(images/interior/global/plus.jpg) left center no-repeat");
   	});*/
	$('div.CollapsiblePanel').each(function(index) {
		if($(this).children('div.CollapsiblePanelContent').is(':visible') && $(this).children("div.CollapsiblePanelContent").children("ul").children("li").length>0)
		{
			//$(this).children('div.CollapsiblePanelTab').css("background","url(images/interior/global/minus.jpg) left center no-repeat");
			
			$(this).children('div.CollapsiblePanelTab').html("<span class='sign'>&ndash;</span>" + $(this).children('div.CollapsiblePanelTab').html());
		}
		else if($(this).children("div.CollapsiblePanelContent").children().length>0)
		{
			//$(this).children('div.CollapsiblePanelTab').css("background","url(images/interior/global/plus.jpg) left center no-repeat");
			$(this).children('div.CollapsiblePanelTab').html("<span class='sign'>+</span>" + $(this).children('div.CollapsiblePanelTab').html());
		}
   	});
	
	/*
	});*/
	
}


function showFirstAccordionPanel(selectorId){
	if(selectorId == null || selectorId == ""){
		$(".AccordionPanelContent").each(function(index){
			if(index == 0){
				$(this).show();										
			}else{
				$(this).hide();
			}
		})
	}else{
		$("#" + selectorId + " .AccordionPanelContent").each(function(index){
			if(index == 0){
				$(this).show();										
			}else{
				$(this).hide();
			}
		})
	}
}


function showFirstPanel(selectorId){
	
	if(selectorId == null || selectorId == ""){
		$(".CollapsiblePanelContent").each(function(index){
			if($(this).parent().children(".CollapsiblePanelTab").text() == $("div#bodyCopy h1:first-child").text())
			{
				$(this).show();
				$(this).parent().addClass("activeTab");
			}
			else
			{
				$(this).hide();
			}
		});
		if($(".activeTab").length != 1){
			$(".CollapsiblePanel").first().addClass("activeTab");
			$(".CollapsiblePanel").children(".CollapsiblePanelContent:first-child").show();
		}
	}else{
		
		$("#" + selectorId + " .CollapsiblePanelContent").each(function(index){
			//alert($(this).parent().children(".CollapsiblePanelTab").text() +"     "+ $("div#bodyCopy h1:first-child").text());
			if($(this).parent().children(".CollapsiblePanelTab").text() == $("div#bodyCopy h1:first-child").text())
			{
				$(this).show();
				$(this).parent().addClass("activeTab");
			}
			else
			{
				$(this).hide();
			}
		});
		
		if($("div.activeTab").length != 1){
			$(".CollapsiblePanel").first().addClass("activeTab");
			$(".CollapsiblePanel:first-child").children(".CollapsiblePanelContent").show();
		}
	}
	addSignToPanel();
}//this is the end of the document.ready function

function recordOutboundLink(link, category, action, label) {
    //_gat._getTrackerByName()._trackEvent(category, action, label);
    _gaq.push(['_trackEvent', category, action, label]);
	setTimeout('document.location = "' + link.href + '"', 100);
  }



