$(document).ready(function($) {
	// initialise history plugin
	$.history.init(history);
			
	$(window).resize(function(){
		positionAbsolute();
	});

	// replace all hrefs with hashed urls for history plugin
	$('a[rel*=popup]').each( function() { 
	  var anchor = $(this).attr('href').replace(/http:\/\/(www.)*toko.nu\//, '').replace(/(.*).html/, '#$1');
	  $(this).attr('href', anchor);
	});
	
	// collapse/expand projects	
	$('.eachpost img').click( function(){
		var postHeight = $(this).parent().parent().height();
		var innerHeight = $(this).parent().parent().find('.inner').height();
				
		if (postHeight > 15) {
			$(this).parent().parent().animate({height: 15}, 'normal');
		} else {
			$(this).parent().parent().animate({height: innerHeight}, 'normal');	
		} 
	});
	
	// show selected projects	
	$('#show-selected').click( function() {
		$.cookie("showselected", "true");
	});
	
	// not selected projects	
	$('#navigation a:not(#show-selected)').click(function() {
		$.cookie("showselected", null)
	 	$.cookie("scrollFromTop", 0);

	});
	
	
	// set scroll to position popup
	$('body.level1 a[rel*=popup]').click( function() {
	 	$.cookie("scrollFromTop", $(window).scrollTop(), { expires: .5} );
		return false;
	});
	
	// to open popup
	$('a[rel*=popup]').click( function() {
		$('html, body').animate({scrollTop:0}, 'normal');
		
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.history.load(hash);
		return false;
	});
	
	// to close popup
		$('a.close, #popup_overlay, #pop-column2, #pop-columnimages').click(function() {
		$.history.load('');
		return false;
	});
});

// load the correct stuff into history
function history(hash) {
	if (hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			hash = encodeURIComponent(hash);
		}
		bodyHeight();
		$('div#popup_overlay:hidden').fadeIn('fast');
		$('div#popup:hidden').load(hash+".html", function() {
			$('div#popup').fadeIn('fast', function(){
				positionAbsolute();
			});
			$('html, body').animate({scrollTop:0}, 'normal');
		});
		
		$('div#popup:visible').fadeOut('fast', function() {
			$('div#popup').load(hash+".html", function() {
				$('div#popup:hidden').fadeIn('fast');
				$('html, body').animate({scrollTop:0}, 'normal');
			});
		});
	} else {
		// start page
		$('html, body').animate({scrollTop: $.cookie('scrollFromTop')}, 'normal');
		$('body').css('overflow', 'visible');
		$('body').height('auto');
		$('div#popup_overlay, div#popup').fadeOut('slow', function(){
			$('div#popup').empty();
		});
	}		
}

function bodyHeight() {	
	var bodyHeight = $('body').height();
	var windowHeight = $(window).height();
	var popupHeight = $('#popup:visible').height();
	
	if (popupHeight < bodyHeight) {
		$('body').css('overflow', 'hidden');
		$('body').height(windowHeight);
	} else {
		$('body').css('overflow', 'visible');
		$('body').height('auto');
	}
};

// make col1 position absolute if the window is too small
function positionAbsolute() {
	var col1Height = $('div#pop-column1').height();
	var viewerHeight = $(window).height();
	
	if ((col1Height + 50) > viewerHeight) {
		$('div#pop-column1').css('position', 'absolute');
	} else {
		$('div#pop-column1').css('position', 'fixed');
	}
}
