$(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);
	
	// 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]);
			}
		});
		
		FB.Event.subscribe('edge.create', function(targetUrl) {
		  _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
		  _gaq.push(['_trackEvent', 'Facebook', 'Like', targetUrl]);
		});
		
		FB.Event.subscribe('edge.remove', function(targetUrl) {
		  _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
		  _gaq.push(['_trackEvent', 'Facebook', 'Unlike', targetUrl]);
		});
		
		FB.Event.subscribe('message.send', function(targetUrl) {
		  _gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
		  _gaq.push(['_trackEvent', 'Facebook', 'Send', targetUrl]);
		});

		twttr.events.bind('tweet', function(event) {
		  if (event) {
		    var targetUrl;
		    if (event.target && event.target.nodeName == 'IFRAME') {
		      targetUrl = extractParamFromUri(event.target.src, 'url');
		    }
		    _gaq.push(['_trackSocial', 'twitter', 'tweet', targetUrl]);
		    _gaq.push(['_trackEvent', 'Twitter', 'Tweet', targetUrl]);
		  }
		});
		
		$('.IN-widget').click(function() {
			_gaq.push(['_trackSocial', 'linkedin', 'share', window.location.href]);
			_gaq.push(['_trackEvent', 'LinkedIn', 'Share', window.location.href]);
		});
						
	}();	
																	
});
