$(document).ready(function() {
	var $body = $('body');
	$body.addClass('js');
	
	if($.browser.msie) {
		$('blockquote p:not(p.citation)').prepend('â€œ').filter(':last').append('â€?');
		$('blockquote p.citation').prepend('â€” ');
	}
	$('blockquote').each(function(i){
		$(this).find('p:not(p.citation):last').addClass('last');
	});
	
	if($(window).width()>1024) {
		$body.addClass('wide');
		$.cookie('site_width', 'wide', {expires: 30, path: '/'});
	}
	else {
		$body.removeClass('wide');
		$.cookie('site_width', null);
	}
	$(window).resize(function(){
		if($(window).width()>1024) {
			$body.addClass('wide');
			$.cookie('site_width', 'wide', {expires: 30, path: '/'});
		}
		else {
			$body.removeClass('wide');
			$.cookie('site_width', null);
		}
	});

	if($.fn.fancybox) {
		$('#portfolio ul ul li a, #threadless li a').fancybox({
			'zoomSpeedIn'			: 600,
			'zoomSpeedOut'			: 600,
			'overlayShow'			: true,
			'hideOnContentClick'	: true,
			'overlayOpacity'		: 0.85
		});
	}
	if($.fn.scrollTo) {
		$('a.scrollto, #footer a.top').click(function(){
			var $clicked = $(this);
			var scroll_id = $clicked.attr('href');
			var $scrollto = $(scroll_id);
			$clicked.blur();
			$.scrollTo($scrollto, 'normal', {
				'onAfter' : function(){
					// add the hash to the URL
					window.location.hash = scroll_id;
					if($scrollto.hasClass('flash')) {
						// flash the targeted item.
						$scrollto.fadeTo(250, 0.33).fadeTo(250, 0.9999).fadeTo(250, 0.33).fadeTo(250, 0.9999);
					}
				}
			});
			return false;
		});
	}
	
	if(!$.browser.msie) {
		$.whenCached = function(path, fn) { 
			var $img = $('<img />').appendTo($body).hide().load(function(){
				fn.call(this);
				$img.remove();
			}).attr('src', path);
		}

		var last = null;
		var $overflow = $('<div></div>').attr('id','overflow').css('left','-9999px');
		var $background = $('<div></div>').attr('id','background');
		var $inner = $background.append('<div></div>');
		var $overlay = $('<div></div>').attr('id','overlay');

		$overflow.append($background).append($overlay);
		$body.append($overflow).addClass('has-swirl-animation');
		
		$.whenCached('/css/img/header.swirl.png', function() {
			var width = parseInt($(this).width(), 10);
			$.whenCached('/css/img/header.colour.2.png', function() {
				setTimeout(function() {
					$overflow.css('left',0);
					$body.css('background-image','none');
					var $move = $background.get(0);
					$move.scrollLeft = width;
					setInterval(function() {
						$move.scrollLeft -= 10;
						if(last == $move.scrollLeft) {
							$move.scrollLeft = width;
						}
						last = $move.scrollLeft;
					}, 100);
				}, 200);
			});
		});
	}
});