(function($) {
	$.fn.autoshrinklist = function(options) {
		//debug(this);

		var settings = $.extend({},$.fn.autoshrinklist.defaults,options);
		
		return this.each(function(){
								  
			var limit = settings['maxLimit'];
			var expandText = settings['expText'];
			var contractText = settings['contText'];
			var expandClass = settings['expClass'];
			var contractClass = settings['contClass'];
			var listElementHideClass = settings['elmHideClass'];
			var parentElement = settings['parListEl'];
			var childElement = settings['childListEl'];
			
			var totalLi = $(this).children(childElement);
			var childCount = totalLi.size();
			if(childCount <= limit){
				//Nothing to do
			}else{
				limit = limit-1;
				for(i=0; i<childCount; i++){
					if(i > limit){
						$(totalLi[i]).addClass(listElementHideClass);
						
						//if this is a dictionary list - get the dd sibling
						if(childElement == 'dt') {
							$(totalLi[i]).next('dd').addClass(listElementHideClass);
						}
					}
				}
				$(this).after('<a href="#" class="' + expandClass + '">' + expandText + '</a>');
			}
			
			var aLink = $(this).next('a.'+expandClass);
			aLink.each(function(){
				count = $(this).prev(parentElement).children().length;
				if(count <= (limit)){$(this).addClass(contractClass);}

				$(this).toggle(function(){
					$(this).addClass(contractClass);
					$(this).html(contractText);
					$(this).prev(parentElement).children(childElement+'.'+listElementHideClass).removeClass(listElementHideClass);

					if(childElement == 'dt') {
						$(this).prev(parentElement).children(childElement).next('dd.'+listElementHideClass).removeClass(listElementHideClass);
					}
					return false;
				},function(){
					$(this).removeClass(contractClass);
					$(this).html(expandText);
					var totalLi = $(this).prev(parentElement).children(childElement);
					var childCount = totalLi.size();
					for(i=0; i<childCount; i++){
						if(i > limit){
							$(totalLi[i]).addClass(listElementHideClass);
							
							if(childElement == 'dt') {
								$(totalLi[i]).next('dd').addClass(listElementHideClass);
							}
						}
					}
					return false;
				});
			});
		});
	};

	$.fn.autoshrinklist.defaults = {
		maxLimit: 3,    
		expText: 'All Related Items',
		contText: 'Less Related Items',
		expClass: 'all',
		contClass: 'less',
		elmHideClass: 'hide',
		parListEl: 'ul',
		childListEl: 'li'
	};
})(jQuery);

$(document).ready(function(){
	swapValues = [];
	$(".swap").each(function(i){swapValues[i] = $(this).val();
		$(this).focus(function(){if ($(this).val() == swapValues[i]) {$(this).val("");}}).blur(function(){ if ($.trim($(this).val()) == "") {$(this).val(swapValues[i]);}});
	});

	//Handle offsite links
	$('#content a').filter(function() {
		var theHref = this;
		if (theHref.hostname && theHref.hostname !== location.hostname) {
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)) {
					if(pageTracker){
						var fixedLink = this.href;
						fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
						fixedLink = '/outgoing/' + fixedLink;
						pageTracker._trackPageview(fixedLink);
					};
				};
			});
		};
	});

	//Automatic PDF file handling
	$("a[@href$='.pdf']:not(.noAutoIcon)").addClass("pdf");
	$("a[@href$='.pdf']").bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if (!code || (code && code == 13)) {
			if(pageTracker){
				var fixedLink = "/downloaded-files/pdf/" + this.pathname;
				pageTracker._trackPageview(fixedLink);
			};
		};
	});

	//Track Downloads
	$("a.download").bind('click keypress', function(event) {
		var code=event.charCode || event.keyCode;
		if (!code || (code && code == 13)) {
			if(pageTracker){
				var fixedLink = "/downloaded-files/misc/" + this.pathname;
				pageTracker._trackPageview(fixedLink);
			};
		};
	});

	//colored OL
	$('ol li').wrapInner('<span class="olcontent"></span>').addClass('olcolor');
	
	// Fixes for the worst browser still in popular use
	if($.browser.msie){
		$('ul#main-nav li,#location').hover(function(){
			$(this).addClass('active');
		},function(){
		$(this).removeClass('active');
		});
	};

	$("body#supportDownloads ul:not(:has(li))").append("<li>No downloads in this category.</li>");

	var limit = 3;
	$("ul:not(.noAutoShrink)",$("#portlets")).each(function(){
		if($(this).parent("div").attr("id") == "multimedia"){limit = 1;}
		$(this).autoshrinklist({maxLimit: limit, expText: 'All Items', contText: 'Less Items' });	
	});
	
	$("dl:not(.noAutoShrink)",$("#portlets")).each(function(){
		if($(this).parent("div").attr("id") == "multimedia"){limit = 1;}
		$(this).autoshrinklist({maxLimit: limit, expText: 'All Items', contText: 'Less Items', parListEl: 'dl', childListEl: 'dt'});	
	});

	
	
	//add left border
	$("table.standard thead th:first").addClass("lb");

	$("a.flash-pop").click(function(){
		var rel = $(this).attr("rel");
		$('#showFlash').css('display', 'block').flash({ swf: '/flash/Player.swf', height: 480, width:640, params: { wmode:'transparent'}, flashvars:{flv:rel}}); 
		return false;
	});
	
	
	$("#content a[href$='.flv']").each(function(){
		rel = $(this).attr("href");
		$(this).addClass("thickbox ").attr({"rel":rel,"href":"#TB_inline?height=420&width=550&inlineId=flashPlayer"});
		$(this).click(function(){
		$('#showFlash').css('display', 'block').flash({ swf: '/flash/Player.swf', height: 400, width:550, params: { wmode:'transparent'}, flashvars:{flv:rel}}); 
			return false;
		});
	});

	$('li.print a').click(function(){
		window.print();
		return false;
	});

	//add left border
	$("table.standard thead th:first").addClass("lb");

	//$("div#comments div.comment:odd").addClass("odd")	
	
	//Removes bottom-border from last item in secondary navigation
	$('div#secondary-nav li:last').addClass('last');
	
	
	$('a.content-edit').parent("p").addClass("logged-in");
});
