Timer = Array();
Timer['GradateOpacity'] = null;

function JavaScriptGateway(){
	SetTopNomble();
	SetBottomNomble();
}

function createXMLHttpRequest(){
	if(window.XMLHttpRequest){
		httpObj = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			httpObj = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			httpObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return httpObj;
}

function SummonNoticeWindow(TargetLayerID,Is_Summon){
	var TargetLayer = document.getElementById(TargetLayerID);
	var BodyLayer = document.getElementById('TOP');
	if(Is_Summon){
		PreLoadData = new Array('../../image/Spacer_000000_60.png','../../image/BG_NoticeWindow.png');
		for(i=0;i<PreLoadData.length;i++){
			PreLoadData[i] = new Image();
		}

		BGLayer = document.createElement('div');
		BGLayer.setAttribute("id","BGLayer");
		BodyLayer.appendChild(BGLayer);
		BGLayer.style.height = '100%';
		BGLayer.style.width = '100%';
		BGLayer.style.position = 'absolute';
		BGLayer.style.zIndex = 10009;
		BGLayer.style.left = '0px';
		var TEMP = document.body.scrollTop || document.documentElement.scrollTop;
		BGLayer.style.top = TEMP + 'px';
		BGLayer.style.backgroundImage = 'url(../image/Spacer_000000_60.png)';

		if(Timer['GradateOpacity'] == null){
			GradateOpacity(TargetLayer,0,100,25,15);
		}else{
			GradateOpacity(TargetLayer,100,100,0);
		}
		TargetLayer.style.top = (TEMP + GetInnerSize('Height') / 2) + 'px';
		TargetLayer.style.zIndex = 10010;
		TargetLayer.style.display = 'block';
	}else{
		GradateOpacity(TargetLayer,100,0,-20,15,true);
//		BGLayer.style.zIndex = 0;
//		BGLayer.style.position = 'relative';
//		BGLayer.style.backgroundImage = 'none';
		BodyLayer.removeChild(BGLayer);
	}
}

function SetBG(BGPlaceID,ImageID,BGPosition,Is_DisplayBG){
	var BGPlace = document.getElementById(BGPlaceID);
	BGPlace.style.backgroundRepeat = 'no-repeat';
	BGPlace.style.backgroundPosition = 'center';
	BGPlace.style.backgroundAttachment = 'fixed';
	if(Is_DisplayBG){
		var Image = document.getElementById(ImageID);
		BGPlace.style.backgroundImage = Image.style.backgroundImage;
		switch(BGPosition){
			case 1 : backgroundPosition = '95%';break;
			case 2 : backgroundPosition = 'center';break;
			case 3 : backgroundPosition = '5%';break;
		}
		BGPlace.style.backgroundPosition = backgroundPosition;
		if(Timer['GradateOpacity'] == null){
			if(CheckModernBlowser() == 2){
				Speed = -15;
				GSecond = 25;
			}else{
				Speed = -50;
				GSecond = 10;
			}
			GradateOpacity(BGPlace,100,3,Speed,GSecond);
		}else{
			GradateOpacity(BGPlace,100,100,0);
		}
	}else{
		BGPlace.style.backgroundImage = 'none';
		if(Timer['GradateOpacity'] == null){
			if(CheckModernBlowser() == 2){
				Speed = 50;
				GSecond = 20;
			}else{
				Speed = 150;
				GSecond = 10;
			}
			GradateOpacity(BGPlace,3,100,Speed,GSecond);
		}else{
			GradateOpacity(BGPlace,100,100,0);
		}
		BGPlace.style.filter = '';
	}
	return true;
}

function OpenLayer(LayName){
	TargetLayer = document.getElementById(LayName);
	TargetLayer.style.display = (TargetLayer.style.display == "block") ? "none" : "block";
}

function OpenMainFormOption(){
	TargetLayer = document.getElementById('MainFormOption_Detail');
	TargetLayer.style.display = (TargetLayer.style.display == "block") ? "none" : "block";
	TargetLayer = document.getElementById('MainFormOption_Console');
	TargetLayer.style.display = (TargetLayer.style.display == "none") ? "block" : "none";
}

function InputNowFocus(TargetLayer,Is_Blur){
	if(Is_Blur){
		TargetLayer.style.fontWeight = 'normal';
		TargetLayer.style.letterSpacing = '0.1em';
	}else{
		TargetLayer.style.fontWeight = 'bold';
		TargetLayer.style.letterSpacing = '0px';
		if(TargetLayer.value == TargetLayer.defaultValue){
			TargetLayer.value = '';
		}
	}
}

function GradateOpacity(TargetLayer,OpacityPercent,EndPercent,Speed,GSecond,Is_DisplayNone,Is_Rgba){
	if(Speed == 0){
		TargetLayer.style.opacity = EndPercent / 100;
		TargetLayer.style.MsFilter = '"progid:DXImageTransform.Microsoft.Alpha(opacity=' + EndPercent + ')"';
		TargetLayer.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + EndPercent + ')';
		if(Timer['GradateOpacity']){
			clearTimeout(Timer['GradateOpacity']);
			window.Timer['GradateOpacity'] = null;
		}
		return true;
	}
	if(!OpacityPercent){
		OpacityPercent = 1;
	}
	OpacityPercent *= 1 + Speed / 100;
	if(OpacityPercent > 100){
		OpacityPercent = 100;
	}
	FilterOpacity = Math.floor(OpacityPercent);
	CSSOpacity = FilterOpacity / 100;

	if(!Is_Rgba){
		TargetLayer.style.opacity = CSSOpacity;
		TargetLayer.style.MsFilter = '"progid:DXImageTransform.Microsoft.Alpha(opacity=' + FilterOpacity + ')"';
		TargetLayer.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + FilterOpacity + ')';
	}else{
		// NotWork...
		TargetLayer.style.backgroundColor = 'rgba(' + Is_Rgba + ',' + CSSOpacity + ')';
	}

	if((Speed < 0 && FilterOpacity > EndPercent) || (Speed > 0 && FilterOpacity < EndPercent)){
		window.Timer['GradateOpacity'] = setTimeout(function(){GradateOpacity(TargetLayer,OpacityPercent,EndPercent,Speed,GSecond,Is_DisplayNone,Is_Rgba)},GSecond);
		return false;
	}else{
		clearTimeout(Timer['GradateOpacity']);
		window.Timer['GradateOpacity'] = null;
		if(Is_DisplayNone && EndPercent == 0){
			TargetLayer.style.display = 'none';
		}
		return true;
	}
}

function CheckModernBlowser(){
	if(window.XMLHttpRequest){
		if(window.ActiveXObject){
			// IE 7
			return 1;
		}else{
			// Opera, Safari, Firefox
			return 2;
		}
	}else{
		//IE 6 and below
		return 0;
	}
}

function SetTopNomble(){
	var TargetLayer = document.getElementById('HeaderColumns');
	TargetLayer.style.position = 'fixed';
	TargetLayer.style.top = '0px';
	TargetLayer.style.zIndex = 10000;
	TargetLayer.style.backgroundColor = '#ffffff';
}
function SetBottomNomble(){
	var TargetLayer = document.getElementById('BottomNomble');
	TargetLayer.style.position = 'fixed';
	TargetLayer.style.top = (GetInnerSize('Height') - 48) + 'px';
	TargetLayer.style.zIndex = 10001;
	TargetLayer.style.backgroundColor = '#ffffff';
}

function GetInnerSize(Mode){
	if(document.all){
		if(Mode == 'Width'){
			InnerSize = document.documentElement.clientWidth;
			if(CheckModernBlowser() == 1){
				InnerSize -= 18;	// For IE7
			}
		}else{
			InnerSize = document.documentElement.clientHeight;
		}
	}else{
		if(Mode == 'Width'){
			InnerSize = window.innerWidth;
			if(CheckModernBlowser() == 2){
				InnerSize -= 18;	// For FireFox
			}
		}else{
			InnerSize = window.innerHeight;
		}
	}
	if(!InnerSize){
		if(Mode == 'Width'){
			InnerSize = 1024;
		}else{
			InnerSize = 480;
		}
	}
	return InnerSize;
}

function InitializeCommentForm(TargetLayer){
	if(!(TargetLayer.value && TargetLayer.defaultValue)){
		return true;
	}
	if(TargetLayer.value == TargetLayer.defaultValue){
		TargetLayer.value = "";
	}
	return true;
}

function SubmitForm(TargetLayer,ActionURL){
	TargetLayer.action = ActionURL;
	return true;
}

function OpenCopyStrings(TargetLayer){
	TargetLayer2 = TargetLayer.parentNode;
	TargetLayer3 = TargetLayer2.getElementsByTagName('ins');
	TargetLayer3[0].style.display = 'inline';
	TargetLayer4 = TargetLayer2.getElementsByTagName('del');
	TargetLayer4[0].style.display = 'none';
	TargetLayer.style.display = 'none';
	if(typeof(clipboardData) == 'defined' || typeof(clipboardData) == 'object'){
		clipboardData.setData('text',TargetLayer3[0].innerHTML);
		alert("クリップボードへのコピーも行いました");
	}else{
		alert("以下の文字列をコピーしてご利用ください:\n\r" + TargetLayer3[0].innerHTML);
	}
	return true;
}

function SetBookMarklet(Mode,Option){
	if(Mode == 'Yahoo'){
		var URL = 'http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t=' + encodeURIComponent(document.title) + '&u=' + encodeURIComponent(window.location.href) + '&opener=bm&ei=UTF-8';
	}else if(Mode == 'Hateb'){
		var URL = 'http://b.hatena.ne.jp/entry/' + window.location.href;
	}else if(Mode == 'WebGyotaku'){
		var URL = 'http://megalodon.jp/?url=' + encodeURIComponent(window.location.href);
	}else if(Mode == 'Google'){
		var URL = 'http://www.google.com/bookmarks/mark?op=add&bkmk=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title);
	}else if(Mode == 'Twitter'){
		if(Option){
			if(!(n = (document.selection)?document.selection.createRange().text:(window.getSelection)?window.getSelection():(document.getSelection)?document.getSelection():'') || typeof(n) == 'object'){
				n= 'サークル『' + Option + '』、はじめました。';
			}
			var httpObj;
			var TargetProgram = "../api/internal/MakeBitlyURL.php";
			var ShortURL = window.location.href;
			if(httpObj = createXMLHttpRequest()){
				httpObj.open("post",TargetProgram,false);
				httpObj.setRequestHeader("content-type","application/x-www-form-urlencoded;charset=UTF-8");
				httpObj.send("LongURL=" + encodeURI(ShortURL).replace(/&/g,"%26"));
				if ((httpObj.readyState == 4) && (httpObj.status == 200)){
					var ShortURL = httpObj.responseText;
				}
			}
		}else{
			n = '同人サークル名をランダム生成します!';
			ShortURL = 'http://www.doujinbu.com/cng/';
		}
		var URL = 'http://twitter.com/home/?status=' + encodeURIComponent(n + ' ≫ ' + ShortURL + ' #CircleNameGenerator ');
		// alert(URL);
	}
	void window.open(URL,'popup','width=720px,height=480px,status=1,location=0,resizable=1,scrollbars=1,left=' + ((GetInnerSize('Width') - 720) / 2) + ',top=' + ((GetInnerSize('Height') - 480) / 2),0);
	return true;
}
