/**
 * 東北電力ビッグスワンスタジアム Site Interface Controller
 * Date: 11/10/2010
 * @author xe-que design works
 * @version 1.0
 *
 */

var MENU_SLIDE_DOWN_TIME = 300; 			// サブメニュースライドダウン時間
var MENU_SLIDE_UP_TIME   = 0;	 			// サブメニュースライドアップ時間
var MENU_SLIDE_UP_DELAY  = 150; 			// サブメニュースライドアップディレイ時間
var SUBMENU_CLASS   = "global-submenu-box";	// サブメニュークラス名
var MENUHOVER_CLASS = "hovered";			// サブメニュー表示時の親メニューのアンカークラス名
var COMMON_PATH = "/hpout05/tohokuden-bigswan/";

/* サブメニューデータ */	
var arraySubMenu = {
	 'global-menu-home':	Array()
	,'global-menu-schedule':Array(
	 	 { title: 'イベントカレンダー', 	href: '/schedule/event/calendar.php' }
	 	,{ title: '陸上個人利用日',		href: '/schedule/ground/' }
	 	,{ title: '会議室予約状況',		href: '/schedule/meeting/' }
	 	,{ title: '見学利用日',			href: '/schedule/visit/' }
	 	)
	,'global-menu-usage':	Array(
	 	 { title: 'グラウンド利用', 		href: '/usage/ground.html' }
	 	,{ title: '会議室利用',			href: '/usage/conference-room.html' }
	 	,{ title: '見学利用',				href: '/usage/tour.html' }
	 	,{ title: '展示会などの利用',		href: '/usage/show.html' }
	 	,{ title: '写真・ロケーション撮影',	href: '/usage/location.html' }
	 	,{ title: '広告協賛企業・広告看板のご案内',		href: '/usage/ad.html' }
	 	,{ title: '各種申請書ダウンロード',	href: '/usage/download.html' }
	 	)
	,'global-menu-stadium':	Array(
	 	 { title: '座席案内', 			href: '/stadium/seat.html' }
	 	,{ title: '施設概要',				href: '/stadium/outline.html' }
	 	,{ title: 'スタジアムマップ',		href: '/stadium/map.html' }
	 	,{ title: '売店・休憩所',			href: '/stadium/stand.html' }
	 	,{ title: 'スタジアムの歩み',		href: '/stadium/archive.html' }
	 	,{ title: '周辺ガイド',			href: '/stadium/around.html' }
		)
	,'global-menu-access':	Array(
	 	 { title: '公共交通機関でのアクセス',	href: '/access/public.html' }
	 	,{ title: 'お車でのアクセス',		href: '/access/car.html' }
	 	,{ title: '駐車場案内',			href: '/access/parking.html' }
		)
	,'global-menu-contact':	Array(
	 	 { title: 'お問い合わせフォーム',	href: 'https://www.tohokuden-bigswan.com/contact/' }
	 	,{ title: 'よくあるご質問',		href: '/contact/faq.html' }
		)
};

jQuery(function( $ ){

	// IE6 のみ iepngfix のズレを修正するため対象 div に修正用クラスを追加
	if (typeof document.body.style.maxHeight == "undefined") {
		$('div.content div:eq(0),div#global-menu-top div:eq(0)').addClass('pngfixfix');
	}

	// フォントサイズ指定を表示(Javascriptオフの場合は非表示)
	$('#font-size').show();

	// ボックス全体をリンク化
	if ($.fn.biggerlink != undefined) {
		$('.biggerlink').biggerlink();
	}

	// Smooth Scroll
	$('a#goto-top[href*=#], a.goto-question-list[href*=#], a.goto[href*=#]').click(function(){
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname){
			var target = $(this.hash);
			target = target.length && target;
			if (target.length){
				var sclpos = 20;
				var scldurat = 600;
				var targetOffset = target.offset().top - sclpos;
				$('html,body')
					.animate({scrollTop: targetOffset}, {duration: scldurat, easing: "easeOutExpo"});
				return false;
			}
		}
	});

	/* サブメニュー */
	$('#global-menu-list>li').each(function(){
		targetMenu = $(this).attr('id');
		if (arraySubMenu[targetMenu].length) {

			subMenu = jQuery('<div/>', { 'class': SUBMENU_CLASS });

			menulist = '';
			jQuery.each(arraySubMenu[targetMenu], function(){
				menulist += '<li class="global-submenu"><a href="' + this['href'] +'">' + this['title'] +'</a></li>';
			});
			menulist = '<ul class="global-submenu-list">' + menulist + '</ul>'; // メニューの構造はここで調節
			subMenu.hide().append(menulist);

			$('#' + targetMenu).append(subMenu).hover(
				// mouse over
				function(){
					$(this).find('a:eq(0)').addClass(MENUHOVER_CLASS);
					target = $(this).find('.global-submenu-box');
					target.stop();
					target.css('height','auto');
					height = target.css('height');
					target.css({"height": 0,'opacity':0}).show().animate({"height": height,'opacity':1}, MENU_SLIDE_DOWN_TIME);
//					target.css({"height": 0}).show().animate({"height": height}, MENU_SLIDE_DOWN_TIME);
				},
				// mouse out
				function(){
					target = $(this).find('.global-submenu-box');
					target.stop();
					height = target.css('height');
					target.delay(MENU_SLIDE_UP_DELAY).animate({"height": 0,'opacity':0}, MENU_SLIDE_UP_TIME,function(){ 
//					target.delay(MENU_SLIDE_UP_DELAY).animate({"height": 0}, MENU_SLIDE_UP_TIME,function(){ 
						$(this).css('height','auto').hide();
						$(this).closest('li').find('a:eq(0)').removeClass(MENUHOVER_CLASS);
					});
				}
			);
		}
	});

	// ol のリストにクラス追加
	$('.content ol').each(function(){
		$(this).children('li').each(function(idx){
			$(this).addClass('n' + (idx + 1));
		});
	});
	
});

$(document).ready(function(){
 	// カーソルをホバーしたアンカー以外のアンカー要素をフェード
	$('.fade-menu').children().hover(function() {
//		$(this).siblings(':not(a.selected)').stop().fadeTo(200, 0.7);
		$(this).siblings().stop().fadeTo(200, 0.7);
	}, function() {
		$(this).siblings().stop().fadeTo(300, 1);
	});

	// カーソルをホバーしたアンカーのフェード
	$('.fade, .image-anchor img').hover(function() {
		$(this).fadeTo(200, 0.7);
	}, function() {
		$(this).fadeTo(300, 1);
	});

	// カーソルをホバーしたアンカーイメージをフラッシュ
	$('.flash a img').hover(function() {
		$(this).fadeTo(10, 0.2);
		$(this).fadeTo(1000, 1, function(){ $(this).removeAttr('style') });
	});
	
	// 偶数行にのみクラスを指定
	$('table tr:even').addClass('even-row');


});

// 要素の固定配置
window.onscroll = window. onresize = function(){

	// IE6 なら処理なし
	if (typeof document.body.style.maxHeight == "undefined") {
		return ;
	}

	// スクロール時の上下余白
	var MENU_SCROLL_PADDING_TOP	= 10;
	var MENU_SCROLL_PADDING_BOTTOM = 50;

	// offset が取得できない場合は処理しない
	offset = $('.page-headline').offset();
	if(offset == null) return;
	offset = offset.top;
	
	// 各値取得
	var target = $('.page-headline img');								// 固定させる要素
	var offset = offset - MENU_SCROLL_PADDING_TOP;						// 上限高さ
	var bottom_offset = $('#footer').offset().top;						// 下限高さ
	var scrolltop = document.documentElement.scrollTop;					// 現在のスクロール量
	if (!scrolltop) scrolltop = self.pageYOffset;
	var offset_left = $('.content-inner').offset().left;				// コンテント左余白
	var menuHeight = target.attr('offsetHeight');						// 固定させる要素の高さ
	var winHeight = document.all ? document.documentElement.clientHeight : window.innerHeight; // Window 高さ

// for debug
//document.title = bottom_offset + '/' + scrolltop + '/' + offset + '/' + menuHeight + '/' + MENU_SCROLL_PADDING_BOTTOM + ':' + (Math.floor(bottom_offset) - menuHeight - offset  - MENU_SCROLL_PADDING_BOTTOM);

	// メニュー高さよりウィンドウが大きい場合のみ処理
	if (menuHeight < winHeight) {
	
		// 下限
		if (scrolltop > (bottom_offset - menuHeight - MENU_SCROLL_PADDING_BOTTOM) ) {
			target.css('top',bottom_offset - menuHeight - MENU_SCROLL_PADDING_BOTTOM - scrolltop + 'px').css('left', offset_left + 'px');
		// 上限
		} else if (scrolltop <= offset) {
			target.removeAttr('style');			
		// スクロール中
		} else if (scrolltop > offset) {
			// IE 6 以外
			if( window.XMLHttpRequest ) { 
				target.css('position','fixed');
				target.css('top',MENU_SCROLL_PADDING_TOP + 'px').css('left', offset_left + 'px');
			// IE 6
			} else {
				target.css('top',scrolltop - offset  + 'px');
			}
		}
	} else {
		target.removeAttr('style');			
	}


}

