//Preload Image
var preloadImage = function(){
	var images = 'http://hokuren-ss.sakura.ne.jp/resource/img/ic_fontsizeL_on.gif';
	var loader = new Asset.images(images);
}
//Load Map
var loadMap = function(){
	if($('shopMap')){
		window.unload=GUnload;
		var dispmap=$('shopMap');
		var address=$('shopAddress').get('text').replace('住所／','');
		if($('shopMap').get('text')){
			var ll=$('shopMap').get('text').split(',');
			if(GBrowserIsCompatible()) {
				$('shopMap').setStyles({'width':640,'height':500,'padding':0});
				var shopName=$$('h2.shopSubttl')[0].get('text');
				var map=new GMap2($('shopMap'));
				map.addControl(new GLargeMapControl());
				map.addControl(new GOverviewMapControl());
				map.addControl(new GMapTypeControl());
				var point=new GLatLng(ll[0],ll[1]);
				map.setCenter(point,17);
				var marker=new GMarker(point);
				map.addOverlay(marker);
				map.openInfoWindowHtml(point,'<h4 style="padding:0 0 0.5em;font-size:1.25em;">'+shopName+'</h4>'+address);
				GEvent.addListener(marker,"click",function(){
					map.openInfoWindowHtml(point,'<h4 style="padding:0 0 0.5em;font-size:1.25em;">'+shopName+'</h4>'+address);
				})
			}else{
				var msg='ご利用のブラウザでは地図を表示することができません。';
				msg.inject(dispmap);
			}
		}else{

			var geocoder=new GClientGeocoder();
			geocoder.getLatLng(address,function(point){
				if(!point){
					var msg='この住所の地図を表示できませんでした。';
					msg.inject(dispmap);
				}else{
					var mapUrl = new IFrame({
					src:'http://maps.google.co.jp/maps?hl=ja&q='+address+'&ie=UTF8&z=14&iwloc=A&ll='+point.y+','+point.x+'&output=embed',
					styles: {
						width:640,
						height:500,
						border:'none'
					}
					});
					mapUrl.inject(dispmap);
				}
			});
		}
	}
}

//Main Image
var mainImages = function () {
	var res = [];
	var initMain = function(e) {
		e.empty();
		e.setStyles({
			'width': 690,
			'height': 330,
			'text-align': 'center'
		});
		var loader = new Element('img', {
			'src': './img/ajax-loader.gif',
			'alt': 'Now Loading...',
			'styles': {'margin-top': '150px'}
		});
		loader.inject(e);
	}

	var readImage = function(e,r) {
		r.each(function(u,i){
			new Request.HTML({
				url: u,
				method: 'get',
				evalScripts: true,
				evalResponse: false,
				onSuccess:function(responseTree,responseElements,responseHTML,responseJavaScript){
					res[i] = responseHTML;
				},
				onComplete:function(){
					return;
				},
				onFailure:function(){
//					alert('Request Failed.');
				}
			}).send();
		});
	}

	var fadeIn = function(e,t,r){
		e.empty().set('html',r);
		t.start('opacity',0,1);
	}

	var initImage = function(elm,r,tFx) {
		tFx.set('opacity',0);
		elm.empty();
		elm.set('html',r);
		tFx.start('opacity',0,1);
	}

	var rotateImage = function(elm,r,tFx) {
		tFx.start('opacity',1,0).chain(function(){fadeIn(elm,tFx,r)});
	}

	var e;
	var flg=0;
	var rep = ['./visual/01_rep.html','./visual/02_rep.html','./visual/03_rep.html','./visual/04_rep.html','./visual/05_rep.html'];
	if (e = $('replacement')){
		if($chk(rep))initMain(e);
		readImage(e,rep);
		var count = 0;
		var tar = new Fx.Tween(e, {
			'duration': 700,
			transitions: Fx.Transitions.backIn
		});
		(function(){initImage(e,res[count],tar);count++;flg++;}).delay(2000);

		var repeat = (function(){
			if(count>=res.length)count=0;
			rotateImage(e,res[count],tar);
			count++;
		}).periodical(4000,this);
	}
}

//Window Close Button
var closeBtnFunction = function () {
	if ($$('.close')) {
		$$('.close').each(function(e) {
			e.getElement('a').addEvent('click', function(){window.close();});
		});
	}
}

//Window Print Button
var printBtnFunction = function () {
	if ($('print')) {
		$('print').addEvent('click', function(){window.print();});
	}
}

//Smooth Scroll
var fixSmoothScroll = function () {
	new SmoothScroll( {
		duration:350,
		transition: Fx.Transitions.Quad.easeInOut,
		wheelStops: false
	}, window);
}

//hightFix
var heightFix = function(t){
	var cn=Array();
	if (!$chk(t)) {cn.push('heightFix');}
	else if ($type(t)=='string') {cn.push(t);} 
	else if ($type(t)=='array') {cn.combine(t);}
	var target=Array();
	var targetH=Array();
	var targetPTop=Array();
	var targetPBot=Array();
	var targetBTop=Array();
	var targetBBot=Array();
	var maxH;
	var ie6=false;
	cn.each(function(e,i) {target.combine($$('.'+e));});
	if ($chk(Browser.Engine.trident)) {
		if (Browser.Engine.version=="4") { ie6=true; }
	}
	target.each(function(e,i) {
		var tmpH=target[i].getSize().y;
		var tmpPTop=target[i].getStyle('padding-top').toInt();
		var tmpPBot=target[i].getStyle('padding-bottom').toInt();
		var tmpBTop=target[i].getStyle('border-top-width').toInt();
		var tmpBBot=target[i].getStyle('border-bottom-width').toInt();
		var totalHeight=tmpH+tmpPTop+tmpPBot+tmpBTop+tmpBBot;
		targetH.push(tmpH);
		targetPTop.push(tmpPTop);
		targetPBot.push(tmpPBot);
		targetBTop.push(tmpBTop);
		targetBBot.push(tmpBBot);
		if(i>0){if(maxH<targetH[i]) {maxH=targetH[i];}}
		else {maxH=targetH[i];}
	});
	target.each(function(e,i) {
		if (!ie6) {var thisH=maxH-targetPTop[i]-targetPBot[i]-targetBTop[i]-targetBBot[i];}
		else {var thisH=maxH;}
		e.setStyle('height',thisH);
	});
}

var reHeightFix = function(t){
	var cn=Array();
	if (!$chk(t)) {cn.push('heightFix');}
	else if ($type(t)=='string') {cn.push(t);} 
	else if ($type(t)=='array') {cn.combine(t);}
	var target=Array();
	cn.each(function(e,i) {target.combine($$('.'+e));});
	target.each(function(e,i) {
		e.setStyle('height','');
	});
	heightFix(t);
}

//Roll Over
var btnRollFunction = function () {
	var thisType="";
	var thisImg = this.src;
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	if (!baseName.match("_on")) {
		baseName += '_on';
		var hoverName = baseName + '.' + tmpImg[1];
		var path = thisType+thisPath.join("/") + '/' + hoverName;
		this.src = path;
	}
}

var btnOutFunction = function () {
	var thisType="";
	var thisImg = this.src;
	var now = this.getParent().getParent().get('class');
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	if (!now.contains("current")) {
		baseName = baseName.replace("_on","");
		var hoverName = baseName + '.' + tmpImg[1];
		var path = thisType+thisPath.join("/") + '/' + hoverName;
		this.src = path;
	}
}

var btnHighlight = function (e) {
	var thisType="";
	var thisImg=e.get('src');
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	if (!baseName.match("_on")) {
		baseName += '_on';
		var hoverName = baseName + '.' + tmpImg[1];
		var path = thisType+thisPath.join("/") + '/' + hoverName;
		e.set('src', path);
	}
}

var btnOffFunction = function (e) {
	var thisImg = e.src;
	if(thisImg.contains("http://")){thisType="http://";}
	else if(thisImg.contains("file://")){thisType="file://";}
	thisImg = thisImg.replace(thisType,"");
	var thisPath = new Array();
	thisPath = thisImg.split("/");
	var thisFile = thisPath.pop();
	var tmpImg = thisFile.split(".");
	var baseName = tmpImg[0];
	baseName = baseName.replace("_on","");
	var hoverName = baseName + '.' + tmpImg[1];
	var path = thisType+thisPath.join("/") + '/' + hoverName;
	e.set('src', path);
}

var fixRollOver = function () {
	$$('.btn').each(function(element) {
		var now = element.getParent().getParent().get('class');
		if (now.contains('current')){
			btnHighlight(element);
		} else {
			$$('.btn').addEvent('mouseover', btnRollFunction);
			$$('.btn').addEvent('mouseleave', btnOutFunction);
		}
	});
}

//External Link
var fixExternalLinks = function () {
	var contentOptions = {};
	$$('a').each(function(element) {
		if (element.getProperty('rel')) {
			if (element.getProperty('rel').contains(',')) {
				var tempArr = element.get('rel').split(',');
				tempArr.each(function (temp,i) {
					temp = temp.replace(' ', '');
					if(temp.contains(':')){
						var ta = temp.split(':');
						contentOptions[ta[0]] = ta[1];
					} else {
						if(temp == 'external') {
							contentOptions['linkType'] = 'external';
						}
					}
				},this);
				if (contentOptions['linkType'] == 'external') {
					if (contentOptions['width'] && contentOptions['height']) {
						if (contentOptions['width'] == 'max') {
							var winWidth = $(window).getSize();
							contentOptions['width'] = winWidth.x;
						}
						if (contentOptions['height'] == 'max') {
							var winWidth = screen.width;
							contentOptions['height'] = screen.height;
						}
						var option = 'scrollbars=yes, width='+contentOptions['width']+', height='+contentOptions['height'];
						element.addEvent('click', function(e) { 
							e = new Event(e);
							e.stop();
							window.open(element.getProperty('href'), 'newwin', option);
						});
					}
				}
			}
			else if (element.getProperty('rel') == 'external') {
				element.addEvent('click', function(e) { 
					e = new Event(e);
					e.stop();
					window.open(this.getProperty('href')); 
				}.bind(element)); 
			}
		}
	});
	var contentOptions = {};
	$$('area').each(function(element) {
		if (element.getProperty('rel')) {
			if (element.getProperty('rel').contains(',')) {
				var tempArr = element.get('rel').split(',');
				tempArr.each(function (temp,i) {
					temp = temp.replace(' ', '');
					if(temp.contains(':')){
						var ta = temp.split(':');
						contentOptions[ta[0]] = ta[1];
					} else {
						if(temp == 'external') {
							contentOptions['linkType'] = 'external';
						}
					}
				},this);
				if (contentOptions['linkType'] == 'external') {
					if (contentOptions['width'] && contentOptions['height']) {
						var option = 'scrollbars=yes, width='+contentOptions['width']+', height='+contentOptions['height'];
						element.addEvent('click', function(e) { 
							e = new Event(e);
							e.stop();
							window.open(element.getProperty('href'), 'newwin', option);
						});
					}
				}
			}
			else if (element.getProperty('rel') == 'external') {
				element.addEvent('click', function(e) { 
					e = new Event(e);
					e.stop();
					window.open(this.getProperty('href')); 
				}.bind(element)); 
			}
		}
	});
}

//Font Size Change
var varDomain = location.hostname;
var images = "";
var fontLFunction = function () {
	var f = this;
	$$('body').setStyle('font-size', fontl);
	var myCookie = Cookie.write('fontsize', 'Large', {domain: varDomain, path: '/'});
	btnHighlight($('fontLarge').getElement('img'));
	btnOffFunction($('fontMiddle').getElement('img'));
	if ($type(f)=='array'){
		f.each(function(e,i){
			reHeightFix(e);
		});
	}
}
var fontMFunction = function () {
	var f = this;
	$$('body').setStyle('font-size', fontm);
	var myCookie = Cookie.write('fontsize', 'Middle', {domain: varDomain, path: '/'});
	btnOffFunction($('fontLarge').getElement('img'));
	btnHighlight($('fontMiddle').getElement('img'));
	if ($type(f)=='array'){
		f.each(function(e,i){
			reHeightFix(e);
		});
	}
}
var fontChgFunction = function (f) {
	var fontLBouncy=fontLFunction.bind(f);
	var fontMBouncy=fontMFunction.bind(f);
	if ($('fontLarge')) { $('fontLarge').addEvent('click', fontLBouncy); }
	if ($('fontMiddle')) { $('fontMiddle').addEvent('click', fontMBouncy); }
}
//Default Font Size
var fontm = '13px';
var fontl = '15px';
var iniDefFontFunction = function () {
	if ($('fontMiddle')) {
		if (Cookie.read("fontsize")) {
			var myCookie = Cookie.read("fontsize");
			if (myCookie == 'Large') {
				fontLFunction();
			} else {
				fontMFunction();
			}
		} else {
			fontMFunction();
		}
	} else {
		fontMFunction();
	}
}
var defaultLocation=function(){
	if($('location')){
		var loc=$('location').getElements('option');
		loc.each(function(e,i){
			if(i==0){e.selected="selected";}
		});
	}
}
var defaultShop=function(){
	if($('location')){
		var def='■市区町村を選択してください';
		var shop=$('shop');
		var n=new Element('option',{
			value: 0,
			html: def
		});
		n.inject(shop);
	}
}
var disableShop=function(){
	if($('shop')){
		var shop=$('shop');
		shop.disabled=true;
	}
}
var enableShop=function(){
	if($('shop')){
		var shop=$('shop');
		shop.disabled=false;
	}
}
var clearShop=function(){
	if($('shop')){
		$('shop').empty();
	}
}
// --- ↓↓↓↓↓↓↓
var getChgLocation=function(){
	if($('location')){
		var loc=$('location');
		var shop=$('shop');
		loc.addEvent('change',function(e){
			clearShop();
			defaultShop();
			disableShop();
			var locname=loc.value;
			var child=[];

	
		
		
		
			var p="道北";
			if(p==locname){
				var c="旭川周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道北";
			if(p==locname){
				var c="留萌周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道北";
			if(p==locname){
				var c="稚内周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
	
		
		
		
			var p="道南";
			if(p==locname){
				var c="倶知安周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道南";
			if(p==locname){
				var c="函館周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道南";
			if(p==locname){
				var c="江差周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道南";
			if(p==locname){
				var c="苫小牧周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道南";
			if(p==locname){
				var c="静内・浦河周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
	
		
		
		
			var p="道央";
			if(p==locname){
				var c="岩見沢周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道央";
			if(p==locname){
				var c="札幌周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
	
		
		
		
			var p="道東";
			if(p==locname){
				var c="北見周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道東";
			if(p==locname){
				var c="帯広周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道東";
			if(p==locname){
				var c="根室周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
		
			var p="道東";
			if(p==locname){
				var c="釧路周辺エリア";
				var n=new Element('option',{
					value: c,
					html: c
				});
				n.inject(shop);
			}
			enableShop();
		
		
	
	

		});
	}
}
var shopSubmit=function(){
	if($('shopSearch')){
		var submit=$('shopSearch').getElement('a');
		var loc=$('location');
		var shop=$('shop');
		submit.addEvent('click',function(e){
			e.stop();
			var locname=loc.getSelected().get('value');
			var shopname=shop.getSelected().get('value');
			var l='';
			var s='';

	
		
			var n="道北";
			if(locname==n){l="north";}
		
		
	
		
		
			var c="旭川周辺エリア";
			if(shopname==c){s="asahikawa";}
		
	
	
		
		
			var c="留萌周辺エリア";
			if(shopname==c){s="rumoi";}
		
	
	
		
		
			var c="稚内周辺エリア";
			if(shopname==c){s="wakkanai";}
		
	
	
	
		
			var n="道南";
			if(locname==n){l="south";}
		
		
	
		
		
			var c="倶知安周辺エリア";
			if(shopname==c){s="kucchan";}
		
	
	
		
		
			var c="函館周辺エリア";
			if(shopname==c){s="hakodate";}
		
	
	
		
		
			var c="江差周辺エリア";
			if(shopname==c){s="esasi";}
		
	
	
		
		
			var c="苫小牧周辺エリア";
			if(shopname==c){s="tomakomai";}
		
	
	
		
		
			var c="静内・浦河周辺エリア";
			if(shopname==c){s="sizuura";}
		
	
	
	
		
			var n="道央";
			if(locname==n){l="central";}
		
		
	
		
		
			var c="岩見沢周辺エリア";
			if(shopname==c){s="iwamizawa";}
		
	
	
		
		
			var c="札幌周辺エリア";
			if(shopname==c){s="sapporo";}
		
	
	
	
		
			var n="道東";
			if(locname==n){l="east";}
		
		
	
		
		
			var c="北見周辺エリア";
			if(shopname==c){s="kitami";}
		
	
	
		
		
			var c="帯広周辺エリア";
			if(shopname==c){s="obihiro";}
		
	
	
		
		
			var c="根室周辺エリア";
			if(shopname==c){s="nemuro";}
		
	
	
		
		
			var c="釧路周辺エリア";
			if(shopname==c){s="kushiro";}
		
	
	
	
			if(s=="0"){
				var url="http://www.hokuren-ss.jp/station/"+l+"/";
				location.href = url;
			}else{
				var url="http://www.hokuren-ss.jp/station/"+l+"/"+s+"/";
				location.href = url;
			}
		});

	}
}
// --- ↑↑↑↑↑↑↑↑

window.addEvent('domready', function() {
	preloadImage();
	loadMap();
	mainImages();

	defaultLocation();
	defaultShop();
	disableShop();

// --- ↓↓↓↓↓↓
	getChgLocation();
	shopSubmit();
// --- ↑↑↑↑↑↑

	//Window Close Button
	closeBtnFunction();
	//Window Print Button
	printBtnFunction();
	//Smooth Scroll
	fixSmoothScroll();
	//heightFix
	heightFix();
	//Roll Over
	fixRollOver();
	//External Link
	fixExternalLinks();
	//Default Font Size
	iniDefFontFunction();
	//heightFix
	heightFix('heightFIx1');
	heightFix('heightFIx2');
	heightFix('heightFIx3');
	//Font Size Change
	fontChgFunction(['heightFIx1','heightFIx2','heightFIx3']);
});
