jQuery(function($) {
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		return null;
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name,"",-1);
	}

	var opts = { resizeEasing: 'easeOutCubic', resizeDuration:600 };

	$("body.blog a > img[src*='.thumb.']").parent().slimbox(opts, function(el) {
		return [el.href, $(el).find('img').attr('alt')];
	});
	$("body.projects a > img[src*='.thumb.']").parent().slimbox(opts, function(el) {
		var heading = $(el).parent().find('h3'),
		    title = (heading.find('img').attr('alt') || heading.text());

		return [el.href, title];
	});
	$("a[href^='http://www.flickr.com/photos/'] > img:first-child[src]").parent().slimbox(opts, function(el) {
		return [el.firstChild.src.replace(/_[mts]\.(\w+)$/, ".$1"),
		(el.title || el.firstChild.alt) + ' <a href="' + el.href + '">…on Flickr »</a>'];
	});

	$("a[href^=http]").each(function() {
		if (this.href.indexOf(location.hostname) === -1) {
			$(this).attr('target', '_blank');
		}
	});

	function autoCufon(font, replacements) {
		var cookieName = "hasFont" + font.replace(/ /, '');

		if (/*readCookie(cookieName) === 'false' || */!detect.test(font)) {
			$('h2').each(function(){
				this.innerHTML = this.innerHTML.replace(/’|‘/,"'");
			});
			$.each(replacements, function(k,v) {
				Cufon.replace(k, { fontFamily: v });
			});
			//createCookie(cookieName, 'false', 14);
		} else {
			//createCookie(cookieName, 'true', 14);
		}
	}

	if (readCookie('hasEngel') !== 'true' && readCookie('hasGillSans') !== 'true') {
		var detect = new Detector();

		autoCufon('Engel Light ltd', {
			'#message p, #message h1' : 'Engel Light ltd'
		});
		autoCufon('Gill Sans', {
			'h2, .music li, #menu li a' : 'Gill Sans',
			'h1 em' : 'Gill Sans Light'
		});
	}
});
