$(document).ready(function()
{	
	// Photo gallery viewer
	if ($('#gallery-nav').length && (screen.width>=1024)) 
	{
		$('#gallery-nav a').colorbox({rel: 'group1', maxWidth: '98%', opacity: '0.8'});
	}	
	
	// Flickr Gallery script http://danhounshell.com/blog/add-a-flickr-widget-to-any-web-site-with-jquery/
	function getFlickr(userid, target, numberToDisplay) 
	{
		var url = "http://api.flickr.com/services/feeds/photos_public.gne?id=" + userid + "&lang=en-us&format=json&jsoncallback=?";		
		$.getJSON(url, displayImages);
	
		function displayImages(data)
		{
			var htmlString = "";		
			var ctr = 0;
			$.each(data.items, function(i, item) 
			{		
				if (ctr < numberToDisplay) 
				{
					var sourceSquare = (item.media.m).replace("_m.jpg", "_s.jpg");
					var sourceOrig = (item.link).replace("_m.jpg", ".jpg");		
					htmlString += '<li>';
					htmlString += '<a href="' + sourceOrig + '" title="' + item.title + '" style="opacity: 1;">';
					htmlString += '<img alt="' + item.title + '" width="75" height="75" src="' + sourceSquare + '"' + '/>';
					htmlString += '</a>';
					htmlString += '</li>';
					ctr = ctr + 1;
				}
			});	
			$('#' + target).append(htmlString);	
		}
	}	
	$('#gallery #footer section:first-child').prepend('<h2 id="photos"><a href="http://www.flickr.com/photos/bircha">Photos from Flickr &raquo;</a></h2><nav><ul id="flickrphotos"></ul></nav>');	
	getFlickr('8416973@N05', 'flickrphotos', 13);
	
	// Content for the Amazon widget
	$('#content aside iframe').load(function()
	{
		var content = $('<h2 id="books">Related books</h2><p class="ingress">Agile &amp; UCD books from Amazon.com</p>');
		$('#content aside ul.links').after(content);
	});
	
	// Google Analytics event tracking
	var gaTracking = function()
	{
		var filetypes = /\.doc$|\.odt$|\.rtf$|\.xls$|\.ppt$|\.zip$|\.pdf$|\.jpg$/i;
		$("a").live("click", function()
		{
			var url = '';
			if ($(this).attr("href").match(/^mailto\:/i)) {
				url = $(this).attr("href").replace(/^mailto\:/i, "");
				_gaq.push(['_trackEvent', 'Links', 'Mailto', '/mailto/' + url]);
			}
			else if ($(this).attr("href").match(/^tel\:/i)) {
				url = $(this).attr("href").replace(/^tel\:/i, "");
				_gaq.push(['_trackEvent', 'Links', 'Tel', '/tel/' + url]);
			}
			else if (location.host != this.host.replace(/\:80$/i, "")) {
				url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
				_gaq.push(['_trackEvent', 'Links', 'Outgoing', '/outgoing/' + url]);
			}
			else if ($(this).attr("href").match(/.+(\/rss\/).+/)) {
				url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
				_gaq.push(['_trackEvent', 'Links', 'RSS', '/rss/' + url]);
			}
			else if ($(this).attr("href").match(filetypes)) {
				var host = location.host.replace(/\./, "\\."), pattern = new RegExp('^(http\:\/\/)*(www\.)*(' + pirkkarannikko.com + ')*\/', 'i');
				url = $(this).attr("href").replace(pattern, "");
				_gaq.push(['_trackEvent', 'Links', 'Downloads', '/downloads/' + url]);
			}
		});
	}();	
																	
});