$(document).ready(function() {
	$('.ContactCarousel').contactcarousel({ visible: 6 }).wrap('<div class="ContactCarouselWrapper">');

    //set omniture values - logged out
    if(!$('body').hasClass('AuthLoggedIn')){
		
		//carousel navigation
		$('.carousel_prev, .carousel_next').click(function(){
			var carouselSource = $(this).parent().parent().parent().parent().parent().attr('id');
			//retrieve carousel id
			switch(carouselSource){
				case 'companies_that_need_logout':
					carouselSource = 'find_bus_seek';
					break;
				case 'companies_that_have_logout':
					carouselSource = 'find_bus_provide';
					break;
				case 'companies_with_similar_interests_logout':
					carouselSource = 'find_bus_similar';
					break;
			}
		});
		
		//utility nav clicks
		$('#LoggedOutUtility li a').click(function(){
			var btnType = $(this).parent().attr('id');
			switch(btnType){
				case 'UtilityLogin':
					btnType = 'login_left';
					break;
				case 'UtilityRegister':
					btnType = 'register_left';
					break;
				case 'UtilityAbout':
					btnType = 'about_left';
					break;
				case 'UtilityNoCard':
					btnType = 'apply_left';
					break;
			}
		});
    } else { //logged in left nav tracking
		
		//utility nav clicks - logged in
		$('#Utility li a').click(function(){
			var btnType = $(this).parent().attr('id');
			switch(btnType){
				case 'UtilityNavProfile':
					btnType = 'profile_left';
					break;
				case 'UtilityNavSettings':
					btnType = 'settings_left';
					break;
				case 'UtilityNavContacts':
					btnType = 'contacts_left';
					break;
				case 'UtilityNavMessages':
					btnType = 'help_left';
					break;
			}			
			return true;
		});
    }
    
    var filterWidth = ($.browser.msie) ? 290 : 275;
	$('ul#haves_category_filter, ul#needs_category_filter, #interests_filter').customList({ width: filterWidth, scrollFactor: 150 });
	$('ul#haves_subcategory_filter, ul#needs_subcategory_filter').customList({ width: 175, scrollFactor: 150 });
	$('.customList li.customList-title span').each(function() {
		truncateListBoxItem($(this));
	});
	
	// new filters
	$('ul#have_category_filter_logout, ul#needs_category_filter_logout').customList({ width: filterWidth, scrollFactor: 150 });
	
	$('#haves_subcategory_filter a').each(function() {
		$(this).click(function() {
			$(this).filterCarousel($('#companies_that_have'));
			return false;
		});
	});
	$('#needs_subcategory_filter a').each(function() {
		$(this).click(function() {
			$(this).filterCarousel($('#companies_that_need'));
			return false;
		});
	});
	$('#haves_category_filter a').each(function() {
		$(this).click(function() {
			truncateListBoxItem($('#customList-haves_category_filter .customList-title span'));
			$(this).filterCarousel($('#companies_that_have'), '#haves_subcategory_filter');
			return false;
		});
	});
	
	$('#needs_category_filter a').each(function() {
		$(this).click(function() {
			truncateListBoxItem($('#customList-needs_category_filter .customList-title span'));
			$(this).filterCarousel($('#companies_that_need'), '#needs_subcategory_filter' );
			return false;
		});
	});
	
	$('#interests_filter a').each(function() {
		$(this).click(function() {
			truncateListBoxItem($('#customList-interests_filter .customList-title span'));
			$(this).filterCarousel($('#companies_with_similar_interests'));
			return false;
		});
	});
	
	// new ones:
	
	$('#have_category_filter_logout a').each(function() {
		$(this).click(function() {
			$(this).filterCarousel($('#companies_that_provide_logout'));
			return false;
		});
	});
	
	$('#needs_category_filter_logout a').each(function() {
		$(this).click(function() {
			$(this).filterCarousel($('#companies_that_need_logout'));
			return false;
		});
	});
	
	$.fn.injectDynamicFilterLinks('#companies_that_have', '#haves_category_filter li a, #haves_subcategory_filter li a');
	$.fn.injectDynamicFilterLinks('#companies_that_need', '#needs_category_filter li a, #needs_subcategory_filter li a');
	$.fn.injectDynamicFilterLinks('#companies_with_similar_interests', '#interests_filter li a');
	
	//new
	$.fn.injectDynamicFilterLinks('#companies_that_provide_logout', '#have_category_filter_logout li a');
	$.fn.injectDynamicFilterLinks('#companies_that_need_logout', '#needs_category_filter_logout li a');
				
	$('#card_flip').flash({ 
	    src: '/flash/rolodex.swf',
		width: 706,
		height: 250,
		wmode: 'transparent', 
		flashvars: { xmlPath: "/connectodex/?component=rolodex", imagePath: "/images/" }
	},
	{
		expressInstall: true,
		version: 9 
	});
	
	function truncateListBoxItem(item) {
		var index = item.text().lastIndexOf('(');
		var text = item.text().substring(0, index - 1);
		var numbers = item.text().substring(index);
		
		if (text.length > 32) {
			text = text.substring(0, 32) + '...';
		}
		
		item.text(text + ' ' + numbers);
	}
	
});

