// AsyncIframe
function AsyncIframe(uri) {
	
	// トークン
	var dt = new Date();
	var token = dt.getTime() + '_' + Math.floor( Math.random() * 100000 );
	
	// iframe作成
	var iframe = $('<iframe width="1" height="1" id="if_' + token + '" src="javascript:false;" style="display: none;" />');
	$('body').append(iframe);
	
	// ドキュメント生成
	var idoc = iframe.contents();
	idoc[0].open();
	idoc[0].writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
	idoc[0].writeln(
	'<html xmlns="http://www.w3.org/1999/xhtml">\
<head>\
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\
<title>-</title>\
<\/head>\
<body>\
<div id="main"></div>\
<\/body>\
<\/html>');
	idoc[0].close();
	
	this._iframe = iframe;
	
	// フォームを追加
	iframe.contents().find('#main').html('<form id="async_form" name="async_form" method="post" action="' + uri + '"></form>');
	this._form = iframe.contents().find('#async_form');
}

AsyncIframe.prototype.formHtml = function(value) {
	this._form.html(value);
};

AsyncIframe.prototype.formAppend = function(value) {
	this._form.append(value);
};

AsyncIframe.prototype.addToken = function(tname, tvalue) {
	this._form.append('<input type="hidden" name="' + tname + '" value="' + tvalue + '" />');	
};

AsyncIframe.prototype.load = function(callback) {
	this._iframe.load(function() {
		var contents = $(this).contents().get(0);
		var data = $(contents).find('body').html();
		data = window.eval('(' + data + ')');
		callback(data);
		$(this).remove();
	});
};

AsyncIframe.prototype.submitForm = function() {
	this._form.submit();
};

// AsyncQueue
AyncQueue = function(){
	this._queue = new Queue();
};

AyncQueue.prototype.enqueue = function(command, params, callback) {
	this._queue.enqueue({
		cmd : command,
		opt : params,
		callbackfunc : callback		
	});
};

AyncQueue.prototype.execute = function(){
	if (this._queue.size() == 1) {
		this.executeNext();
	}
};

AyncQueue.prototype.executeNext = function(){
	if(this._queue.size() > 0) {
		var item = this._queue.first();
		item.cmd(item.opt, item.callbackfunc);
	}
};

AyncQueue.prototype.dequeue = function () {
	return this._queue.dequeue();
};

