/*====================================================================================================
//////////////////////////////////////////////////////////////////////////////////////////////////////

 author : http://www.metaphase.co.jp
 created: 2008/04/05
 update : 2009/02/06
 
 Licensed under the GNU Lesser General Public License version 2.1
 
 リンク先のHTML文書など body 要素内のソースを現在のページ内にモーダルウインドウとして表示する
 
 [how to open the modal-window]
 <a href="../mw.html" class="show-detail">open modalwindow</a>
                               or
 <a href="javascript:yomotsuModalWindow.set('../mw.html');">open modalwindow</a>

 [how to replace the modal-window]
 <a href="javascript:yomotsuModalWindow.replace('../mw2.html');">open modalwindow</a>
 
 [how to close the modal-window]
 <a href="javascript:yomotsuModalWindow.remove();">close</a></p>

//////////////////////////////////////////////////////////////////////////////////////////////////////
====================================================================================================*/

var yomotsuModalWindow = {

	conf : {
		triggerClassName       : "show-detail", // クリックしたときウインドウを開く a 要素につける class 属性
		//displayBasePositionTop : 150, // ( px )
		displayBaseMaxWidth    : 936, // ( px )
		backgroundMaxOpacity   : 0.70 // [ 0.00 ~ 1.00 ]
	},
	
	start : function(){
		try {
			window.addEventListener('load', this.trigger, false);
		} catch (e) {
			window.attachEvent('onload', this.trigger);
		}
	},
	
	trigger : function(){
	var a = document.links;
		for(var i=0; i<a.length; i++){
			if(new RegExp("\\b" + yomotsuModalWindow.conf.triggerClassName + "\\b").exec(a[i].className)){
				a[i].onclick = function(){
					if(this.href.match(/\..*html?|\.xml|\.php|\.asp/)){
						yomotsuModalWindow.set(this.href);
						return false;
					}
				}
			}
		}
	},

	set : function(URI){
		if(document.getElementById("display-base")) return;
		
		var displayBase = document.createElement("div");
		displayBase.id = "display-base";
		displayBase.style.position = "absolute";
		displayBase.style.zIndex   = "100";
		displayBase.style.top      = '-9999px';
		displayBase.style.left     = '-9999px';
		displayBase.style.width    = yomotsuModalWindow.conf.displayBaseMaxWidth + "px";
		document.body.appendChild(displayBase);
		background.set();
		
		this.getHTMLSrc(URI);
	},
	
	replace : function(URI){
		this.getHTMLSrc(URI);
	},
	
	getHTMLSrc : function(URI){
		var req = window.XMLHttpRequest ? new XMLHttpRequest() : (function() {
				try      { return new ActiveXObject("Msxml2.XMLHTTP");    }
				catch(e) { return new ActiveXObject("Microsoft.XMLHTTP"); }
		})();
		
		req.onreadystatechange = function(){ 
			if (req.readyState != 4 ) return ;
			if (req.status == 200 || req.status==0) { 
				
				var resText = req.responseText;
				if ( navigator.appVersion.indexOf( "KHTML" ) > -1 ) {
					var esc = escape( resText );
					if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
						resText = decodeURIComponent( esc );
					}
				}
				
				if(resText.match(/<body.*>/)){
					var htmlBodyStart = resText.indexOf("<body")+(resText.match(/<body.*>/)+"").length;
					var htmlBodyEnd   = resText.indexOf("</body>");
					resText = resText.slice(htmlBodyStart,htmlBodyEnd);
				}
				document.getElementById("display-base").innerHTML = resText;
				yomotsuModalWindow.alignment();
				background.fix()
			}
			else{
				if(document.getElementById("display-base").childNodes){
					var displayBase = document.getElementById("display-base");
					displayBase.innerHTML = "<p style='background:#fff;'>loading...</p>";
					yomotsuModalWindow.alignment();
				}
			}
		}
		req.open('GET', URI, true); 
		req.send(null);
	},
	
	alignment : function(){
		var i, childWidth, contentMaxWidth = 0, displayBase = document.getElementById("display-base");
		
		for( i=0 ; i<displayBase.childNodes.length ; i++){
			childWidth = displayBase.childNodes[i].offsetWidth;
			contentMaxWidth = contentMaxWidth < childWidth ? childWidth : contentMaxWidth ;
		}
		var displayBaseWidth    = Math.min(yomotsuModalWindow.conf.displayBaseMaxWidth, contentMaxWidth) ;
		var displayBaseTop      = Math.max(getPageOffset().yOffset + Math.floor(getClientSize().height / 2 - displayBase.offsetHeight / 2),getPageOffset().yOffset);
		displayBase.style.width = displayBaseWidth + "px";
		displayBase.style.top   = displayBaseTop + 'px';
		displayBase.style.left  = getPageSize().width / 2 - ( displayBaseWidth / 2) + "px";	
	},
	
	remove : function(){
		var displayBase = document.getElementById("display-base");
		if(displayBase)
			document.body.removeChild(displayBase);
		background.remove();
	}
	
}


yomotsuModalWindow.start();


//-----------------------------------------------------
//  get page size
//-----------------------------------------------------

var getPageSize = function(){
	var w1=w2=h1=h2=0, width, height;
			
	if (document.documentElement) {
		w1 = document.documentElement.scrollWidth;
		h1 = document.documentElement.scrollHeight;
	}
	
	if (document.body) {
		w2 = document.body.scrollWidth;
		h2 = document.body.scrollHeight;
	}
	
	var width  = Math.max(w1, w2);
	var height = Math.max(h1, h2);
		
	return {width: width, height: height};
}

//---------------------------

var getPageOffset = function(){
	return {
		xOffset: document.body.scrollLeft || document.documentElement.scrollLeft,
		yOffset: document.body.scrollTop  || document.documentElement.scrollTop
	};

}

var getClientSize = function(){
	return {
		width : window.innerWidth ? window.innerWidth : ( document.documentElement && document.documentElement.clientWidth != 0 ) ? document.documentElement.clientWidth : document.body.clientWidth,
		height : window.innerHeight ? window.innerHeight : ( document.documentElement && document.documentElement.clientHeight != 0 ) ? document.documentElement.clientHeight : document.body.clientHeight
		};
}

//-----------------------------------------------------
//  background
//-----------------------------------------------------


var background = {
	conf : {
		id : "back-screen"
	},
	
	alpha : 0,
	
	set : function(){
		this.hideSelectElements(); // for IE 6 select-zIndex BUG	
		var bg = document.createElement("div");
	
		bg.id = this.conf.id;
		bg.style.position = "absolute";
		bg.style.left     = 0;
		bg.style.top      = 0;
		bg.style.zIndex   = 99;
		//bg.style.width    = Math.max(getPageSize().width, yomotsuModalWindow.conf.displayBaseMaxWidth) +"px";
		bg.style.width    = "100%";
		bg.style.height   = getPageSize().height +"px";
		bg.style.filter     ="alpha(opacity="+this.alpha*100+")";
		bg.style.MozOpacity =this.alpha;
		bg.style.opacity    =this.alpha;
		
		document.body.appendChild(bg);
		
		this.alpha = 0;
		this.fadeIn();
	},
	
	fadeIn : function(){
		var bg = document.getElementById(this.conf.id);
		this.alpha += 0.10;
		if(bg.style.opacity)        	bg.style.opacity    = this.alpha;
		else if(bg.style.filter)	    bg.style.filter     = "alpha(opacity="+this.alpha*100+")";
		if (this.alpha < yomotsuModalWindow.conf.backgroundMaxOpacity ) {
			bg.onclick = function(){return};
			window.setTimeout("background.fadeIn()", 25);
		}
		else {
			bg.onclick = yomotsuModalWindow.remove;
		}
	},
	
	fadeOut : function(){
		var bg = document.getElementById(this.conf.id);
		bg.onclick = function(){return};
		this.alpha -= 0.10;
		if(bg.style.opacity)        	bg.style.opacity    = this.alpha;
		else if(bg.style.filter)	    bg.style.filter     = "alpha(opacity="+this.alpha*100+")";
		if (this.alpha > 0.05) {
			window.setTimeout("background.fadeOut()", 20);
		}
		else{
			document.body.removeChild(bg);
			this.alpha = 0;
		}
	},
	
	remove : function(){
		var bg = document.getElementById(background.conf.id);
		if(bg)
			this.fadeOut();
		this.showSelectElements(); // for IE 6 select-zIndex BUG	
	},
	
	fix : function(){
		var bg = document.getElementById(background.conf.id);
		if(bg){
			//bg.style.width  = getPageSize().width  +"px";
			bg.style.width  = "100%";
			bg.style.height = getPageSize().height +"px";
		}
	},
	
	//-----------------------------------------------------
	//  select elements z-index BUG ( IE 6 )
	//-----------------------------------------------------
	
	hideSelectElements : function(){
		if ((typeof document.documentElement.style.zoom != "undefined")&&(typeof document.documentElement.style.msInterpolationMode == "undefined")){
			var selectEle = document.getElementsByTagName("select");
			for(i=0;i<selectEle.length;i++){
				selectEle[i].style.visibility = "hidden";
			}
			
			var selectEle2 = document.getElementById("display-base").getElementsByTagName("select")
			for(i=0;i<selectEle2.length;i++){
				selectEle[i].selectEle2.visibility = "visible";
			}
		}
	},
	
	showSelectElements : function (){
		if ((typeof document.documentElement.style.zoom != "undefined")&&(typeof document.documentElement.style.msInterpolationMode == "undefined")){
			var selectEle = document.getElementsByTagName("select");
			for(i=0;i<selectEle.length;i++){
				selectEle[i].style.visibility = "visible";
			}
		}
	}
	
}

try {
	window.addEventListener('resize', background.fix, false);
} catch (e) {
	window.attachEvent('onresize', background.fix);
}

