var session_id = "20171121080011";

function parentpage_adjust_height() {
	window.parent.setup_iframe_height();
}

function language_setup() {
	jQuery.i18n.properties({
		name : 'common',
		path : '../lang/',
		mode : 'map',
		language : window.parent.get_language_setup(),
		callback : function() {
			$('#header_text').html($.i18n.prop('index_cropsetup'));

			$('#crop_region_1_setting_txt').html($.i18n.prop('crop_region_1_setting_txt'));
			$('#crop_region_2_setting_txt').html($.i18n.prop('crop_region_2_setting_txt'));
			$('#crop_region_3_setting_txt').html($.i18n.prop('crop_region_3_setting_txt'));
			$('#crop_region_4_setting_txt').html($.i18n.prop('crop_region_4_setting_txt'));
			
			for(var i=1;i<=4;++i){
				$("#crop_region_" + i + "_status_txt").html($.i18n.prop('crop_region_status_txt'));
				$("#crop_region_" + i + "_off_txt").html($.i18n.prop('crop_region_off_txt'));
				$("#crop_region_" + i + "_on_txt").html($.i18n.prop('crop_region_on_txt'));

				$("#crop_region_" + i + "_x_txt").html($.i18n.prop('crop_region_x_txt'));
				$("#crop_region_" + i + "_y_txt").html($.i18n.prop('crop_region_y_txt'));
				$("#crop_region_" + i + "_w_txt").html($.i18n.prop('crop_region_w_txt'));
				$("#crop_region_" + i + "_h_txt").html($.i18n.prop('crop_region_h_txt'));
			}

			$('#crop_setting').val($.i18n.prop('crop_setting'));
		}
	});
}

function crop_setting() {
	var ptmp = "";
	var crop_enable;
	var crop_region_x;
	var crop_region_y;
	var crop_region_w;
	var crop_region_h;

	for(var i = 1; i <= 4; ++i) {
		crop_enable = $("#crop_region_" + i + "_enable").val();
		crop_region_x = $("#crop_region_" + i + "_x").val();
		crop_region_y = $("#crop_region_" + i + "_y").val();
		crop_region_w = $("#crop_region_" + i + "_w").val();
		crop_region_h = $("#crop_region_" + i + "_h").val();
		
		ptmp += "crop" + (i-1) + "_enable=" + crop_enable + "&";
		ptmp += "crop" + (i-1) + "_x=" + crop_region_x + "&";
		ptmp += "crop" + (i-1) + "_y=" + crop_region_y + "&";
		ptmp += "crop" + (i-1) + "_w=" + crop_region_w + "&";
		ptmp += "crop" + (i-1) + "_h=" + crop_region_h + "&";
	}
	$.ajax({
		type : "GET",
		url : "http://" + xc_gethost() + "/set_crop" + "?" + ptmp,
		cache : false,
		success : function(data) {
			var ret = $(data).find("return").text();
			if("ok" == ret) {
				alert($.i18n.prop('crop_setting_success'));
			} else {
				alert($.i18n.prop('crop_setting_failed'));
			}
		},
		error : function() {
			alert($.i18n.prop('crop_setting_failed'));
		}
	});
}

function crop_preset() {
	$.ajax({
		type : "GET",
		url : "http://" + xc_gethost() + "/get_crop",
		cache : false,
		success : function(data) {
			var wnd = $(data).find("getcrop").attr("wnd");
			wnd = parseInt(wnd);
			for(var i = 1; i <= wnd; ++i) {
				$("#crop_region_" + i + "_setting_txt").removeAttr("style");
				$("#crop_region_" + i + "_status_txt").removeAttr("style");
				$("#crop_region_" + i + "_enable").removeAttr("style");

				var crop_enable = $(data).find("crop" + (i - 1)).attr("enable");
				var crop_region_x = $(data).find("crop" + (i - 1)).attr("x");
				var crop_region_y = $(data).find("crop" + (i - 1)).attr("y");
				var crop_region_w = $(data).find("crop" + (i - 1)).attr("w");
				var crop_region_h = $(data).find("crop" + (i - 1)).attr("h");

				if(crop_enable == 1) {
					$("#crop_region_" + i + "_on_txt").attr("selected", "selected");
				} else {
					$("#crop_region_" + i + "_off_txt").attr("selected", "selected");
				}
				
				$("#crop_region_" + i + "_x").val(crop_region_x);
				$("#crop_region_" + i + "_y").val(crop_region_y);
				$("#crop_region_" + i + "_w").val(crop_region_w);
				$("#crop_region_" + i + "_h").val(crop_region_h);
				
				crop_region_status_change(i);
			}
		},
		error : function() {

		}
	});
}

function crop_region_status_change(index) {
	var status = $("#crop_region_"+index+"_enable").val();
	if(status == 0) {
		$("#crop_region_"+index+"_x_block").attr("style", "display:none;");
		$("#crop_region_"+index+"_y_block").attr("style", "display:none;");
		$("#crop_region_"+index+"_w_block").attr("style", "display:none;");
		$("#crop_region_"+index+"_h_block").attr("style", "display:none;");
	} else if(status == 1) {
		$("#crop_region_"+index+"_x_block").removeAttr("style");
		$("#crop_region_"+index+"_y_block").removeAttr("style");
		$("#crop_region_"+index+"_w_block").removeAttr("style");
		$("#crop_region_"+index+"_h_block").removeAttr("style");
	}
	parentpage_adjust_height();
}

function crop_region_reset()
{
	for(var i = 2; i <= 4; ++i) {
		$("#crop_region_" + i + "_setting_txt").attr("style", "display:none;");
		$("#crop_region_" + i + "_status_txt").attr("style", "display:none;");
		$("#crop_region_" + i + "_enable").attr("style", "display:none;");
		crop_region_status_change(i);
	}
}

$(function() {
	language_setup();
	crop_region_reset();
	crop_preset();
});