var ARTLUNG = {
	'cache' : {},
	'saveCache': function(cache_attribute_name, html) {
		this.cache[cache_attribute_name] = html;
	},
	'haveCache': function(cache_attribute_name) {
		return (typeof(this.cache[cache_attribute_name]) !== 'undefined');
	},
	'getCache': function(cache_attribute_name) {
		return this.cache[cache_attribute_name];
	},
	'loadFromHash': function(){
		var slug = window.location.hash.replace(/#/, '');
		var id = '#id-' + slug;
		if ($(id).length > 0 ) {
			$(id).trigger('click');
			return true;
		}
		return false;
	}
};
$(document).ready(function() {

	$('.nav-previous a, .nav-next a, #all-headers a:not(.selected)').hover(function(){
		$(this).children('img').stop().animate({opacity:1.0}, 'fast');
	}, function(){
		$(this).children('img').stop().animate({opacity:0.5}, 'fast');
	});
	
	$('#all-headers').delegate( 'a.h', 'click', function(){
		var numbers = $(this).attr('href').split('-')[1];
		var filename = '/headers/header-' + numbers + '.php';
		location.hash = '#' + numbers;
		$('.selected').removeClass('selected');
		$(this).addClass('selected');
		if (ARTLUNG.haveCache(numbers)) {
			$('#loader').html(ARTLUNG.getCache(numbers));
		} else {
			$('#loader').addClass('loading').html('').load( filename, function(responseText) {
				ARTLUNG.saveCache(numbers, responseText);
				// if (typeof(pageTracker) !== 'undefined' && ARTLUNG.google_analytics_on === true) {
				// 	pageTracker._trackPageview(load_url);
				// }
				$('#loader').removeClass('loading');
			});
		}
		return false;
	});
	var hoverInterval;
	
	function goUp() {
		$("#encloser").animate({marginTop: '-=1000'}, 10000, function(){
		});
	}
	function goDown() {
		$("#encloser").animate({marginTop: '+=1000'}, 10000, function(){
		});
	}
	
	$('a#up').hover(
	        function() {
	            // call doStuff every 100 milliseconds
	            hoverInterval = setInterval(goUp, 100);
	        },
	        function() {
	            // stop calling doStuff
	            clearInterval(hoverInterval);
				$('#encloser').stop();
	        }
	    ).hide();
		$('a#down').hover(
		        function() {
		            // call doStuff every 100 milliseconds
		            hoverInterval = setInterval(goDown, 100);
		        },
		        function() {
		            // stop calling doStuff
		            clearInterval(hoverInterval);
					$('#encloser').stop();
		        }
		    ).hide();
	
	
	/*
	$('a#down').bind('mouseover', function(){
		$("#encloser").animate({marginTop: '+=200'}, 1000, function(){
		});
	}).bind('mouseout', function(){
		$('#encloser').stop();
	});
	*/
	
	$(window).bind( 'hashchange', function(){
		ARTLUNG.loadFromHash();
	});
	if (!ARTLUNG.loadFromHash()) {
		$('#all-headers a:first').trigger('click');
	}
	
	
	

});

