var nav = null;

$(document).ready(function() {
	
	// IE6,7 fix
	if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 8 && parseInt(jQuery.browser.version, 10) > 4) {
		$('div#site.nav h2 a').animate({
			opacity: 0.6,
			backgroundColor: '#f6f6f6'
		}, 1);
	}
	
	var photos_loaded = false;
		
	// accordion tabs for nav
	nav = $('div#site.nav').tabs('div#site.nav div.section', {
		tabs: 'h2 a',
		effect: 'slide',
		current: 'current',
		api: true,
		initialIndex: -1,
		onClick: function(event, index) {

			if (index == 1 && !photos_loaded) {
				
				$('div.section.photography').animate({
					height: $('#loader').height().toString()
				}, 'slow');
				
				var thumbs = $('div.photography ul#thumbs');
				thumbs.css({visibility:'hidden',overflow:'hidden',height:0});
				
				var thumbcount = 0;
				var thumbtotal = 0; 
				
				// fetch photos from flickr
				$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=98dc657924dca6197d3acb7715dcaea1&user_id=39697594%40N00&tags=bestof&format=json&jsoncallback=?",
				  function(data){
						thumbtotal = data.photos.photo.length;
	          $.each(data.photos.photo, function(i,item){
	            thumbs.append('<li><a href="http://www.flickr.com/photos/' + item.owner + '/' + item.id + '"><img src="http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_t.jpg" /></a></li>');
	          });
						
						// opacity fade in on hover over photo thumbs
						$('ul#thumbs li a img').load(function() {
							thumbcount++;
						}).bind('mouseenter', function(e) {
							$(this).stop().animate({
								opacity: 1.0
							});
						}).bind('mouseleave', function(e) {
							$(this).stop().animate({
								opacity: 0.7
							});
						});
						
						// wait until all images are loaded before firing masonry
						var i = setInterval(function () {
							if (thumbcount < thumbtotal)
								return;
							clearInterval(i);
							thumbs.masonry();			
							thumbs.css({visibility:'visible',overflow:'auto',height:'auto'});	// give dimensions to calc height
							var h = thumbs.height() + 20;
							thumbs.css({visibility:'hidden',overflow:'hidden',height:0}); // hide for animation
							$('#loader, div.section.photography').animate({ 
								height: h.toString() + "px"	
							}, 'slow', function() {
								thumbs.css({visibility:'visible',overflow:'auto',height:'auto'}); // show again
								$('#loader').fadeOut('slow');
								$('div.section.photography').css('height', 'auto');
							});							
							
							$(window).resize(function() {
								$('div.photography ul#thumbs').masonry();
							});
							
						}, 10);
						
	        }
				);
				
				photos_loaded = true;
				
			}
			
		}	
	});
	
	$('div#site.nav').history();

	$('div#site.nav h2 a').bind('mouseenter', function(e) {
		$(this).stop().animate({
			paddingLeft: '0.5em',
			opacity: 1
		}, 250, 'easeOutQuad');
	}).bind('mouseleave', function(e) {
		$(this).stop().animate({
		 	paddingLeft: '0',
			opacity: 0.6
		}, 250, 'easeOutQuad');
	});	
	
});
