function init_upload_image(PARM_PROCESS_FLG,level) {
	document.getElementById('PARM_PROCESS_FLG').value = PARM_PROCESS_FLG;
	init_upload(level,'popup');
	init_rotate_left(level,'popup');
	init_rotate_right(level,'popup');

	//ポップアップ画面固有の処理
	init_apply_img(level,'popup');

	//ポップアップ画面固有の処理
	get_upload_img(level,'popup');
}

//イメージファイルを初期化する
function get_upload_img(level,screen)
{
	var adjustPath = get_adjust_path(level);
	var url = adjustPath + "/api/test/get_upload_img.php";

	xmlhttpPost_getUploadImg(url,level,screen);


//	$.get(url, {1:1}, function(xml) {
//			var item_count = $(xml).find("item_count").text();alert(item_count);
//			if (item_count == 1) {
//				var path = decodeURIComponent($(xml).find("path").text());
//				set_image(adjustPath+path,screen);
//				//ポップアップ画面の調整処理
//				if(screen == 'popup'){
//					// TODO: ウィンドウ高さの調整
//					if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
//						$("#sub_window_main").css("height","435px");
//						$("#TB_window").css("marginTop","-280px");
//					}
//
//				}
//			}
//	},'xml');
}

function init_upload(level,screen) {
	var url = get_adjust_path(level)+"/api/test/upload_image.php?" + new Date().getTime()+"&screen="+screen;
	// 完成イメージを確認するボタン（アップロード）
	var uploadbtnId,uploadfileId;
	if(screen == 'popup'){
		uploadbtnId = "#upload_button";
		uploadfileId = "upload_file";

	} else if( screen == 'step2'){
		uploadbtnId = "#upload_button_step2";
		uploadfileId = "upload_file_step2";
	}

	$(uploadbtnId).bind("click",{url:url,uploadfileId:uploadfileId,screen:screen,level:level},ajaxFileUpload);

}


function ajaxFileUpload(event)
{
	var url = event.data.url;
	var uploadfileId = event.data.uploadfileId;
	var screen = event.data.screen;
	var level = event.data.level;

	$.ajaxFileUpload
	(
		{
			url:url,
			secureuri:false,
			fileElementId:uploadfileId,
			dataType: 'xml',
			success: function (data, status)
			{
				$('#'+uploadfileId).attr("value", "");

	            var error = $(data).find("error").text();

				// アップロードエラー
				if (1 == error) {
					// エラー処理
					if(screen == 'step2'){
						$("#error_message3").css("display","");
						$("#error_message4").css("display","none");
						$("#error_message7").css("display","none");
						$("#error_message5").css("display","none");
					}else{
						$("#error_message1").css("display","");
						$("#error_message2").css("display","none");
						$("#error_message6").css("display","none");
					}
				} else if (2 == error) {
					// エラー処理
					if(screen == 'step2'){
						$("#error_message4").css("display","");
						$("#error_message3").css("display","none");
						$("#error_message7").css("display","none");
						$("#error_message5").css("display","none");
					}else{
						$("#error_message2").css("display","");
						$("#error_message1").css("display","none");
						$("#error_message6").css("display","none");
					}
				} else if(3 == error){
					// エラー処理
					if(screen == 'step2'){
						$("#error_message7").css("display","");
						$("#error_message4").css("display","none");
						$("#error_message3").css("display","none");
						$("#error_message5").css("display","none");
					}else{
						$("#error_message6").css("display","");
						$("#error_message2").css("display","none");
						$("#error_message1").css("display","none");
					}
				}else {
					load_start(screen);
					if(screen == 'step2'){
						$("#error_message7").css("display","none");
						$("#error_message3").css("display","none");
						$("#error_message4").css("display","none");
						$("#error_message5").css("display","none");
						$("#upload_img_1").css("display","none");
						$("#upload_img_2").css("display","none");
						$("#upload_img_3").css("display","block");
					}else{
						$("#error_message6").css("display","none");
						$("#error_message2").css("display","none");
						$("#error_message1").css("display","none");
					}

					var path = get_adjust_path(level)+decodeURIComponent($(data).find("path").text());
					var width_orig = $(data).find("imw").text();
					var height_orig = $(data).find("imh").text();
					//ポップアップ画面の調整処理
					if(screen == 'popup'){
						// TODO: ウィンドウ高さの調整
						if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
							$("#sub_window_main").css("height","435px");
							$("#TB_window").css("marginTop","-280px");
						}
					}
					set_image(path,screen,width_orig,height_orig,error);
					$("#rotation").css("display","block");
					$("#rotation > #upload_help_title").css("display","block");
					$("#rotation > #rotate_fukidashi").css("display","block");
					$("#rotation > #uploaded_image").css("display","block");
					$("#rotation > #bottom_btn_moushikomu").css("display","block");
					window.location.hash = "#jumpImg";
				}
			},
			error: function (data, status, e)
			{
				alert("ファイルは正しくアップロードされない。");
			}
		}
	)

	return false;

}

// 左90度回転ボタン
function init_rotate_left(level,screen) {
	init_rotate(level, true,screen);
}

// 右90度回転ボタン
function init_rotate_right(level,screen) {
	init_rotate(level, false,screen);
}

function init_rotate(level, isLeft,screen) {
	var urlLeft = get_adjust_path(level)+"/api/test/upload_image.php?rotate=left&" + new Date().getTime();
	var urlRight= get_adjust_path(level)+"/api/test/upload_image.php?rotate=right&" + new Date().getTime();
	var bottonID,orgfileID;
	if(screen == 'popup'){
		bottonID = "#rotate_btn_right";
		orgfileID = "#orgfilename";

	} else if( screen == 'step2'){
		bottonID = "#rotate_btn_right_step2";
		orgfileID = "#orgfilename_step2";
	}
	var url = urlLeft;

	if (!isLeft) {
		if(screen == 'popup'){
			bottonID = "#rotate_btn_left";

		} else if( screen == 'step2'){
			bottonID = "#rotate_btn_left_step2";
		}
		url = urlRight;
	}
	$(bottonID).click(function () {
		var filename = $(orgfileID).attr("value");
		url += "&f=" + filename;

		rotate_start();

		$.get(url, get_hashed_param(url), function (xml) {

			var error = $(xml).find("error").text();
			var path = get_adjust_path(level)+decodeURIComponent($(xml).find("path").text());
			var width_orig = $(xml).find("imw").text();
			var height_orig = $(xml).find("imh").text();
			if (1 == error) {
				// TODO
			} else {
				set_image(path,screen,width_orig,height_orig,error);
			}
		}, 'xml');
	});
}

function set_image(path,screen,width_orig,height_orig,error) {
	var imgObj = new Image();
	imgObj.src = path;
	// remove	imgObj.onload = function () {
	imgObj.onload = null;
	var image = $("<img>");
	image.attr("src", path);
	/* --- 追加 --- START */
	var width=0;
	var height=0;
	var width_max  = 308;//420;
	var height_max = 470;//640;

	if(width_orig && height_orig){
		// 最大の高さ・幅を設定する

		//縦が大きい場合
		if (height_orig > width_orig){

			//縦が縦のの画面表示最大値より大きい場合
			if(height_orig > height_max){
				height = height_max;
			}else{
				height = height_orig;
			}
			//横が横の画面表示最大値より大きい場合
			if(width_orig > width_max){
				width = width_max;
			}else{
				width = width_orig;
			}
		//横が大きい場合（横は等倍で縮める）
		}else{

			//横が横の画面表示最大値より大きい場合
			if(width_orig > width_max){
				width = width_max;
			}else{
				width = width_orig;
			}
			//縦は横の倍率に合わせる
			height = (width / width_orig) * height_orig;
		}
	} else {
		width = width_max;
		height = height_max;

	}
	image.attr("width", width);
	image.attr("height", height);

	var pos = (height_max / 2) - (height/2);

	var style;
	if(width){
		image.css({'position':'relative','top':pos});
		style="style=\"margin:"+pos+"  auto;";
		style=style+"display:block;\"";
	}
	var uploadedImgId,orgfileID;
	if(screen == 'popup'){
		uploadedImgId = "#uploaded_image";
		orgfileID = "#orgfilename";

	} else if( screen == 'step2'){
		uploadedImgId = "#uploaded_image_step2";
		orgfileID = "#orgfilename_step2";
	}

	var tmp = "<img src=\"" + path
	+ "\" width = '"+width+"' height = '"+height+"' id=\"uploadImg\""+style+"/>";
	if(screen == 'popup'){
		tmp = "<a name=\"jumpImg\" target=\"jumpImg\" id=\"jumpImg\"/>" + tmp;
	}
//	$(uploadedImgId).html(image);
	$(uploadedImgId).html(tmp);
		filename = path;
		filename = filename.replace(/^..*\//g,'');
		filename = filename.replace(/\?..*$/g,'');

		$(orgfileID).attr("value", filename);
		load_finish(screen);

// remove	};

}

function load_start(screen) {
	var rotationId;
	if(screen == 'popup'){
		rotationId = "#rotation";

	} else if( screen == 'step2'){
		rotationId = "#rotation_step2";
	}

	$(rotationId).css("display","none");
//	$("#now_uploading").css("display","block");
}
function rotate_start() {
	// NOP
}
function load_finish(screen) {
	var rotationId;
	if(screen == 'popup'){
		rotationId = "#rotation";

	} else if( screen == 'step2'){
		rotationId = "#rotation_step2";
	}

	$(rotationId).css("display","block");

	if( screen == 'step2'){
		$("#openFail").css("display","block");
		$("#btn_moushikomu").css("display","block");
	}

//	$("#now_uploading").css("display","none");

}


//「この内容を申込」ボタンを押下する初期化
function init_apply_img(level,screen) {
	// 「この内容を申込」ボタン
	$("#btn_moushikomu_image").click(function () {

			var adjustPath = get_adjust_path(level);
			xmlhttpPost_uploadimg(adjustPath+'/api/test/update_image.php','form_upload',level);

			//parent.tb_remove();
			$("#TB_imageOff").unbind("click");
			$("#TB_closeWindowButton").unbind("click");
			$("#TB_window").fadeOut("fast",function(){
				$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
			});
			$("#TB_ajaxContent").remove();
			$("#TB_load").remove();
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
				$("body","html").css({height: "auto", width: "auto"});
				$("html").css("overflow","");
			}
			document.onkeydown = "";
			document.onkeyup = "";

			// modify H.takahashi >>>>>>>>>>>>>
			ajax_scrolling(true);
			// <<<<<<<<<<<<< modify H.takahashi

	});
	// 「キャンセル」ボタン
	$("#btn_cancel_longhand_image").click(function () {

			document.getElementById('TEGAKI_IMG_DELETE').value=true;
			var adjustPath = get_adjust_path(level);
			xmlhttpPost_uploadimg(adjustPath+'/api/test/update_image.php','form_upload');

			//parent.tb_remove();
			$("#TB_imageOff").unbind("click");
			$("#TB_closeWindowButton").unbind("click");
			$("#TB_window").fadeOut("fast",function(){
				$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();
			});
			$("#TB_ajaxContent").remove();
			$("#TB_load").remove();
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
				$("body","html").css({height: "auto", width: "auto"});
				$("html").css("overflow","");
			}
			document.onkeydown = "";
			document.onkeyup = "";

			// modify H.takahashi >>>>>>>>>>>>>
			ajax_scrolling(true);
			// <<<<<<<<<<<<< modify H.takahashi

			//document.getElementById("flowerImg").src = "../../img/frower/item/k_gift_ph_p1.jpg";
			//return false;
	});
}

