$(document).ready(function(){
    
	//save vote up click
	$('div#votes a#vote-up').click(function(){
		var myDate=new Date()

		$.getJSON("/content/vote_up/" + $(this).attr('_post_id') + '?' + myDate.getTime(), function(json){
			
			if (json.errors) {
				
				// build the error string
				err = '';
				for (i=0; i<json.errors.length; i++) {
					err += json.errors[i] + "\n";						
				}
				alert(err);
									
			} else {
				
				// if there are no errors
				// build the message string
				msg = '';
				for (i=0; i<json.success.length; i++) {
					msg += json.success[i] + "\n";						
				}
				//alert(msg);
				
				// update the vote count by 1;
				var c = $('span#vote-count').html();
				if(c == 'no recommendations') {
					//alert('c: ' + c);
					c=0;
				}
				var nc = parseInt(c) + 1;
				//alert(curr);
				//alert(newc);
				if(nc == 1) { 
					w = " user recommends";
				}
				else { 
					w = " users recommend";
				}
				$('span#vote-count').html(nc + w);
			}
		
		});
		
		return false;
	});
    
  // allow thumbnail images to be zoomed in on
	$('div.thumbnails a').click(function(){
		
		var alt_html 	= $(this).parent('div.thumbnail-container').find('div.thumbnail-caption').html();
		var alt_attr 	= $(this).find('img').attr('alt');
		var src 			= $(this).find('img').attr('_lg');
		var cre				= $(this).parent('div.thumbnail-container').find('div.thumbnail-credit').html();
		var cap 			= cre != '' ? alt_html + ' <span class="credit">Photo: ' + cre + '</span>' : alt_html;
		
		// set class="selected"
		$('div.thumbnails img').each(function(){			
			if ($(this).attr('_lg') == src) {
				$(this).addClass('selected');
			} else {
				$(this).removeClass('selected');
			}																	
		});
		
		// fade out main, fade in clicked image
		$('div.feature').fadeOut('fast', function(){
																							
			$('div.feature img.feature-image').attr('src', src);
			$('div.feature img.feature-image').attr('alt', alt_attr);		
			$('div.feature span.caption').html( cap );
			
			$(this).fadeIn('fast');
																							
		});
		
		return false;
		
	}).hover(
		function() {
			$(this).find('img').addClass('selected');
		}, 
		function() {
			$(this).find('img').removeClass('selected');
		}
	);
	
	$('a.share-click').click(function(){
		$('div.share').show();
		$('img.collapse-button').show();
		$('img.expand-button').hide();
	});
	
	$('img.expand-button').click(function(){
		$('div.share').show();
		$('img.collapse-button').show();
		$('img.expand-button').hide();
	});
        
  $('img.collapse-button').click(function(){
		$('div.share').hide();
		$('img.collapse-button').hide();
		$('img.expand-button').show();
	});
	
});
