$(document).ready(function() {

	//display exif on image
	$("div.exif").hide();
	
	$("div.post-photo").hover(
	function(){//in
		$("div.exif").wait().fadeIn();
	},
	function(){//out
		$("div.exif").fadeOut();
	});

	
	//photo stream ajax
	PS_OFFSET = 1;
	PS_NUMBER = 7;
	
	function get_ps_thumbs(type) {
		
		var old_thumbs = $("ul.photo-stream li").fadeOut();
		//some nasty loading image
		$("ul.photo-stream").addClass('loading');
		
		if(type == 'next') {
			var offset = PS_OFFSET - PS_NUMBER;
		} else if(type == 'previous') {
			var offset = PS_OFFSET + PS_NUMBER;
		} else {
			return false;
		}
		
		$.ajax({
			type: "GET",
			url: "wp-content/themes/siabarfs/functions.php",
			data: "task=ps&offset=" + offset + "&num=" + PS_NUMBER,
			timeout: "10000",
			error: function(xhr, desc) {
				old_thumbs.fadeIn();
				$("ul.photo-stream").removeClass('loading');
				//display an error
				//alert(xhr.responseText);
			},
			success: function(html){
				//insert new data
				$("ul.photo-stream").append(html);
				//remove old ones & loading image
				old_thumbs.remove();
				$("ul.photo-stream").removeClass('loading');
				//show the new data
				$("ul.photo-stream li").hide().fadeIn();
				
				//change vars for next request
				if (offset < 0) PS_OFFSET = 1;
				else PS_OFFSET = offset;
			}
		});
	}
	
	$("a#ps_previous").click(function(){get_ps_thumbs('previous')});
	$("a#ps_next").click(function(){get_ps_thumbs('next')});
	
	//show/hide comments
	function toggle_comments(){
		function displayComplete(){
				$.scrollTo("a#comments_link", 700);
			}
		$("div.comments").slideToggle('normal',displayComplete);
	}
	//comments loading
	$("div.comments").hide();
	$("a#comments_link").click(function(){
		toggle_comments();
	});
	
	//fix for front-page post
	if (jQuery.url.attr("anchor") == 'comments'){
		toggle_comments();
	}
	
});
