/**
 * jQuery.autoshrinklist - Shrink display of listed itmes
 * @developer Angela Larson 
 * @Last Modification: 05/24/2010
 * @Notes: This plugin can be used to automatically shrink ul lists and provide a link for
 * expanding the link. The current default list of parameters are: 
 * maxLimit:  The manimum number of items to display     
 * expText: The text for the link to expand the displayed list
 * contText: The text to display to contract the displayed list
 * expClass: The class assigned to the link when displaying all items
 * contClass: The class assigned to the link when limiting the display
 * elmHideClass: The class assigned to the li element when hiding list elements
 * ***Currently this plugin only works with the ul list elements - Beware of trailing commas in definitions - 
 * trailing commas will break functionality in IE***
 * @examples:
 *	example1:
 *		<html>
 *			<head>
 *				<script type='text/javascript'>
 *					$(function() {
 *						$("#portlets ul").autoshrinklist();
 *					});
 *				</script>
 *			</head>
 *			<body>
 *				<div id='example' ></div>
 *			</body>
 *		</html>
 *
 *	example2:
 *		<html>
 *			<head>
 *				<script type='text/javascript'>
 *					$(function() {
 *						$.fn.autoshrinklist.defaults.maxLimit = 4
 *						$("#portlets ul").autoshrinklist();
 *					});
 *				</script>
 *			</head>
 *			<body>
 *				<div id='example' ></div>
 *			</body>
 *		</html>
 *
 *	example3:
 *		<html>
 *			<head>
 *				<script type='text/javascript'>
 *					$(function() {
 *						$("#portlets ul").autoshrinklist({maxLimit: 4 });
 *					});
 *				</script>
 *			</head>
 *			<body>
 *				<div id='example' ></div>
 *			</body>
 *		</html>
 */

(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');
	
	//Nav
	$('ul#main-nav li').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-solutions").autoshrinklist({maxLimit: 3, expText: 'All Related Solutions', contText: 'Less Related Solutions' });
	$("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' });


	//add left border
	$("table.standard thead th:first").addClass("lb");
	
	whereitat = window.location + "";
	splitUrl = whereitat.split("/");
	if(splitUrl[3] == "About"){
		$("li#about-pbbi").addClass("current");
	}else if(splitUrl[3] == "Communities"){
		$("li#communities").addClass("current");
	}else if(splitUrl[3] == "Partners"){
		$("li#partners").addClass("current");
	}else if(splitUrl[3] == "Products"){
		$("li#products").addClass("current");
	}else if(splitUrl[3] == "Resources"){
		$("li#resources").addClass("current");
	}else if(splitUrl[3] == "Solutions"){
		$("li#solutions").addClass("current");
	}else if(splitUrl[3] == "Support"){
		$("li#support").addClass("current");
	}else if(splitUrl[3] == "index.html"){
		$("li#home").addClass("current");	
	}
	
	$("a.flash-pop").click(function(){
		var rel = $(this).attr("rel");
		$('#showFlash').css('display', 'block').flash({ swf: '/flash/Player.swf', height: 400, width:550, 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");
	
	//bios
	
	bioArray = $("div.col1 div.bio");
	var even = 1;
	for(i=0; i<bioArray.length; i++){
		if(i == 0){
			$(bioArray[0]).addClass("firstBio");
		}
		if(even == 1){
			$(bioArray[i]).addClass("nrm");
			even = 0;
		}else{
			even = 1;
		}
	}
	
	$("div#comments div.comment:odd").addClass("odd")	
	
	//Removes bottom-border from last item in secondary navigation
	$('div#secondary-nav li:last').addClass('last');
	
	$('div#secondary-nav ul').each(function(){
		if($(this).children("li").length == 0){
			$(this).remove();
		}
	});
	
	$('a.content-edit').parent("p").addClass("logged-in");
});
