$(document).ready(function () {
							
	$(".news-story .extended-text").hide();
	
	var showSpeed = 800;
	var hideSpeed = 500;
	
	// OPEN A NEWS ITEM
	// ----------------------------
	
	function openNews() {
		
		var head = $(this);
		var news = $(this).parent();
		var newsID = head.attr('id').replace('h-','');
		
		// Load image paths through JSON, if there's no photo
		
		if (news.find('.photos').length == 0) {
		
			var newsID = head.attr('id').replace('h-','');
			$.getJSON("/news/images.php", {'id':newsID}, function(json){
				// check to make sure there are some images
				if (json.images.length > 0) {
					// add the holder for photos
					var photos = $('<div class="photos"></div>').prependTo(news);
					// loop through, adding the images, sliding them into view when loaded
					$.each(json.images, function(i,img){
						// add the individual photo holder
						var photo = $('<div class="photo"></div>').appendTo(photos).hide();
						// add the image
						$('<img src="'+ img.src +'" alt=""/>').appendTo(photo).load(function(){
							$(this).closest('div.photo').slideDown(showSpeed);
						});
						// add the link, if we have a url
						if (img.url) photo.find('img').wrap('<a href="'+ img.url +'"/>');
					});
				}
			});
			
		} else {
		
			// open photos if they already exist
			news.find('.photos .photo').stop().slideDown(showSpeed);
			
		}
		
		// Make the headline active
		head.addClass('active');
		
		// Set Cufon color
		var pdate = '#' + head.attr('id') + ' .publish-date';
		Cufon.set('color','#de740b').replace(pdate);
		
		// Open extended text
		head.next('.extended-text').slideDown(showSpeed);
		
		// Switch click over to plus/minus link
		head.unbind('click', openNews);
		head.find('.more').bind('click', closeNews);
		
		return false;
	}
	
	
	// CLOSE A NEWS ITEM
	// ----------------------------
	
	function closeNews() {
		
		var news = $(this).parents('.news-story');
		var closeLink = news.find('.more');
		var head = closeLink.parent();
		var photos = news.find('.photos .photo');
		
		// Make the headline inactive
		head.removeClass('active');
		
		// Set Cufon color
		var pdate = '#' + head.attr('id') + ' .publish-date';
		Cufon.set('color','#555').replace(pdate);
		
		// Close extended text and photos
		head.next('.extended-text').slideUp(hideSpeed);
		photos.slideUp(hideSpeed);
		
		// Switch click back to headline div
		closeLink.unbind('click', closeNews);
		head.bind('click', openNews);
		
		return false;
	}
	
	
	// SET INITIAL BEHAVIORS
	// ----------------------------
	
	$(".news-story .headline")
		// Rollovers for headline div
		.hover(
			function(){
				var head = $(this);
				head.addClass('hover');
				if (!head.hasClass('active')) {
					// do Cufon rollover if not active
					var pdate = '#' + head.attr('id') + ' .publish-date';
					Cufon.set('color','#000').replace(pdate);
				}
			},
			function(){
				var head = $(this);
				head.removeClass('hover');
				if (!head.hasClass('active')) {
					// do Cufon rollover if not active
					var pdate = '#' + head.attr('id') + ' .publish-date';
					Cufon.set('color','#555').replace(pdate);
				}
			}
		)
		// Set the click event
		.bind('click', openNews);
		
	// Set a different click event for headline-only lists
	$(".headlines-only .news-story .headline")
		.unbind('click', openNews)
		.bind('click', function(){
			window.location.href = $(this).find('.more').attr('href');
			return false;
		});
	
	// Add close button to extended text
	$("<a href='#Close' class='item-link close'>Close</a>")
		.appendTo(".news-story .extended-text p:last-child")
		.bind('click', closeNews);
	
});