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('/c/media/img/blank.gif'); 
		$('#logo img, .sticker, .mod-recruit span.friend, .bnr-urgent').ifixpng();
		$('.action-progress .container, .mod-action .top, .mod-action .btm').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').addClass('sub-site').not(':last').append('|');
	$.each([$('#legals li')],function(){ $(this).not(':last').append('|'); });
	
	// Hides more panels which are made visible by 'Show More' links
	$('.more-pnl').each(function(i,elem){
		var innerMargins = 0;
		var globalHeight = parseInt($(this).outerHeight());
		$.data(this,"height",globalHeight);
		$(this).css('height',0);
	});

	// Show more links - displays 'More panels' when clicked
	$('.show-more').click(function(e){
		$(this).animate({opacity:0.001},"fast");
		e.preventDefault();
		var more_pnl = $(this).parent().next('.more-pnl');
		more_pnl.find('.show-less').css('opacity',1);
		more_pnl.animate({height:more_pnl.data("height")+'px'},1000,"easeInOutCubic");
	});
	
	// Show less links - collapses 'More panels' when clicked
	$('.show-less').click(function(e){
		e.preventDefault();
		$(this).animate({opacity:0.001},"fast");
		var more_pnl = $(this).parents('.more-pnl');
		more_pnl.animate({height:'0px'},1000,"easeInOutCubic");
		more_pnl.parent().find('.show-more').animate({opacity:1},"fast");
	});

	// 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}); });
	
	// Add ONE logo to last paragraph on content pages (when last paragraph has class 'last'
	var last_para = $('.gr-content p.last');
	var str_len = last_para.text().length;
	var last_char = '<span class="one-logo">'+last_para.text().charAt(str_len - 1)+'</span>';
	$('.gr-content .last').text(last_para.text().substring(0,str_len - 1)).append(last_char);
		
	// Add drop cap to article, when class is applied to start paragraph
	$('.drop-cap').each(function(){
		var paragraph = this;
		var node = paragraph.firstChild;
    	var text = node.nodeValue;
    	var first_letter = text.substr(0,1);
    	node.nodeValue = text.slice(1);
    	$(paragraph).wrap('<div class="first-para clearfix"></div>');
    	$('<div class="clear"></div>').insertAfter($(paragraph).parent());
    	$('<span class="first-letter">'+first_letter+'</span>').insertBefore($(this).parent());
	});
	
	// Adds large quotation marks to paragraph with class
	$('.quote-lrg').each(function(){
		var paragraph = this;
		var node = paragraph.firstChild;
    	var text = node.nodeValue;
    	var quotation = text.substr(0,1);
    	node.nodeValue = text.slice(1);
    	$('<span class="big-quote">'+quotation+'</span>').insertBefore($(paragraph));
	});
	
	// Rollovers for issues and topics
	$('#issues-ov .issue,.hot-topics .topic').hover(function(){
		$(this).addClass('hover').css('cursor','default');
	},function(){
		$(this).removeClass('hover');
	});
	
	//	Clear Defaults
	var clearDefaults = $("form input:text.clear_default");
	var clearFields = new Array();

	// Loop through the qualifying fields and store their ID and value in an array.
	clearDefaults.each(function(i){
		clearFields.push([$(this).attr("id"),$(this).val()]);
	});

	// On focusing on a qualifying field, loop through the array, find the matching array element, and set the field's value to empty.
	clearDefaults.focus(function(){
		$(this).css('color','#000');
		for(i=0;i<clearFields.length;i++){
			if($(this).attr("id") == clearFields[i][0] && $(this).val() == clearFields[i][1]){
				$(this).val("");
			}
		}
	});

	// When removing focus from a qualifying field, loop through the array, find the matching array element, and if the value of the field is empty, set it back to its default.
	clearDefaults.blur(function(){
		for(i=0;i<clearFields.length;i++){
			if($(this).attr("id") == clearFields[i][0] && $(this).val() == ""){
				$(this).val(clearFields[i][1]);
				$(this).removeAttr('style');
			}
		}
	});
	
	//	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();
		});
    });
    
    // Share bar dropdown menu for additional items
    $('.share a.share-more-dd').mouseover(function(e){
    	e.preventDefault();
    	e.stopPropagation();
    	var dropdown = $(this);
    	var menu = $(this).parent().find('.share-additional');
    	if(!menu.filter(':visible').length){
	    	dropdown.addClass('active');
	    	menu.mouseover(function(e){ e.stopPropagation(); }).show().find('li:last').css('background','none');
	    	$(document).bind("mouseover",function(){
	    		menu.hide();
	    		dropdown.removeClass('active');
	    		$(this).unbind("click");
	    	});
    	}else{
    		menu.hide();
    		dropdown.removeClass('active');
    	}
    });
    
    // Table striper - currently for top 5 tables, but could extended for anything 
    $('.mod-top-5 table, #action-status').each(function(i, elem){
    	$(this).find('tr:even').addClass('alt');
    });
		
});