function resizeElements() {
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var minWidth = 1300;
	var minHeight = 600;
	var optimalWidth = 2000;
	var optimalHeight = 1000;
	
	if( windowHeight < minHeight ) {
		$('#mainContent').css({
			'position' : 'static',
			'margin-top' : '20px'	
		});
	} else if( windowHeight > minHeight && $('#mainContent').css('position') == 'static' ) {
		$('#mainContent').css('position', 'absolute');
	}
	
	if( windowWidth < minWidth ) {
		var imgActualWidth = $('.stretch').width();
		var imgActualHeight = $('.stretch').height();

		$('.stretch').css({
			'width' : imgActualWidth,
			'height' : imgActualHeight
		});
		
		if( windowHeight > minHeight ) {
			$('.stretch').css({
				'width' : 'auto',
				'height' : '100%'
			});
		}
	}
	
	if( windowHeight > optimalHeight ) {
		$('.stretch').css({
			'width' : 'auto',
			'height' : windowHeight+'px'
		});
		
		if( windowWidth < optimalWidth ) {
			$('.stretch').css({
				'width' : windowWidth+'px',
				'height' : 'auto'
			});
		}
	}
}

function parseTwitterDate(tdate) {
    var system_date = new Date(Date.parse(tdate));
    var user_date = new Date();
    if (K.ie) {
        system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
    }
    var diff = Math.floor((user_date - system_date) / 1000);
    if (diff <= 1) {return "just now";}
    if (diff < 20) {return diff + " seconds ago";}
    if (diff < 40) {return "half a minute ago";}
    if (diff < 60) {return "less than a minute ago";}
    if (diff <= 90) {return "one minute ago";}
    if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
    if (diff <= 5400) {return "1 hour ago";}
    if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
    if (diff <= 129600) {return "1 day ago";}
    if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
    if (diff <= 777600) {return "1 week ago";}
    return "on " + system_date;
}

var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();

$(function() {		
	$('#comm-icons a').hover(function() {
		var img = $('img', this);
		var imgUrl = img.attr('src');
		var hoverUrl = imgUrl.replace('inact', 'act');
		
		$(img).attr('src', hoverUrl);
		$('span', this).animate({
			opacity: 1
		}, 500);
	}, function() {
		var img = $('img', this);
		var imgUrl = img.attr('src');
		var url = imgUrl.replace('act', 'inact');
		
		$(img).attr('src', url);
		
		$('span', this).animate({
			opacity: 0
		}, 500);
	});
	
	$(window).on('load, resize', function() {
		resizeElements();
	});

	$.getJSON('https://api.twitter.com/1/statuses/user_timeline/tomasmitana.json?count=1&callback=?', function(data) {
		var tweetWithMentions = data[0].text.replace(/(^|\W+)\@([\w\-]+)/gm, '$1<a href="http://twitter.com/$2" target="_blank">@$2</a>');
		var tweetWithMentionsAndHashes = tweetWithMentions.replace(/(^|\W+)\#([\w\-]+)/gm, ' <a href="http://twitter.com/search/%23$2" target="_blank">#$2</a>');
		var finalTweet = tweetWithMentionsAndHashes.replace(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi, ' <a href="$2" target="_blank">$2</a>')
				
		$('#latest-tweet p').html(''+finalTweet);
		$('#latest-tweet small').html('<a href="http://twitter.com/tomasmitana/status/'+data[0].id_str+'" title="'+data[0].created_at+'" target="_blank">'+data[0].created_at+'</a>');
		$('#latest-tweet small a').text(parseTwitterDate(data[0].created_at));
		$('#latest-tweet').fadeIn();
	});
});
