(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 totalLi = $(this).children('li');
			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);
					}
				}
				$(this).after('<a href="#" class="' + expandClass + '">' + expandText + '</a>');
			}
			
			var aLink = $(this).next('a.'+expandClass);
			aLink.each(function(){
				count = $(this).prev('ul').children().length;
				if(count <= (limit)){$(this).addClass(contractClass);}

				$(this).toggle(function(){
					$(this).addClass(contractClass);
					$(this).html(contractText);
					$(this).prev('ul').children('li.'+listElementHideClass).removeClass(listElementHideClass);
					return false;
				},function(){
					$(this).removeClass(contractClass);
					$(this).html(expandText);
					var totalLi = $(this).prev('ul').children('li');
					var childCount = totalLi.size();
					for(i=0; i<childCount; i++){
						if(i > limit){
							$(totalLi[i]).addClass(listElementHideClass);
						}
					}
					return false;
				});
			});
		});
	};

	$.fn.autoshrinklist.defaults = {
		maxLimit: 3,    
		expText: 'All Related Items',
		contText: 'Less Related Items',
		expClass: 'all',
		contClass: 'less',
		elmHideClass: 'hide'
	};

	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>");
	
	$("ul#rel-multimedia").autoshrinklist({maxLimit: 1, expText: 'All Mulitmedia', contText: 'Less Multimedia' });
	$("ul#rel-products").autoshrinklist({maxLimit: 3, expText: 'All Related Products', contText: 'Less Related Products' });
	$("ul#rel-whitepapers").autoshrinklist({maxLimit: 3, expText: 'All Related White Papers', contText: 'Less Related White Papers' });
	$("ul#rel-casestudies").autoshrinklist({maxLimit: 3, expText: 'All Case Studies', contText: 'Less Case Studies' });
	$("ul#rel-datasheets").autoshrinklist({maxLimit: 3, expText: 'All Related Data Sheets and Brochures', contText: 'Less Related Data Sheets and Brochures' });
	$("ul#rel-articles").autoshrinklist({maxLimit: 3, expText: 'All Related Articles', contText: 'Less Related Articles' });
	$("ul#rel-archivedwebinars").autoshrinklist({maxLimit: 3, expText: 'All Related Webinars', contText: 'Less Related Webinars' });
	$("ul#rel-events").autoshrinklist({maxLimit: 3, expText: 'All Events', contText: 'Less Events' });
	$("ul#rel-moreinfo").autoshrinklist({maxLimit: 3, expText: 'All Information', contText: 'Less Information' });
	$(".genericMore").autoshrinklist({maxLimit: 3, expText: 'Show All', contText: 'Show Less', expClass: 'show', contClass: 'back'});


	//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");
});