var json;
var current;
var reason;
$(document).ready(function(){
	
	$.getJSON(
		'/js/holidays.js',
		function(def){
			json = def;
			checkStatus();
			var timer = setInterval("checkStatus()",60000);
			var tipPos = $('#office').offset();
			var tip = $('<div id="office_tip_wrapper"><div id="office_tip"></div><div id="tip_bubble"></div></div>').css({display:'none'});
			$('#office').after(tip);
			$('#office').hover(
				function(){ // over
					$('#office_tip').html(reason);
					$('#office_tip_wrapper').css("top", tipPos.top- $('#office_tip_wrapper').height() + 5).fadeIn();
//					alert($('#office_tip_wrapper').height());
				},
				function(){ // out
					$('#office_tip_wrapper').fadeOut();
				}
			);
		}
	);
});

function checkStatus(){
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth() +1;
	var day = now.getDate();
	var today = year +'/'+ month+'/'+day;
	var week = now.getDay();
	var status = 1; // 1=online, 2=offline, 3=outnow
//	var reason;
	var url = json.default_url;
	// 最初にステータスをチェック
	//$('#office').html(json.holiday[today]);
	if(json.status != 'auto'){
		status = json.status;
		if(json.status == 1){
			reason = json.open_message;
			url = json.open_url;
		}else if(json.status == 3){
			reason = json.outnow_message;
			url = json.outnow_url;
		}else if(json.status == 2){
			reason = json.closed_message;
			url = json.closed_url;
		}else{
			// 設定がなかったら。。。ということはないが。
			$('#office').html('no status is defined at line 35');
			return false;
		}
	// 次に休日設定(ステータススケジュール？)をチェックする
	}else if(json.holiday[today] != null){
		reason = json.holiday[today].message;
		status = json.holiday[today].status;
		url = json.holiday[today].url;
	// 定休日チェック
	}else if(json.regular[week] != null){
		reason = json.regular_holiday_message;
		status = 2;
		url = json.regular_holiday_url;
	}else{
		// 営業時間チェック
		var min = now.getMinutes();
		var hour = now.getHours();
		if(min < 10) { min = "0" + min; }
		var mintime = hour +':'+ min;
		mintime = toMin(mintime);
		//alert(mintime);
		//$('#office').html(mintime +' - ' +toMin(json.closeTime));
		if(!(mintime >= toMin(json.openTime) && mintime < toMin(json.closeTime))){
			//alert('現在:'+mintime+' 開始時:'+def.openTime+' 終了時:'+def.closeTime);
			reason = json.closed_message;
			status = 2;
			url = json.closed_url;
		}else{
			// ここまできてようやく営業時間中
			reason = json.open_message;
			status = 1;
			url = json.open_url;
		}
	}
	if(current != status){
		// openにあわせて画像の入れ替え
		var image;
		if(status == 1){
			image = '<img src="/image/online.png" alt="'+reason+'" />';
		}else if(status == 2){
			image = '<img src="/image/offline.png" alt="'+reason+'" />';
		}else if(status == 3){
			image = '<img src="/image/outnow.png" alt="'+reason+'" />';
		}else{
			$('#office').html('no status is defined at line 80');
			return false;
		}
		//張り込むhtmlを仕上げる
		url = '<a href="'+url+'">'+image+'</a>';
		$('#office').html(url);
		current = status;
	}
	
}

function toMin(time){
	var tTime = time.split(":");
	time = tTime[0] * 60 +(tTime[1] - 0);
	return time;
}
