$(document).ready(function(){
	function embed(div) {
		$(div).embedly({
                type:'rich',
                maxWidth: 500,
                method: 'after',
                key:'4822f0babdc211e0a7dc4040d3dc5c07'
        }, function(o){
                if(o!=null) {
					$(div).html("<br/>" + o.title + "<br/>");
                	$('<img/>').attr('src', o.thumbnail_url).prependTo(div);
				}
        });
	}

	$('#header h2:not(:first)').hide();

//	$('#bodyContainer header h1').append(' <span class="marker">*</span>');//.prepend('<span class="marker">{</span> ');

	// $('.content-sections').each(function() {
	// 		$(this).prev().find('h1').append(
	// 			$('<span/>').addClass('marker').text($('li', this).size())
	// 		);
	// 	});
	
	$('.activity p').each(function() {
		// match links, but not to yfrog, etc
		$('a', this).each(function() {
			if($(this).text().match(/^http/) && !$(this).text().match(/yfrog\.com/)) {
				embed(this);
			} else {
				console.log(this);
			}
		});
	});

	$('a.thumbnail').each(function() {
		embed(this);
	});

	$('#disqus_thread').each(function() {
	   var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
	   dsq.src = 'http://rayh.disqus.com/embed.js';
	   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);

		$('<a/>').attr('href',"http://disqus.com").attr('class',"dsq-brlink").html('blog comments powered by <span class="logo-disqus">Disqus</span>').appendTo($(this).parent());
	});
	
	$(window).scroll(function(e){ 
  		nav = $('nav'); 
		console.log("Window offset", $(window).scrollTop(), " > element offset", nav.offset().top);
  		if ($(window).scrollTop() > nav.offset().top) {
			$('#navigationContainer').addClass('sticky');
  		} else {
			$('#navigationContainer').removeClass('sticky');
		} 
	});
	
  	SyntaxHighlighter.all();

		// $("a.panelLink").click(function() {								
		// 	var lastId = $('#bodyContainer .panel:visible').slideUp().attr('id');
		// 	$($(this).attr('href')).slideDown();
		// 	//.find('h1').remove().appendTo('#navigation');
		// 
		// 	if($(this).attr('href')=="#home") {
		// 		$('#navigation').slideUp();
		// 	} else {
		// 		$('#navigation').slideDown();	
		// 	}
		// 	$('#navigation .navigateBack').attr('href', '#' + lastId);
		// 	return false;
		// });
			
	// $('.column').toggle(function() {
	// 	$(this)
	// 		.animate({width:'38%', fontSize:'125%'},300)
	// 		.siblings('.column').animate({width:'13%',fontSize:'77%'},300);
	// }, function() {
	// 	$('.column')
	// 		.animate({width:'17.5%', fontSize:'100%'},300);
	// });
	
	// setInterval(function() {		
	// 	$('#images img:').get(0).hide().siblings(":hidden").get(0).show();
	// }, 1000);
	
	
	setInterval(function() {		
		$('#header h2:visible').fadeOut(function() {
			var elements = $('#header h2').not(this);
			var index = Math.floor(Math.random() * elements.length);
			
			$(elements.get(index)).fadeIn();
		});
	}, 5000);
	
	// $.getJSON("latitude.php", function(data) {
	// 	$('#location').text(data.features[0].properties.reverseGeocode);
	// 	//$('#imageOfMe').append('<img src=' + data.features[0].properties.photoUrl + "/>");
	// });
	
	// Skype status
	$('.skype .description').each(function() {
		$.getJSON("/skype", function(data) {
			$('.skype .description').html(data ? " (online)" : " (offline)");
		});
	})
	
	// $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=44514518@N05&size=s&lang=en-us&format=json&callback=?", function(data){
	// 	
	// });

	// twitter status
	// $.getJSON("http://twitter.com/statuses/user_timeline/rayh.json?&callback=?", function(data) {
	// 	$('.twitterColumn .loading').hide();
	// 	for(var i = 0; i < data.length; i++) {
	// 		if(data[i].in_reply_to_screen_name==null) {
	// 			var text = data[i].text.replace(/(http:\/\/[^\s]+)/, "<a href='$1'>$1</a>");
	// 			$('<li class="project"><span class="type">' + data[i].created_at.replace(/\s+\+\d{4}\s+\d{4}/,'') + '</span><span class="description">' + text + '</span><a href="http://twitter.com/rayh/status/' + data[i].id + '">permalink</a></li>')
	// 				.appendTo('#twitter')
	// 				.slideDown();
	// 		}
	// 	}
	// });
	
	// google reader public feed
	// $.getJSON("http://www.google.com/reader/public/javascript/user%2F12000072838757430321%2Fstate%2Fcom.google%2Fbroadcast?&callback=?", function(data) {
	// 	$('.newsColumn .loading').hide();
	// 	$(data.items).each(function() {
	// 		console.log(this);
	// 		$('<li class="project"><span class="type">' + this.title + '</span><span class="description">' + (this.summary==null ? this.content : this.summary) + '</span><a href="' + this.alternate.href + '">' + this.origin.title + '</a></li>')
	// 		.appendTo('#googlereader')
	// 		.slideDown();
	// 	});
	// });
	
	// babylong bingo
	 $('#babylon .redraw').click(function() {
		jQuery.getJSON( "babylon/babylon.js", function(bingo) {
			var randomized = bingo.sort(function(a, b) {
				return Math.random()-0.5;
			});
			var size = $('#babylon input[name=gridSize]').val();
			var html = "";
			var i = 0;
			for(var y = 0; y < size; y++) {
				html+="<tr>";
				for(var x = 0; x < size; x++) {
					html+="<td style='width:" + (100/size) + "%;height:" + (100/size) + "%;' id='cell_" + x + "_" + y + "'>" + randomized[i++] + "</td>";
				}
				html+="</tr>";
			}	
			$("#bingoGrid").html(html);
		});
	});
	$('#babylon .redraw').click();
	
});

