$(document).ready(function() {
	function open_dialog()
	{
		$('#overlay').fadeIn('fast');
		$('#dialog_supercontainer').css('display', 'table');
		$('#dialog').fadeIn();
	}
	
	function zoom_photo(photo_id)
	{
		$('#album_detail').html('<div id="loader" class="loading"></div>');
		var img = new Image();
		$(img).attr('src', 'uploads/processed/'+photo_id+'.jpg').load(function() {
			$(this).hide().height(533);
			$('#loader').removeClass('loading').append(this);
			$(this).fadeIn();
		});
		$('.album_photo_inactive').css('display', 'block');
		$('#album_photo'+photo_id+'_inactive').css('display', 'none');
	}
	
	function fill_dialog(album_id)
	{
		$.getJSON('albums/detail/'+album_id,
			function(album) {
				$('#album_title_h').html(album.title);
				Cufon.refresh();
				document.title = 'CJvanderEnde.com | '+album.title;
			}
		);

		$.getJSON('photos/album/'+album_id,
			function(photos) {
				$.each(photos, function(i, photo) {
					$('#album_thumbs').append('<div id="album_photo'+photo.id+'" class="album_photo" style="width:'+photo.width+'px; height:'+photo.height+'px" title="'+photo.title+'"><img src="uploads/processed/'+photo.id+'t.jpg" width="'+photo.width+'" height="'+photo.height+'" /><div id="album_photo'+photos[i].id+'_inactive" class="album_photo_inactive">&nbsp;</div></div>');
				});
				zoom_photo(photos[0].id);

				//mouseover and click effects for album thumbnails
				$('.album_photo').css('cursor', 'pointer').bind('mouseover', function() {
					var varname = '#'+$(this).attr('id')+'_inactive';
					$(varname).css('display', 'none');
				}).bind('mouseleave', function() {
					if( $(this).attr('id').match(/[0-9]*$/) != $('#album_detail').html().match(/([0-9]*)\.jpg/)[1] )
					{
						var varname = '#'+$(this).attr('id')+'_inactive';
						$(varname).css('display', 'block');
					}
				}).bind('click', function() {
					zoom_photo($(this).attr('id').match(/[0-9]*$/));
				});
			}
		);
	}

	//mouseover and click effects for album thumbnails
	$('.album_photo').css('cursor', 'pointer').bind('mouseover', function() {
		var varname = '#'+$(this).attr('id')+'_inactive';
		$(varname).css('display', 'none');
	}).bind('mouseleave', function() {
		if( $(this).attr('id').match(/[0-9]*$/) != $('#album_detail').html().match(/([0-9]*)\.jpg/)[1] )
		{
			var varname = '#'+$(this).attr('id')+'_inactive';
			$(varname).css('display', 'block');
		}
	}).bind('click', function() {
		zoom_photo($(this).attr('id').match(/[0-9]*$/));
	});
	
	function close_dialog()
	{
		$('#dialog').fadeOut();
		$('#overlay').fadeOut();
		$('#dialog_supercontainer').css('display', 'none');
		$('#album_thumbs').html('');
		$('#album_detail').html('');
		$('#album_title_h').html('');
		document.title = 'CJvanderEnde.com | Military Aviation Photography';
	}
	
	//mouseover and click effects in timeline
	$('.timeline_photo').css('cursor', 'pointer').bind('mouseover', function() {
		$('.timeline_photo_zoom').css('display', 'none');
		var varname = '#'+$(this).attr('id')+'_zoom';
		$(varname).css('display', 'block');
	}).bind('click', function() {
		open_dialog();
		fill_dialog( $(this).attr('id').match(/[0-9]*$/) );
	});
	$('.timeline_photo').bind('mouseleave', function() {
		$('.timeline_photo_zoom').css('display', 'none');
	});
	
	//album close
	$('#album_close').bind('click', function() {close_dialog()});
	
	//mouseover and click effects in publications
	$('.publications_photo').css('cursor', 'pointer').bind('mouseover', function() {
		$('.publications_photo_view').css('display', 'none');
		var varname = '#'+$(this).attr('id')+'_view';
		$(varname).css('display', 'block');
	}).bind('click', function() {
		window.open('publications/' + $(this).attr('id').match(/[0-9]*$/));
	});
	$('.publications_photo').bind('mouseleave', function() {
		$('.publications_photo_view').css('display', 'none');
	});
	
	/*
	 * SLIDES
	 */
	if($('#slide_container').length)
	{
		$('#slide_container').prepend('<div class="slide">&nbsp;</div><div class="slide">&nbsp;</div>').css('overflow', 'hidden');
		var current_position = 1,
		    slide_width = $('.slide').width(),
		    nslides = $('.slide').length,
		    new_position = 2;
		$('.slide').css('float', 'left').wrapAll('<div id="slides_inner"></div>');
		$('#slides_inner').css('width', slide_width * nslides);
		$('#slide_container').prepend('<div id="slide_controls"><div id="move_left" class="slide_control"><span>&lt;&lt</span></div><div id="move_right" class="slide_control"><span>&gt;&gt;</span></div><br class="clearall" /></div>');
		$('.slide_control').css('height', $('#timeline').height()).bind('click', function() {
			new_position = ($(this).attr('id') == 'move_right') ? current_position + 1 : current_position - 1;
			move(new_position);
		});
		
		$('#timeline').css('width', $(document).width());
		
		function move(position, speed)
		{
			if(!speed)
				speed = 612;
			current_position = position;
			manage_controls();
			position--;
			$('#slides_inner').animate({
				'marginLeft': slide_width * (-position) - 168
			}, speed);
		}
		
		function manage_controls()
		{
		    if(current_position <= 2)
				$('#move_left').hide();
		    else
				$('#move_left').show();
			if(current_position == nslides - 1)
				$('#move_right').hide();
			else
				$('#move_right').show();
		}
		manage_controls();
		$('#timeline').css('display', 'block');
		setTimeout(function() { move(new_position, 888); }, 66);
	}
});
