function Copper(uri, user, password) {
	this.uri = uri;
	this.user = user;
	this.password = password;
	this.conn = new AjaxCSI();
	this.props = new Array();
	this.resources = new Array();
}

Copper.prototype.property = function(name, value) {
	this.props[name] = value;
}

Copper.prototype.info = function(uri, func) {
	uri = m_encodeURI(uri);
	var action = this.uri+'/info?rest.uri='+uri;
	if (this.user) {
		action += '&rest.user='+this.user;
	}
	if (this.password) {
		action += '&rest.password='+this.password;
	}
	this.conn.httpAsyncXML(action, func, 'GET', null);
}

Copper.prototype.resource = function(uri, data) {
	this.resources[this.resources.length] = new Array(uri, data);
}

Copper.prototype.upload = function(form) {
	
}

Copper.prototype.transcode = function(spec, file) {
	var copper = this;
	var action = this.uri+'/open?rest.timeout=1200000';
	if (this.user) {
		action += '&rest.user='+this.user;
	}
	if (this.password) {
		action += '&rest.password='+this.password;
	}
	copper.conn.httpAsyncXML(action, function(dom) {
		var message = dom.getElementsByTagName('message')[0];
		var code = message.getAttribute('code');
		if (code != '1012') {
			alert(message.firstChild.nodeValue+'('+code+')');
			return;
		}
	    copper.sessionId = message.firstChild.nodeValue;
		
		var finish = function() {
			var data = m_toPostParameters(copper.props);
			action = copper.uri+"/properties?rest.id="+copper.sessionId;
			copper.conn.httpAsyncXML(action, function(dom) {
				if(file) {
					var form = spec;
					form.action = copper.uri+"/transcode?rest.id="+copper.sessionId
	        			+"&rest.async=true";
					form.submit();
				}
				else {
					var uri = m_encodeURI(spec);
					action = copper.uri+"/transcode?rest.id="+copper.sessionId
		        		+"&rest.mainURI="+uri+"&rest.async=true";
					copper.conn.httpAsyncXML(action, function(dom) {
						var message = dom.getElementsByTagName('message')[0];
						var code = message.getAttribute('code');
						if (code != '1011') {
							alert(message.firstChild.nodeValue+'('+code+')');
							return;
						}
						copper.trackMessages();
					});
				}
			}, 'POST', data);
		};
		
		var i = 0;
		var resource = function() {
			if (i < copper.resources.length) {
				var res = m_encodeURI(copper.resources[i][0]);
				var data = copper.resources[i][1];
				action = copper.uri+'/resources?rest.id='+copper.sessionId+'&rest.uri='+res;
				copper.conn.httpAsyncXML(action, function(dom) {
					++i;
					resource();
				}, 'POST', data, 'application/octet-stream');
			}
			else {
				finish();
			}
		};
		resource();
	}, 'GET', null);
}

Copper.prototype.messages = function(func, wait) {
	var uri = this.uri+"/messages?rest.id="+this.sessionId;
	if (wait) {
		uri += '&rest.wait='+wait;
	}
	this.conn.httpAsyncXML(uri, func);
}

Copper.prototype.trackMessages = function() {
	this.srcLen = this.srcPos = 0;
	this.errorCode = this.errorMessage = null;
	this.results = new Array();
	var copper = this;
	this.messages(function(dom) {
		copper.getMessage(dom);
	});
}

Copper.prototype.getMessage = function(dom) {
	// メッセージ
	var messages = dom.getElementsByTagName('messages')[0];
	if (messages) {
		var messages = messages.getElementsByTagName('message');
		for ( var i = 0; i < messages.length; ++i) {
			var code = messages[i].getAttribute('code');
			var message = messages[i].firstChild.nodeValue;
			if (code.charAt(0) == '3' || code.charAt(0) == '4') {
				this.errorCode = code;
				this.errorMessage = message;
				alert(message);
			}
			if (this.messageFunc) {
				var args = new Array();
				for (var j = 0; ; ++j) {
					args[j] = messages[i].getAttribute('arg'+j);
					if (args[j] == null) {
						break;
					}
				}
				this.messageFunc(code, message, args);
			}
		}
	}
	
	// 結果
	var results = dom.getElementsByTagName('results')[0];
	if (results) {
		results = results.getElementsByTagName('result');
		for (var i = this.results.length; i < results.length; ++i) {
			var uri = results[i].getAttribute('uri');
			this.results[i] = uri;
			if (this.resultFunc) {
				this.resultFunc(uri);
			}
		}
	}
	
	// 進行状況
	var progress = dom.getElementsByTagName('progress')[0];
	if (this.progressFunc && progress) {
		var changed = false;
		var srcLen = progress.getAttribute('length');
		var srcPos = progress.getAttribute('read');
		if (srcLen && srcLen != this.srcLen) {
			this.srcLen = srcLen;
			changed = true;
		}
		if (srcPos && srcPos != this.srcPos) {
			this.srcPos = srcPos;
			changed = true;
		}
		if (changed) {
			this.progressFunc(this.srcLen, this.srcPos);
		}
	}
	
	var message = dom.getElementsByTagName('message')[0];
	var code = message.getAttribute('code');
	if (code != '1013') {
		// 終了
		if (this.finishFunc) {
			this.finishFunc(this.errorCode, this.errorMessage);
		}
		return;
	}
	
	var copper = this;
	this.messages(function(dom) {
		copper.getMessage(dom);
	}, 3000);
}

Copper.prototype.getResult = function(uri) {
	return this.uri+"/result?rest.id="+this.sessionId+"&rest.uri="+uri
}

Copper.prototype.abort = function(func) {
	var uri = this.uri+"/abort?rest.id="+this.sessionId;
	this.conn.httpAsyncXML(uri, func);
}
