// Triggers scrolling from right to left on carousel (used on timer to trigger auto scroll)
function scrollCarousel(){
	$('.jcarousel-next').trigger('click');
}

jQuery(function($){

	// Home page carousel
	var carousel_arr = [];
	
	// Temporary containers for form data in carousel
	var form_data = [];
	var new_item;
	
	var msie = $.browser.msie;
	
	// Initialization 
	function carouselInitCallback(carousel){
		$.each($('#carousel ul').children(),function(){
			carousel_arr.push($(this).html());
		});
		$('.jcarousel-next,.jcarousel-prev').insertAfter('#carousel-wrap');
		var auto_carousel = setInterval("scrollCarousel()", 3000);
		$('#carousel, .jcarousel-next, .jcarousel-prev').mouseover(function(){
			carousel.options.animation = 1000;
			carousel.options.easing = 'easeOutCubic';
			clearInterval(auto_carousel);
			carousel.startAuto(0);
		});
	}
	
	// Fired before carousel moved, appends new item to carousel to give illusion of infininate list
	function carouselInCallback(carousel, item, i, state, evt){
		var idx = carousel.index(i, carousel_arr.length);
		new_item = carousel.add(i,carousel_arr[idx - 1]);
		// Apply PNG Fix for IE6
		$('#carousel .desc-wrap, #carousel .lbl').ifixpng();
	}
	
	// Fired after carousel move, removes item from end of carousel (same one which was appended beforehand)
	function carouselOutCallback(carousel, item, i, state, evt){
		carousel.remove(i);
	}
	
	// Invoke home page carousel
	$('#home #carousel ul').jcarousel({
		scroll:1,
		auto: 4,
		wrap: 'circular',
		initCallback: carouselInitCallback,
		itemVisibleInCallback: {onBeforeAnimation: carouselInCallback},
		itemVisibleOutCallback: {onAfterAnimation: carouselOutCallback},
		easing:'easeInOutCubic',
		animation:1500
	});
	
	// If good ole IE, apply some PNG fixes and allow for hover state on previous and next buttons
	if(msie){
		$('#carousel .desc-wrap, #carousel .lbl, .jcarousel-prev, .jcarousel-next').ifixpng();
		$('.jcarousel-prev,.jcarousel-next').hover(function(){
			$(this).iunfixpng().css('background-position','-53px 0');
		},function(){
			$(this).css('background-position','0 0').ifixpng();
		});
	}
	
	// Basic hide/display functionality for 'Attend Event' panel - invoked by clicking the Attend an Event button
	$('#carousel .attend-event').live("click",function(e){
		e.preventDefault();
		var item = $(this).parents('.item');
		var event_pnl = $(this).parents('li').find('.pnl-event');
		if(!msie){
			item.fadeOut("medium",function(){
				event_pnl.fadeIn("medium");
			});
		}else{
			item.hide();
			event_pnl.show();
		}
	});
	
	// Close the 'Attend an Event' panel and show the default state
	$('#carousel .close').live("click",function(e){
		e.preventDefault();
		var item = $(this).parents('li').find('.item');
		var event_pnl = $(this).parents('.pnl-event');
		if(!msie){
			event_pnl.fadeOut("medium",function(){
				item.fadeIn("medium");
			});
		}else{
			event_pnl.hide();
			item.show();
		}
	});
	
});

jQuery(function($){
	
	// Add a class to the body to indicate the presence of JavaScript.
	$("body").addClass("has_js");
	
	// Fix for IE6 Flickering Effect and apply PNG Fix to global PNG components
	if($.browser.msie){
		$.ifixpng('/img/blank.gif'); 
		$('#logo img, .sticker, .mod-recruit span.friend, .bnr-urgent').ifixpng();
		$('.action-progress .container, .mod-action .top, .mod-action .btm').ifixpng();
		$('.jcarousel-next:hover,.jcarousel-prev:hover, .lupe').ifixpng();
		try{
			document.execCommand("BackgroundImageCache", false, true);
		}catch(err){}
	}

	// FF2 Opacity Flicker Bug Fix
	if ($.browser.mozilla && parseFloat($.browser.version) < 1.9 && navigator.appVersion.indexOf('Mac') !== -1) {$('body').css('opacity', 0.9999);}
	
	// Add separators to nav items
	$('#top-links li').filter(':lt(3)').addClass('sub-site').append('|').end().filter(':gt(2)').not(":last").append('&bull;');
	$.each([$('#legals li')],function(){ $(this).not(':last').append('|'); });
	
	// Removes bottom dotted line and bottom margin off last mod-list item if required.
	$('.no-btm-bdr li:last-child').each(function(){ $(this).css({background:'none', marginBottom:0}); });
	
	// Rollovers for issues and topics
	$('#issues-ov .issue,.hot-topics .topic').hover(function(){
		$(this).addClass('hover').css('cursor','default');
	},function(){
		$(this).removeClass('hover');
	});
	

	//	Image button rollovers
	$(".rollover").live("mouseover",function(){
		var newSrc = $(this).attr("src").replace("-off.","-on.");
		$(this).attr("src",newSrc);
	}).live("mouseout",function(){
		var newSrc = $(this).attr("src").replace("-on.","-off.");
		$(this).attr("src",newSrc);
	});
	
	// For site-wide carousels that run the length of the page
	$('.wide-carousel').each(function(){
		var $this = $(this);
		var carousel_wrap = $this.find('.carousel-container');
		var the_carousel = carousel_wrap.find('.carousel');
		// Invoke carousel
		the_carousel.jCarouselLite({
		 	speed: 500,
		 	scroll:2,
       		btnNext: ".next",
        	btnPrev: ".prev",
        	visible: 6,
        	circular: false,
        	btnGo: ['.carousel-reset']
    	});
    	
    	// Hide carousel if caro-collapse class is applied
	 	if($this.hasClass('caro-collapse')){
	 		the_carousel.hide();
    		carousel_wrap.removeClass('loading').hide();
    	} 
    		
    	// Force width and apply hover filter class to each item
	 	the_carousel.css({'width':'820px'}).find('h4 a').each(function(i, elem){
	 		$(elem).hover(function(){
	 			$(this).parents('li').addClass('hover').find('.filter').css('opacity',0.7);
	 		}, function(){
	 			$(this).parents('li').removeClass('hover').find('.filter').css('opacity',0);
	 		});
	 		
	 		$(elem).click(function(e){
	 			e.preventDefault();
	 			the_carousel.fadeOut("medium", function(){
	 				carousel_wrap.slideUp("medium", function(){
	 					window.location.href = $(elem).attr('href');
	 				});
	 			});
	 		});
	 		// Set filter as invisible initially
	 		carousel_wrap.find('.filter').css('opacity',0);	
	 	});
	 	
	 	// If carousel is open, and click is registered off the carousel, trigger carousel close
	 	function checkCarouselClick(e){
			if(!$(e.target).parents('.wide-carousel').length){
				$('.carousel-container:visible').prev().find('a').trigger("click");
			}
	 	}
	 	
	 	// Toggles hide/display of full width carousel
		$this.find('.learn a').click(function(e){
			e.preventDefault();
			$(this).toggleClass('active');
			if(carousel_wrap.is(':visible')){
				the_carousel.fadeOut("medium",function(){
					carousel_wrap.slideUp("medium");
					$this.find('.carousel-reset').trigger('click');
					$(document).unbind("click");
				});
			}else{
				$('.carousel-container:visible').prev().find('a').trigger("click");
				carousel_wrap.slideDown("medium",function(){
					the_carousel.fadeIn("medium");
					$(document).bind("click",checkCarouselClick);
				});
			}
		});
		// Prevent default functionality of previous/next buttons
		$this.find('.prev,.next').click(function(e){
			e.preventDefault();
		});
    });
    
});