var tw_getnum = 1;
 $(function(){
    //	alert("TEST");
	$.ajax({
		type: "GET",
		    url: "/jp/twitter/data/TweetOrder.js",
		    dataType: "json",
		    success: function(msg){
		    $("#twitter-menu ul").html('');
		    $.each(msg, function(i,item) {
				if(tw_getnum == 1) getTweet1(item);
				if(tw_getnum == 2) getTweet2(item);
				tw_getnum++;
		    });
/*
 $('div.twitter dl').each(function(){
  var url = $(this).find('a').attr('href');
  $(this).find('a').removeAttr('href');
  $(this).click(function(){
    window.open(url);
  });
});
*/
		}
	    });

});  

function getTweet1(sname){
    var jsonurl = "/jp/twitter/data/" + sname + ".json";
    $.getJSON(jsonurl, function(json){
	    TweetCb1(json);
	});
}

function getTweet2(sname){
    var jsonurl = "/jp/twitter/data/" + sname + ".json";
    $.getJSON(jsonurl, function(json){
	    TweetCb2(json);
	});
}

function TweetCb1(ob) {
    var img = '<img src="' +  ob[0]['user']['profile_image_url'] + '" width="48" height="48" />';
    $(".tweet:nth-child(1) dt").html(img);
    var mes = '<span class="account">' + ob[0]['user']['name'] + '</span> <span class="tweet">' + formatTwitString(ob[0]['text']) + '</span> <span class="date">' + relativeTime(ob[0]['created_at']) + '</span> </dd>';
    $(".tweet:nth-child(1) dd").html(mes);

  var url = "/jp/twitter/#"+ob[0]['user']['screen_name'];
  $(".tweet:nth-child(1) dl").click(function(){
  _AN.trackPage('/_sqextop/jp/twitter/link_tweetlist.html');
    window.open(url);
  });

}

function TweetCb2(ob) {
    var img = '<img src="' +  ob[0]['user']['profile_image_url'] + '" width="48" height="48" />';
    $(".tweet:nth-child(2) dt").html(img);
    var mes = '<span class="account">' + ob[0]['user']['name'] + '</span> <span class="tweet">' + formatTwitString(ob[0]['text']) + '</span> <span class="date">' + relativeTime(ob[0]['created_at']) + '</span> </dd>';
    $(".tweet:nth-child(2) dd").html(mes);

  var url = "/jp/socialmedia/#"+ob[0]['user']['screen_name'];
  $(".tweet:nth-child(2) dl").click(function(){
  _AN.trackPage('/_sqextop/jp/twitter/link_tweetlist.html');
    window.open(url);
  });

}

function formatTwitString(str) {
    str=' '+str;
    str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
    str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
    str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
    return str;
}


function relativeTime(pastTime) {
    var past1 = pastTime.split('+' ,2);
    var past2 = String(past1[1]).split(' ' ,2);
    var origStamp = Date.parse(past1[0]+past2[1]);
    var curDate = new Date();
    var currentStamp = curDate.getTime() - 60*60*9 * 1000;

    var difference = parseInt((currentStamp - origStamp)/1000);

    if(difference < 0) return false;

    if(difference <= 5)                     return "たったいま";
    if(difference <= 20)                    return "秒前後";
    if(difference <= 60)                    return "分未満前";
    if(difference < 3600)                   return parseInt(difference/60)+"分前";
    if(difference <= 1.5*3600)              return "約1時間前";
    if(difference < 23.5*3600)              return Math.round(difference/3600)+"時間前";
    if(difference < 1.5*24*3600)    return "約1日前";

    var jpDate = new Date();
    jpDate.setTime(origStamp+60*60*9 * 1000);

    var min =  (jpDate.getMinutes() < 10) ? String("0"+jpDate.getMinutes()) : String(jpDate.getMinutes());
    //return String(jpDate.getMonth()+1) +'月'+ String(jpDate.getDate()) + '日 ' + String(jpDate.getHours()) + ':' + min;
    var yy = String(jpDate.getYear());

    return yy.substr(2)+'.'+String(jpDate.getMonth()+1) +'.'+ String(jpDate.getDate());
}
