<?php


//parameter

@$studyUID = $_GET['studyUID'];
@$seriesUID = $_GET['seriesUID'];
@$wl = $_GET['wl'];
@$ww = $_GET['ww'];
@$win_flag = $_GET['win_flag'];
@$power = $_GET['power'];
@$negate = $_GET['negate'];
@$rotate = $_GET['rotate'];

if(!@$negate){$negate = 0;}
if(!@$rotate){$rotate = 0;}
if(!@$power){$power = 1;}

	require("Sajax.php");

	function create_check($jpg_file){
	if(!file_exists($jpg_file)){
		usleep(10);
		create_check($jpg_file);
	}
	}


	
        function convert($target_dicom,$changeed_wl, $changeed_ww,$negate_check,$rotate_check,$power) {
		
		if($rotate_check != "0"){
		@$rotate_tag = "+" . $rotate;
		}else{
		@$rotate_tag = "";
		}
		if($negate_check !=0){
		@$negate_tag = "+P";
		}else{
		@$negate_tag = "";
		}
		
		$add_tag = $changeed_ww . "_" . $changeed_wl .  "_" . $power . "_" . $negate_tag  . "_" . $rotate_tag;
		
		
        @$convert = exec("which convert");
		@$convert_y1 = exec("which convert-y1");
        $dcm2pnm = "/usr/local/dicom/bin/dcmj2pnm";
        $dir=dirname(escapeshellcmd($target_dicom));
        $jpg_file=($target_dicom . "-" . $add_tag . ".jpg");
		if(!file_exists("$jpg_file")){
        exec("$dcm2pnm +oj $negate_tag $rotate_tag +Ww $changeed_wl $changeed_ww $target_dicom $jpg_file &",$output,$return);
			if($power != 1){
			exec("$convert_y1  -s $power  -n -n   $jpg_file  $jpg_file &",$output2,$return2);
			}
			create_check($jpg_file);
		}
        return $jpg_file;
		}


        function convert_for_apply($toward,$num,$target_dicom,$changeed_wl,$changeed_ww,$negate_check,$rotate_check,$power) {
		
		if($rotate_check != "0"){
		@$rotate_tag = "+" . $rotate_check;
		}else{
		@$rotate_tag = "";
		}
		if($negate_check !=0){
		@$negate_tag = "+P";
		}else{
		@$negate_tag = "";
		}
		
		$add_tag = $changeed_ww . "_" . $changeed_wl .  "_" . $power . "_" . $negate_tag  . "_" . $rotate_tag;




		
        @$convert = exec("which convert");
		@$convert_y1 = exec("which convert-y1");
        $dcm2pnm = "/usr/local/dicom/bin/dcmj2pnm";
        $dir=dirname(escapeshellcmd($target_dicom));
        $jpg_file=($target_dicom . "-" . $add_tag . ".jpg");
		if(!file_exists("$jpg_file")){
        exec("$dcm2pnm +oj $negate_tag $rotate_tag +Ww $changeed_wl $changeed_ww $target_dicom $jpg_file &",$output,$return);
			if($power != 1){
			exec("$convert_y1  -s $power  -n -n   $jpg_file  $jpg_file &",$output2,$return2);
			}
			create_check($jpg_file);
		}
		
		$result_val = $toward . ":" . $num . ":" . $jpg_file;

		
        return $result_val;
		}

        function get_ct($start_original_x,$start_original_y,$end_original_x,$end_original_y,$dicom_path) {
		
        @$get_ct = exec("which convert-y2");
        exec("$get_ct $start_original_x $start_original_y $end_original_x $end_original_y $dicom_path",$val);
		$str = split(",",$val[0]);
		$str[0] = round(trim($str[0]));
		$str[1] = round(trim($str[1]));
		$str[2] = round(trim($str[2]));
		$result = "ave:" . $str[0] . "<br>min:" . $str[1] . "<br>max:" . $str[2];
		
        return $result;
		}

	
	sajax_init();
	// $sajax_debug_mode = 1;
	sajax_export("convert");
	sajax_export("convert_for_apply");
	sajax_export("get_ct");
	sajax_handle_client_request();
	
?>

<?php



@$date = str_replace(" ","_",$date);
@$date = str_replace(":","-",$date);





?>

<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>
<title><? echo $seriesUID ?></title>
<meta http-equiv=content-type content="text/html; charset=x-euc-jp">
<meta http-equiv="imagetoolbar" content="no">

<?php 



	$opener_prop = "top.opener.parent.back.document.windata.";


?>




<script src="./libjs/prototype.js" type="text/javascript"></script>
<script src="./libjs/DrawingCanvas.js" type="text/javascript"></script>
<script src="./libjs/wz_jsgraphics.js" type="text/javascript"></script>
<script src = "./jslb_ajax.js" harset= "utf-8"></script>


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

//ajax component

		var convert_target,sub_wl, sub_ww ,cb_1,cb_2;
		var wl = "";
		var ww = "";
		


	<?php
	sajax_show_javascript();
	?>
	
	var jpg_file_name = "";
	var jpg_file_name_tmp = "";
	var jpeg_return_check = false ;
	var negate_check = "<?php echo $negate ;?>";
	var rotate_check = "<?php echo $rotate ;?>";
	var power = "<?php echo $power ;?>";
	var seriesUID = "<?php echo $seriesUID ?>";
	var studyUID = "<?php echo $studyUID ?>";


//AJAX wl/ww change component 
	function do_convert_cb(jpg_file_name) {

		jpg_file_name_tmp = jpg_file_name + '.tmp.jpg' ;
		
		imagestest[num].src = jpg_file_name;
		jpgset[num] = jpg_file_name;
		referset[num] = jpg_file_name_tmp;
            document.image1.src = imagestest[num].src;
           // document.hyouji.src = imagestest[num].src;
		jpeg_return_check = true;


//		alert(imagestest[num].src);
	}
	
	

	function do_convert(convert_target,sub_wl,sub_ww,negate_check,rotate_check,power) {
		// get the folder name
		ww = sub_ww;
		wl = sub_wl;
		x_convert(convert_target,sub_wl, sub_ww,negate_check,rotate_check,power, do_convert_cb);
	}




//AJAX wl/ww change component for all image apply



	function do_convert_for_apply_cb(result_val) {
	
	var value = result_val.split(":");
	var toward = value[0];
	var num_tag = value[1];
	var jpg_file_name = value[2];


		jpg_file_name_tmp = value[2] + '.tmp.jpg' ;
		
		
		imagestest[value[1]].src = value[2];
		jpgset[value[1]] = value[2];
		referset[value[1]] = jpg_file_name_tmp;
		
		
		if(num == num_tag){
			document.image1.src = imagestest[num].src;
            //document.hyouji.src = imagestest[num].src;
		}
		
//		jpeg_return_check = true;
		if(toward == "low"){
		document.getElementById('create_check_low').innerHTML = "low:" +  (eval(value[1]) + 1)  ;

		}
		if(toward == "high"){
		document.getElementById('create_check_high').innerHTML = "high:" +  (eval(value[1]) + 1)  ;

		}
		if(toward == "AJAX"){
		document.getElementById('create_check_AJAX').innerHTML = "AJAX on:" +  (eval(value[1]) + 1)  ;

		}


	}

	function do_convert_for_apply(toward,num,convert_target,sub_wl,sub_ww,negate_check,rotate_check,power) {
		x_convert_for_apply(toward,num,convert_target,sub_wl, sub_ww,negate_check,rotate_check,power, do_convert_for_apply_cb);
	}

//AJAX get CT value component

	function do_get_ct_cb(result) {
		
		document.getElementById('huv').innerHTML = result  ;
//		alert(result);
	}
	
	function do_get_ct(start_original_x,start_original_y,end_original_x,end_original_y,dicom_path){
		x_get_ct(start_original_x,start_original_y,end_original_x,end_original_y,dicom_path, do_get_ct_cb);
	}


</script>

<script language="JavaScript" type="text/javascript" src="menus_image.js"></script>




<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function IMAGEopen(cgurl,cgwid,cghei){
 myWin = window.open(cgurl,'_blank','toolbar=0,location=0,dependent=1,directories=0,status=0,menu=0,scrollbars=1,resizable=1,width='+cgwid+',height='+cghei+'').focus();
}





function IMAGEreplace(cgurl){
 myWin = location.replace(cgurl);
}


var url = "";
var ano = "";
var ano_char = "";
var get_wl = "";
var get_ww = "";






function checkout_wlww(){
  url = 'check_image_wlww.php?requestType=WADO&studyUID=<?php echo $studyUID ?>&seriesUID=<?php echo $seriesUID ?>&win_flag=1&wl=' + wl + '&ww=' + ww ;


window.open(url)

}


function change_wlww(wl,ww){
  url = 'show_image.php?requestType=WADO&studyUID=<?php echo $studyUID ?>&seriesUID=<?php echo $seriesUID ?>&win_flag=1&wl=' + wl + '&ww=' + ww ;


IMAGEreplace(url)

}

function change_filter(power,negate,rotate){
  url = 'show_image.php?requestType=WADO&studyUID=<?php echo $studyUID ?>&seriesUID=<?php echo $seriesUID ?>&win_flag=1&power=' + power + '&negate=' + negate + '&wl=' + wl + '&ww=' + ww + '&rotate=' + rotate;


IMAGEreplace(url)

}


function change_mag(mag_power){
ritu = mag_power;
h=document.images.image1.height;
w=document.images.image1.width;
//document.images.hyouji.height=h*ritu;
//document.images.hyouji.width=w*ritu;

}

function change_move_ratio(ratio){
mouse_move_ratio = ratio;
}

function change_wlww_set(){




  url = 'show_image.php?requestType=WADO&studyUID=<?php echo $studyUID ?>&seriesUID=<?php echo $seriesUID ?>&win_flag=1&wl=' + wl + '&ww=' + ww ;
 IMAGEreplace(url);
}




function change_9(){




  url = 'show_image_9.php?requestType=WADO&studyUID=<?php echo $studyUID ?>&seriesUID=<?php echo $seriesUID ?>&win_flag=1&wl=' + wl + '&ww=' + ww ;
 IMAGEreplace(url);
}

function change_1(){


  url = 'show_image.php?requestType=WADO&studyUID=<?php echo $studyUID ?>&seriesUID=<?php echo $seriesUID ?>&win_flag=1&wl=' + wl + '&ww=' + ww ;
 IMAGEreplace(url);
}

function Wait(){
    var d1 = new Date().getTime();
    var d2 = new Date().getTime();
    while( d2 < d1+1000*1.0 ){    //1.0秒待つ
        d2=new Date().getTime();
    }
    return;
}


function setLayerText()
{
document.getElementById('image_number').innerHTML  = '1/' + image_num_end;
}

var num = 0;

function echo_dicom() {
//		window.open(dicom_path);
var dicom;
dicom=window.open("","","width=5,height=5,top=0,left=0");
dicom.focus();
dicom.document.write('<body bgcolor="#000000" onload=javascript:location.replace("' + dicom_path + '");setTimeout("window.close()","10000")>');
dicom.document.write('</body></html>');
dicom.document.close();
}

function echo_refer() {
		window.open('marking.php?jpg_file_name=' + jpg_path + '&jpg_file_name_tmp=' + refer_path + '&ruid=<?php echo @$ruid ?>&patid=<?php echo @$patid ?>&browser=<?php echo @$browser ?>','<?php echo @$win ?>','toolbar=no,location=no,top=<?php echo @$top ?>,left=<?php echo @$left ?>,directories=no,status=no,menu=no,scrollbars=yes,resizable=no,width=570,height=730');
		


}



var drawFlg = false;
var continueFlg = false;
var canvas_start_x = "";
var canvas_start_y = "";
var dist_check_flag = false;

function start_measure_dist(evt){
}


function start_evaluate_huv(evt){
}

	function getX(event) {
		if (event.offsetX) {
			return event.offsetX;
		}
		return event.layerX;
	}

	function getY(event) {
		if (event.offsetY) {
			return event.offsetY;
		}
		return event.layerY;
	}

	function buildCanvas(width,hight) {
		var canvas = new DrawingCanvas($("canvas"), width, hight);
		canvas.setBgColor("transparent");
		canvas.setLineColor("#00f");
		canvas.setLineWidth(3);
		return canvas;
	}



function catch_scrolling(orig_image_num,selected_table,value_x,value_y,value_z){

var abs ;
var start_flag = 0;
var now_min_value ;
var now_min_num ;

//alert("test");
if(sync_flag == 1){
	switch(selected_table){
	case 'z':
	if(sync_defference_val != 0){
	value_z = value_z - sync_defference_val;
	}
	for(i = 0; i < image_num_end ; i++){
		abs = tableset_z[i] - value_z;
		abs = Math.abs(abs);
			if(start_flag == 0){
			now_min_value = abs;
			start_flag = 1;
			}
		if(now_min_value >= abs){
		now_min_num = i;
		now_min_value = abs;
		}
		if(now_min_value < abs){
		break;
		}
	}
	num = now_min_num;
	setImagefit();
	break;
	case 'y':
	if(sync_defference_val != 0){
	value_y = value_y - sync_defference_val;
	}
	for(i = 0; i < image_num_end ; i++){
		abs = tableset_y[i] - value_y;
		abs = Math.abs(abs);
			if(start_flag == 0){
			now_min_value = abs;
			start_flag = 1;
			}
		if(now_min_value >= abs){
		now_min_num = i;
		now_min_value = abs;
		}
		if(now_min_value < abs){
		break;
		}
	}
	num = now_min_num;
	setImagefit();
	break;
	case 'x':
	if(sync_defference_val != 0){
	value_x = value_x - sync_defference_val;
	}
	for(i = 0; i < image_num_end ; i++){
		abs = tableset_x[i] - value_x;
		abs = Math.abs(abs);
			if(start_flag == 0){
			now_min_value = abs;
			start_flag = 1;
			}
		if(now_min_value >= abs){
		now_min_num = i;
		now_min_value = abs;
		}
		if(now_min_value < abs){
		break;
		}
	}
	num = now_min_num;
	setImagefit();
	break;
	case 'variable':
	for(i = 0; i < image_num_end ; i++){
		if(tableset_x[i] == value_x && tableset_y[i] == value_y && tableset_z[i] == value_z ){
		now_min_num = i;
		num = now_min_num;
		setImagefit();
		break;
		}
	}
	break;
	}
}



}

var sync_flag = 1;
var sync_original_val = 0;
var sync_changed_val = 0;
var sync_target_axis = 0;
var sync_defference_val = 0;

function sync_change() {
	if(sync_flag == 1){
	sync_flag = 0;
	sync_target_axis = window.opener.document.getElementById('<?php echo $seriesUID?>').target_orientation.value;
//	alert(sync_target_axis);
	if(sync_target_axis == "x"){
	sync_original_val = tableset_x[num];
	}else if(sync_target_axis == "y"){
	sync_original_val = tableset_y[num];
	}else if(sync_target_axis == "z"){
	sync_original_val = tableset_z[num];
	}
	
//	alert(sync_original_val);
	}else{
	sync_flag = 1;
	if(sync_target_axis == "x"){
	sync_changed_val = tableset_x[num];
	}else if(sync_target_axis == "y"){
	sync_changed_val = tableset_y[num];
	}else if(sync_target_axis == "z"){
	sync_changed_val = tableset_z[num];
	}
	
	sync_defference_val = sync_original_val - sync_changed_val;
//	alert(sync_defference_val);
	
	}
}

function setImagefit() {
    if (document.images ) {
			if(imagestest[num] && imagestest[num].complete){
			
			if(negate_check == 0){var negate_check_tag = "";}else{var negate_check_tag = "+P"};
			if(rotate_check == 0){var rotate_check_tag = "";}else{var rotate_check_tag = "+" + rotate_check};
			
			var compare_tag = negate_check + ":" + rotate_check + ":" + power + ":" + wl + ":" + ww;
			
			
			var path=imagestest[num].src;
			var file=path.substring(path.lastIndexOf('/',path.length)+1,path.length); 
			var path_dicom=dicomset[num];
			var file_dicom=path_dicom.substring(path_dicom.lastIndexOf('/',path_dicom.length)+1,path_dicom.length); 
			
//			if(compare_tag == default_tag){
//			var target_converted_file = file_dicom + ".jpg";
//			}else{
			var target_converted_file = file_dicom + "-" + ww + "_" + wl +  "_" + power + "_" + negate_check_tag  + "_" + rotate_check_tag + ".jpg";
//			}
			
			//var log = file + "_" + target_converted_file;
			//var log = default_tag + "_" + compare_tag;
			
			
			if(file != target_converted_file ){
			do_convert_for_apply("AJAX",num,dicomset[num],wl,ww,negate_check,rotate_check,power);
			
			//alert(log);
			}else{
			
            document.image1.src = imagestest[num].src;
            //document.hyouji.src = imagestest[num].src;
			}
			dicom_path = dicomset[num];
			jpg_path = jpgset[num];
			refer_path = referset[num];
			document.image_prop.present.value = num + 1;
			document.getElementById('image_number').innerHTML = (num+1) + '/' + image_num_end;
			document.getElementById('table_position').innerHTML =  tableset[num];
				if(dist_check_flag == true ){
		 		drawFlg = false;
		 		canvas.clear();
				}
			
			window.opener.document.getElementById('<?php echo $seriesUID?>').image_num.value = num;
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_x.value = (tableset_x[num] -  eval(sync_defference_val));
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_y.value = (tableset_y[num] +  eval(sync_defference_val));
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_z.value = (tableset_z[num] + eval(sync_defference_val));
			
			
            return true;
			}else{
//			num = num - 1;
			return true;
			}
    }
    return false;
}

function setImageup() {
    		if(num == (image_num_end - 1)){
			num = 0;
			}else{
			num = num + 1;
			}
			if(imagestest[num] && imagestest[num].complete){
			
			if(negate_check == 0){var negate_check_tag = "";}else{var negate_check_tag = "+P"};
			if(rotate_check == 0){var rotate_check_tag = "";}else{var rotate_check_tag = "+" + rotate_check};
			
			var compare_tag = negate_check + ":" + rotate_check + ":" + power + ":" + wl + ":" + ww;
			
			
			var path=imagestest[num].src;
			var file=path.substring(path.lastIndexOf('/',path.length)+1,path.length); 
			var path_dicom=dicomset[num];
			var file_dicom=path_dicom.substring(path_dicom.lastIndexOf('/',path_dicom.length)+1,path_dicom.length); 
			
//			if(compare_tag == default_tag){
//			var target_converted_file = file_dicom + ".jpg";
//			}else{
			var target_converted_file = file_dicom + "-" + ww + "_" + wl +  "_" + power + "_" + negate_check_tag  + "_" + rotate_check_tag + ".jpg";
//			}
			
			var log = file + "_" + target_converted_file;
			var log = default_tag + "_" + compare_tag;
			
			
			if(file != target_converted_file && multiframe_flag == 0){
			do_convert_for_apply("AJAX",num,dicomset[num],wl,ww,negate_check,rotate_check,power);
			
			//alert(log);
			}else{
			
            document.image1.src = imagestest[num].src;
            //document.hyouji.src = imagestest[num].src;
			}
			dicom_path = dicomset[num];
			jpg_path = jpgset[num];
			refer_path = referset[num];
			document.image_prop.present.value = num + 1;
			document.getElementById('image_number').innerHTML = (num+1) + '/' + image_num_end;
			document.getElementById('table_position').innerHTML =  tableset[num];
				if(dist_check_flag == true ){
		 		drawFlg = false;
		 		canvas.clear();
				}
			
			if(sync_flag == 1){
			
			window.opener.document.getElementById('<?php echo $seriesUID?>').image_num.value = num;
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_x.value = tableset_x[num];
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_y.value = tableset_y[num];
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_z.value = tableset_z[num];
			
			
			if(sync_defference_val != 0){
				if(sync_target_axis == "x"){
				window.opener.document.getElementById('<?php echo $seriesUID?>').position_x.value = eval(tableset_x[num]) + eval(sync_defference_val) ;
				}else if(sync_target_axis == "y"){
				window.opener.document.getElementById('<?php echo $seriesUID?>').position_y.value = eval(tableset_y[num]) + eval(sync_defference_val) ;
				}else if(sync_target_axis == "z"){
				window.opener.document.getElementById('<?php echo $seriesUID?>').position_z.value = eval(tableset_z[num]) + eval(sync_defference_val) ;
				}
			}
			
			window.opener.cacth_paging(studyUID,seriesUID);
			
			}
			
            return true;
			}else{
			num = num - 1;
			return true;
			}
    
    return false;
}

function setImagedw() {

		if(num == 0){
			num = image_num_end - 1;
			}else{
			num = num - 1;
			}
	    if(imagestest[num] && imagestest[num].complete){
			
			if(negate_check == 0){var negate_check_tag = "";}else{var negate_check_tag = "+P"};
			if(rotate_check == 0){var rotate_check_tag = "";}else{var rotate_check_tag = "+" + rotate_check};
			
			var compare_tag = negate_check + ":" + rotate_check + ":" + power + ":" + wl + ":" + ww;
			
			
			var path=imagestest[num].src;
			var file=path.substring(path.lastIndexOf('/',path.length)+1,path.length); 
			var path_dicom=dicomset[num];
			var file_dicom=path_dicom.substring(path_dicom.lastIndexOf('/',path_dicom.length)+1,path_dicom.length); 
			
//			if(compare_tag == default_tag){
//			var target_converted_file = file_dicom + ".jpg";
//			}else{
			var target_converted_file = file_dicom + "-" + ww + "_" + wl +  "_" + power + "_" + negate_check_tag  + "_" + rotate_check_tag + ".jpg";
//			}
			
			//var log = file + "_" + target_converted_file;
			//var log = default_tag + "_" + compare_tag;
			

			if(file != target_converted_file && multiframe_flag == 0){
			do_convert_for_apply("AJAX",num,dicomset[num],wl,ww,negate_check,rotate_check,power);

			//alert(log);
			}else{

            document.image1.src = imagestest[num].src;
            //document.hyouji.src = imagestest[num].src;
			}
			
			dicom_path = dicomset[num];
			jpg_path = jpgset[num];
			refer_path = referset[num];
			document.image_prop.present.value = num + 1;
			document.getElementById('image_number').innerHTML = (num+1) + '/' + image_num_end;
			document.getElementById('table_position').innerHTML =  tableset[num];
				if(dist_check_flag == true ){
		 		drawFlg = false;
		 		canvas.clear();
				}
			
			if(sync_flag == 1){
			
			window.opener.document.getElementById('<?php echo $seriesUID?>').image_num.value = num;
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_x.value = tableset_x[num];
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_y.value = tableset_y[num];
			window.opener.document.getElementById('<?php echo $seriesUID?>').position_z.value = tableset_z[num];
			
			if(sync_defference_val != 0){
				if(sync_target_axis == "x"){
				window.opener.document.getElementById('<?php echo $seriesUID?>').position_x.value = eval(tableset_x[num]) + eval(sync_defference_val) ;
				}else if(sync_target_axis == "y"){
				window.opener.document.getElementById('<?php echo $seriesUID?>').position_y.value = eval(tableset_y[num]) + eval(sync_defference_val) ;
				}else if(sync_target_axis == "z"){
				window.opener.document.getElementById('<?php echo $seriesUID?>').position_z.value = eval(tableset_z[num]) + eval(sync_defference_val) ;
				}
			}
			
			window.opener.cacth_paging(studyUID,seriesUID);
			
			}
			
            return true;
    }
    return false;
}

function key_press(evt) {
	evt.preventDefault();
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
       var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
       if (elem) {
           var charCode = (evt.keyCode) ? evt.keyCode : 
               ((evt.which) ? evt.which : evt.charCode);
           if ( (charCode == 34) || (charCode == 32) || (charCode == 40) || (charCode == 49)) {
           		setImageup();
           } else if ( (charCode == 33) || (charCode == 8) || (charCode == 38)  || (charCode == 50)) {
               setImagedw();
           } else {
		       return false;
		   }
       }
    }
}

function wheel(){
  swt=event.wheelDelta;
  if(swt<=-120){setImageup()}
    else{setImagedw()}
  }
	
function wheel2(e){
  e.preventDefault();
  var delta = e.detail * -1;
  	if(delta <0){
		setImageup();
	}else{
		setImagedw();
	}
  }

// get pointing pozition

function getMouseX(e){
  if(window.opera)
          return e.clientX
  else if(document.all)   
          return document.body.scrollLeft+event.clientX
  else if(document.layers||document.getElementById)
          return e.pageX
}
function getMouseY(e){
  if(window.opera)   
          return e.clientY
  else if(document.all)   
          return document.body.scrollTop+event.clientY
  else if(document.layers||document.getElementById)
          return e.pageY
}



var pos_down_x = "";
var pos_down_y = "";
var mouse_down_flag = false;
var pos_up_x = "";
var pos_up_y = "";
var sub_x = "";
var sub_y = "";
var wl = "";
var ww = "";

var realtime_ratio = 0.05 ;


<?php 

	if($wl){
        print("wl = $wl;");
        print("ww = $ww;");
	}
?>



        var rightButton = false;

function mouse_down(evt){


}

var mouse_move_ratio = "0.3";

function realtime_wlww(evt){
}

function mouse_up(evt){
}



// magnification



Px=0;                      //表示位置(x座標)
Py=50;                      //表示位置(y座標)
mag=200;                      //拡大鏡の大きさ
ritu=2;                      //倍率
baseimg="";          //画像	

deb=""; 

var h,w,Px,Py,deb,x,y,mag,baseimg,ritu;
function set(){
h=document.images.image1.height;
w=document.images.image1.width;
//document.images.hyouji.height=h*ritu;
//document.images.hyouji.width=w*ritu;
x=mag;y=mag;
}


function big(e){
}



//	_pack

function resize_pack(){
	ImageChange();
	set();
}




//window resize

function change(size){

if(size == 'big'){ 
window.resizeBy(100, 100); 
}
else if(size == 'small'){ 
window.resizeBy(-100, -100); 
}


} 


//sumnailing (tiling)

var now_pos_x_cli = 1;
var now_pos_y_cli = 1;
var xtarget = "";
var ytarget = "";
var get_param_x;
var get_param_y;

var win_sum_path = '<?php echo $opener_prop?>' + 'win_colum_sum.value';
var win_size_path = '<?php echo $opener_prop?>' + 'win_size_trf.value';

var win_sum = eval(win_sum_path);
var win_size =  eval(win_size_path);
var win_size_y ;


var loop_flag = 0;




function tile() {



	    var block = document.getElementById("wlww_data");
        block.style.display = "none";

	

	
	var now_pos_x_path ;
	var now_pos_y_path ;
	var path_prop;


<?php
$now_pos_x_path_php = $opener_prop . "now_pos_x.value";
$now_pos_y_path_php = $opener_prop . "now_pos_y.value";

 ?>

 now_pos_x_path = '<?php echo $opener_prop?>' + 'now_pos_x.value';
 now_pos_y_path = '<?php echo $opener_prop?>' + 'now_pos_y.value';
 path_prop = '<?php echo $opener_prop?>';


	win_size_y = eval(win_size) + 100;
	window.resizeTo(win_size,win_size_y);

	if(eval(now_pos_x_path) > eval(win_sum)){
	now_pos_x_path = 1;
	now_pos_y_path = 1;
	<?php echo $now_pos_x_path_php ?> = 1;
	<?php echo $now_pos_y_path_php ?> = 1;
	
	now_pos_x_cli = eval(now_pos_x_path);
	now_pos_y_cli = eval(now_pos_y_path);
	
	now_pos_x_cli_val = 'xvalue' + now_pos_x_cli;
	now_pos_y_cli_val = 'yvalue' + now_pos_y_cli;
	xtarget = path_prop + now_pos_x_cli_val + '.value';
	ytarget = path_prop + now_pos_y_cli_val + '.value';

    get_param_x = eval(xtarget);
	get_param_y = eval(ytarget);
	
	window.moveTo(get_param_x,get_param_y) ;
	
	<?php echo $now_pos_x_path_php ?> = 2;
	<?php echo $now_pos_y_path_php ?> = 2;

	}else{

//	alert('eles pattern');
	
	now_pos_x_cli = eval(now_pos_x_path);
	now_pos_y_cli = eval(now_pos_y_path);

	
	now_pos_x_cli_val = 'xvalue' + now_pos_x_cli;
	now_pos_y_cli_val = 'yvalue' + now_pos_y_cli;
	xtarget = path_prop + now_pos_x_cli_val + '.value';
	ytarget = path_prop + now_pos_y_cli_val + '.value';

    get_param_x = eval(xtarget);
	get_param_y = eval(ytarget);
	
	window.moveTo(get_param_x,get_param_y) ;
	
	
	<?php echo $now_pos_x_path_php ?> = parseInt(now_pos_x_cli) + 1;
	<?php echo $now_pos_y_path_php ?> = parseInt(now_pos_y_cli) + 1;
	
	}

//	alert(get_param_x);
//	alert(get_param_y);

	
	

}
/*
function loadD_script(file_name){
	function onloaded(d,callback){
	　　　　var rawData=d.responseText;//C.jsの内容（文字列）
	　　　　var jsElement=document.createElement("script");
	　　　　jsElement.type="text/javascript";
	　　　　var flag="loadFlag_"+(new Date).getTime();
	　　　　jsElement.text=flag+"=1;"+rawData;
	　　　　//ソースを流し込みつつフラグ変数をソースに追加
	　　　　document.getElementsByTagName("head")[0].appendChild(jsElement);
	　　　　function loop(){
	　　　　　　　　if(!!window[flag]){
	　　　　　　　　　　　　callback();//フラグを認識した時点でループ終了
	　　　　　　　　}
	　　　　　　　　else{
	　　　　　　　　　　　　setTimeout(loop,50);
	　　　　　　　　}
	　　　　}
	　　　　if(typeof callback=="function"){
	　　　　　　　　loop();//コールバックが関数ならばループ開始
	　　　　}
	}
	function nextFunc(){
//	　　　　testC();
	}
	sendRequest(function(res){onloaded(res,nextFunc)},'','GET',file_name);
}
*/

function initDrag() {
	
	loadD_script("wlww_change.js");
	loadD_script("apply_setting.js");
  	document.onmousedown = mouse_down;
	document.onmousemove = null;
   	document.onmouseup = mouse_up;
	document.body.style.cursor = "move";
	
	document.ondblclick = apply_setting;
	
	var block = document.getElementById("wlww_data");
    //block.style.display = "block";
	
    return;
}

function measure_dist() {

  	loadD_script("start_measure_dist.js");
	document.getElementById("canvas").style.display = "block";
	document.onmousedown = start_measure_dist;
	drawFlg = false;
	dist_check_flag = true;
	document.body.style.cursor = "crosshair";
	document.onmousemove = null;
   	document.onmouseup = null;
    return;
}

function evaluate_huv() {

  	loadD_script("start_evaluate_huv.js");
	document.getElementById("canvas").style.display = "block";
	document.onmousedown = start_evaluate_huv;
	drawFlg = false;
	dist_check_flag = true;
	document.body.style.cursor = "crosshair";
	document.onmousemove = null;
   	document.onmouseup = null;
    return;
}

function select_mod(mod_val){
	 if(mod_val == "big" ){
	 	event_clear();
		loadD_script("big.js")
	 document.onmousemove=big;
	 }
	 if(mod_val == "initDrag" ){
	 	event_clear();
	 initDrag();
	 }
	 if(mod_val == "measure_dist" ){
		event_clear();
	 measure_dist();
	 }
	 if(mod_val == "evaluate_huv" ){
		event_clear();
	 evaluate_huv();
	 }
	 if(mod_val == "no" ){
		event_clear();
	 }
}

function event_clear(){

	 //document.hyouji.style.display = "none";
	 document.getElementById('canvas').style.display="none";
//	 document.getElementById('canvas').value=null;
	 document.body.style.cursor = "default";
	 document.onmousemove=null;
	 document.onmousedown=null;
	 document.onmouseup=null;
	 document.ondblclick=null;
	 canvas_start_x = "";
	 canvas_start_y = "";
	 dist_check_flag = false;
	 drawFlg = false;
	 canvas.clear();

}


var myHandle  = 0;
var local_cache_num = 0;
var finish_flag = 0;
var now_stock_flag = 0;
var now_stock_num = 0;
	
	function cache_check(end){
	
	for(count=now_stock_num;count < end;count++){
	

		if(imagestest[count] && imagestest[count].complete){
			if(document.image_prop.local){
			document.image_prop.local.value = count + 1 ;
			
			document.getElementById('local_load_status').innerHTML = 'local:' + (count + 1) ;
		if(end == count + 1){
		clearInterval(myHandle );
		myHandle = 0;
		finish_flag = 1;
		document.getElementById('now_load_status').style.display = "none";
		document.getElementById('local_load_status').style.display = "none";
//		document.status.style.display = "none";
		document.status.src = "./img/complete.gif";
		}
			}
		}
		else{
			if(now_stock_flag == 0){
			now_stock_num = count;
			now_stock_flag = 1;
			}
		return false;
		}
		now_stock_flag = 0;
		
		}	
	}

	function count_local_cache(js_total_num){
	if(finish_flag == 0){
		myHandle  = setInterval('cache_check(' + js_total_num + ')',1000);	
		}
	}


var target_num = 0;

// apply all image with setting
	function apply_setting(){
	}

	function apply_setting_with_param(change_power,change_negate_check,change_rotate_check,change_wl,change_ww){
	}


//END -->
</script>

<style id="main" type="text/css">
body {background-color:#000000; font-family:Arial,Helvetica,sans-serif; font-size:12px;}

#information { 
     background-color: transparent; text-align: left;
     position: absolute; top: 50px; left: 0px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#image_number { 
     background-color: transparent; text-align: left;
     position: absolute; top: 100px; left: 0px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#wl_ww_value { 
     background-color: transparent; text-align: left;
     position: absolute; top: 120px; left: 0px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#load_status { 
     background-color: transparent; text-align: left;
     position: absolute; top: 120px; left: 0px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#local_load_status { 
     background-color: transparent; text-align: left;
     position: absolute; top: 50px; left: 180px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#now_load_status { 
     background-color: transparent; text-align: left;
     position: absolute; top: 70px; left: 180px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#distance { 
     background-color: transparent; text-align: left;
     position: absolute; top: 50px; left: 180px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#huv { 
     background-color: transparent; text-align: left;
     position: absolute; top: 50px; left: 250px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#create_check_low { 
     background-color: transparent; text-align: left;
     position: absolute; top: 70px; left: 180px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#create_check_high { 
     background-color: transparent; text-align: left;
     position: absolute; top: 90px; left: 180px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

#create_check_AJAX { 
     background-color: transparent; text-align: left;
     position: absolute; top: 115px; left: 180px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 15pt ;
     font-weight : normal;  }

.image1 {border-style:solid;border-width:2px;border-color: #ffffff;}

#table_position { 
     background-color: transparent; text-align: left;
     position: absolute; top: 25px; left: 30px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#select_menus { 
     background-color: transparent; text-align: left;
     position: absolute; top: 0px; left: 0px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#menubar { 
     background-color: transparent; text-align: left;
     position: absolute; top: 0px; left: 60px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#image_prop_data { 
     background-color: transparent; text-align: left;
     position: absolute; top: 25px; left: 8px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#wlww_data { 
     background-color: transparent; text-align: left;
     position: absolute; top: 0px; left: 280px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#status_menu { 
     background-color: transparent; text-align: left;
     position: absolute; top: 30px; left: 5px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#scale_image { 
     background-color: transparent; text-align: left;
     position: absolute; top: 150px; left: 5px; 
     width: 250px; height: 20px; visibility: visible;  z-index : 0;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }


#canvas {
	position: absolute; top: 50px; left: 0px; 
	border-style: solid;
	border-color: Darkslateblue;
	border-width: 0px;
	margin: 0px;
}

#info {color:white}
</style>

<link rel="stylesheet" href="menus.css" type="text/css" />


<script language="JavaScript">
//  onmousemove="big(event);"
</script>

</head>

<?php 
//print("<body  onDrag='return false;'>");


print("<body  onDrag='return false;'  style=\"overflow:hidden;\" onResize=\"resize_pack()\" >");



?>


<script language="JavaScript">
//if(document.images){

	var imagestest = new Object();
	var dicomset = new Object();
	var jpgset = new Object();
	var referset = new Object();
	var tableset = new Object();
	var tableset_x = new Object();
	var tableset_y = new Object();
	var tableset_z = new Object();
	
	var multiframe_flag = 0;




<?php


//
//	2002.1.1
//	DICOM to WWW Gateway :PHP script
//	Made by Norya Yokohama % Medical Division @ WERC
//  2004.9.19
//  added by tadashi tsuchimoto @ MITT belong to yokohamazyuku
//  show images strongly powered by Yokohama Zyukutyou m(v_v)m
//

  include "init.inc";
  include "param_setting.ini";
  include "sql_generator.ini";
  include "file_mod.inc";
  $include_path=".";
  require("$include_path/dbconnect.ini");


//DB query

$db_handle = db_contact_start($db_type,$target_host,$target_db);

//patient query

$sql = sql_generator("show_image");
$rtn_image = $db_handle->prepare($sql);
$rtn_image->bindParam(1, $seriesUID);
$rtn_image->bindParam(2, $studyUID);



$rtn_image->execute();


 
 
  @$patid=chop($patid);
  @$name=chop($name);

  @$fdate=formatDate(chop($date));






//tmpディレクトリ作成・取得

if(!$ww){
	$wwwl = "default";
	}
if($ww){
	$wwwl = $ww . $wl;
	}


$studyUID=trim($studyUID);
$seriesUID=trim($seriesUID);
@$target_dir=touch_dir($studyUID,$seriesUID,$wwwl);

//make button exec applet viewer
@$target_html=($target_path . "viewer.html");



$js_count_start = 0;


$js_count_end = $rtn_image->rowCount();
$image_total_num = $js_count_end ;

 print("var image_num_start = $js_count_start;");
 print("var image_num_end = $js_count_end;");


//js dicom param
 print("var dicom_path = \"\";");
 print("var jpg_path = \"\";");
 print("var refer_path = \"\";");
 print("var scale_ratio = \"\";");




//anotatin font

		$font = 10;
	$i = "0";
	$starter_tag = 0;

//table position array

$table_set_x = array();
$table_set_y = array();
$table_set_z = array();

$multiframe_num = array();

  while ($image_row = $rtn_image->fetch(PDO::FETCH_ASSOC)) {
//	print_r($patient_row);
	$path = $image_row['path'] ;
	$sernum = $image_row['sernum'] ;
	$iman = $image_row['imanum'] ;
	
	if($starter_tag == 1){
	print("<script language='JavaScript'>");
	}




	
	//add dcm4chee root path
	$path = $file_root_path . $path;
	

	

	//file path set
    $file_name=basename($path);
     
    $target_path=($target_dir . "/" );     
    $target_file_path=$target_path . $file_name . ".dcm";



	//Symbolic link original file to target files. 
	if(is_link($target_file_path)){
		if($path != linkinfo($target_file_path)){
		system("rm -f $target_file_path");
		symlink($path,$target_file_path);
		}
	}
	else{
		symlink($path,$target_file_path);
	}


	if(!@$tag_param_check ){
	$name_in_dicom = get_dicom_value("0010,0010",$target_file_path);
	//id
	$id_in_dicom = get_dicom_value("0010,0020",$target_file_path);
	//row 
	$row = get_dicom_value("0028,0010",$target_file_path);
	//col
	$col = get_dicom_value("0028,0011",$target_file_path);
	//slice thick etc
	@$slice_thick = get_dicom_value("0018,0050",$target_file_path);
	@$slice_between = get_dicom_value("0018,0088",$target_file_path);
	
	@$patient_position = get_dicom_value("0018,5100",$target_file_path);
	
	
	//
	$multiframe_tag = get_dicom_value("0028,0008",$target_file_path);
	
	if($multiframe_tag and $multiframe_tag != "" ){
	//print("alert(\"$multiframe_tag\");");
	print("multiframe_flag = 1;");
	$multiframe_flag = 1;
	$multiframe_num[$i] = $multiframe_tag;
	}
	
	
	// pixel space
	$ps_origin = get_dicom_value("0028,0030",$target_file_path);
	
	$p_size = explode("\\",$ps_origin);
	if(@$p_size[0] != "" and @$p_size[1] = ""){
	$px_size_x = trim($p_size[0]);
	$px_size_y = trim($p_size[1]);
	}else{
	$px_size_x = 1;
	$px_size_y = 1;
	}

	
	$p_size = $p_size;
	
	
	$matrix = $row . "x" . $col;
	
	$x_parameter = round($px_size_x * $col);
	$y_parameter = round($px_size_y * $row);
	
	
	if($col <= 128 or $row <= 128){
	$power = 3 ;
	 print("power = $power;");
	}elseif($col <= 320 or $row <= 320){
	$power = 2 ;
	 print("power = $power;");
	}
	
	if(!$wl){
	@$wl_from_dicomtag = get_dicom_value("0028,1050",$target_file_path);
	@$ww_from_dicomtag = get_dicom_value("0028,1051",$target_file_path);
	$wl = $wl_from_dicomtag;
	$ww = $ww_from_dicomtag;
	}
	
	//$fov = get_dicom_value("0018,1100",$target_file_path);
	if($x_parameter != "0" and $y_parameter != "0"){
	$fov = "FOV:" . $x_parameter . "x" . $y_parameter . "mm";
	}
	
	if($y_parameter != 0){
	$scale_ratio = (100 / $y_parameter );
	print("scale_ratio = $scale_ratio;");
	}

	$tag_param_check = 1;
	}
	
	

		
			$jpg_file_name_tmp = @$jpg_file_name . ".tmp.jpg" ;




	if(!$wl and $wlww_false != 1){
	$ww = system("$dcm_to_text $target_file_path | grep \"0028 1051\" | awk '{print $3;}'");
	$wl = system("$dcm_to_text $target_file_path | grep \"0028 1050\" | awk '{print $3;}'");

	}

		if($rotate != "0"){
		$rotate_tag = "+" . $rotate;
		}else{
		$rotate_tag = "";
		}
		if($negate !=0){
		$negate_tag = "+P";
		}else{
		$negate_tag = "";
		}
		
		$add_tag = "-" . $ww . "_" . $wl . "_" . $power . "_" . $negate_tag  . "_" . $rotate_tag;
	

	print("imagestest[\"$i\"] = new Image();");


		if(!$wl){
		
		$target_file_path_tmp_jpg = $target_file_path . ".jpg";
		if(!file_exists("$target_file_path_tmp_jpg")){
		if(!$negate and !$power and !$rotate){
		$jpg_file_name=dcmtojpg2($target_file_path);	
		}else{
		$jpg_file_name=dcmtojpg2_filter($target_file_path,$power,$negate,$rotate);
		}
		}else{ $jpg_file_name = $target_file_path_tmp_jpg; }
		$tmp_file_name = $target_file_path;
		}
		else{
		$target_file_path_tmp_jpg = $target_file_path . $add_tag . ".jpg";
		if(!file_exists("$target_file_path_tmp_jpg")){
		if(!$negate and !$power and !$rotate){
		$jpg_file_name=dcmtojpg2_set($target_file_path,$ww,$wl);
		}else{
		$jpg_file_name=dcmtojpg2_set_filter($target_file_path,$power,$negate,$rotate,$ww,$wl);
		}
		}else{ $jpg_file_name = $target_file_path_tmp_jpg; }
		}
		
		 if(!file_exists("$target_file_path_tmp_jpg")){
		print("imagestest[\"$i\"].src = \"$jpg_file_name\";");
		print("jpgset[\"$i\"] = \"$jpg_file_name\";");
		}
		else{
		print("imagestest[\"$i\"].src = \"$target_file_path_tmp_jpg\";");
		print("jpgset[\"$i\"] = \"$target_file_path_tmp_jpg\";");
		}

	print("dicomset[\"$i\"] = \"$target_file_path\";");
	print("referset[\"$i\"] = \"$jpg_file_name_tmp\";");

	
	
	//get table position
	@$table_position = get_dicom_value("0020,0032",$target_file_path);
	$table_position = str_replace("\\",":",trim($table_position));
	
	$table_array = explode(":",$table_position);
	
	if(@$table_array[0] != "" and @$table_array[1] != "" and@$table_array[2] != ""){
	print("tableset_x[\"$i\"] = \"$table_array[0]\";");
	print("tableset_y[\"$i\"] = \"$table_array[1]\";");
	print("tableset_z[\"$i\"] = \"$table_array[2]\";");
	}else{
	print("tableset_x[\"$i\"] = \"no position data\";");
	print("tableset_y[\"$i\"] = \"no position data\";");
	print("tableset_z[\"$i\"] = \"no position data\";");
	}

	
	$table_set_x[] = @$table_array[0];
	$table_set_y[] = @$table_array[1];
	$table_set_z[] = @$table_array[2];
	
	print("tableset[\"$i\"] = \"$table_position	\";");
	

	if($starter_tag == 1){
	$convert_num = $i + 1;
	print("document.image_prop.now.value = \"$convert_num\";");
	print("document.getElementById('now_load_status').innerHTML = \"server:$convert_num\";");
	
	}


	if($i == 2){
		if($table_set_x[0] == $table_set_x[1] and $table_set_x[0] == $table_set_x[2]){
		$canditate_x_test0 = $table_set_x[0];
		$canditate_x_test1 = $table_set_x[1];
		$canditate_x_test2 = $table_set_x[2];
		
			$canditate_x = 1;
		}else{$canditate_z = 0;}
		if($table_set_y[0] == $table_set_y[1] and $table_set_y[0] == $table_set_y[2]){
			$canditate_y = 1;
		}else{$canditate_y = 0;}
		if($table_set_z[0] == $table_set_z[1] and $table_set_z[0] == $table_set_z[2]){
			$canditate_z = 1;
		}else{$canditate_z = 0;}
		
		
		if((@$canditate_x == 1) and (@$canditate_y == 1)){
		$variable_orientation = "z";
		print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		}
		elseif((@$canditate_y == 1) and (@$canditate_z == 1)){
		$variable_orientation = "x";
		print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		}
		elseif((@$canditate_z == 1) and (@$canditate_x == 1)){
		$variable_orientation = "y";
		print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		}elseif((@$canditate_z == 1) or (@$canditate_x == 1) or (@$canditate_y == 1)){
		$variable_orientation = "variable";
		print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		}
	}
	
	
	//write dicom file path
	$imgURL=("imgURL" . $i);


	if($starter_tag == 0){

	$resolution = ltrim(exec("identify  $jpg_file_name | awk {'print $3'} | sed 's/+/x/g'"));
$res_list = split("x", "$resolution");
print("var resolution_x = $res_list[0];");
print("var resolution_y = $res_list[1];");
$res_value_x = $res_list[0];
$res_value_y = $res_list[1];
if($res_value_x != $res_value_y){
	if($res_value_x > $res_value_y){
	$mag_rate_y = $res_value_y / $res_value_x;
//		$mag_rate_y = round($mag_rate_y, 2);
	$mag_rate_x = 1;
	}
	else{
	$mag_rate_x = $res_value_x / $res_value_y;
//		$mag_rate_x = round($mag_rate_x, 2);
	$mag_rate_y = 1;
	}
}else{
$mag_rate_x = 1;
$mag_rate_y = 1;
}



//transfer position data

print("window.opener.document.getElementById('$seriesUID').patient_position.value = '$patient_position';");
print("window.opener.document.getElementById('$seriesUID').image_num.value = num;");
print("window.opener.document.getElementById('$seriesUID').position_x.value = tableset_x[num];");
print("window.opener.document.getElementById('$seriesUID').position_y.value = tableset_y[num];");
print("window.opener.document.getElementById('$seriesUID').position_z.value = tableset_z[num];");
	
?>

//	Wait();

// zooming function

var now_size="";
var now_win_size="";
var zoom_flag = 0;
var non_zoom_width_size = "";
var non_zoom_height_size = "";



//image wheel defult tag
var default_tag = negate_check + ":" + rotate_check + ":" + power + ":" + wl + ":" + ww;

function ImageChange(){
  if(document.getElementById){

	if(window.innerWidth > window.innerHeight){
	document.getElementById("image1").width = (window.innerHeight - 4)*<?php echo $mag_rate_x ?>;
	document.getElementById("image1").height = (window.innerHeight - 4)*<?php echo $mag_rate_y ?>;
	non_zoom_width_size = document.getElementById("image1").width;
	non_zoom_height_size = document.getElementById("image1").height
		if(document.getElementById("scale")){
		document.getElementById("scale").height = (scale_ratio)*((window.innerHeight - 4)*<?php echo $mag_rate_y ?>);
		}
	now_size =   window.innerHeight - 0;
	}
	else{
	document.getElementById("image1").width = (window.innerWidth - 4)*<?php echo $mag_rate_x ?>;
	document.getElementById("image1").height = (window.innerWidth - 4)*<?php echo $mag_rate_y ?>;
	non_zoom_width_size = document.getElementById("image1").width;
	non_zoom_height_size = document.getElementById("image1").height
		if(document.getElementById("scale")){
		document.getElementById("scale").height = (scale_ratio)*((window.innerWidth - 4)*<?php echo $mag_rate_y ?>);
		}
	now_size =  window.innerHeight - 0;
	}
	//alert(non_zoom_width_size +" "+ non_zoom_height_size);
}
}

function pixel_to_mm_change(val){
/*
	if(window.innerWidth > window.innerHeight){
		var result = val / ((scale_ratio) * document.getElementById("image1").heigh / 100 );
	}
	else{
		var result = val / ((scale_ratio) * document.getElementById("image1").width / 100 );
	}
*/
	var result = val / ((scale_ratio) * document.getElementById("image1").width / 100 );
//	alert(val);
//	alert(val);
	return result;
}


function x_point_calc(val){
		var result = Math.round(val * ( <?php echo $col ?> / document.getElementById("image1").width ));
	return result;
}
function y_point_calc(val){
		var result = Math.round((val) * ( <?php echo $row ?> / document.getElementById("image1").height ));
	return result;
}



function echo_paging(){

if(window.opener.document.getElementById('<?php echo $seriesUID?>'))
{
 
//var sample = window.opener.document.getElementById('<?php echo $seriesUID?>').value;
//var sample = document.getElementById('table_position').innerHTML;

//alert(sample); 

//window.opener.document.getElementById('<?php echo $seriesUID?>').value = sample;

window.opener.document.getElementById('<?php echo $seriesUID?>').value = num;
}else{
alert('cant not catch wlww_data');
}

}

</script>	



<table>
<tr><td>





&nbsp





<div id="select_menus">

<a href="javascript:echo_dicom()"><img SRC=./img/dicom.gif></a>


<a href="javascript:echo_refer()"><img SRC=./img/refer.gif ></a>



<!--
<a href="javascript:echo_paging()"><img SRC=./img/refer.gif ></a>
-->



</div>

</td>
<td>


<div id="menubar">
<img id="menuImg_0" 
class="menuImg" src="./img/set_off.gif" border="0">
<img id="menuImg_1" 
class="menuImg" src="./img/mode_off.gif" border="0">
<img id="menuImg_2" 
class="menuImg" src="./img/catalog_off.gif" border="0">
<img id="menuImg_3" 
class="menuImg" src="./img/filter_off.gif" border="0">
<img id="menuImg_4" 
class="menuImg" src="./img/mag_off.gif" border="0">
</div>



</td>

<td>


<div id="wlww_data" style="display:none; margin-left:0px">
<form name="wlww" action="show_image.php" method="GET"  >
<input type=hidden  size="3" name="wl">
<input type=hidden  size="3" name="ww">
<input type=hidden name=studyUID value="<? echo $studyUID; ?>">
<input type=hidden name=seriesUID value="<? echo $seriesUID; ?>">
<input type=hidden name=negate value="<? echo $negate; ?>">
<input type=hidden name=rotate value="<? echo $rotate; ?>">
<input type=hidden name=power value="<? echo $power; ?>">
<input type=hidden name=win_flag value="1">
<input type="submit" value="apply">
</form>
</div>

</td>
<td>
<form name="image_prop">
<div id="image_prop_data" style="display:block; margin-left:20px">
<font color=white></font><input type="hidden" size="2" name="present">
<font color=white></font><input type="hidden" size="2" name="local">
<font color=white></font><input type="hidden" size="2" name="now">
<font color=white></font><input type="hidden" size="2" name="max">
</form>
</div>
</td>
</tr>
</table>


</div>

<?php 



if(@$wl and @$ww){
print("<script language=\"JavaScript\"><!--");
print("document.wlww.wl.value = $wl;");
print("document.wlww.wl.value = $ww;");
print("</script>");
}


@$size_mod = $size - 5 ;

?>







<script language="JavaScript"><!--

//<IMG SRC=$jpg_file_name WIDTH="$size" height="$size" name=main id=image1 > 

document.write("<IMG src='"+baseimg+"'name='main' id='image1' class=image1 height=<?php echo $size_mod?>  width=<?php echo $size_mod ?>  onSelectStart='return false;' style='position:absolute;top:"+Py+"px;left:"+Px+"px; '>" );

//document.write("<IMG src='"+baseimg+"'name='hyouji'  id='hyouji' style='position:absolute;top:0px;left:0px;display:none;'>");

</script>	


<script language='JavaScript'>


//  document.images[7].src = imagestest[0].src;
//  document.images[8].src = imagestest[0].src;



  document.main.src = imagestest[0].src;
  //document.hyouji.src = imagestest[0].src;



//  document.onmousemove = mouseMove;

  dicom_path = dicomset[0];
  jpg_path = jpgset[0];
  refer_path = referset[0];
  document.onkeydown = key_press;
  document.onmousewheel = wheel;
  document.addEventListener("DOMMouseScroll",wheel2,false);

  initMenus();
  <?php 
  if(!$win_flag){
  print("tile();");
  }
  ?>
  set();
  resize_pack();
  count_local_cache(<?php echo $image_total_num ?>);
  
  document.image_prop.max.value = <?php echo $image_total_num ?>;
  document.image_prop.present.value = 1;



</script>

	

<div id="information">
<?php 

@$info_tag =  $id_in_dicom . " <b>" . $name_in_dicom . "</b> " . $date . "<br>" . $fov . " " . $col . "x" . $row . "<br>thick" . $slice_thick . " Space" . $slice_between . "<br><b>". $serdes . "</b><br>"  . $table_psition;

echo $info_tag;

?>



</div>

<div id="image_number">
</div>
<div id="wl_ww_value">
</div>
<div id="table_position">
</div>
<div id="load_status">
</div>
<div id="local_load_status">
</div>
<div id="now_load_status">
</div>
<div id="distance">
</div>
<div id="huv">
</div>
<div id="create_check_low">
</div>
<div id="create_check_high">
</div>
<div id="create_check_AJAX">
</div>


<div id="status_menu">
<img id="status" src="./img/ajax-loader.gif" >
</div>

<div id="scale_image">
<img id="scale" src="./img/scale_img.gif" >
</div>

<div id="canvas"></div>

	
	<?php
	$starter_tag = 1;
	}
	if($starter_tag == 1){

	print("</script>");
	}

$i = $i + 1;

}


system("chmod -R 777 tmp/$patid");

print("<script language='JavaScript'>");

print("ImageChange();");

print("document.image_prop.max.value = $image_total_num ;");



	if($ww_from_dicomtag){
		print("wl = $wl_from_dicomtag;");
		print("ww = $ww_from_dicomtag;");
	}

print("var canvas = buildCanvas(screen.width,screen.height);");


print("document.getElementById(\"canvas\").style.display = \"none\";");


print("</script>");



?> 


</script>


<?php
	
	print("<script language='JavaScript'>");
	if(@$multiframe_flag == 1 ){
	

	
	$sql = sql_generator("show_image");
$rtn_image = $db_handle->prepare($sql);
$rtn_image->bindParam(1, $seriesUID);
$rtn_image->bindParam(2, $studyUID);



$rtn_image->execute();

	
	
	  while ($image_row = $rtn_image->fetch(PDO::FETCH_ASSOC)) {
	  	$i = 0;
		$path = $image_row['path'] ;
		$sernum = $image_row['sernum'] ;
		$iman = $image_row['imanum'] ;
		    $file_name=basename($path);
    $target_path=($target_dir . "/" );
    $target_file_path_without_dcm=$target_path . $file_name ;
    $target_file_path=$target_path . $file_name . ".dcm";
	
	
	//for dcm4chee 
	$path = $file_root_path . $path;
	

	$dcm2pnm = "/usr/local/dicom/bin/dcmj2pnm";
	system("$dcm2pnm +Fa +oj  $target_file_path $target_file_path");
	
	//Symbolic link original file to target files. 
	if(is_link($target_file_path)){
		if($path != readlink($target_file_path)){
		system("rm -f $target_file_path");
		symlink($path,$target_file_path);
		}
	}
	else{
		symlink($path,$target_file_path);
	}
	
	$fn = trim($multiframe_num[$i]);

	$fn_addone = $fn + 1 ;
	$image_total_num = $fn;
	print("image_num_end = $fn ;");

	
	$j_min = 1;
	
	for ($j=0;$j < $fn_addone;$j++) {
		

		@$frag = $pre1 . $j;
		$png_file=($target_file_path_without_dcm . $frag . ".png");
	
		$jpg_file_name = $target_file_path . "." . $j . ".jpg";




			$jpg_file_name_tmp = "$jpg_file_name" . ".tmp.jpg" ;

	
	@$imagesrc = "imagesrc" . "$j_min";
	print("imagestest[\"$j\"] = new Image();");

	print("imagestest[\"$j\"].src = \"$jpg_file_name\";");
	print("jpgset[\"$j\"] = \"$jpg_file_name\";");
	
	print("dicomset[\"$j\"] = \"$target_file_path\";");
	print("referset[\"$j\"] = \"$jpg_file_name_tmp\";");

	if($starter_tag == 1){
	$convert_num = $j_min + 1;
	print("document.image_prop.now.value = \"$convert_num\";");
	}



		


		}
		$i += 1;
	  }
	}
	print("</script>");
?>



</body>
</html>
