

// GlayLayer コンストラクタ
/**
 * GlayLayer
 * @param {jQuery} gl jQuery オブジェクト
 */
function GlayLayer(gl) {
	this.gl = gl;
	// for IE 8
	this.gl.fadeTo(0,0);
}
// GlayLayer クラスメソッド
GlayLayer.toString = function () {
	return "GlayLayer";
}
// GlayLayerクラス プロトタイプ
GlayLayer.prototype ={
	// インスタンスメソッド
	addEventListener:function (str,fn) {
		this.gl.bind(str,fn);
	},
	removeEventListener:function (str,fn) {
		this.gl.unbind(str,fn);
	},
	fadeToShow:function (callback) {
		this.setWindowSize();
		this.gl.css("display","block");
		this.gl.fadeTo("normal",0.75,callback);
	},
	fadeToHide:function () {
		this.gl.fadeTo("normal",0,function(){
			$(this).css("display","none");
		});
	},
	setTop:function () {
		this.gl.css("top",$(document).scrollTop()+"px");
	},
	setExpression:function (prop,value) {
		this.gl.get(0).style.setExpression(prop,value);
	},
	setWindowSize:function(){
		this.gl.css({
			height:$(window).height()+"px",
			width:$(window).width()+"px"
		});
	},
	stopAnimation:function () {
		this.gl.stop();
	}
}


// OverLayer コンストラクタ
/**
 * OverLayer
 * @param {jQuery} ol jQuery オブジェクト
 */
function OverLayer(ol) {
	this.ol = ol;
}
// OverLayer クラスメソッド
OverLayer.toString = function () {
	return "OverLayer";
}
// OverLayerクラス プロトタイプ
OverLayer.prototype ={
	// インスタンスメソッド
	fadeToShow:function (w,h,callback) {
		this.ol.css({
			marginLeft:"-"+w/2+"px",
			marginTop:"-"+h/2+"px"
		});
		this.ol.css("display","block");
		// for IE 6
		this.ol.fadeTo(0,1);
		// for IE 8
		this.ol.animate({
			width:w+"px",
			height:h+"px"
		},"normal",null,callback);
	},
	resetInvisible:function () {
		this.ol.css({
			display:"none",
			width:"0px",
			height:"0px"
		});
	},
	resetVisible:function () {
		this.ol.css({
			display:"block",
			width:"0px",
			height:"0px"
		});
		// for IE 6
		this.ol.fadeTo(0,0);
	},
	adjustWH:function (w,h,callback) {
		this.ol.animate({
			marginLeft:"-"+w/2+"px",
			marginTop:"-"+h/2+"px",
			width:w+"px",
			height:h+"px"
		},"normal",null,callback);
	},
	setTop:function () {
		this.ol.css("top",($(document).scrollTop()+$(window).height()/2)+"px");
	},
	setExpression:function (prop,value) {
		this.ol.get(0).style.setExpression(prop,value);
	},
	stopAnimation:function () {
		this.ol.stop();
	}
}


// Anchor コンストラクタ
/**
 * Anchor
 * @param {jQuery} a jQuery オブジェクト
 */
function Anchor(a) {
	this.a = a;
}
// Anchorクラス プロトタイプ
Anchor.prototype ={
	// インスタンスメソッド
	addEventListener:function (str,fn) {
		this.a.bind(str,fn);
	}
}


// Ifm コンストラクタ
/**
 * iframe
 * @param {jQuery} ifm jQuery オブジェクト
 */
function Ifm(ifm) {
	this.ifm = ifm;
	this.firstDiv;
	this.flg = false;
	this.first = true;
}
// Ifmクラス プロパティ
Ifm.LOADED = "loaded";
// Ifmクラス プロトタイプ
Ifm.prototype ={
	// インスタンスメソッド
	addEventListener:function (str,fn) {
		this.ifm.bind(str,fn);
	},
	loadHTML:function (href) {
		this.flg = true;
		this.ifm.css("display","block");
		this.ifm.attr("src",href);
		this.loadedHTML();
	},
	removeHTML:function () {
		this.ifm.attr("src","");
		this.ifm.css("display","none");
		this.first = true;
	},
	loadedHTML:function () {
		var scope = this;
		
		this.ifm.load(function () {
			if(scope.flg){
				var ifm = $(this);
				
				scope.firstDiv = ifm.contents().find('body > div:first-child');
				
				var thisWidth = scope.getContentsWidth();
				var thisHeight = scope.getContentsHeight();
				
				ifm.animate({
					width:thisWidth,
					height:thisHeight
				},"normal");
				
				// カスタムイベントの送出
				ifm.trigger(Ifm.LOADED,[thisWidth,thisHeight]);
			}
			scope.flg = false;
		});
	},
	getContentsWidth:function () {
		if(!this.firstDiv) return false;
		return this.firstDiv.outerWidth();
	},
	getContentsHeight:function () {
		if(!this.firstDiv) return false;
		return this.firstDiv.outerHeight();
	},
	reset:function () {
		// for IE 6
		this.ifm.css({
			width:"1px",
			height:"1px"
		});
	},
	stopAnimation:function () {
		this.ifm.stop();
	}
}


// CloseBtn コンストラクタ
/**
 * CloseBtn
 * @param {jQuery} a jQuery オブジェクト
 */
function CloseBtn(a) {
	this.a = a;
}
// CloseBtnクラス プロパティ
CloseBtn.CLASS_NAME = "Close"
// CloseBtnクラス プロトタイプ
CloseBtn.prototype ={
	// インスタンスメソッド
	addEventListener:function (str,fn) {
		this.a.bind(str,fn);
	},
	removeEventListener:function (str,fn) {
		this.a.unbind(str,fn);
	}
}


// NaviBtn コンストラクタ
/**
 * NaviBtn
 * @param {jQuery} a jQuery オブジェクト
 */
function NaviBtn(a) {
	this.a = a;
}
// NaviBtnクラス プロパティ
NaviBtn.CLASS_NAME = "Navi"
// NaviBtnクラス プロトタイプ
NaviBtn.prototype ={
	// インスタンスメソッド
	addEventListener:function (str,fn) {
		this.a.bind(str,fn);
	},
	removeEventListener:function (str,fn) {
		this.a.unbind(str,fn);
	}
}




// 実行
$(function () {
	// scrolling , frameborder は IE 8 対策
	$("body").append('<div id="' + GlayLayer.toString() + '"></div><div id="' + OverLayer.toString() + '"><iframe src="" frameborder="0" scrolling="no"></iframe></div>');
	
	var glayLayer = new GlayLayer($("#" + GlayLayer.toString()));
	var overLayer = new OverLayer($("#" + OverLayer.toString()));
	var anchor = new Anchor($("a.ModalWindow"));
	var iframe = new Ifm($("#" + OverLayer.toString()+" iframe"));
	var closeBtn;
	var naviBtn;
	
	anchor.addEventListener("click",start);
	iframe.addEventListener(Ifm.LOADED,loadComplete);
	
	function start() {
		// iframe のコンテンツサイズを取得するため。
		overLayer.resetVisible();
		iframe.reset();
		
		glayLayer.fadeToShow(glayLayerVisibled($(this).attr("href")));
		
		setWindow();
		
		return false;
	}
	
	function glayLayerVisibled(href) {
		return function () {
			iframe.loadHTML(href);
		}
	}
	
	function loadComplete(event,w,h){
		closeBtn = new CloseBtn(iframe.ifm.contents().find("."+CloseBtn.CLASS_NAME+" a"));
		naviBtn = new NaviBtn(iframe.ifm.contents().find("."+NaviBtn.CLASS_NAME+" a"));
		
		if(iframe.first){
			overLayer.fadeToShow(w,h,addEvent);
			iframe.first = false;
		}else{
			removeEvent();
			overLayer.adjustWH(w,h,addEvent);
		}
	}
	
	function addEvent(){
		glayLayer.addEventListener("click",closeLayer);
		closeBtn.addEventListener("click",clickCloseBtn);
		naviBtn.addEventListener("click",clickNaviBtn);
	}
	
	function removeEvent() {
		glayLayer.removeEventListener("click",closeLayer);
		closeBtn.removeEventListener("click",clickCloseBtn);
		naviBtn.removeEventListener("click",clickNaviBtn);
	}
	
	function clickCloseBtn() {
		closeLayer();
		return false;
	}
	
	function clickNaviBtn() {
		stopAllAnimation();
		
		iframe.loadHTML($(this).attr("href"));
	}
	
	function closeLayer() {
		removeEvent();
		stopAllAnimation();
		
		iframe.removeHTML();
		glayLayer.fadeToHide();
		overLayer.resetInvisible();
	}
	
	function setWindow() {
		// for IE 6
		if($.browser.msie && $.browser.version < 7){
			glayLayer.setTop();
			overLayer.setTop();
			
			$(window).scroll(function(){
				glayLayer.setExpression("top","$(document).scrollTop()+'px'");
				overLayer.setExpression("top","($(document).scrollTop()+$(window).height()/2)+'px'");
			});
			
			// ガタツキ防止
			$("html").css({
				backgroundImage:"url(null)",
				backgroundAttachment:"fixed"
			});
		}
		
		$(window).resize(function(){
			glayLayer.setWindowSize();
		});
	}
	
	function stopAllAnimation() {
		glayLayer.stopAnimation();
		overLayer.stopAnimation();
		iframe.stopAnimation();
	}
});





