function getUrlActionPath(){
	var	appDir=pathRemoveFileSpec(g_app.path),fso=getFSO();
	return fso.BuildPath(appDir,"setting\\customize\\url_action.json");
}
function getCurrentUrlAction(){
	return parseJsonFile(getUrlActionPath());
}
function getUrlActionDisplayName(cmd){
	if(-1!==cmd.indexOf("script:")){
		return _getScriptDisplayName(cmd);
	}
	var	cmdCnt=g_urlActionCmds.length,i;
	for(i=0;i<cmdCnt;++i){
		var	acmd=g_urlActionCmds[i];
		if(acmd.cmd===cmd){
			return acmd.txt;
		}
	}
	return cmd;
}
function getUrlActionCmd(action){
	if(action.script){
		return "script:"+action.script;
	}
	if(action.action){
		return action.action;
	}
	return "none";
}
function initUrlActionCmd(options,event){
	var	cmdCnt=g_urlActionCmds.length,i;
	options.length=0;
	for(i=0;i<cmdCnt;++i){
		var	cmd=g_urlActionCmds[i];
		if(cmd.event.indexOf(event)!==-1){
			addOption(options,cmd.cmd,cmd.txt);
		}
	}
	
	// script
	var	scriptList=getScriptList(),scriptCnt=scriptList.count;
	for(i=0;i<scriptCnt;++i){
		var	filename=scriptList.filename(i);
		if(queryScriptAction(filename,SCRIPTACTION_URLACTION)){
			addOption(options,"script:"+filename,_getScriptDisplayName(filename));
		}
	}
}
function refreshUrlAction(){
	g_urlAction=getCurrentUrlAction();
	if(g_urlAction){
		_refreshUrlAction("idUrlActionBefore", g_urlAction.before,false);
		_refreshUrlAction("idUrlActionComplete", g_urlAction.complete,false);
	}
}
function _isEqualEventsLight(l,r){
	var	lOk=undefined!==l,rOk=undefined!==r;
	if(lOk!==rOk){
		return false;
	}
	if(lOk){
		if(l.length!==r.length){
			return false;
		}
	}
	return true;
}
function _isEqualEvents(l,r){
	if(l){
		var	len=l.length;
		for(i=0;i<len;++i){
			var	lEv=l[i],rEv=r[i];
			if( (lEv.url!==rEv.url)||
				(lEv.action&&rEv.script)||
				(lEv.script&&rEv.action)||
				(lEv.action!==rEv.action)||
				(lEv.script!==rEv.script)){
				return false;
			}
		}
	}
	return true;
}
function isEqualUrlAction(l,r){
	// check:    before complete
	// no check: emulation
	if( !_isEqualEventsLight(l.before,r.before)||
		!_isEqualEventsLight(l.complete,r.complete)||
		!_isEqualEvents(l.before,r.before)||
		!_isEqualEvents(l.complete,r.complete)){
		return false;
	}
	return true;
}
function _refreshUrlAction(contentId,actions,isAdd){
	var	content=$$(contentId);
	var	cnt=actions?actions.length:0,parent=$(content);
	if(!isAdd){
		parent.empty();
	}else{
		// remove "empty" content
		if(3==content.firstChild.nodeType){
			parent.empty();
		}
	}
	if(cnt){
		var	c=document.createElement
			,textInputBase=$(c("<input type=text name=v class='cUrlActionInput cInline'>"))
			,selectCmdBase=$(c("<select name=v class='cUrlActionCmd cInline'>"))
			,btnDelBase=$(c("<button>"))
			,btnUpBase=btnDelBase.clone()
			,btnDownBase=btnDelBase.clone();
		btnDelBase.append($("<span>削除</span>")).addClass("cUrlActionDel");
		btnUpBase.append($("<span></span>")).addClass("cUrlActionUp");
		btnDownBase.append($("<span></span>")).addClass("cUrlActionDown");
		for(i=0;i<cnt;++i){
			
			var	action=actions[i];
			var	data={contentId:contentId,action:action}
				,div=$(c("<div>")).text(": ").attr("id",contentId+i)
				,formInput=$(c("<form name="+contentId+"Input"+i+" class=cInline>"))
				,textInput=textInputBase.clone();
			div.data("action",data);
			
			var	formCmd=$(c("<form class='cDummy cInline'>"))
				,selectCmd=selectCmdBase.clone()
				,btnDel=btnDelBase.clone()
				,btnUp=btnUpBase.clone()
				,btnDown=btnDownBase.clone();
			selectCmd.change(function(ev){changeUrlActionCmd(ev.target)});
			if(isAdd){
				div.addClass("cAdd cHide");
				textInput.addClass("cMod");
				selectCmd.addClass("cMod");
			}
			textInput.val(action.url);
			
			var	optCmd=selectCmd.get(0).options
				,cmd=getUrlActionCmd(action);
			optCmd.length=1;
			optCmd[0].value=cmd;
			optCmd[0].text=getUrlActionDisplayName(cmd);
			div .append(formInput.append(textInput))
				.append(formCmd.append(selectCmd))
				.append(btnDel)
				.append(btnUp)
				.append(btnDown);
			
			if(isAdd&&parent.get(0).firstChild){
				$(parent.get(0).firstChild).before(div);
			}else{
				parent.append(div);
			}
			if(isAdd){
				div.slideDown("n");
				break;
			}
		}
	}else{
		parent.text("なし");
	}
}
function addUrlAction(btn){
	var	parentNode=btn.parentNode
		,contentId=btn.contentId
		,formInput=document.forms[contentId+"Input"]
		,url=formInput.v.value;
	if(url.length){
		var	formCmd=document.forms[contentId+"Cmd"]
			,selCmd=formCmd.v.value
			,action={"url":url}
			,iScript=selCmd.indexOf("script:")
		if(-1!==iScript){
			 action.script=selCmd.substr(7);
		}else{
			action.action=selCmd;
		}
		_refreshUrlAction(contentId,[action],true);
		formInput.v.value="";
		formInput.v.focus();
		selectOption(formCmd.v.options,"none");
	}
	btn.disabled=true;
}
function delUrlAction(btn,action,isDel){
	var	parentNode=btn.parentNode;
	$(":input:not(button.cUrlActionDel,button.cUrlActionDelCancel)",parentNode).attr("disabled",isDel);
	action.action.isDel=isDel;
	$(":first",btn).text(isDel?"取消":"削除");
	$(btn).toggleClass("cUrlActionDel cUrlActionDelCancel");
}
function _contentToUrlAction(content,actions){
	while(content){
		var	action=$(content).data("action").action;
		if(action&&!action.isDel&&action.url){
			var	add={"url":action.url};
			if(action.script){
				add.script=action.script;
			}else{
				add.action=action.action;
			}
			actions.push(add);
		}
		content=content.nextSibling;
	}
}
function _hasUrlAction(content){
	return content.firstChild&&1==content.firstChild.nodeType;
}
function saveUrlAction(){
	// before
	var	before=$$("idUrlActionBefore"),urlAction={};
	if(_hasUrlAction(before)){
		urlAction.before=[];
		_contentToUrlAction(before.firstChild,urlAction.before);
	}
	
	// complete
	var	complete=$$("idUrlActionComplete");
	if(_hasUrlAction(complete)){
		urlAction.complete=[];
		_contentToUrlAction(complete.firstChild,urlAction.complete);
	}

	// compare current setting
	var	current=getCurrentUrlAction();
	if(current&&isEqualUrlAction(current,urlAction)){
		return false;
	}

	// browser emulation
	if(g_urlAction&&g_urlAction.emulation){
		urlAction.emulation=g_urlAction.emulation;
	}

	var	json=JSON.stringify(urlAction,undefined," ");
	writeUnicodeTextFile(getUrlActionPath(), json);
	return true;
}
function changeUrlActionCmd(elem){
	$(elem).addClass("cMod");
	var	action=$.data(elem.parentNode.parentNode,"action");
	if(action){
		var	value=elem.value;
		action=action.action;
		if(value.indexOf("script:")!==-1){
			action.action=undefined;
			action.script=value.substr(7);
		}else{
			action.script=undefined;
			action.action=value;
		}
	}
}
$("button.cUrlActionAdd:enabled").live("click", function(ev){
	addUrlAction(ev.target);
});
$("button.cUrlActionUp,button.cUrlActionDown").live("click",function(ev){
	var	parent=ev.target.parentNode
		,swp="cUrlActionDown"===ev.target.className?
		parent.nextSibling:parent.previousSibling;
	if(swp){
		swp.swapNode(parent);
	}
});
$("button.cUrlActionDel,button.cUrlActionDelCancel").live("click",function(ev){
	var	isDel="cUrlActionDel"===ev.target.className
		,action=$.data(ev.target.parentNode,"action");
	delUrlAction(ev.target,action,isDel);
});
$("input.cUrlActionInput").live("focus blur",function(ev){
	var	focusin="focusin"===ev.type;
	g_ext.preventTransAccel(focusin);
	if(!focusin){
		var action=$.data(ev.target.parentNode.parentNode,"action");
		if(action){
			action=action.action;
			if(action.url!=ev.target.value){
				action.url=ev.target.value;
				$(ev.target).addClass("cMod");
			}
		}
	}
	return true;
});
$("form.cDummy .cUrlActionCmd:enabled").live("mousedown focus",function(ev){
	var	select=ev.target,value=select.value
		,action=$.data(select.parentNode.parentNode,"action"),contentId;
	if(action){
		contentId=action.contentId;
	}else{
		contentId=select.parentNode.name;
	}
	var	event=(contentId.indexOf("Complete")===-1?"before":"complete");
	initUrlActionCmd(select.options,event);
	if(!selectOption(select.options,value)){
		addOption(select.options,value,getCmdDisplayName(value)).selected=true;
	}
	$(select.form).removeClass("cDummy");
	return false;
});
$("form[name=idUrlActionBeforeInput]").live("keydown",function(ev){
	$$("idAppUrlActionBeforeAdd").disabled=false;
});
$("form[name=idUrlActionCompleteInput]").live("keydown",function(ev){
	$$("idAppUrlActionCompleteAdd").disabled=false;
});
$("form[name=idUrlActionBeforeInput],form[name=idUrlActionCompleteInput]").live("focus blur",function(ev){
	toggleCueBanner(ev,g_cueBannerTxt[ev.target.form.name]);
});
(function(){refreshUrlAction();})();