//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function showFlash(objLink){
	//削除
	hideFlash();
	var today = new Date();
	var time = today.getTime();
	var fileName=objLink.href;
	var fileType=objLink.type;
	var domain = "http://"+document.domain;
	var temp="M_CODE="+fileName+"&DOMAIN="+domain;
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var objBody = document.body;
	var objOverlay = document.createElement("div");
	//var myTag= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="overlayFlash" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="preview.swf?' + time + '" /><param name="quality" value="high" /><param name="salign" value="lt" /><param name="FlashVars" value="'+temp+'" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="preview.swf?' + time + '" FlashVars="'+temp+'" quality="high" salign="lt" swLiveConnect=true wmode="transparent" bgcolor="#ffffff" width="100%" height="100%" name="overlayFlash" id="overlayFlash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	var myTag= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="overlayFlash" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="./flash/Preview/preview.swf?' + time + '" /><param name="quality" value="high" /><param name="salign" value="lt" /><param name="FlashVars" value="'+temp+'" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="./flash/Preview/preview.swf?' + time + '" FlashVars="'+temp+'" quality="high" salign="lt" swLiveConnect=true wmode="transparent" bgcolor="#ffffff" width="100%" height="100%" name="overlayFlash" id="overlayFlash" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	var tarHeight = 500;
	var tarWidth = 450;
	var nameList01=fileName.split(",");
	var nameList02=nameList01[0].split("/");
	var mType=nameList02[nameList02.length-1].substring(0,2);
	if(mType!="BM"&&mType!="SE"&&mType!="VO")
	{
		tarHeight=500;
	}
	else
	{
		tarHeight=160;
	}
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.display = 'block';
	objOverlay.style.position = 'absolute';
	objOverlay.style.zIndex = '300';
	//
 	objOverlay.style.width 	= (tarWidth+'px');
	objOverlay.style.height = (tarHeight+'px');
	var _cur01 = (document.body.scrollTop||document.documentElement.scrollTop);
	objOverlay.style.top 	= (_cur01+'px');
	var _cur02 = (document.body.scrollLeft||document.documentElement.scrollLeft);
	objOverlay.style.left 	= (_cur02+'px');
	objOverlay.innerHTML = myTag;
	objBody.insertBefore(objOverlay, objBody.firstChild);
	objOverlay.style.display = 'block';
	setting();
}
function AdjustmentPos()
{
	var arrayPageSize = getPageSize();
	var el= document.getElementById("overlay");
	//縦
	var cur01 = (document.body.scrollTop||document.documentElement.scrollTop);
	var myTop = el.offsetTop;
	var d01 = cur01-myTop;
	el.style.top = (myTop+d01+'px');
	//横
	var cur02 = (document.body.scrollLeft||document.documentElement.scrollLeft);
	var myLeft = el.offsetLeft;
	var d02 = cur02-myLeft;
	el.style.left = (myLeft+d02+'px');
}
function setting()   
{
        try {   
		window.addEventListener('scroll', scrollEvent, false);
        } catch (e){   
		window.attachEvent('onscroll',scrollEvent);  
        }  
	function resizeCheck(myEvents)
	{
		AdjustmentPos();
	}
	window.onresize = resizeCheck
}
function scrollEvent()   
{
	AdjustmentPos()
    	try {   
        	window.addEventListener('load', setting, false);   
    	} catch (e) {   
        	window.attachEvent('onload', setting);   
    	}
}
function hideFlash()
{
	var el= document.getElementById("overlay");
	if(el!=null)
	{
		var objBody = document.body;
		objBody.removeChild(objBody.firstChild);
	}
}
function initoverLayFlash(){
	if (!document.getElementsByTagName){ return; }
	
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "overLay")){
				

			anchor.onclick = function () 
			{
				showFlash(this);
				/*
				var id=PlugIn_Detection("Flash");
				
				if(PlugIn_Detection("Flash")=="true")
				{
					showFlash(this);
				}
				else alert(" no flash");
				*/
				return false;
				
			}
		}
		
	}
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}
addLoadEvent(initoverLayFlash);
function PlugIn_Detection(tartget)
{
//Flash
//Windows Media Player
//Java
//Shockwave
//RealPlayer
//QuickTime
//Acrobat Reader
//SVG Viewer
	var Re="false";
	var agt=navigator.userAgent.toLowerCase();
	var ie  = (agt.indexOf("msie") != -1);
	var ns  = (navigator.appName.indexOf("Netscape") != -1);
	var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
	var mac = (agt.indexOf("mac")!=-1);

	if (ie && win) {	pluginlist = detectIE("Adobe.SVGCtl","SVG Viewer") + detectIE("SWCtl.SWCtl.1","Shockwave Director") + detectIE("ShockwaveFlash.ShockwaveFlash.1","Shockwave Flash") + detectIE("rmocx.RealPlayer G2 Control.1","RealPlayer") + detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") + detectIE("PDF.PdfCtrl.5","Acrobat Reader"); }
	if (ns || !win) {
			nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
			pluginlist = detectNS("image/svg-xml","SVG Viewer") + detectNS("application/x-director","Shockwave Director") + detectNS("application/x-shockwave-flash","Shockwave Flash") + detectNS("audio/x-pn-realaudio-plugin","RealPlayer") + detectNS("video/quicktime","QuickTime") + detectNS("application/pdf","Acrobat Reader");
	}

	function detectIE(ClassID,name) { result = false; document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n'); if (result) return name+','; else return ''; }
	function detectNS(ClassID,name) { n = ""; if (nse.indexOf(ClassID) != -1) if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; return n; }

	pluginlist += navigator.javaEnabled() ? "Java," : "";
	if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);

	if (pluginlist.indexOf(tartget)!=-1)
	//if (pluginlist.indexOf(tartget)==-1)
	{
		Re="true";
	}
	return Re;
}
