	var event_focus = null;

	$(function(){

		// AJAXのキャッシュOFF
		$.ajaxSetup({ cache: false });

		$('a.ajax').live('click', function() {
			if ($(this).attr('conf') != undefined) {
				if (!confirm($(this).attr('conf'))) {
					return false;
				}
			}
			
			event_focus = null;
			if ($(this).attr('target') == undefined) {
				event_focus = this;
			} else {
				event_focus = $(this).attr('target');
			}
			if (event_focus) {
				$(event_focus).fadeOut();
			}

			var url = ($(this).attr('href').indexOf(root_url) >= 0) ? $(this).attr('href') : root_url + $(this).attr('href');
			jQuery.getJSON(url, null, function (data) {
				switch (data.state) {
					case 'comp':
						$(event_focus).hide();
						$(event_focus).after(data.html);
						$(event_focus).fadeIn();
						if (typeof('load_init') != 'function') load_init($(event_focus));
						break;
					case 'switch':
						$(event_focus).html(data.html);
						$(event_focus).fadeIn();
						if (typeof('load_init') != 'function') load_init($(event_focus));
						break;
					case 'remove':
						$(event_focus).hide();
						break;
				}
				event_focus = null;
			});
			return false;
		});

		$('form .ajax_submit').live('click', function () {
			var event_btn = this;
			var this_form = this.form;
			var command = $(this).attr('command');			// 成功時の処理
			var alertmsg  = $(this).attr('alertmsg');				// 結果表示エリア
			if (alertmsg == undefined) {
				alertmsg = '.alertmsg';
			}

			if ($(this).attr('conf') != undefined) {
				if (!confirm($(this).attr('conf'))) {
					return false;
				}
			}

			$(event_btn).attr('disabled', true);
			$(alertmsg).hide();
			jQuery.postJSON( root_url + $(this_form).attr('action'), $(this_form).serializeArray(), function (data) {
				if (data.msg != '') {
					if ($(alertmsg).size() > 0) {
						$(alertmsg).html(data.msg);
						$(alertmsg).fadeIn();
					}
				}
				$(event_btn).attr('disabled', false);

				if (data.state == 'err') return;

				if (typeof('load_list') != 'function') load_list();

				if (data.redirect != undefined) {
					$(this_form).delay(1000);
					location.href = data.redirect;
				}

				switch (command) {
					case "reset":
						$('input:text, input:checkbox, input:radio, select, textarea', this_form).val('');
						break;
					case 'remove':
						$(this_form).html('');
						break;
				}
			});
		});

		$('.imgfile_clear').live('click', function(){
			if (!confirm('画像を削除してよろしいですか？')) return;
			var textfield = $(this).attr('target');
			$('#' + textfield).val('');
			$('#' + textfield + '_clear').hide();
			$('#' + textfield + '_link').hide();
		});

		$('.pdffile_clear').live('click', function(){
			if (!confirm('PDFを削除してよろしいですか？')) return;
			var textfield = $(this).attr('target');
			$('#' + textfield).val('');
			$('#' + textfield + '_clear').hide();
			$('#' + textfield + '_link').hide();
		});

		$('.ajax_load[url]').each(function(){
			$(this).hide();
			var thisbox = this;
			jQuery.getJSON(root_url + $(this).attr('url'), null, function(data){
				if (data.state == 'ok' || data.state == 'err') {
					$(thisbox).html(data.html);
					$(thisbox).fadeIn();
					if (typeof('load_init') != 'function') load_init($(thisbox));
				}
			});
		});

		$('input.imgupload').each (function () {
			set_imgupload(this);
		});

		$('input.pdfupload').each (function () {
			set_pdfupload(this);
		});

		$('input.fileupload').each (function () {
			set_fileupload(this);
		});
	});

	function set_imgupload (imgfield) {
		var textfield = $(imgfield).attr('id');
		var dirname = $(imgfield).attr('rel');
		$('#' + textfield).hide();
		$('#' + textfield).before('<a id="' + textfield + '_select" class="imgselect">Upload</a>');
		$('#' + textfield).after('&nbsp;&nbsp;<a id="' + textfield + '_clear" class="imgfile_clear" target="' + textfield + '">Del</a>');
		$('#' + textfield).after('&nbsp;&nbsp;<a id="' + textfield + '_link" class="imgfile_link" href="' + root_url + '/upload/' + $('#' + textfield).val() + '">View</a>');
		if ($('#' + textfield).val() == '') {
			$('#' + textfield + '_clear').hide();
			$('#' + textfield + '_link').hide();
		}
		new AjaxUpload('#' + textfield + '_select', {
			action: root_url + '/' + dirname + '/imgupload/',
			name: 'imgfile',
			target: textfield,
			responseType: 'json',
			onSubmit: function(file, ext) { disp_loader(this._settings.target, 'show'); },
			onComplete: function(file, json) {
				$('#' + this._settings.target).val(json.url);
				$('#' + this._settings.target + '_link').attr('href', root_url + '/upload/' + json.url);
				if ($('#' + this._settings.target).val() != '') {
					$('#' + this._settings.target + '_clear').show();
					$('#' + this._settings.target + '_link').show();
				}
				disp_loader(this._settings.target, 'hide');
			},
			onError: function(file, response){
				$('#' + this._settings.target).val('');
				$('#' + this._settings.target + '_clear').show();
				$('#' + this._settings.target + '_link').text('');
				$('#' + this._settings.target + '_link').hide();
				disp_loader(this._settings.target, 'hide');
			}
		});
	}

	function set_pdfupload (pdffield) {
		var textfield = $(pdffield).attr('id');
		var dirname = $(pdffield).attr('rel');
		$('#' + textfield).hide();
		$('#' + textfield).before('<a id="' + textfield + '_select" class="pdfselect">Upload</a>');
		$('#' + textfield).after('&nbsp;&nbsp;<a id="' + textfield + '_clear" class="pdffile_clear" target="' + textfield + '">Del</a>');
		$('#' + textfield).after('&nbsp;&nbsp;<a id="' + textfield + '_link" class="blank" href="' + $('#' + textfield).val() + '">View</a>');
		if ($('#' + textfield).val() == '') {
			$('#' + textfield + '_clear').hide();
			$('#' + textfield + '_link').hide();
		}
		new AjaxUpload('#' + textfield + '_select', {
			action: root_url + '/' + dirname + '/pdfupload/',
			name: 'pdffile',
			target: textfield,
			responseType: 'json',
			onSubmit: function(file, ext) { disp_loader(this._settings.target, 'show'); },
			onComplete: function(file, json) {
				$('#' + this._settings.target).val(json.url);
				$('#' + this._settings.target + '_link').attr('href', root_url + '/upload/' + json.url);
				if ($('#' + this._settings.target).val() != '') {
					$('#' + this._settings.target + '_clear').show();
					$('#' + this._settings.target + '_link').show();
				}
				disp_loader(this._settings.target, 'hide');
			},
			onError: function(file, response){
				$('#' + this._settings.target).val('');
				$('#' + this._settings.target + '_clear').show();
				$('#' + this._settings.target + '_link').text('');
				$('#' + this._settings.target + '_link').hide();
				disp_loader(this._settings.target, 'hide');
			}
		});
	}

	function set_fileupload (filefield) {
		var textfield = $(filefield).attr('id');
		var dirname = $(filefield).attr('rel');
		$('#' + textfield).after('<input type="button" id="' + textfield + '_select" class="imgselect" value="Upload" />');
		new AjaxUpload('#' + textfield + '_select', {
			action: root_url + '/' + dirname + '/dataupload/',
			name: 'datafile',
			target: textfield,
			responseType: 'json',
			onComplete: function(file, json) {
				$('#' + this._settings.target).val(json.url);
			},
			onError: function(file, response){
				$('#' + this._settings.target).val('');
			}
		});
	}

	function disp_loader(name, func) {
		switch (func) {
			case 'show':
				$('#' + name).after('<img src="' + root_url + '/img/tools/loader.gif" id="' + name + '_loader" class="loader" />');
				$('input:submit, input:button, button').attr('disabled', 'disabled');
				break;
			case 'hide':
				$('#' + name + '_loader').remove();
				$('input:submit, input:button, button').removeAttr('disabled');
				break;
		}
		return;

		var id = '#' + name + '_loader';
		switch (func) {
			case 'show':
				$(id).show();
				$('input:submit, input:button, button').attr('disabled', 'disabled');
				break;
			case 'hide':
				$(id).hide();
				$('input:submit, input:button, button').removeAttr('disabled');
				break;
		}
	}

	jQuery.postJSON = function(url, data, callback) {
		jQuery.post(url, data, callback, "json");
	};


