// シェアボックス関連
var ShareBoxEnabled = true;
var ShareBoxVisible = false;
var NavShareObj;
var ShareBoxObj;

function SharBoxConfirmExist(){
	ShareBoxEnabled = true;
	NavShareObj = document.getElementById('NavShare');
	ShareBoxObj = document.getElementById('ShareBox');
	
	document.getElementById('NavsSharing').className = "";
	
	NavShareObj.href = 'javascript:TapShareBox()';
}
function TapShareBox(){
	if(ShareBoxVisible){
		CloseShareBox();
	}else{
		ShowShareBox();
	}
}
function ShowShareBox(){
	if(!ShareBoxEnabled){
		return true;
	}
	
	NavShareObj.className = 'on';
	ShareBoxObj.className = 'on';
	ShareBoxVisible = true;
	
	addEvent(DocRoot, 'click', CloseShareBox);
	addEvent(NavShareObj, 'click', eventBubbleCancel);
	addEvent(ShareBoxObj, 'click', eventBubbleCancel);
}
function CloseShareBox(){
	if(!ShareBoxEnabled){
		return true;
	}
	
	NavShareObj.className = 'off';
	ShareBoxObj.className = 'off';
	ShareBoxVisible = false;
	
	removeEvent(DocRoot, 'click', CloseShareBox);
	removeEvent(NavShareObj, 'click', eventBubbleCancel);
	removeEvent(ShareBoxObj, 'click', eventBubbleCancel);
}

function DrawTumblrButton(title, htmltitle, combinedtitle, uri, picsrc){
	if(picsrc){
		document.write('<a href="http://www.tumblr.com/share/photo?source=' + encodeURIComponent(picsrc) + '&caption=' + encodeURIComponent('<a href="' + uri + '">' + combinedtitle + '</a>') + '&clickthru=' + encodeURIComponent(uri) + '" title="' + htmltitle + 'の画像をTumblrで共有" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:81px; height:20px; background:url(http://platform.tumblr.com/v1/share_1.png) top left no-repeat transparent;">Tumblr</a>');
	}else{
		document.write('<a href="http://www.tumblr.com/share" title="' + htmltitle + 'をTumblrで共有" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:81px; height:20px; background:url(http://platform.tumblr.com/v1/share_1.png) top left no-repeat transparent;">Tumblr</a>');
	}
}








// 検索ボックス関連
var SearchBoxVisible = false;
var NavSearchObj;
var SearchBoxObj;
var SearchBoxFloatObj;
var SearchRotationObj;
var SearchXHR;

var SearchInputObj;
var SearchXhrBoxObj;

var CurrentSearch = '';

function SearchBoxConfirmExist(){
	NavSearchObj = document.getElementById('NavSearch');
	SearchBoxObj = document.getElementById('SearchBox');
	SearchBoxFloatObj = document.getElementById('SearchBoxFloat');
	SearchRotationObj = document.getElementById('SearchRotation');
	
	SearchXHR = newXHR();
	
	if(location.host.indexOf('amaitorte.jp') != -1){
		
		document.getElementById('NavsSharing').className = "";
		
		NavSearchObj.href = 'javascript:TapSearchBox()';
		
		SearchInputObj = document.getElementById('SearchInput');
		SearchXhrBoxObj = document.getElementById('SearchXhrBox');
		
		if(isIOS){
			addEvent(document, 'keyup', SearchEnterIOS);
		}
	}
}

function TapSearchBox(){
	if(SearchBoxVisible){
		CloseSearchBox();
	}else{
		ShowSearchBox();
	}
}
function ShowSearchBox(){	
	NavSearchObj.className = 'on';
	SearchBoxObj.className = 'on';
	SearchBoxVisible = true;
	
	SearchInputObj.focus();
	
	addEvent(DocRoot, 'click', CloseSearchBox);
	addEvent(NavSearchObj, 'click', eventBubbleCancel);
	addEvent(SearchBoxFloatObj, 'click', eventBubbleCancel);
}
function CloseSearchBox(){
	NavSearchObj.className = 'off';
	SearchBoxObj.className = 'off';
	SearchBoxVisible = false;
	
	SearchInputObj.blur();
	
	removeEvent(DocRoot, 'click', CloseSearchBox);
	removeEvent(NavSearchObj, 'click', eventBubbleCancel);
	removeEvent(SearchBoxFloatObj, 'click', eventBubbleCancel);
}



function SearchEnterIOS(event){
	if(!event.keyCode){
		// Do none.
	}else if(event.keyCode == 13){
		SearchInputObj.blur();
	}
}
function SubmitSearch(){	
	if(!SearchXHR){
		return false;
	}
	
	if(CurrentSearch != SearchInputObj.value){
		CurrentSearch = SearchInputObj.value;
		
		if(CurrentSearch == ''){
			SearchRotationObj.className = '';
			SearchInputObj.className = '';
			
			SearchXhrBoxObj.innerHTML = '';
		}else{
			if(SearchXHR.readyState != 4){
				SearchXHR.abort();
			}
			SearchRotationObj.className = 'on';
			SearchInputObj.className = 'on';
			
			SearchXHR.onreadystatechange = ShowSearchResult;
			SearchXHR.open("GET", 'http://amaitorte.jp/corefiles/search?mode=xhr&q=' + encodeURIComponent(SearchInputObj.value), true);
			SearchXHR.send(null);
		}
	}
	
	return false;
}
function ShowSearchResult(){
	if(SearchXHR.readyState == 4){
		SearchRotationObj.className = '';
		
		if(SearchXHR.status == 200){
			SearchXhrBoxObj.innerHTML = SearchXHR.responseText;
		}else if(SearchXHR.statusText){
			SearchXhrBoxObj.innerHTML = '<p>申し訳ありません<br />技術的な問題により、現在ご利用頂けません<br />' + SearchXHR.status + ' ' + SearchXHR.statusText + '</p>';
		}else{
			SearchXhrBoxObj.innerHTML = '<p>申し訳ありません<br />予期しない問題により、現在ご利用頂けません</p>';
		}
	}
}
function SetSearchMaxHeight(){
	ViewportSize();
	if(ViewportHeight <= 705){
		SearchXhrBoxObj.style.maxHeight = (ViewportHeight - 85 + 19) + 'px';
	}else{
		SearchXhrBoxObj.style.maxHeight = (ViewportHeight - 85) + 'px';
	}
}
addEvent(window, 'load', SetSearchMaxHeight);
addEvent(window, 'resize', SetSearchMaxHeight);

function CalcViewportSize(){
	ViewportSize();
	FeedCookie('devprof', screen.width + 'x' + screen.height + '/' + ViewportWidth + 'x' + ViewportHeight + '/' + DevicePixelRatio);
}
addEvent(window, 'load', CalcViewportSize);





//スクロール位置
function SaveScrollPosition(){
	ViewportSize();
	
	FeedCookie('ViewportLeft', ViewportLeft, '/', 7);
	FeedCookie('ViewportTop', ViewportTop, '/', 7);
}
function LoadScrollPosition(refferCheck){
	if(document.referrer.indexOf(refferCheck) != -1){
		if(ReverseCookie('ViewportTop')){
			window.scrollTo(ReverseCookie('ViewportLeft'), ReverseCookie('ViewportTop'));
		}
	}
	addEvent(window, 'unload', SaveScrollPosition);
	
	FeedCookie('ViewportLeft', 0, '/', 0);
	FeedCookie('ViewportTop', 0, '/', 0);
}


//プリフェッチ
function Prefetcher(){
	if(isGecko || DeviceScreen != 'desktop'){
		return false;
	}
	
	var PrefetcherImages = new Array();
	var linkEles = new Array();
	linkEles = document.getElementsByTagName('link');
	
	for(i = 0; linkEles.length > i; i++){
		if(linkEles[i].getAttribute('rel') == 'prefetch'){
			var tempnum = PrefetcherImages.length;
			PrefetcherImages[tempnum] = new Image();
			PrefetcherImages[tempnum].src = linkEles[i].getAttribute('href');
		}
	}
}



//サイズ
/*var BodyZoomIsChanged = false;
function SetBodyZoom(){
	ViewportSize();
	if(ViewportWidth < 960){
		document.getElementsByTagName('body')[0].style.zoom = ViewportWidth / 1000;
		BodyZoomIsChanged = true;
	}else if(BodyZoomIsChanged){
		document.getElementsByTagName('body')[0].style.zoom = 1;
		BodyZoomIsChanged = false;
	}
}
if(isAppleWebkit){
	addEvent(window, 'resize', SetBodyZoom);
	addEvent(window, 'load', SetBodyZoom);
}*/





//日付
function MakeAllSotaiTime(){	
	var tempAllElements = new Array();
	tempAllElements = document.getElementsByTagName("time");
	
	for(i = 0; i < tempAllElements.length; i++){
		if(tempAllElements[i].getAttribute('data-amtime')){
			tempAllElements[i].innerHTML = SotaiTime(tempAllElements[i].getAttribute('data-amtime'));
		}
	}
}

function SotaiTime(inputtime){
	NowTime = parseInt((new Date()).getTime() / 1000);
	tempUptime = NowTime - inputtime;
	UpDateYYYYMMDD = YYYYMMDD(inputtime);
	UpDateYYYYMM = YYYYMM(inputtime);
	
	tempKeisoku = NowTime;
	tempNdaysAgo = 0;
	while(YYYYMMDD(tempKeisoku) > UpDateYYYYMMDD){
		tempNdaysAgo ++;
		tempKeisoku -= 86400;
	}
	
	tempKeisoku = YYYYMM(NowTime);
	tempNmonthsAgo = 0;
	while(tempKeisoku > UpDateYYYYMM && tempNmonthsAgo < 16){
		tempNmonthsAgo ++;
		tempKeisoku --;
		if(tempKeisoku % 100 == 0){
			tempKeisoku -= 88;
		}
	}
	
	if(tempUptime < 10){
		return '今';
	}else if(tempUptime <= 92){
		return '1分前';
	}else if(tempUptime <= 60 * 50){
		return Math.round(tempUptime / (60)) + '分前';
	}else if(tempUptime <= 60 * 60 * 1.5){
		return '1時間前';
	}else if(tempUptime <= 60 * 60 * 6){
		return Math.round(tempUptime / (60 * 60)) + '時間前';
	}else if(tempUptime <= 60 * 60 * 24){
		return Math.round(tempUptime / (60 * 60)) + '時間前';
	}else if(tempNdaysAgo == 0){
		return '今日';
	}else if(tempNdaysAgo == 1){
		return '昨日';
	}else if(tempNdaysAgo < 7){
		return tempNdaysAgo + '日前';
	}else if(tempUptime <= 60 * 60 * 24 * 7 * 1.5){
		return '1週間前';
	}else if(tempUptime <= 60 * 60 * 24 * 7 * 2.5){
		return Math.round(tempUptime / (60 * 60 * 24 * 7)) + '週間前';
	}else if(tempUptime <= 60 * 60 * 24 * 7 * 4 && tempNmonthsAgo == 0){
		return Math.round(tempUptime / (60 * 60 * 24 * 7)) + '週間前';
	}else if(tempNmonthsAgo == 0){
		return '今月';
	}else if(tempNmonthsAgo == 1){
		return '先月';
	}else if(tempNmonthsAgo < 10){
		return tempNmonthsAgo + 'ヶ月前';
	}else if(tempNmonthsAgo < 14){
		return '1年前';
	}else{
		return 'とても昔';
	}
}
function YYYYMMDD(sec){
	thistime = new Date();
	thistime.setTime(sec * 1000);
	return (thistime.getFullYear()) * 10000 + (thistime.getMonth() + 1) * 100 + thistime.getDate();
}
function YYYYMM(sec){
	thistime = new Date();
	thistime.setTime(sec * 1000);
	return (thistime.getFullYear()) * 100 + (thistime.getMonth() + 1);
}
