$(document).ready(function() {
	addHoverClass();
	addCloseButtonToCLAMenu();
	addMenuEffects();
	truncateLinks();
	fixBreadcrumbs();
});

//------------------------------------------------------------------------
function addHoverClass() {
// Finds any non-anchor elements that have a :hover pseudoclass defined in CSS,
// and adds the "over" class to them on mouseover. This is because IE does not
// support pseudoclasses on elements other than anchors.
	
	if (document.all && document.getElementById) {
		setEvent = function(objID) {
			$(objID).hover(
				function(){$(this).addClass('over');},
				function(){$(this).removeClass('over');}
			);
		}
		setEvent('#clamenu');
	}
}


//------------------------------------------------------------------------
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = '; expires=' + date.toGMTString();
	}
	else var expires = '';
	document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,'',-1);
}



//------------------------------------------------------------------------
function addMenuEffects() {
	$('#clamenu').hover(
		function(){$('#clamenucontent').show();},
		function(){$('#clamenucontent').hide();});
}

$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {$(self).dequeue();}, time);
	});
};

function addCloseButtonToCLAMenu() {
	$('#clamenucontent').prepend('<a class="clamenuclose" href="#">X</a>');
	$('.clamenuclose').click(function() {$('#clamenucontent').hide(); $(this).blur();});
}


//------------------------------------------------------------------------


function truncateLinks() {
//	Looks for any URLs on the page that contain both cla.purdue.edu domain and an index.html reference,
// 	and removes the index from the URL. Because Collage won't, that's why.

	var links = document.getElementsByTagName('a');
	for (var a=0; a<links.length; a++) {
		href = links[a].href;
		//More Collage won't do: write the current URL. (Yes, I'm sure.) This is for the "report a problem" link.
		/* no longer needed due to upgrade to Ajax problem reporting ... right?
		if (href.indexOf('cla.purdue.edu')>-1 && href.indexOf('prevurl=/cla/')>-1) {
			href = href.substr(0, href.lastIndexOf('prevurl=/cla/') + 8) + document.location;
			links[a].href = href;
		}
		*/
		if (href.indexOf('cla.purdue.edu')>-1 && href.indexOf('/index.')>-1) {
			href = href.substr(0, href.lastIndexOf('/') + 1);
			links[a].href = href;
		}
	}
}

//----------------------------------------------------------------------

function fixBreadcrumbs() {
	if($('#breadcrumbs span a:first').text() == "_") {
		$('#breadcrumbs span a:first').remove();
		var fi = $('#breadcrumbs span').html().indexOf(' href=');
		var newstr = $('#breadcrumbs span').html().substring(fi-3);
		$('#breadcrumbs span').replaceWith(newstr);
	}
}
