/* 
 * 表示用スクリプト
 *  fadeout.js 使用 i = i.replace(/\w{3}/,"");
 * #open,#base,dataPrv,#expData,#close,#bg
 * file の読み込み設定：拡張子が「.dsm」の読み込み元ファイル内、idが「expData」内のコンテンツを
 * 表示先ファイルのID「controlPane」内へ追加する。
 * tableForm -> controlPane if suffix eq dsp
 *
 * ログサンプル↓
 * {{{file@title*2@text}}}
 *
 * HTML変換サンプル↓
 * <a style="##" title="title*2" href="../americanIdlSwf/file.swf" rel="class" rev="maincontent" class="swf newPW">
		<span style="##" class="refPoint">
			<span class="captionMov" style="##">text</span><span class="arrowSwf" style="##">&nbsp;</span>
		</span>
	</a>
* 
 * {{{url@logfile.imp@mode@elname}}}
* 
* 
<div class="btnTypBox"><a href="$logfile.imp" title="url" rel="mode" rev="elname" class="imp"><span class="btnBgFw"></span><span class="btnBgMd">$name</span><span class="btnBgBk"></span></a></div>
* 
*/
 
$(document).ready(function() {

	$( "#titleBtm").hover(
		function() {
			$("#mdfiButton").css("display","block");
		},
		function() {
			$("#mdfiButton").css("display","none");
		}
	);
	$("a.newPW").bind("click", function(event){
		event.preventDefault();
		var a_title = $(this).attr("title")? $(this).attr("title") :$(this).children("span").html();
		var href = $(this).attr("href");
		var a_mode = $(this).attr("rel");
		var a_elname = $(this).attr("rev");
		var suffix =  href.replace(/\?.+$/,"");/*cgiの引数削除*/
		suffix.match(/.+\.(.[^\.]+)?$/) ;/**/
		suffix = RegExp.$1?RegExp.$1:"html";
		href =  href.replace(/\.imp$/,"");
		var query ={
			data:href,
			title:a_title,
			mode:a_mode,
			elname:a_elname
		};
		callback[suffix](query);
	});
});

var callback = { 
	dsm : function(query) { 	
		query.data += " #expData";
		var info ={ "data" : query.data}
		prvInfo(info,"imp");
	},
	dsp : function(query) { 	
		query.data = query.data.replace(/dsp/,"cgi") + " #loginForm";
		var info ={
			"data" : query.data,
			"width" : 600,
			"height": 800
		}
		prvInfo(info,"imp");
	},
	jpg : function(query) { 	
		$.getJSON("../contents/prvImg.cgi",{name:query.data,title:query.title}, prvInfo);
	},
	dsf : function(query) { 
		$.getJSON("../americanIdlSwf/expJS.cgi",{name:query.data,title:query.title}, prvInfo);
	},
	swf : function(query) { 
		$.getJSON("../americanIdlSwf/expJS.cgi",{name:query.data,title:query.title}, prvInfo);
	},
	zip : function(query) { 
		$.getJSON("../contents/pwch.cgi",prvInfo);
	},
	html : function(query) { 	
		var features = "scrollbars=yes,resizable=yes";
		window.open(query.data,"sub",features);
	},
	imp : function(query) { 	
		$.getJSON("../contents/imp_html.cgi",{url:query.title,logfile:query.data,mode:query.mode,elname:query.elname,type:'abs'}, prvInfo);
	},
	sav : function(query) { 	
		$.getJSON("../contents/imp_html.cgi",{url:query.title,logfile:query.data,mode:query.mode,elname:query.elname}, prvInfo);
	}
}
				
function prvSnd(query){
	$("#sndCnt h2").replaceWith(query.data);
	//return false;
}

function prvInfo(query,type){

	var easTypeW ="easeInCirc";
	var scroll_y = $(window).scrollTop() + 10;
	scroll_y = scroll_y + "px";
	var w = query.width;
	var img_h = query.height;
	var fullWidth = document.body.clientWidth + "px";
	var fullHeight = document.body.clientHeight + $("html").height()+ "px";
	if (query.path){query.path = query.path + ".html";}
	//var fullHeight = document.body.clientHeight + "px";
	var cssObj = {
		width:fullWidth,
		height:fullHeight
	}
	$("body").append("<div id='bgBlack'></div><div id='bgWhiteBase'><div id='bgWhite'></div></div>");
	$("#bgWhiteBase").css(cssObj);
	$("#bgBlack").css(cssObj).animate(
		{opacity: 0.8}, 
		{
			duration:2,
			easing:easTypeW,
			complete:function(){
			
				$(this).css("display","block");
				$("#bgWhite").css("marginTop", scroll_y)
				.animate(
					{height: img_h}, 
					{
						duration:800,
						easing:easTypeW,
						complete:function(){
							$(this)
							.animate(
								{width:w},
								{
									duration:800,
									easing:easTypeW,
									complete:function(){
										/*imp:loginForm*/
										if(type == "imp"){
											$("#bgWhite").load(query.data,loginPane);/*loginPane:callBackFunc*/
										}else{
											$("#bgWhite").append(query.data).css("height","auto");
											controlPane();
											if(query.fileTitle == "impCaption"){
												$("#info").load(query.path + " #caption").css("height","auto");/*loginPane:callBackFunc*/
											}
										}
									}
								}
							);
						}
					}
				);
			}
		});
}
	
function controlPane(){
	$("#bgWhite").append("<div id='close' style='width:70px;height:22px;display:block'>CLOSE</div>");
	$("#close").click(function() {
		$("#bgBlack,#bgWhiteBase").remove();
		return false;
	});
}

function loginPane(){

	$("#bgWhite").append("<div id='close'>CLOSE</div>");
	var full_height = $("#loginForm").height();
	$("#bgWhite").css("height", full_height);
	$("#loginForm").validate();
	$("#close").click(function() {
		$("#bgBlack,#bgWhiteBase").remove();
		return false;
	});
}

