var pageSize = 'large';
var posts = null;
var moreBt = $('<a href="javascript:void();" class="moreButton">Se mere »</a>');


$(document).ready(function(){
	// Get tweet
	var username='fdfkarlebo';
	var format='json';
	var twitterUrl='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?';
	$.getJSON(twitterUrl,function(tweets){
		var tweet = getCurrentTweet(tweets);
		var created = new Date(tweet.created_at).getTime();
		var now = new Date().getTime();
		var age = 86400000*7;
		if (created > now-age) {
			tweet = '<h4>Nyt fra twitter (pip-ikon)</h4><p>'+convertLinks(tweet.text)+'</p>';
			$("#twitter").css({'display':'block','opacity':0}).html(tweet).animate({'opacity':1, 'margin-top':'-.5em', 'margin-bottom':0}, 700);
		}
	});
	
	// Remove width and height from images
	$('img').removeAttr('height').removeAttr('width');
	
	// Shorten pages for small devices
	posts = $('article.post');
	// We never want to do anything to the first post, so we remove it
	posts.splice(0,1);
	$(window).bind('resize', checkViewportSize);
	
	// Make init check
	checkViewportSize();

	// Listen for clicks
	moreBt.bind('click', function() {
		$(posts[0]).css('display','');
		posts.splice(0,1);
		if (posts.length == 0) {
			moreBt.remove();
		}
	});
});


function checkViewportSize() {
	//console.log($(window).width(), '770');
	if ($(window).width() <= '770') {
		if (pageSize != 'small') {
			console.log('Shorten page');
			pageSize = 'small';
			posts.css('display', 'none');
			moreBt.appendTo($('#content'));
		}
	} else {
		 if (pageSize != 'large') {
			console.log('Enlarge page');
			pageSize = 'large';
			posts.css('display', '');
		}
	}
}

function getCurrentTweet(tweets) {
	// Finds newest tweet, wich is not from this site
	for(var t in tweets) {
		if (tweets[t].text.indexOf('Nyt på hjemmesiden:') == -1) {
			return tweets[t];
		}
	}
	// If nothing found - return something that is to old to be displayed
	return {'created_at': 0};
}

function convertLinks(string) {
	urlb = 'http://'; // URL begin
	urlt = '@'; // Twitter user
	urle = ' '; // URL end
	if (string.indexOf(urlb) > -1 || string.indexOf(urlt) > -1) {
		if (string.indexOf(urlb) > -1 && (string.indexOf(urlb) < string.indexOf(urlt) || string.indexOf(urlt) == -1)) {
			start = string.indexOf(urlb);
			link = string.slice(start);
			if (link.indexOf(urle) > -1) {
				link = link.slice(0, link.indexOf(urle));
			}
			end = start + link.length;
			link = '<a href="'+link+'">'+link+'</a>';
			newEnd = start + link.length;
			string = string.slice(0, start)+link+convertLinks(string.slice(end));
		} else if (string.indexOf(urlt) > -1) {
			start = string.indexOf(urlt);
			link = string.slice(start+1);
			if (link.indexOf(urle) > -1) {
				link = link.slice(0, link.indexOf(urle));
			}
			end = start + link.length+1;
			link = '<a href="http://twitter.com/'+link+'">@'+link+'</a>';
			newEnd = start + link.length;
			string = string.slice(0, start)+link+convertLinks(string.slice(end));
		}
	}
	return string;
}
