// #require mootools.js
var display_interleague;
var default_interleague;
if (window.addEvent) {
	window.addEvent('domready', function () {
		// ※交流戦順位表を表示しない場合
		if (!display_interleague) {
			var tab = $('tab-standings-interleague');
			if (tab)	tab.setStyle('display', 'none');
			$$('a.tab-standings-switch').each(function (link) {
				link.getParent().setHTML(link.innerHTML);
				if (link.getParent())	link.remove();
			});
			return;
		}
		// タブ・コントローラ
		var TabSwitch = new Class({
			initialize: function (prefix, show, fnSwitches) {
				this.prefix = prefix || 'tab-';
				this.switches = $$('a.' + this.prefix + 'switch');
				this.ids = this.switches.map(function (item) {
					var idx = item.href.indexOf('#');
					return item.href.substr(idx + 1, 999);
				});
				var tabImages = {};
				this.switches.each(function (item, idx) {
					var id = this.ids[idx];
					var imgs = item.getElements('img').map(function (img) {
						var img_off = new Image;
						var img_on = new Image;
						var img_act = new Image;
						if (img.src.match(/^(.+?)(?:-on|-act)?(\.(gif|jpg|jpeg|xbm|png))$/)) {
							img_off.src = RegExp.$1 + RegExp.$2;
							img_on.src = RegExp.$1 + "-on" + RegExp.$2;
							img_act.src = RegExp.$1 + "-act" + RegExp.$2;
						} else {
							img_off = img_on = img_act = img;
						}
						var the_obj = {
							target: img,
							normal: img_off,
							img_on: img_on,
							img_act: img_act
						};
						return the_obj;
					}, this);
					tabImages[id] = imgs;
					item.addEvent('mouseenter', (function () {
						if (id == this.current)	return;
						tabImages[id].each(function (theObj) {
							theObj.target.src = theObj.img_on.src;
						});
					}).bindAsEventListener(this));
					item.addEvent('mouseleave', (function () {
						if (id == this.current)	return;
						tabImages[id].each(function (theObj) {
							theObj.target.src = theObj.normal.src;
						});
					}).bindAsEventListener(this));
					item.addEvent('click', (function(event) {
						event = new Event(event).stop();
						this.switchContent(id);
					}).bindAsEventListener(this));
				}, this);
				this.tabImages = tabImages;
				var tabContents = {};
				this.ids.each(function (id) {
					tabContents[id] = $(id);
				});
				this.tabContents = tabContents;
				this.current = '';
				this.fnSwitches = fnSwitches;
				this.switchContent($defined(this.tabContents[show]) ? show : this.ids[0]);
			},
			switchTab: function (id) {
				if (!$defined(id) || !this.tabImages[id])	return;
				this.ids.each(function (o_id) {
					if (o_id != id) {
						this.tabImages[o_id].each(function (theObj) {
							theObj.target.src = theObj.normal.src;
						});
					}
				}, this);
				this.tabImages[id].each(function (theObj) {
					theObj.target.src = theObj.img_act.src;
				});
			},
			switchContent: function (id) {
				if (!$defined(id) || !this.tabContents[id])	return;
				if (id == this.current)	return;
				this.current = id;
				this.switchTab(id);
				if (this.fnSwitches && this.fnSwitches[id]) {
					this.fnSwitches[id]();
					return;
				}
				(function() {
					this.ids.each(function (o_id) {
						if (o_id != id) {
							this.tabContents[o_id].setStyle('display', 'none');
						}
					}, this);
					this.tabContents[id].setStyle('display', 'block');
				}).call(this);
			}
		});
		// メイン
		$$(['top-standings']).each(function (box) {
			var body = box.getElement('div.content-box-body');
			var bodyInner = body.getElement('div.content-box-inner');
			var c_league = bodyInner.getElementById('standings-c_league');
			var interleague = bodyInner.getElementById('standings-interleague');
			var buttonArea = box.getElement('div.content-box-foot-buttons');
			if (!buttonArea) {
				buttonArea = new Element('div', {
					'class': 'content-box-foot-buttons'
				}).injectTop(box.getElement('div.content-box-foot'));
			}
			var optFx = {duration: 300, transition: Fx.Transitions.Sine.easeInOut, wheelStops: false};
			var fx;
			if (window.ie6) {
				fx = new Fx.Elements([body, bodyInner], optFx);
			} else {
				fx = new Fx.Styles(body, optFx);
			}
//			var fx2 = new Fx.Scroll(bodyInner, optFx);
			var height1 = body.getStyle('height').toInt();
			var height2 = interleague.offsetHeight - 1;
			var scrollTop = c_league.offsetHeight;
			var optInterLeague = {'height': height2};
			var optCLeague = {'height': height1};
			if (window.ie6) {
				optInterLeague = {'0': optInterLeague, '1': optInterLeague};
				optCLeague = {'0': optCLeague, '1': optCLeague};
				fx.set(optCLeague);
			}
			window.switchInterLeague = function switchInterLeague() {
				interleague.setStyle('display', 'block');
				c_league.setStyle('display', 'none');
				var optInterLeague = {'height': interleague.offsetHeight - 1};
				if (window.ie6) {
					optInterLeague = {'0': optInterLeague, '1': optInterLeague};
				}
//				fx.start(optInterLeague).chain(function () {
//					$('c_league-standings-link').setStyle('display', 'none');
//					$('interleague-standings-link').setStyle('display', 'block');
//				});
//				fx2.scrollTo(0, c_league.offsetHeight);
				fx.set(optInterLeague);
				$('c_league-standings-link').setStyle('display', 'none');
				$('interleague-standings-link').setStyle('display', 'block');
				$('c_league-standings-date').setStyle('display', 'none');
				$('interleague-standings-date').setStyle('display', 'block');
			};
			window.switchCLeague = function switchCLeague() {
				c_league.setStyle('display', 'block');
				interleague.setStyle('display', 'none');
				var optCLeague = {'height': c_league.offsetHeight - 1};
				if (window.ie6) {
					optCLeague = {'0': optCLeague, '1': optCLeague};
				}
//				fx.start(optCLeague).chain(function () {
//					$('c_league-standings-link').setStyle('display', 'block');
//					$('interleague-standings-link').setStyle('display', 'none');
//				});
//				fx2.scrollTo(0, 0);
				fx.set(optCLeague);
				$('c_league-standings-link').setStyle('display', 'block');
				$('interleague-standings-link').setStyle('display', 'none');
				$('c_league-standings-date').setStyle('display', 'block');
				$('interleague-standings-date').setStyle('display', 'none');
			};
			if (window.opera) {
				$('tabs-standings').injectTop(body);
			}
			tabSwitch = new TabSwitch(
				'tab-standings-',
				default_interleague ? 'standings-interleague' : 'standings-c_league',
				{
					'standings-c_league': switchCLeague,
					'standings-interleague': switchInterLeague
				}
			);
		});
	});
}

