<script type="text/javascript">//<![CDATA[
XHR.poll(5, '<%=luci.dispatcher.build_url("admin/nas/amule/status")%>', null,
	function(x, data) {
		var tb = document.getElementById('amule_status');
		var btn = document.getElementById("amule_startstop");
		var btn_tmpl = '<input class="cbi-button cbi-button-apply" id="btn_startstop" style="font-size: 100%;" value="PID: NUMBER"type="button" onclick="onclick_startstop(this.id)">'
		if (data && tb) {
			if (data.amuled) {
				var links = '<em><%:aMule daemon is running.%></em>';
				if (data.amuleweb) {
					links += '<input class="cbi-button mar-10" type="button" value="<%:Open aMuleWeb%>" onclick="openWebUI();" />';
				}
				tb.innerHTML = links;
			} else {
				tb.innerHTML = '<em><%:aMule daemon is not running.%></em>';
			}
			
			if (data.amuled_pid == "0") {
				btn.innerHTML = btn_tmpl.replace("PID: NUMBER", "<%:Start aMule%>"); 
			} else {
				btn.innerHTML = btn_tmpl.replace("NUMBER", data.amuled_pid); 
			}
		}
		
	}
);
function _data2elements(x, data) {
		var btn = document.getElementById("btn_startstop");
		if ( ! btn ) { return; }	// security check
		if (data.amuled_pid == "0") {
			btn.value = "<%:Start aMule%>";
			btn.className = "cbi-button cbi-button-apply";
			btn.disabled = false;
		} else {
			btn.value = "PID: " + data.amuled_pid;
			btn.className = "cbi-button cbi-button-reset";
			btn.disabled = false;
		}
}
// event handler for start/stop button
function onclick_startstop(id) {
	// do start/stop
	var btnXHR = new XHR();
	btnXHR.post('<%=url('admin/nas/amule/startstop')%>', { token: '<%=token%>' },
		function(x, data) { _data2elements(x, data); }
	);
}
// event handler for download files
function onclick_down_kad(id) {
    if(confirm("<%:Existing file will be overwritten, do you really want to proceed?%>"))
    {
	var btnXHR = new XHR();
	btnXHR.post('<%=url('admin/nas/amule/down_kad')%>', { token: '<%=token%>' },
		function(x) {}
	);
    }
}

function onclick_down_ed2k(id) {
    if(confirm("<%:Existing file will be overwritten, do you really want to proceed?%>"))
    {
	var btnXHR = new XHR();
	btnXHR.post('<%=url('admin/nas/amule/down_ed2k')%>', { token: '<%=token%>' },
		function(x) {}
	);
    }
}

function openWebUI() {
	var curWwwPath = window.document.location.href;
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	var localhostPath = curWwwPath.substring(0, pos);
	var auth_port = document.getElementById("cbid.amule.main.web_port");
	if (auth_port.value == "") {
		auth_port_value = "44711"
	} else {
		auth_port_value = auth_port.value
	};
	var url = "http:" + localhostPath.substring(window.location.protocol.length) + ":" + auth_port_value;
	window.open(url)
};
//]]>
</script>
<style>.mar-10 {margin-left: 10px; margin-right: 10px;}</style>
<fieldset class="cbi-section">
	<legend><%:aMule Status%></legend>
	<p id="amule_status">
		<em><%:Collecting data...%></em>
	</p>
	<p id="amule_startstop">
		<em><%:Collecting data...%></em>
	</p>
</fieldset>
