$(function(){

	ajaxlinks();
	magicscroll();
	
	/*
	$('#container img').lazyload({
		threshold: 100,
		container: $('#content')
	});
	*/
	
  $.trackPage('UA-11811581-2');
	$('a').track({label:function($e){return $e.attr('title')}});

});

var ajaxlinks = function(){
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload, "");
		
		// set onlick event for buttons
		$('.ajax a').click(function(){
			var hash = $(this).attr('href');
			hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
			
			/*
			$('#content').prepend('<div id="loader"></div>').load(href + ' #content *', function(){
				Cufon.refresh();
				$('#loader').remove();
				$('html:not(:animated), body:not(:animated)').animate({scrollTop: 0}, 300);
			});
			*/
			
			return false;
		});
};

var pageload = function(hash){
	var url;
	if(hash) {
		url = hash;
	} else {
		url = '/';
	}
		$('#content').prepend('<div id="loader"></div>').load(url+' .ajaxcontent', function(data){
			Cufon.refresh();
			$('#loader').remove();
			$('html:not(:animated), body:not(:animated)').animate({scrollTop: 0}, 300);
		});
}

var magicscroll = function(){
	// magic scroll
	// inspired by http://www.blackestate.co.nz/
	// see their source for details
	var $fixed = $('#aside');
	$fixed.css({'position': 'absolute'});
	var objTop = $fixed.offset().top;
	var $window = $(window);
	var booFixed = ($fixed.css('position') == 'fixed');
	$window.scroll(function (){
		if ($window.scrollTop() > objTop - parseInt($fixed.css('marginTop'))){
			if ($.browser.msie && $.browser.version == "6.0"){
				$fixed.css('top', $window.scrollTop());
			} else if (!booFixed){
				$fixed.css({
					position: 'fixed',
					top: 0
				});
				booFixed = true;
			}
		} else {
			if (booFixed){
				$fixed.css({
					position: 'absolute',
					top: 0
				});
				booFixed = false;
			}
		}
	});
};
