$(document).ready(function() {

	// start by setting a var whether the page is in print mode or not
	if ( $("#nav").is(':visible')) {
		printed = false;
		} else {
		printed = true;
		}


	$('#hdNews li:nth-child(1)').addClass('first');


	// Mouseover for arrow navigation buttons
	$('.arrows a').mouseover(function() {
		imgsrc = $(this).children("img").attr("src");
			
		if (imgsrc) {
			if (imgsrc.match('OFF')) {
				imgsrcNew = imgsrc.replace('OFF', 'ON');
				$(this).children("img").attr("src", imgsrcNew);
			}
		}
	});
	
	// Mouseout 
	$('.arrows a').mouseout(function() {
		$(this).children("img").attr("src", imgsrc);
	});

	if ($("#roundlist").length > 0) {
	$('#roundlist').columnize({
		columns:3
		});	
	}
	
	// highlight active subnav item
	if (location.pathname.substring(1)) {
		// get the path
		thePath = location.pathname.substring(1);
		thePath = '/' + thePath;
		$('#subnav a[href$="' + thePath + '"]').addClass('selected');
	}
	
	if (!printed) {
	$('#columns').jScrollPane({showArrows:true});
	$('#news li').jScrollPane({scrollbarWidth:6});
	}

	//for some reason this is necessary, as the dropdown news items weren't clicking right
	$('#news a').mousedown(function() {
		document.location = $(this).attr('href');
		return false;
	});
	
	//$('#columns')._scrollable();

	// handle anchor links in the content block

	$('.jScrollPaneContainer a[href^="#"]').click(function() {
		target = $(this).attr('href');
		target = target.replace('#', '');
        $('#columns')[0].scrollTo('a[name="'+target+'"]', 1000);
        return false;
   	});


	
	//apply selected state to links	
	$('body.recipients #subnav a, body.categories #subnav a').click( function() {
		var link = $(this);
		$('#subnav a.selected').removeClass('selected');
		link.addClass('selected');		
		href = link.attr('href');
		if (href == '') {		
		return false;
		}
	});
	

		
	
	// handle showing recipient thumbnails in subnav 
	$('#subnav #catRec a').mouseover(function() {
		$('.navThumb').hide();
		$(this).next().show();
	});

	$('li.recipients span a').mouseover(function() {
		$('.navThumb').hide();
		$(this).prev().show();
	});
	
	$('li.recipients div.last').mouseover(function() {
		$(this).css('zIndex','100');
	});
	$('li.recipients div.last').mouseout(function() {
		$(this).css('zIndex','50');
	});
	
	$('#subnav a, li.recipients span a').mouseout(function() {
		$('.navThumb').hide();
	});
	

	
	
	// show/hide the links underneath the recipient photos
	$('body.recipients #subnav a, body.categories #subnav a').click(function() {
		code = $(this).html();
		
		if (code == 'bio') {
			$('#speechtext').hide();
			
			$('#biotext').show();
			$('#columns').css('top',0).jScrollPane({showArrows:true});
			return false;
		}
		
		
		if (code == 'speech' && showspeech) {
			$('#biotext').hide();
			
			$('#speechtext').show();
			$('#columns').css('top',0).jScrollPane({showArrows:true});	
			return false;		
		}
		
		if (code == 'jurors') {
			if (typeof(originaltext) == 'undefined') {
			originaltext = $('#text').html();
			}
			newtext = $('#jurors').html();
			$('#text').html(newtext);
			$('#columns').css('top',0).jScrollPane({showArrows:true});	
			return false;		
		}
		
		if (code == 'abstract') {
			if (typeof(originaltext) != 'undefined') {
			newtext = originaltext;
			$('#text').html(newtext);
			}
			$('#columns').css('top',0).jScrollPane({showArrows:true});	
			return false;		
		}
		
	});
	

	
	// load large-size news photos from thumbnails
	$('.thumbs a').click(function() {
		origSrc = $(this).children("img").attr("src");
		newSrc = origSrc.replace('/tn', '');
		$('#rightRail').html('<img src="'+newSrc+'" />');
		return false;
	});
	
	
	// search field
	$('#search input').focus(function() {
		if ($(this).val() == 'SEARCH') { 
			$(this).val(''); 
			$(this).css({fontFamily:'Arial, Helvetica, sans-serif',fontSize:'11px'});
			}
	});
	
	$('#search input').blur(function() {
		if ($(this).val() == '') { 
			$(this).val('SEARCH'); 
			$(this).css({fontFamily:'"Arial Black", Helvetica, sans-serif',fontSize:'9px'});
			}
	});


	// Dropdown navigation ------------------------------------------------------------- */
		

		$('#nav ul.sf-menu').superfish({ 
			//delay: 0,                         // delay on mouseout in ms 
			animation:     {opacity:'show'},   	// fade-in and slide-down animation 
			speed:'fast'						// faster animation speed
		}); 
		
		$('#subnav.sf-menu').superfish({ 
			//delay: 0,                         // delay on mouseout in ms 
			animation:     {opacity:'show'},   	// fade-in and slide-down animation 
			speed:'fast'						// faster animation speed
		}); 

	// Carousels ---------------------------------------------------------------------- */
	
	// add check for visibility, since the print css will hide the header
	if (!printed) {
		$('#newsCarousel').jcarousel();
		$('#recipientCarousel').jcarousel({scroll:1});
		}
		

	// Event Slider ------------------------------------------------------------------- */

	// determine the width of the content
	
	sliderWidth = $('#eventSlide #inner').width();
	sliderOffset = -50;
	
	newWidth  = sliderWidth - sliderOffset;

	$("#eventSlide").animate({ 
        right: "0px"
      }, 1500 );
      sliderStatus = 'open';

	$("#slideBtn").click(function(){
		if (sliderStatus == 'open') {
			$("#eventSlide").animate({ 
	        	right: newWidth+"px"
	      	}, 1000 );
	      	sliderStatus = 'closed';
	      } else {
			$("#eventSlide").animate({ 
	        	right: "0px"
	      	}, 1500 );	  
	      	sliderStatus = 'open';    
	      }
	      return false;
	});


});
