$(function(){

	/****** 初期設定 ******/
	var content = $('#contents'),
			contain = $('#container'),
			contmain = $('#contentsMain'),
			header = $('#header'),
			gnav = $('#gnav'),
			footer = $('#footer'),
			opPos,
			clPos = clPositon(),
			gnavs = gnav.find('li[id^=gnav]').find('a'), //id=gnav**が付与されたli中のa要素を抽出
			locHash = window.location.hash;
			content.css('display','none');
			contain.css('top',clPos);
	
	/****** トリガー設定用 ******/
	gnavs.each(function(){ //gnavsのすべての要素に対して繰り返し処理実施
		var gn = $(this),
				box = $(gn.attr('href')); //各コンテンツを変数box内に保持

		//固有URL生成用ハッシュ処理
		/*if(gn.attr('href')==locHash){
			gn.addClass('current');
			contmain.empty();
			contmain.append(box);
			opPos = opPositon();
			toggleOpenAction(content,contain,opPos);
		}*/

		$(this).click(function(evt){
			if(!$(this).hasClass('current')){ //open
				gnavs.removeClass('current'); //クリックした要素以外のclassを削除
				$(this).addClass('current');
				contmain.empty();
				contmain.append(box);
				opPos = opPositon();
				toggleOpenAction(content,contain,opPos);
			} else { //close
				gnavs.removeClass('current');
				clPos = clPositon();
				toggleCloseAction(content,contain,clPos);
			}
			evt.preventDefault(); //href属性#hogeでのページ内リンクをキャンセル
		})
	});

	/****** トグルアクション関数 ******/
	
	//トグルオープン（open）
	function toggleOpenAction(cc,ct,op){
		cc.show();
		ct.animate(
			{top:op}
		)
	}
	//トグルクローズ（close）
	function toggleCloseAction(cc,ct,cp){
		cc.hide();
		ct.animate(
			{top:cp}
		)
	}
	
	/****** 位置算出用関数 ******/
	
	//オープン状態に取る位置を算出
	function opPositon(){
		getSize();
		cMargin = Math.round((docHeight - 60 - hdHeight - gnHeight - ftHeight - ctHeight)/2);
		if(docHeight < ctHeight){cMargin=0;}
		if(cMargin < 0){cMargin=0;}
		return cMargin;
	}
	
	//クローズ状態に取る位置を算出
	function clPositon(){
		getSize();
		nMargin = Math.round((docHeight - 100 - hdHeight - gnHeight - ftHeight)/2);
		if(nMargin < 0){nMargin=0;}
		return nMargin;
	}
	
	//各DOM要素のサイズ取得
	function getSize(){
		docHeight = $(window).height();
		hdHeight = header.height();
		gnHeight = gnav.height();
		ftHeight = footer.height();
		ctHeight = content.height();
		cnHeight = contain.height();
	}

	//ウインドウリサイズ時の処理
	$(window).resize(function(){
		if(content.css('display')=='none'){
			currentPos = clPositon();
		} else {
			currentPos = opPositon();
		}
		contain.css('top',currentPos);	
	})
})

