﻿/**
*	swffit v2.0 (10/18/2008) <http://swffit.millermedeiros.com/>
*	Copyright (c) 2008 Miller Medeiros <http://www.millermedeiros.com/>
*	This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
var swffit = function(){
	var NS=(navigator.appName=='Netscape')?true:false,win=window,doc=document,html=doc.getElementsByTagName('html')[0],_ft,_mw,_mh,_xw,_xh,_hc,_vc;
	//ヘッダー分縮めるように変更
	var header_h = 0;
	function fit(t,w,h,xw,xh,hc,vc){
		_ft=t;
		_mw=w;
		_mh=h;
		_xw=xw;
		_xh=xh;
		_hc=(hc||hc==null)?true:false;
		_vc=(vc||vc==null)?true:false;
		swfobject.createCSS("#"+_ft,"position:absolute; width:100%; height:100%");
		swfobject.createCSS("#"+_ft+" object","position:absolute");
		swfobject.addDomLoadEvent(startFit);
	}
	function startFit(){
		html.style.height=doc.body.style.height='100%';
		html.style.overflow='auto';
		doc.body.style.margin=doc.body.style.padding='0';
		if(swfobject.getObjectById(_ft)){
			_ft=swfobject.getObjectById(_ft);
		}else if(NS){
			_ft=doc.getElementById(_ft).getElementsByTagName('object')[0];
		}else{
			_ft=doc.getElementById(_ft);
		}
		addResizeEvent(resize);
		swfobject.addDomLoadEvent(resize);
	}
	function stopFit(w,h){
		removeResizeEvent(resize);
		_ft.style.top=_ft.style.left='auto';
		_ft.style.marginTop=_ft.style.marginLeft='0';
		var w=(w==null)?'100%':w,h=(h==null)?'100%':h;
		setWidth(w);
		setHeight(h);
	}
	function setWidth(w){
		_ft.style.width=(isNaN(w))?w:w+"px";
	}
	function setHeight(h){
		_ft.style.height=(isNaN(h))?h:h+"px";
	}
	function addResizeEvent(fn){
		if(win.addEventListener){
			win.addEventListener('resize',fn,false);
		}else if(win.attachEvent){
			win.attachEvent('onresize',fn);
		}
	}
	function removeResizeEvent(fn){
		if(win.removeEventListener){
			win.removeEventListener('resize',fn,false);
		}else if(win.detachEvent){
			win.detachEvent('onresize',fn);
		}
	}
	function resize(){
		var iw=(NS)?win.innerWidth:doc.body.clientWidth,ih=(NS)?win.innerHeight:doc.body.clientHeight;
		if(iw>_xw&&_xw){
			setWidth(_xw);
			if(_hc){
				_ft.style.left='50%';
				_ft.style.marginLeft=-(_xw*.5)+'px';
			}
		}else{
			if(iw>_mw&&(iw<_xw||!_xw)){
				setWidth('100%');
			}else{
				setWidth(_mw);
			}
			if(_hc){
				_ft.style.left='auto';
				_ft.style.marginLeft='0';
			}
		}
		if(ih-header_h>_xh&&_xh){
			setHeight(_xh);
			if(_vc){_ft.style.top='50%';
				_ft.style.marginTop=-(_xh*.5)+'px';
			}
		}else{
			if(ih-header_h>_mh&&(ih-header_h<_xh||!_xh)){	//ヘッダー分調整
				//setHeight('100%');
				setHeight(ih-header_h);
			}else{
				setHeight(_mh);
			}
			if(_vc){
				_ft.style.top='auto';
				_ft.style.marginTop='0';
			}
		}
	}
	
	return{addResizeEvent:addResizeEvent,removeResizeEvent:removeResizeEvent,stopFit:stopFit,fit:fit}
}();
