/*****
	FUNCTIONS
*****/
// Equal column height
jQuery.fn.equalCols = function(){
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		$(this).css({'height': maxHeight});
	});

};

/*****
	WHEN EVERYTHING HAS LOADED
*****/

$(function(){

	$.ajax({
		type: "GET",
		url: "http://www.instapaper.com/rss/363202/2aL1r05DgXxRWplFgsipbit6A0",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('item').each(function(){
				var title = $(this).find('title').text();
				var url = $(this).find('link').text();
				$('<div class="items"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#rss');
				$(this).find('desc').each(function(){
					var brief = $(this).find('brief').text();
					var long = $(this).find('long').text();
					$('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
					$('<div class="long"></div>').html(long).appendTo('#link_'+id);
				});
			});
		}
	});

});

$(window).bind("load",function(){

    // Footer top row equal height columns
    $("#footerCategories, #footerAbout").equalCols();
	
	$(".polaroid").each(function() {
		$(this)
			.css({
				"-webkit-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg)',
				"-moz-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg)'
			})
		.hover(function() {
			$(this).css({
				"-webkit-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1.05)',
				"-moz-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1.05)'
			})
		}, function() {
			$(this).css({
				"-webkit-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1)',
				"-moz-transform": 'rotate(' + (Math.floor(Math.random()*10)-5) + 'deg) scale(1)'
			})
		});
	});
	
	// Lightbox for 'Photos' page
	$("a[rel=group], a.footerThanks").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'fade',
		'speedIn'		: 400,
		'transitionOut'	: 'fade',
		'speedOut'		: 200,
		'changeSpeed'	: 200,
		'overlayOpacity': 0.8,
		'overlayColor'	: '#000',
		'padding'		: 0,
		'centerOnScroll': true
	});
	
	// Center images on load
	$("a.polaroid img").each(function(){
		$(this).css("position","relative");  
		var thePosition = ($(this).height() - 120) / 2;
		$(this).animate({"top":"-" + thePosition + "px","left":"-" + thePosition + "px"}, 700); 
	});  
	
	// Wrap respond title with link, hide form & toggle on click
	var respondText = $("#respond h3").html();
	var heightOfRespondForm = $("#commentform").height();
	$("#respond h3").remove();
	$("#respond").prepend('<h3><a href="#">' + respondText + '</a></h3>');
	$("#commentform").css("height",heightOfRespondForm).slideUp();
	$("#respond h3").click(function(){
		$("#commentform").slideToggle();
		return false;
	});
	
	// Change archive date on link hover
	$("#archive ul li a").hover(function(){
		$(this).parent("li").find("time").addClass("hovered");
		console.log("Over");
	}, function(){
		$(this).parent("li").find("time").removeClass("hovered");
		console.log("And Out");
	});
		
});