/****************************
/var 0 
/lastUpdate 2011/03/08 19:20
/MSKCH
****************************/
var UA = navigator.userAgent;
var smartPhoneUA = false;
if( (/iPhone|iPod/.test(UA)) && !(/iPad/.test(UA)) ){
	$('head')
	.append('<link href="style_i.css" rel="stylesheet" type="text/css" media="all" />')
	.append('<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1" />');
	
	onorientationchange=changeOrientation;

	smartPhoneUA = true;
	try{
		if(navigator.userAgent){
			if(navigator.userAgent.match(/iPod/)){
				window.scrollTo(0,0);
			}else if(navigator.userAgent.match(/iPhone/)){
				setTimeout(function(){window.scrollTo(0,1)},100);
			}
		}
	}catch(e){
	}
} else {
	document.write('<link href="style.css" rel="stylesheet" type="text/css" media="all" />');
}

var subTitle=null;
var subCount;

var SBTG = {
		num:{},
		tURL:'http://twitter.com/',
		dd:'data/'	//データの存在するドメイン
	};

	
	

(function($){$(document).ready(function(){
	var defaultList = 'all';
	if(location.hash.match(/^#all$|^#todo$|^#pend$|^#done$/)){
		defaultList = location.hash.toString().replace('#','');
		setTab(defaultList);
	} else if(location.hash.match(/^#tw\d+/)){
		$.getJSON(SBTG.dd + 'all_full.js', function(data){
			var targetCase = location.hash.toString().replace(/\?[^#]*/,'').match(/(\d+)/g);
			var targetData=[];
			subCount=targetCase.length;
			for(var i=0;i<targetCase.length;i++){
				if(data.list[data.list.length - targetCase[i]])
				targetData[i]=data.list[data.list.length - targetCase[i]]
			}
			parentTweet(targetData);
			$('#more-btn').hide();
			$.getJSON(SBTG.dd + 'num.js', function(data){mainTweet(data);});
			$('#tabs > li').removeClass();
		});
	} else if(/\?[^#]*s\d+/.test(location)) {
		$.getJSON('summaries.js', function(data){
			var summaryId=location.toString().match(/s(\d+)/)[1]
			var targetCase =[];
			if(data[summaryId]){
				targetCase=data[summaryId].tw;
				subTitle=data[summaryId].title;
				subCount=targetCase.length;
			}
			$.getJSON(SBTG.dd + 'all_full.js', function(data){
				var targetData=[];
				for(var i=0;i<targetCase.length;i++){
					if(data.list[data.list.length - targetCase[i]])
					targetData[i]=data.list[data.list.length - targetCase[i]]
				}
				parentTweet(targetData);
				$('#more-btn').hide();
				$.getJSON(SBTG.dd + 'num.js', function(data){mainTweet(data);});
				$('#tabs > li').removeClass();
			});
		});
	} else {
		setTab(defaultList);
	}
	
	if(smartPhoneUA)changeOrientation();

	var subAfterY = $('#sub').offset().top + $('#sub').height();
	$('#fixedPageTop').css({top:subAfterY+'px'});

})})(jQuery);



function changeOrientation(){
	if(/90/.test(window.orientation)){
		$('body').addClass('landscape');
	} else if(window.orientation==0) {
		$('body').removeClass('landscape');
	}
}

function setTab(list){
	if(/\?[^#]*s\d+/.test(location.toString())){
		window.location.replace(location.pathname+'#'+list);
	} else {
		loadList(list+'_05');
		$('#tab-'+list+',#tab-i-'+list).addClass('current-list').siblings().removeClass();
	}
}


function setMoreBtn(list){
	var bList=list.replace(/_.*/,'');
	if(!/_full/.test(list)&&SBTG.num[bList]>5){
		$('#more-btn').show().unbind().one('click',function(){loadList(bList+'_full');$('#more-btn').hide()});
	} else {
		$('#more-btn').hide();
	}
}


function loadList(list){
	//JSON
	$.getJSON(SBTG.dd + 'num.js', function(data){mainTweet(data);});
	$.getJSON(SBTG.dd + list + '.js', function(data){mainTweet(data);setMoreBtn(list);});
}


function mainTweet(t){
	(t.num)?numSet(t.num):parentTweet(t.list);
}

function numSet(t){
	SBTG.num = t;
	//alert(SBTG.num.all+','+SBTG.num.todo+','+SBTG.num.pend+','+SBTG.num.done);
	var doneCount = (SBTG.num.done < 100)?'0' + SBTG.num.done:SBTG.num.done;
	setCount = doneCount.split('');
	countImg = '';
	if(smartPhoneUA){countImg = doneCount}
	else{
		for(i=0;i<setCount.length;i++){
			countImg += '<img src="img/fig_count'+setCount[i]+'.gif" width="40" height="64" />';
		}
	}
	
	$('#done-count').html(countImg);
}

function parentTweet(t){
	var listData = '';

	for(i in t){
		var p = t[i];
		RE = (p.re.length==0)?'':childTweet(p.re);
		
		setNum = p.id.toString().split('');
		idWidth=(setNum.length>2)?idWidth=42*2/setNum.length:42;
		idImg = '';
		if(smartPhoneUA){idImg = p.id}
		else{
			for(i=0;i<setNum.length;i++){
				idImg += '<img src="img/fig_num_'+setNum[i]+'.gif" width="'+idWidth+'" height="115" />';
			}
		}
		
		setFlag = p.flag.toString().split('');
		flagW = (smartPhoneUA && p.flag=='111')?'width="60" height="17"':'';
		flagImg = '';
		for(i=0;i<setFlag.length;i++){
			if(setFlag[i]==1)flagImg += '<img src="img/icon_flag_'+ i +'.gif" '+flagW+' />';
		}

		var tImg = (p.images && !smartPhoneUA)?imgLoad(p.images):'';
		var tIcon = (p.icon==null)?'<dl class="noIcon">':'<dl><dt><img src="'+ p.icon+ '" width="48" height="48" /></dt>';
		var tNotification = (p.notification=='NON')?' class="noNotification">':' class="hasNotification"><span class="notification-'+ p.notification+ '"></span>';
//add i.yamashita 2011/9/30 サムネイル非表示設定の場合はユーザー名も非表示
		var tName = '';
		if(p.icon){
			tName ='<span><a href="' + SBTG.tURL + p.name + '" target="_blank">' + p.name + '</a>';
		}

		listData += '<li id="tw'+p.id+'"><h2>'
			+ idImg
			+ '</h2><h3'
			+ tNotification + p.title + '</h3>'
			+ tIcon
			+ '<dd><p>'
			+ p.text
			+ '</p><div><p><strong>'+PT.status
			+ flagImg
			+ '</strong>'
			+ tImg
//i.yamashita 2011/9/30
			+ tName
/* ユーザ名非表示 */
			//+ '<span><a href="' + SBTG.tURL + p.name + '" target="_blank">'
			//+ p.name
			//+ '</a><span>' 
			+ '<span>' + p.time
			+ '</span></span></p></div>'
			+ RE
			+ '</dd></dl></li>';
	}

	if(listData=='')listData='<div id="noTweet"><p>該当するツイートはありません。</p></div>'
	document.getElementById('tgtr').innerHTML = listData;
	if(/\?[^#]*open/.test(location.toString())||/^#tw\d+$/.test(location.hash.toString())){
		$('#tgtr dd > ul').show();
		$('.childToggle').addClass('close');
	}
	if(subTitle){
		$('#subTitle').prepend('<h2><img src="img/bg_subtitle.gif" /><strong><img src="img/icon_subtitle.gif" />〈'+subTitle+'〉に関する案件</strong><span>全'+subCount+'件</span></h2>');
		if(!smartPhoneUA){
			$('#subTitle img:eq(0)').width($('#subTitle').width()).height($('#subTitle').height());
			$('#subTitle img:eq(1)').height($('#subTitle > h2 > strong:eq(0)').height()+8);
			$('#subTitle').show();
		}
	} else {
		$('#subTitle').hide();
	}
}

var ctp = '<p class="childToggle" onclick="childToggle(this);return false"><a href="#" onclick="return false" class="open">'+PT.syousai+'</a><a href="#" onclick="return false" class="close">'+PT.tojiru+'</a></p>'
function childTweet(t){
	var c = ctp+'<ul>';
	for(j in t){
		var n = t[j];
		var tImg = (n.images && !smartPhoneUA)?imgLoad(n.images):'';
		var tIcon = (n.icon==null)?'<dl class="noIcon">':'<dl><dt><img src="'+ n.icon+ '" width="48" height="48" /></dt>';
//add i.yamashita 2011/9/30 サムネイル非表示設定の場合はユーザー名も非表示
		var tName = '';
		if(n.icon){
			tName ='<span><a href="' + SBTG.tURL + n.name + '" target="_blank">' + n.name + '</a>';
		}

		c += (j==0)?'<li class="firstTweet">':'<li>';
		c +=  tIcon
		+ '<dd><p>'
		+ n.text
		+ '</p><div><p>'
		+ tImg
//i.yamashita 2011/9/30
			+ tName
/* ユーザ名非表示 */
		//+ '<span><a href="' + SBTG.tURL + n.name + '" target="_blank">'
		//+ n.name
		//+ '</a><span>'
		+ '<span>' + n.time
		+ '</span></span></p></div>'
		+ '</dd></dl></li>';
	}
	c += '</ul>';
	return c;
}

function childToggle(el){
	$(el).toggleClass('close').next().toggle();
}


var fbt=0;
if(!smartPhoneUA)$(window).scroll(fixedBtn);
function fixedBtn(){
	var subAfterY = $('#header').height() + $('#sub').height();
	wst = $(window).scrollTop();
	wh = $(window).height();
	fbPoint = wst + wh;
	if(wst == 0||wst+wh-40<subAfterY||$(document).height()<1100){
		fbPoint = subAfterY;
	} else if (wst+wh > $(document).height()-110) {
		fbPoint = $(document).height()-155;
	} else {
		fbPoint -= 40;
	}

	$('#fixedPageTop').stop().animate({top:fbPoint+'px'},fbt,function(){fbt=300});
}

var goToMoveFlag = 0;
function goTo(id){
	goToMoveFlag = 1;
	this.now = $(window).scrollTop();
	this.goal = $('#'+id).offset().top;

	goToMove = setInterval(function(){
		this.now = this.now + (this.goal - this.now)/4;
		window.scrollTo(0,this.now);
		if(Math.abs(this.goal - this.now) < 1){
			clearInterval(goToMove);
			goToMoveFlag = 0;
		}
	},33);
}

function wheel(event){
	if(goToMoveFlag == 1){
		clearInterval(goToMove);
		goToMoveFlag = 0;
	}
}

if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;

function imgLoad(img){
	var imgNode = '<em>';
	for(i=0;i<img.length;i++){
		imgNode += '<a href="'+img[i]+'" target="_blank" class="tpcImg"><img src="'+img[i]+'" style="margin-left: 10px" /></a>'
	}
	imgNode += '</em>';
	return imgNode;
}

