grep               = {};
grep_focus         = false;
grep_focus_name    = '';
area_keys          = {};
area_keys.hokkaido = [
	'hokkaido  : 北海道'
];

area_keys.tohoku = [
	'aomori    : 青森',
	'iwate     : 岩手',
	'miyagi    : 宮城',
	'akita     : 秋田',
	'yamagata  : 山形',
	'fukushima : 福島',
	'niigata   : 新潟'
];

area_keys.kanto = [
	'tokyo     : 東京',
	'kanagawa  : 神奈川',
	'saitama   : 埼玉',
	'chiba     : 千葉',
	'ibaraki   : 茨城',
	'tochigi   : 栃木',
	'gunma     : 群馬',
	'yamanashi : 山梨',
	'nagano    : 長野'
];

area_keys.tokai = [
	'aichi     : 愛知',
	'gifu      : 岐阜',
	'shizuoka  : 静岡',
	'mie       : 三重'
];

area_keys.hokuriku = [
	'toyama    : 富山',
	'ishikawa  : 石川',
	'fukui     : 福井'
];

area_keys.kansai = [
	'osaka     : 大阪',
	'hyogo     : 兵庫',
	'kyoto     : 京都',
	'shiga     : 滋賀',
	'nara      : 奈良',
	'wakayama  : 和歌山'
];

area_keys.chugoku = [
	'tottori   : 鳥取',
	'shimane   : 島根',
	'okayama   : 岡山',
	'hiroshima : 広島',
	'yamaguchi : 山口'
];

area_keys.shikoku = [
	'tokushima : 徳島',
	'kagawa    : 香川',
	'ehime     : 愛媛',
	'kochi     : 高知'
];

area_keys.kyushu = [
	'fukuoka   : 福岡',
	'saga      : 佐賀',
	'nagasaki  : 長崎',
	'kumamoto  : 熊本',
	'oita      : 大分',
	'miyazaki  : 宮崎',
	'kagoshima : 鹿児島',
	'okinawa   : 沖縄'
];

if (location.href.isin('/3G/')) {
	document.write('<style type="text/css">');
	document.write('div.station-group {');
	document.write('display : none;');
	document.write('}');
	document.write('</style>');
}




// param  : <string>, <string>, <boolean>
// return : <void>
// -----------------------------------------------------------------------------
function printGrepArea (area, area_name, station) {
	var station_tab = '';
	if (station) {
		station_tab += '<a href="javascript:grepArea(\'station\')">';
		station_tab += '<img src="/mb/shared/img/area/grep/station_' + station + '.gif" ';
		station_tab += 'width=' + (station == 'area' ? 165 : 155) + '" height="25" name="station" alt="';
		
		switch (station) {
			case 'index'  : station_tab += '開局予定の駅';   break;
			case 'result' : station_tab += '開局された駅';   break;
			case 'area'   : station_tab += 'ご利用可能な駅'; break;
		}
		
		station_tab += '" />';
		station_tab += '</a>';
		
		SBM.img.load('/mb/shared/img/area/grep/area.gif');
		SBM.img.load('/mb/shared/img/area/grep/station_' + station + '.gif');
	}
	else {
		station_tab = '<img src="/mb/shared/img/area/grep/station_none.gif" width="15" height="25" alt="" />';
	}
	
	var html  = '<table cellspacing="0" class="grep">';
	    html += '<tr>';
	    html += '<th colspan="' + (area_keys[area].length + 1) + '">';
	    html += station ? '<a href="javascript:grepArea(grep_focus_name)" onclick="focusArea(grep_focus)">' : '';
	    html += '<img src="/mb/shared/img/area/grep/area_on.gif" width="295" height="25" name="area" alt="地域で選ぶ" />';
	    html += station ? '</a>' : '';
	    html += station_tab;
	    html += '</th>';
	    html += '</tr>';
	    html += '<tr>';
	    html += '<td>';
	    html += '<a href="javascript:grepArea()" onclick="focusArea(this)" id="first">';
	    html += area_name + '全域';
	    html += '</a>';
	    html += '</td>';
	
	for (var i = 0; i < area_keys[area].length; i++) {
		area_keys[area][i] = area_keys[area][i].split(' ').join('');
		
		var key  = area_keys[area][i].split(':')[0];
		var name = area_keys[area][i].split(':')[1];
		
		grep[key] = true;
		
		html += '<td>';
		html += '<a href="javascript:grepArea(\'' + key + '\')" onclick="focusArea(this)"';
		html += (i ==  area_keys[area].length - 1 ? ' class="last"' : '') + '>';
		html += name;
		html += '</a>';
		html += '</td>';
	}
	
	html += '</tr>';
	html += '</table>';
	html += '<div id="grep-nomatch">';
	html += '<img src="/mb/shared/img/area/grep/nomatch.gif" width="460" height="25" alt="該当するサービスエリアはありません。" />';
	html += '</div>';
	
	document.write(html);
	grep_focus = document.getElementById('first');
	
	if (station) {
		grep.station = true;
		area_keys[area][area_keys[area].length] = 'station';
	}
}





// param  : <string>
// return : <void>
// -----------------------------------------------------------------------------
function grepArea (key) {
	var tr       = document.getElementsByTagName('tr');
	var tr_true  = new Array();
	var tr_false = new Array();
	
	if (arguments.length >= 1) {
		for (var i = 0; i < tr.length; i++) {
			var th = tr[i].firstChild.nodeType == 1
			       ? tr[i].firstChild
			       : tr[i].firstChild.nextSibling;
			
			if (th.nodeName == 'TH' && th.className.indexOf(key) >= 0) {
				tr_true[tr_true.length] = tr[i];
				
				if (th.getAttribute('rowspan') >= 2) {
					var rowspan = th.getAttribute('rowspan');
					var tr_next = tr[i];
					
					for (var j = 1; j < rowspan; j++) {
						tr_true[tr_true.length] = tr_next.nextSibling.nodeType == 1
						                        ? tr_next.nextSibling
						                        : tr_next.nextSibling.nextSibling;
						tr_next                 = tr_next.nextSibling.nodeType == 1
						                        ? tr_next.nextSibling
						                        : tr_next.nextSibling.nextSibling;
					}
					i += rowspan - 1;
				}
			}
			else if (th.nodeName == 'TH'){
				var class_name   = th.className.split(' ');
				var class_exists = false;
				
				for (var j in class_name) {
					if (grep[class_name[j]]) {
						class_exists = true;
						break;
					}
				}
				
				if (!class_exists) { continue; }
				
				tr_false[tr_false.length] = tr[i];
				
				if (th.getAttribute('rowspan') >= 2) {
					var rowspan = th.getAttribute('rowspan');
					var tr_next = tr[i];
					
					for (var j = 1; j < rowspan; j++) {
						tr_false[tr_false.length] = tr_next.nextSibling.nodeType == 1
						                          ? tr_next.nextSibling
						                          : tr_next.nextSibling.nextSibling;
						tr_next                   = tr_next.nextSibling.nodeType == 1
						                          ? tr_next.nextSibling
						                          : tr_next.nextSibling.nextSibling;
					}
					i += rowspan - 1;
				}
			}
		}
	}
	else {
		tr_true = tr;
	}
	
	if (tr_true.length >= 1) {
		document.getElementById('grep-nomatch').style.display = 'none';
	}
	else {
		document.getElementById('grep-nomatch').style.display = 'block';
	}
	
	displayArea(tr_true,  true);
	displayArea(tr_false, false);
	existsAreaInCategory();
	existsAreaInGroup();
	focusMode(key);
}





// param  : <element>, <boolean>
// return : <void>
// -----------------------------------------------------------------------------
function displayArea (tr, display) {
	for (var i = 0; i < tr.length; i++) {
		tr[i].style.display = display ? '' : 'none';
	}
}





// param  : 
// return : <void>
// -----------------------------------------------------------------------------
function existsAreaInCategory () {
	var table = document.getElementsByTagName('table');
	
	for (var i = 0; i < table.length; i++) {
		if (table[i].className == 'area-table') {
			var exists_area = false;
			var tr          = table[i].getElementsByTagName('tr');
			
			for (var j = 0; j < tr.length; j++) {
				if (tr[j].style.display != 'none') {
					exists_area = true;
					break;
				}
			}
			
			var h2 = table[i].previousSibling.nodeType == 1
			       ? table[i].previousSibling
			       : table[i].previousSibling.previousSibling;
			
			if (exists_area) {
				h2.style.display       = '';
				table[i].style.display = '';
			}
			else {
				h2.style.display       = 'none';
				table[i].style.display = 'none';
			}
		}
	}
}





// param  : 
// return : <void>
// -----------------------------------------------------------------------------
function existsAreaInGroup () {
	var div = document.getElementsByTagName('div');
	
	for (var i = 0; i < div.length; i++) {
		if (div[i].className.indexOf('grep-group') >= 0) {
			var h2           = div[i].getElementsByTagName('h2');
			var exists_month = false;
			
			for (var j = 0; j < h2.length; j++) {
				if (h2[j].style.display != 'none') {
					exists_month = true;
					break;
				}
			}
			
			if (exists_month) {
				div[i].style.display = '';
			}
			else {
				div[i].style.display = 'none';
			}
			
		}
	}
}





// param  : <element>
// return : <void>
// -----------------------------------------------------------------------------
function focusArea (obj) {
	var area = document.getElementById('first').parentNode.parentNode.getElementsByTagName('a');
	
	for (var i = 0; i < area.length; i++) {
		area[i].style.fontWeight   = 'normal';
		area[i].style.color        = '#003399';
		area[i].style.background   = '#ffffff';
		area[i].style.borderBottom = '2px solid #dddddd';
	}
	
	obj.style.fontWeight   = 'bold';
	obj.style.color        = '#ffffff';
	obj.style.background   = '#abb3bc';
	obj.style.borderBottom = '2px solid #8d95a0';
	
	grep_focus      = obj;
	grep_focus_name = obj.href.split('(')[1].split(')')[0].split('\'').join('');
}





// param  : <string>
// return : <void>
// -----------------------------------------------------------------------------
function focusMode (key) {
	if (grep['station']) {
		var div           = document.getElementsByTagName('div');
		var station_group = new Array();
		for (var i = 0; i < div.length; i++) {
			if (div[i].className.indexOf('station-group') >= 0) {
				station_group[station_group.length] = div[i];
			}
		}
		
		if (arguments.length >= 1 && key == 'station') {
			var area = document.getElementById('first').parentNode.parentNode.getElementsByTagName('a');
			
			for (var i = 0; i < area.length; i++) {
				if (area[i] == grep_focus) {
					area[i].style.fontWeight   = 'bold';
					area[i].style.color        = '#ffffff';
					area[i].style.background   = '#cccccc';
					area[i].style.borderBottom = '2px solid #aaaaaa';
				}
				else {
					area[i].style.fontWeight   = 'normal';
					area[i].style.color        = '#cccccc';
					area[i].style.background   = '#ffffff';
					area[i].style.borderBottom = '2px solid #dddddd';
				}
			}
			
			SBM.img.change('area'   , false);
			SBM.img.change('station', true);
			
			for (var j = 0; j < station_group.length; j++) {
				station_group[j].style.display = 'block';
			}
		}
		else {
			SBM.img.change('area'   , true);
			SBM.img.change('station', false);
			for (var j = 0; j < station_group.length; j++) {
				station_group[j].style.display = 'none';
			}
		}
	}
}

(function () {
	var ready = SBM.tpl.ready;
	SBM.tpl.ready = function () {
		ready.apply(SBM.tpl, []);
		if (location.href.replace(/^.+\?/, '') == 'station') {
			switch (location.href.replace(/^.+3G\/(.+)\.html\?.+$/, '$1')) {
				case 'tohoku/area':
				case 'kanto/area':
				case 'tokai/area':
				case 'kansai/area':
				case 'chugoku/area':
				case 'kyushu/area':
					grepArea('station');
			}
		}
	};
})();
