﻿$(function(){
//レイアウト変更
	layout();
//外部リンク
	$('a[@href^="http://"]').click(function(){
		window.open(this.href, "_blank");
		return false;
	});
//要素非表示
	$('.JSoff').css('display','none');
	$('.entryMore div').css('display','none');
//placeholder
	if($('#search').val() != '') { setoff(); }
	$('#search').focus(function(){ setoff(); })
	$('#search').blur(function(){ seton(); })
//Entrymore
	$('.entryMore h2').toggle(function(){
		$('+div',this).css('display','block');
		$(this).addClass('open');
	},
	function(){
		$('+div',this).css('display','none');
		$(this).removeClass('open');
	})
//Twitter
	$('#Twitter').append('<ul id="tweets">');
	twitter();
});

$(window).resize(function () { layout(); });

function seton() { if($('#search').val() == '') { $('#bSearch label').attr('class','labelon'); }}
function setoff() { $('#bSearch label').attr('class','labeloff'); }

function twitter() {
	$("#tweets").empty();
	$("#tweets").html("<p>Loading...</p>");
	$("#tweets").empty();
	$('#tweets').jqtwitter("YamadaEnako");
//	setTimeout(twitter, 60000);
}
(function($){$.fn.jqtwitter = function(username, options) {
if(username) {
	var defaultSettings = {
		count: '3',
		idimg: 'img#profile'
	}

	var settings = $.extend(defaultSettings, options);

	var url = "http://twitter.com/status/user_timeline/"+username+".json?count="+settings.count+"&callback=?";

	var holder = this;

	$.getJSON(url, function(data) {
		$.each(data, function(i, item) {
			$('' + settings.idimg + '').attr("src", item.user["profile_image_url"]);
			holder.append("<li>" + item.text.discoverLinks().discoverUsers().discoverTags().discoverImages() + "<span class=\"createdat\">created at " + relative_time(item.created_at) + "<!-- via " + item.source + "--></span></li>");
		});
	});
} else {
	console.debug("jQuery jqtwitter requires a username");
}

// Take the relative time
function relative_time(time_value) {
		var values = time_value.split(" ");
		time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		var parsed_date = Date.parse(time_value);
		var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
		var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
		delta = delta + (relative_to.getTimezoneOffset() * 60);

		var r = '';
		if (delta < 60) {
		r = 'less than a minute ago';//um minuto atrs
		} else if(delta < 120) {
		r = 'about a minute ago';//alguns minutos atrs
		} else if(delta < (45*60)) {
		r = (parseInt(delta / 60)).toString() + ' minutes ago';//minutos atrs
		} else if(delta < (90*60)) {
		r = ' uma hora atrs';
		} else if(delta < (24*60*60)) {
		r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';//horas atrs
		} else if(delta < (48*60*60)) {
		r = '1 dia atrs';
		} else {
		r = (parseInt(delta / 86400)).toString() + ' days ago';//dias atrs
		}
		return r;
}

// Discover users
String.prototype.discoverUsers = function() {
	return this.replace(/@[\w]+/g, function(m) {
		return '<a href="http://www.twitter.com/'+m.replace('@','')+'">'+m+'</a>';
	});
};

// Discover tags
String.prototype.discoverTags = function() {
	return this.replace(/ #[\w]+/g, function(t) {
		return '<a href="http://search.twitter.com/search?q='+t.replace('#', '%23')+'">'+t+'</a>';
	});
};

// Discover all links
String.prototype.discoverLinks = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		return m.link(m);
	});
}; 

// Discover all images
String.prototype.discoverImages = function() {
	return this.replace(/>http\:\/\/twitpic\.com\/[0-9a-zA-Z]+</g, function(k) {
		return '><br \/><img src="'+k.replace('<','').replace('>','').replace('pic\.com\/','pic\.com\/show\/mini\/')+'" width="75" height="75" class="tpic" \/><';
	});
}; 
return this;
};})(jQuery);

function layout() {
	$wsize = $(window).width();
	if ($wsize < 1024 && $wsize >= 925) {
		$('#layout').attr('href', '\/blog\/css\/layout920\.css');
	} else if ($wsize < 925 && $wsize >= 768) {
		$('#layout').attr('href', '\/blog\/css\/layout768\.css');
	} else if ($wsize < 768 && $wsize >= 480) {
		$('#layout').attr('href', '\/blog\/css\/layout480\.css');
	} else if ($wsize < 480) {
		$('#layout').attr('href', '\/blog\/css\/layout320\.css');
	} else {
		$('#layout').attr('href', '\/blog\/css\/layout\.css');
	}
//	$("#test").text($wsize);
}

