
var AJAX_WAIT_TIME = 500;
var Wait_flag = false;
var Mouseover_color = "#cf6";

/*------------------------------------------------*/
//ページ移動
//start:レコードの先頭位置(0-n)
/*------------------------------------------------*/
function jump(start) 
{
	document.go.start.value = start;
	document.go.submit();
}

/*------------------------------------------------*/
//ソート順変更
//field:ソートするフィールド名
/*------------------------------------------------*/
function sortby(field) 
{
	document.go.sort.value = field;
	document.go.start.value = 0;
	document.go.submit();
}

/* goSearchに置き換え */
function search(field, val)
{
	goSearch(field, val);
}

/*------------------------------------------------*/
//検索
//field:検索対象のフィールド名
//val:検索値
/*------------------------------------------------*/
function goSearch(field, val)
{
	document.go.elements[field].value = val;
	if (document.go.start) document.go.start.value = 0;
	document.go.submit();
}

/*------------------------------------------------*/
//検索値を設定
//field:検索対象のフィールド名
//val:検索値
/*------------------------------------------------*/
function setValue(field, val)
{
	document.go.elements[field].value = val;
}

/*------------------------------------------------*/
//フォームをリセットする
//obj:フォームオブジェクト
/*------------------------------------------------*/
function allReset()
{
	with (document.go)
	{
		for (i = 0; i < elements.length; i++)
		{
			elements[i].value = "";
		}
	}
}

/*----------------------------*/
// FORMの同期処理
/*----------------------------*/
function sync()
{
	var obj 	= document.search;
	var syncObj = document.go;

	for (i = 0; i < obj.elements.length; i++)
	{
		var element = obj.elements[i];

		switch (element.type)
		{
			case "text":
				var name = element.name;
				var str = element.value;
				syncObj.elements[name].value = str;
				break;

			case "checkbox":
				var name = element.name;
				var str = "";
				if (element.checked) str = "on";
				syncObj.elements[name].value = str;
				break;

			case "select-one":
				if (typeof(element[element.selectedIndex]) != "unknown")
				{
					var name = element.name;
					var str = element[element.selectedIndex].value;
					syncObj.elements[name].value = str;
				}
				break;
		}
	}
}

/*------------------------------------------------*/
//
/*------------------------------------------------*/
function getCookie(theName)
{
	theName += "=";
	var theCookie = document.cookie + ";";
	
	var start = theCookie.indexOf(theName);
	if (start != -1)
	{
		var end = theCookie.indexOf(";", start);
		return unescape(theCookie.substring(start + theName.length, end));
	}
	return false;
}

/*------------------------------------------------*/
//
/*------------------------------------------------*/
function writeCookie(theName, theValue, theDay)
{
	var a = new Date(theDay);
	var expDay = a.toGMTString();

	document.cookie = theName + "=" + escape(theValue) + ";expires=" + expDay + ";";
}

/*------------------------------------------------*/
//
/*------------------------------------------------*/
function check_browser()
{
	myOP = window.opera;            // OP
	myN6 = document.getElementById; // N6
	myIE = document.all;            // IE
	myN4 = document.layers;         // N4

	if      (myOP) myBR="O6"; // ブラウザは OP6以上
	else if (myIE) myBR="I4"; // ブラウザは IE4以上
	else if (myN6) myBR="N6"; // ブラウザは NS6以上
	else if (myN4) myBR="N4"; // ブラウザは NN4
	else           myBR="";   // ブラウザは 不明

	return myBR;
}

/*----------------------------*/
// ボタンのロールオーバー処理
/*----------------------------*/
function toOver(obj)
{
	if (obj.parentNode.className != 'selected') obj.parentNode.className = 'over';
}

function toOut(obj)
{
	if (obj.parentNode.className != 'selected') obj.parentNode.className = '';
}

/*----------------------------*/
//
/*----------------------------*/
function mOver(obj)
{
	obj.parentNode.style.backgroundColor = Mouseover_color;
}

function mOut(obj)
{
	obj.parentNode.style.backgroundColor = '';
}

/*----------------------------*/
//
/*----------------------------*/
function resetDate()
{
	document.search.year.value  = "";
	document.search.month.value = "";
	if (document.search.day) document.search.day.value   = "";
	sync();
}

/*----------------------------*/
//
/*----------------------------*/
function today()
{
	document.search.year.value	= (new Date()).getFullYear();
	document.search.month.value	= (new Date()).getMonth() + 1;
	if (document.search.day) document.search.day.value	= (new Date()).getDate();
	sync();
}

/*----------------------------*/
//
/*----------------------------*/
function thisMonth()
{
	document.search.year.value	= (new Date()).getFullYear();
	document.search.month.value	= (new Date()).getMonth() + 1;
	if (document.search.day) document.search.day.value	= "";
	sync();
}

/*----------------------------*/
// 日付選択用 OPTION出力
/*----------------------------*/
function outputNumOptions(min, max)
{
	for (i = min; i <= max; i++)
	{
		str = "<option value=\"" + i + "\">" + i;
		document.write(str);
	}
}

/*----------------------------*/
//アラームセット
/*----------------------------*/
function setAlarm(date, title)
{
	var obj = document.alarm;
	obj.date.value = date;
	obj.title.value = title;
	obj.submit();
}

/*----------------------------*/
// ステータス欄への出力
/*----------------------------*/
function ss(str)
{
	window.status = str;
	return true;
}

/*----------------------------*/
// クエリの取得
/*----------------------------*/
function getQueries()
{
	var query = new Array();

	var s = location.search.split("?");
	if (s.length == 2)
	{
		var p, tmp;

		p = s[1].split("&");

		for (i = 0; i < p.length; i ++)
		{
			tmp = p[i].split("=");
			if (tmp.length == 2)
			{
				query[tmp[0]] = tmp[1];
			}
		}
	}
	return query;
}

/*----------------------------*/
//指定されたオブジェクトの背景色を変える
/*----------------------------*/
function col(obj)
{
	obj.style.backgroundColor = "#ffff55";
	obj.style.color = "black";
	obj.style.textDecoration = "none";
}

/*----------------------------*/
//オブジェクトを非表示にする
/*----------------------------*/
function hide(id)
{
	document.getElementById(id).style.display = "none";
}

/*----------------------------*/
//Ajaxオブジェクト作成
/*----------------------------*/
function createAjaxObj()
{
	var httprequest = false;
	if (window.XMLHttpRequest)
	{
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType) httprequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject)
	{
		try
		{
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				httprequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	return httprequest;
}


/*----------------------------*/
// 指定されたURLからXMLファイルを読み込みcallback処理を行う
//
// #createAjaxObj()
/*----------------------------*/
function ajaxSendRequest(url, callback, params)
{
	this.url = url;
	this.callback = callback;
	this.params = params;//ユーザが利用するためのパラメータ（任意）
	this.ajaxobj = createAjaxObj();

	if (this.ajaxobj)
	{
		var instance = this;
		instance.ajaxobj.onreadystatechange = function()
		{
			if (instance.ajaxobj.readyState == 4)
			{
				if (instance.ajaxobj.status == 200)
				{
					instance.callback(instance);
				}
			}
		}
		this.ajaxobj.open('GET', this.url, true);
		this.ajaxobj.send(null);
	}
}

/*----------------------------*/
//アクセスカウント用
//
// #createAjaxObj()
/*----------------------------*/
function countMe(url, type)
{
	var ajaxobj = createAjaxObj();
	ajaxobj.onreadystatechange = function(){return true}
	var url = "/count.php?url=" + encodeURIComponent(url) + "&type=" + type;
	ajaxobj.open('GET', url, true);
	ajaxobj.send(null);
}

/*----------------------------*/
//
/*----------------------------*/
function isPlayingNow(dateObj)
{
	var date1 = dateObj.getTime() - 5 * 60 * 1000;
	var date2 = dateObj.getTime() + 120 * 60 * 1000;
	var now  = (new Date()).getTime();

	if (now > date1 && now < date2) return true;
	else return false;
}

/*----------------------------*/
//
/*----------------------------*/
function isRecentlyUpdated(dateObj, hour)
{
	var msec = dateObj.getTime();
	var time = (new Date()).getTime() - hour * 60 * 60 * 1000;

	if (msec > time) return true;
	else return false;
}

/*----------------------------*/
//
/*----------------------------*/
function goto(uri, mode)
{
	if (uri)
	{
		if (!mode) location.href= uri;
		else window.open(uri);
	}
}

/*----------------------------*/
// セレクタのインデックスを移動する
/*----------------------------*/
function moveSelectorIndex(obj, val, is_loopable)
{
	var retval = true;
	var idx = parseInt(obj.selectedIndex);

	idx += val;
	if (idx < 0)
	{
		if (is_loopable)
		{
			idx = obj.length - 1;
		}
		else
		{
			idx = 0;
			retval = false;
		}
	}
	else if (idx >= obj.length)
	{
		if (is_loopable)
		{
			idx = 0;
		}
		else
		{
			idx = obj.length - 1;
			retval = false;
		}
	}
	obj.options[idx].selected = true;
	return retval;
}

/*----------------------------*/
//
/*----------------------------*/
function setWaitTimer(msec)
{
	Wait_flag = true;
	setTimeout("resetWaitTimer()", msec);
}

/*----------------------------*/
//
/*----------------------------*/
function resetWaitTimer()
{
	Wait_flag = false;
}

function googleIt(word, target, domain)
{
	document.google.q.value = word;
	document.google.t.value = target;
	document.google.d.value = (domain) ? domain : "";
	document.google.submit();
}

/*----------------------------*/
//
/*----------------------------*/
function moveTeamMenu(val)
{
	if (document.fportal)
	{
		moveSelectorIndex(document.fportal.team, val, true);

		goSearch('team', document.fportal.team.options[document.fportal.team.selectedIndex].value);
	}
}

/*----------------------------*/
//
/*----------------------------*/
function moveHolizontalDateMenu(val)
{
	var t = Current_holizontal_date.getTime();
	var s = 60 * 60 * 24 * 1000;
	Current_holizontal_date.setTime(t + s * val);

	drawHolizontalDateMenu(Current_holizontal_date);
}

/*----------------------------*/
//
/*----------------------------*/
function drawHolizontalDateMenu(dateobj)
{
	var q = getQueries();
	var team_encoded = (q['team']) ? q['team'] : "";

	var t = dateobj.getTime();
	var s = 60 * 60 * 24 * 1000;
	var last_month = 0;
	var tag = "";
	var str = "";
	var newdate = new Date();

	var type = location.href.match(/match|tv|radio|event|birthday|ticket/);
	if (!type) type = "match";

	for (var i = -3; i < 4; i ++)
	{
		newdate.setTime(t + s * i);
		var year  = newdate.getFullYear();
		var month = newdate.getMonth() + 1;
		var day   = newdate.getDate();
		var url = "/schedule/" + type + "/daily/?date=" + year + "-" + month + "-" + day + "&team=" + team_encoded;

		str = (last_month != month) ? month + "/" : "";
		str += (i == 0) ? "<font size=+1>" + day + "</font>" : day;

		tag += "<td nowrap onClick=\"location.href='" + url + "'\" onMouseOver=\"this.style.backgroundColor='orange';this.style.color='white'\" onMouseOut=\"this.style.backgroundColor='';this.style.color=''\">" + str + "</td>";

		last_month = month;
	}
	tag = "<table align=\"center\" cellspacing=\"0\"><tr><td onClick=\"moveHolizontalDateMenu(-7)\" onMouseOver=\"this.style.backgroundColor='orange';this.style.color='white'\" onMouseOut=\"this.style.backgroundColor='';this.style.color=''\">＜</td>" + tag + "<td onClick=\"moveHolizontalDateMenu(7)\" onMouseOver=\"this.style.backgroundColor='orange';this.style.color='white'\" onMouseOut=\"this.style.backgroundColor='';this.style.color=''\">＞</td></tr></table>";
	document.getElementById("h_date_menu").innerHTML = tag;
}

/*----------------------------*/
//
/*----------------------------*/
function twit(url, text)
{
	var params = {
		version:    '2.0.1',
		login:      'chiqlo',
		apiKey:     'R_48b5fdd27457f6c01abe5696d2300dd4',
		history:    '0',
		longUrl:    url
	};

	var api_url = "http://api.bit.ly/shorten?"
	    + "version=" + params.version
	    + "&longUrl=" + encodeURIComponent(params.longUrl)
	    + "&login=" + params.login
	    + "&apiKey=" + params.apiKey
	    + "&history=" + params.history
	    + "&format=json&callback=?";

	$.getJSON(api_url, function(json)
	{
		window.open('http://twitter.com/?status=' + encodeURIComponent(text + ' ' + json.results[params.longUrl].shortUrl));
	});
}

function twitbot(url, text)
{
	if (!confirm("'" + text + "'\n\nこの話題についてTwitterでコメントしますか？")) return false;

	$.getJSON("/ajax/twitter_post_bot.php",
		{
			"url" : url,
			"text" : text
		},
		function(json){
			if (parseInt(json.status_id) > 0)
			{
				window.open('http://twitter.com/j_bmark/status/' + json.status_id);
			}
			else
			{
				alert("サーバが応答しませんでした\nしばらくたってからお試し下さい");
			}
		}
	);
}


/*----------------------------*/
//
/*----------------------------*/
function getLocaleDateString(dateObj)
{
	var year  = dateObj.getFullYear();
	var month = dateObj.getMonth() + 1;
	var day   = dateObj.getDate();
	var youbi = getYoubi(dateObj)
	return year + "年" + month + "月" + day + "日(" + youbi + ")";
}

/*----------------------------*/
//
/*----------------------------*/
function hmb_schedule_click(type)
{
	var url = "/schedule/";
	var queries = getQueries();
	var team = (typeof(queries['team']) == 'undefined') ? "" : queries['team'];


	switch (type)
	{
		case "schedule":
			var year  = (typeof(queries['year'])  == 'undefined') ? new Date().getFullYear()  : queries['year'];
			var month = (typeof(queries['month']) == 'undefined') ? new Date().getMonth() + 1 : queries['month'];

			url += "team/" + "?year=" + year + "&month=" + month + "&team=" + team;
			break;

		case "match":
		case "tv":
		case "radio":
		case "event":
		case "birthday":
		case "ticket":
			var match = location.href.match(/daily|monthly/);

			if (match == "daily")
			{
				var d = Current_holizontal_date;
				var year  = d.getFullYear();
				var month = d.getMonth() + 1;
				var day   = d.getDate();

				var date_query = year + "-" + month + "-" + day;

				url += type + "/daily/" + "?date=" + date_query + "&team=" + team;
			}
			else
			{
				var year  = (typeof(queries['year'])  == 'undefined') ? new Date().getFullYear()  : queries['year'];
				var month = (typeof(queries['month']) == 'undefined') ? new Date().getMonth() + 1 : queries['month'];

				url += type + "/monthly/" + "?year=" + year + "&month=" + month + "&team=" + team;
			}
			break;

		default:
			alert("ERROR");
			return;
	}

	location.href = url;

}

