﻿// JavaScript Document



/* 特定のクラス属性の画像のロールオーバー表現を実装する
----------------------------------------------------------------*/
var SwapImageFactory = {
	
	init: function(extra) {
		var images = new Array();
		images.extend($$("#mainNavi img"));
		images.extend($$("#commonNavi img"));
		images.extend($$("#mapLink img"));
		if(extra) images.extend(extra);
		images.each(this.create.bind(this));
	},
	
	create: function(obj) {
		if(obj.className == "cancel") return;
		obj.swappedSrc = this.getCache(obj);
		obj.originalSrc = obj.src;
		obj.addEvents({
			"mouseover": this.swap.bindWithEvent(this, obj),
			"mouseout": this.recover.bindWithEvent(this, obj)
		});
	},
	
	getCache: function(obj) {
		var swappedImage = new Image();
		swappedImage.src = obj.src.replace(".gif", "-on.gif");
		return swappedImage.src;
	},
	
	swap: function(event, obj) {
		obj.src = obj.swappedSrc;
	},
	
	recover: function(event, obj) {
		obj.src = obj.originalSrc;
	}
}

/* スクロール機能を実装する
----------------------------------------------------------------*/
var ScrollLink = {
	
	// プロパティ
	///////////////////////////////////////////////////////////
	
	scrollContainer: null,
	
	// メソッド
	///////////////////////////////////////////////////////////
	
	// 初期化
	init: function() {
		
		// プロパティを初期化
		this.scrollContainer = new Fx.Scroll(document.body, {
			transition: Fx.Transitions.Cubic.easeOut,
			duration: 1500
		});
		
		// スコープをバインドする
		var self = this;
		
		// イベントを登録
		var scrollers = $$(".scroller");
		scrollers.extend($$("#localNavi a"));
		scrollers.each(function(obj){
			obj.addEvent("click", self.scrollToTarget.bindWithEvent(self, obj));
		});
	},
	
	// 目的地へスクロールする
	scrollToTarget: function(event, anc) {
		
		// 目的地へのIDを取得する
		var index = anc.href.lastIndexOf("#") + 1;
		var targetId = anc.href.substr(index, anc.href.length-1);

		// topへのスクロールかどうかで処理を分ける
		if(targetId != "top")	this.scrollContainer.toElement(targetId);
		else					this.scrollContainer.toTop();
		
		// デフォルトの挙動を停止させる
		event.stop();
	}
};

/* サブウインドウでリンクを開く
----------------------------------------------------------------*/
var WindowOpener = {
	
	// プロパティ
	///////////////////////////////////////////////////////////
	windowName: "sub_visa",
	
	// メソッド
	///////////////////////////////////////////////////////////
	
	// 初期化
	init: function(anchorList, windowName) {
		
		if(windowName != "" || windowName != null) this.windowName = windowName;
		
		var links = anchorList;
		var self = this;
		links.each(function(obj, i) {
			obj.addEvent("click", self.openSubWindow.bindWithEvent(self, obj.href));
		});
	},
	
	openSubWindow: function(event, href) {
		window.open(href, "_blank",' status=no, toolbar=no, location=no, menubar=no, scrollbars=yes, resizable=yes, width=600');
		event.stop();
	}
}

/* GoogleMapの開閉を制御する
----------------------------------------------------------------*/
var MapController = {
	
	// プロパティ
	///////////////////////////////////////////////////////////
	
	mapContainer: null,
	mapElement: null,
	slideMap: null,
	fadeInTweener: null,
	
	// メソッド
	///////////////////////////////////////////////////////////
	
	// 初期化
	init: function() {
		// エレメントを初期化
		this.setupElements();
		// スコープをバインド
		var self = this;
		// 開くリンクを設定
		var mapOpeners = $$(".mapOpener");
		mapOpeners.each(function(obj){
			obj.addEvent("click", self.openMap.bindWithEvent(self));
		});
		// 閉じるリンクを設定
		var mapClosers = $$(".mapCloser");
		mapClosers.each(function(obj){
			obj.addEvent("click", self.closeMap.bindWithEvent(self));
		});
	},
	
	// 要素を初期化
	setupElements: function() {
		// マップ
		var mapContainer = this.mapContainer = $("mapContainer");
		
		mapContainer.setStyle("display", "block");
		
		// MODIFIED 091103
		var is8 = (Browser.Engine.version >= 5);
		var mId = (Browser.Engine.name && !is8)? "map" : "gmap";
		var mapElement = this.mapElement = $(mId);
		
		this.fadeInTweener = new Fx.Tween(mapElement);
		
		this.slideMap = new Fx.Slide(mapContainer, {
			transition: Fx.Transitions.Cubic.easeOut,
			duration: 1000
		});
		
		mapContainer.setStyles({
			"height": "auto",
			"overflow": "auto"
		});
		
		this.slideMap.hide();
	},
	
	// マップを開く
	openMap: function(event) {
		this.mapElement.setStyle("visibility", "hidden");
		this.slideMap.slideIn();
		this.slideMap.slideIn().chain(this.fadeIn.bind(this));
		event.stop();
	},
	
	// マップを閉じる
	closeMap: function(event) {
		this.slideMap.slideOut();
		this.fadeOut();
		event.stop();
	},
	
	fadeIn: function() {
		this.mapElement.setStyles({
			"visibility": "visible",
			"opacity": "0.001"
		});
		this.fadeInTweener.start("opacity", "0.999");
	},
	
	fadeOut: function() {
		var scope = this;
		this.fadeInTweener.start("opacity", "0.001").chain(function(){
			scope.mapElement.setStyle("visibility", "hidden");
			scope.slideMap.slideOut();
		});
	}
	
};

var ModelCourseMap = new Class({
	
	// MAP
	mapUnit: null,
	slideMapUnit: null,
	
	// SCROLLER
	scrollBody: null,
	
	// CONTROLER
	openerList: null,
	closerList: null,
	
	////////////////////////////////////////////////////////////
	
	// コンストラクタ
	initialize: function() {
		this.init();
	},
	
	// 初期化
	init: function() {
		// 変数の初期化
		this.openerList = new Array();
		this.closerList = new Array();
		
		this.scrollBody = new Fx.Scroll(document.body);
	},
	
	// マップエレメントを登録
	registMap: function(mapUnit) {
		this.mapUnit = mapUnit;
		this.setupMapUnit();
		//this.slidemapUnit.hide();
	},
	
	// マップエレメントを初期化
	setupMapUnit: function() {
		// CSS制御 : this.mapUnit.setStyles({"height": "0px", "overflow": "hidden"});
		this.slideMapUnit = new Fx.Tween(this.mapUnit);
	},
	
	// マップを開くためのリンクを登録
	registOpener: function(openerList) {
		var scope = this;
		openerList.each(function(opener) {
			var rel = opener.rel;
			opener.addEvent("click", scope.openMap.bindWithEvent(scope, rel));
			scope.openerList.push(opener);
		});
	},
	
	// マップを開くためのリンクを登録
	registCloser: function(closerList) {
		var scope = this;
		closerList.each(function(closer){
			var rel = closer.rel;
			closer.addEvent("click", scope.closeMap.bindWithEvent(scope, rel));
			scope.closerList.push(closer);
		});
	},
	
	openMap: function(event, rel) {
		var scope = this;
		var targetElement = $(rel);
		this.scrollBody.toElement(targetElement).chain(function() {
			scope.slideMapUnit.start("height", "500px");
		});
		event.stop();
	},
	
	closeMap: function(event, rel) {
		var scope = this;
		var targetElement = $(rel);
		this.scrollBody.toElement(targetElement).chain(function() {
			scope.slideMapUnit.start("height", "0px");
		});
		event.stop();
	}
});

/* モデルコースのGoogleMapの開閉を制御する
----------------------------------------------------------------*/
/*
var ModelCourseMap = {
	
	slideMap: null,
	currentMap: null,
	scrollBody: null,
	mapList: null,
	mapElement: null,
	mapOpeners: null,
	mapClosers: null,
	printLink: null,
	fadeTweener: null,
	iframes: null,
	
	init: function(mapElement, mapList, mapOpeners, mapClosers, printLink) {
		
		// 地図を保持するエレメント
		this.mapElement = mapElement;
		this.setupMapElement();
		
		// 地図を開くためのエレメント
		this.mapOpeners = mapOpeners;
		this.setupMapOpeners();
		
		// 地図を閉じるためのエレメント
		this.mapClosers = mapClosers;
		this.setupMapClosers();
		
		// 地図リスト
		this.mapList = mapList;
		this.setupMapList();
		
		// 印刷ページへのリンク
		this.printLink = printLink;
		this.setupPrintLink();
		
		// スクロールする機能
		this.setupScrollBody();
	},
	
	// 要素を初期化
	setupMapElement: function() {
		
		var mapElement = this.mapElement;
		mapElement.setStyles({
			"display": "block",
			"visibility": "visible"
		});
		
		this.fadeTweener = new Fx.Tween(mapElement);
		
		this.slideMap = new Fx.Slide(mapElement);
		this.slideMap.hide();
	},
	
	setupMapOpeners: function() {
		var mapOpeners = this.mapOpeners;
		mapOpeners.each(this.initMapOpener.bind(this));
	},
	
	setupMapClosers: function() {
		var mapClosers = this.mapClosers;
		mapClosers.each(this.initMapCloser.bind(this));
	},
	
	setupMapList: function() {
		for(var i=0, imax=this.mapList.length; i<imax; i++) {
			var map = this.mapList[i];
			map.setStyles({
				"visibility": "hidden",
				"z-index": "-1",
				"opacity": "0.001",
				"height": "440px"
			});
		}
	},
		
	setupPrintLink: function() {
		var scope = this;
		this.printLink.addEvent("click", function(event){
			window.open(scope.printLink.href,'sub_visa',' status=no, toolbar=no, location=no, menubar=no, scrollbars=yes, resizable=yes, width=600');						
			event.stop();
		})
	},
	
	setupScrollBody: function() {
		this.scrollBody = new Fx.Scroll(document.body);
	},
	
	setPrintLink: function(targetMap) {
		this.printLink.href = "print_" + targetMap + ".html";
		this.setupPrintLink();
	},
	
	initMapOpener: function(opener) {
		var initIndex = opener.href.lastIndexOf("#");
		var tmpTarget = opener.href.substr(initIndex, opener.href.length);
		var targetMap = tmpTarget.replace("#course", "").toLowerCase();
		opener.addEvent("click", this.openMap.bindWithEvent(this, targetMap));
	},
	
	initMapCloser: function(closer) {
		closer.addEvent("click", this.closeMap.bindWithEvent(this));
	},
	
	showTargetMap: function(targetMap) {
		
		this.setupMapList();
		var map = this.getTargetMap(targetMap);
		map.setStyles({
			"display": "block",
			"visibility": "visible",
			"opacity": "0.001"
		});
	},
	
	getTargetMap: function(targetMap) {
		for(var i=0, imax=this.mapList.length; i<imax; i++){
			var tmpMap = this.mapList[i];
			var tmpMapId = tmpMap.id.toLowerCase();
			mapId = tmpMapId.replace("map", "");
			if(mapId == targetMap){
				return tmpMap;
			}
		}		
	},
	
	openMap: function(event, targetMap) {
		
		this.mapElement.setStyles({
			"opacity": "0.001",
			"visibility": "hidden"
		});
		
		var scope = this;
		
		function step03() {
			scope.mapElement.setStyles({
				"visibility": "visible",
				"z-index": "1"
			});
			
			scope.enableTargetIframe(targetMap);
			
			var map = scope.getTargetMap(targetMap);
			scope.registMap(map);
			new Fx.Tween(map).start("opacity", "0.999");
			
			scope.fadeTweener.start("opacity", "0.999");
		}
		
		function step02() {
			scope.slideMap.slideIn().chain(step03);	
		}
		
		function step01() {
			scope.scrollBody.toElement($("navi")).chain(step02);
		}
		
		this.showTargetMap(targetMap);
		this.setPrintLink(targetMap);
		this.disableIframes();
		
		step01();
		
		event.stop();
	},
	
	closeMap: function(event) {
		
		var scope = this;
		
		function step02() {
			scope.mapElement.setStyle("visibility", "hidden");
			scope.slideMap.slideOut();
			scope.disableIframes();
		}
		
		function step01() {
			scope.fadeTweener.start("opacity", "0.001").chain(step02);
			new Fx.Tween(scope.currentMap).start("opacity", "0.001");
			scope.unregistMap();
		}
		
		step01();
		
		event.stop();
	},
	
	registMap: function(mapElement) {
		this.currentMap = mapElement;
	},
	
	unregistMap: function() {
		this.currentMap = null;
	},
	
	// IFrame関連
	//////////////////////////////////////////////////////////
	registIframes: function(iframes) {
		this.iframes = iframes;
	},
	
	disableIframes: function() {
		for(var i=0, imax=this.iframes.length; i<imax; i++){
			var iframe = this.iframes[i];
			iframe.getParent().setStyles({"z-index": "-1"});
			iframe.setStyles({
				"display": "none",
				"z-index": "-1"
			});
		}
	},
	
	enableTargetIframe: function(targetMap) {
		for(var i=0, imax=this.iframes.length; i<imax; i++){
			var iframe = this.iframes[i];
			
			//iframe.getParent().setStyle("z-index", "-1");
			
			var parentId = iframe.getParent().id;
			parentId = parentId.toLowerCase().replace("map", "");
			if(targetMap == parentId){
				iframe.setStyles({
					"display": "block",
					"z-index": "1"
				});
				iframe.getParent().setStyle("z-index", "1");
			}
		}
	}
	
};
*/


