<?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'];

@$ruid = $_GET['ruid'];
@$patid = $_GET['patid'];

if(!@$negate or $negate == ""){$negate = 0;$negate_tag="";}
if(!@$rotate or $rotate == ""){$rotate = 0;$rotate_tag="";}
if(!@$power or $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] . " min:" . $str[1] . " 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();
	
?>

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

<style type="text/css">
body
{
	margin: 0px;
	padding: 0px;
}



div.draggable
{

	width: 100px;
    background-color: black;  
	margin: 0px;
	padding: 0px;
}
div.draggable img
{
	cursor: move;
}

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

#main_palette_0_info { 
     background-color: transparent; text-align: left;
	 visibility: visible;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

#main_palette_1_info { 
     background-color: transparent; text-align: left;
	 visibility: visible;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }
	 
#main_palette_2_info { 
     background-color: transparent; text-align: left;
	 visibility: visible;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }
	 
#main_palette_3_info { 
     background-color: transparent; text-align: left;
	 visibility: visible;
	 color : white ;
	 font-size   : 10pt ;
     font-weight : normal;  }

.tooltip{
width: 200px; color:#000;
font:lighter 11px/1.3 Arial,sans-serif;
text-decoration:none;text-align:center}

.tooltip span.top{padding: 30px 8px 0;
    background: url(bt.gif) no-repeat top}

.tooltip b.bottom{padding:3px 8px 15px;color: #548912;
    background: url(bt.gif) no-repeat bottom}

</style>


<script type="text/javascript"
    src="./jquery/jquery.js">
</script>


<script type="text/javascript"
    src="./interface-1.2/interface.js">
</script>

<script src="./libjs/DrawingCanvas.js" type="text/javascript"></script>

<script src="./BubbleTooltips/BubbleTooltips.js" type="text/javascript"></script>


<script language="JavaScript">
	
//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 ;?>";



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

		jpg_file_name_tmp = jpg_file_name + '.tmp.jpg' ;
		
		var num = image_count_now[active_target_id];
		imagestest[active_target_id][num].src = jpg_file_name;
		jpgset[active_target_id][num] = jpg_file_name;
		//referset[active_target_id][num] = jpg_file_name_tmp;
		var target_img = active_target_id + "_img"
            document.getElementById(target_img).src = imagestest[active_target_id][num].src;
           
		
		if(main_palette_id[0] == active_target_id){
		document.getElementById('main_palette_0_img').src = imagestest[active_target_id][num].src;
		document.getElementById('main_palette_0_img_mag').src = imagestest[active_target_id][num].src;
		}
		if(main_palette_id[1] == active_target_id){
		document.getElementById('main_palette_1_img').src = imagestest[active_target_id][num].src;
		document.getElementById('main_palette_1_img_mag').src = imagestest[active_target_id][num].src;
		}
		if(main_palette_id[2] == active_target_id){
		document.getElementById('main_palette_2_img').src = imagestest[active_target_id][num].src;
		document.getElementById('main_palette_2_img_mag').src = imagestest[active_target_id][num].src;
		}
		if(main_palette_id[3] == active_target_id){
		document.getElementById('main_palette_3_img').src = imagestest[active_target_id][num].src;
		document.getElementById('main_palette_3_img_mag').src = imagestest[active_target_id][num].src;
		}
		
		jpeg_return_check = true;


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

	function do_convert(convert_target,sub_wl,sub_ww,negate_check,rotate_check,power) {
		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[active_target_id][value[1]].src = jpg_file_name;
		jpgset[active_target_id][value[1]] = jpg_file_name;
		referset[active_target_id][value[1]] = jpg_file_name_tmp;
		
		var num = image_count_now[active_target_id];
		var target_img = active_target_id + "_img"
		
		if(image_count_now[active_target_id] == num_tag){
			document.getElementById(target_img).src = imagestest[active_target_id][num].src;
			if(main_palette_id[0] == active_target_id){
			document.getElementById('main_palette_0_img').src = imagestest[active_target_id][num].src;
			document.getElementById('main_palette_0_img_mag').src = imagestest[active_target_id][num].src;
		}
			if(main_palette_id[1] == active_target_id){
			document.getElementById('main_palette_1_img').src = imagestest[active_target_id][num].src;
			document.getElementById('main_palette_1_img_mag').src = imagestest[active_target_id][num].src;
			}
			if(main_palette_id[2] == active_target_id){
			document.getElementById('main_palette_2_img').src = imagestest[active_target_id][num].src;
			document.getElementById('main_palette_2_img_mag').src = imagestest[active_target_id][num].src;
			}
			if(main_palette_id[3] == active_target_id){
			document.getElementById('main_palette_3_img').src = imagestest[active_target_id][num].src;
			document.getElementById('main_palette_3_img_mag').src = imagestest[active_target_id][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);
	}


//ajax end


	var study_series_list = new Array();
	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 target_orientation = new Object();
	var dicom_path = new Object();
	var jpg_path = new Object();
	var refer_path = new Object();
	var scale_ratio = new Object();
	var fov = new Object();
	var col_from_dicomtag = new Object();
	var row_from_dicomtag = new Object();
	var wl_from_dicomtag = new Object();
	var ww_from_dicomtag = new Object();
	var patient_position = new Object();
	var name_in_dicom = new Object();
	var id_in_dicom = new Object();
	var slice_thick = new Object();
	var slice_between = new Object();
	var px_size_x = new Object();
	var px_size_y = new Object();
	var x_y_ratio = new Object();
	var matrix = new Object();
	var image_information = new Object();
	var series_description = new Object();
	var main_palette_id = new Object();
	var array_multiframe_tag = new Object();
	
	var power_each_series = new Object();
	var negate_each_series = new Object();
	var rotate_each_series = new Object();
	
	var image_count_now = new Object();
	var image_count_server = new Object();
	var image_count_cache = new Object();
	var image_count_max = new Object();

	var window_width = window.innerWidth;
	var window_height = window.innerHeight;
	var window_width_half = (window_width - 200)/ 2 ;
	var window_width_position =  window_width_half + 200;
	var window_height_half = window_height / 2 ;
	var window_height_position = window_height_half;
	
	if(((window_width - 200)/ 2) > (window_height / 2)){
	var palette_size = (window_height / 2);
	}else{
	var palette_size = ((window_width - 200)/ 2);
	}
	
	var enlarge_flag = 0;
	var enlarge_palette_num ;
	
	var sync_flag_for_series = new Object();
	var multiframe_flag_for_series = new Object();
	var sync_flag = 1;
	var sync_original_val_for_series = new Object();
	var sync_changed_val_for_series = new Object();
	var sync_target_axis_for_series = new Object();
	var sync_defference_val_for_series = new Object();
	var sync_defference_val = 0;
	
	var function_list = new Array();
	var single_img_function_list = new Array();
	var apply_series_function_list = new Array();
	var parameter_function_list = new Array();
	var divide_function_list = new Array();
	

var active_key_value = 0;

function active_key0() {
	active_key_value = 0;
}
function active_key1() {
	active_key_value = 1;
}
function active_key2() {
	active_key_value = 2;
}
function active_key3() {
	active_key_value = 3;
}

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(active_key_value);
           } else if ( (charCode == 33) || (charCode == 8) || (charCode == 38)  || (charCode == 50)) {
               setImagedw(active_key_value);
           } else {
		       return false;
		   }
       }
    }
}


function wheel0(e){
	  e.preventDefault();
  var delta = e.detail * -1;
  	if(delta <0){
		setImageup(0);
	}else{
		setImagedw(0);
	}
}
function wheel1(e){
	  e.preventDefault();
  var delta = e.detail * -1;
  	if(delta <0){
		setImageup(1);
	}else{
		setImagedw(1);
	}
}
function wheel2(e){
	  e.preventDefault();
  var delta = e.detail * -1;
  	if(delta <0){
		setImageup(2);
	}else{
		setImagedw(2);
	}
}
function wheel3(e){
	  e.preventDefault();
  var delta = e.detail * -1;
  	if(delta <0){
		setImageup(3);
	}else{
		setImagedw(3);
	}
}


function cacth_paging(seriesUID,target_palette_id){



	var orig_image_num = image_count_now[seriesUID] ;
	var orig_position_x = tableset_x[seriesUID][orig_image_num] ;
	var orig_position_y = tableset_y[seriesUID][orig_image_num] ;
	var orig_position_z = tableset_z[seriesUID][orig_image_num] ;
	var orig_target_orientation = target_orientation[seriesUID] ;

	//var list_num = study_series_list.length;

	for(i = 0; i < study_series_list.length; i++){
		var target_seriesUID = study_series_list[i];
		var refered_orientation = target_orientation[target_seriesUID] ;
		if(seriesUID != target_seriesUID){
			if(orig_target_orientation == refered_orientation){
						catch_scrolling(seriesUID,target_seriesUID,orig_image_num,orig_target_orientation,orig_position_x,orig_position_y,orig_position_z,target_palette_id);
			}
		}else{
		var target_seriesUID_with_img = target_seriesUID + "_img"
		document.getElementById(target_seriesUID_with_img).src = imagestest[seriesUID][orig_image_num].src;
		}
	}
	
	


if( main_palette_id[0] == seriesUID && target_palette_id != 0){
document.main_palette_0_img.src = imagestest[seriesUID][orig_image_num].src;
document.getElementById('main_palette_0_info').innerHTML = image_information[seriesUID][orig_image_num];
if(negate_each_series[seriesUID] == 1){
document.getElementById('main_palette_0_info').style.color  = "black";
}else{
document.getElementById('main_palette_0_info').style.color  = "white";
}
}
if( main_palette_id[1] == seriesUID && target_palette_id != 1){
document.main_palette_1_img.src = imagestest[seriesUID][orig_image_num].src;
document.getElementById('main_palette_1_info').innerHTML = image_information[seriesUID][orig_image_num];
if(negate_each_series[seriesUID] == 1){
document.getElementById('main_palette_1_info').style.color  = "black";
}else{
document.getElementById('main_palette_1_info').style.color  = "white";
}
}
if( main_palette_id[2] == seriesUID && target_palette_id != 2){
document.main_palette_2_img.src = imagestest[seriesUID][orig_image_num].src;
document.getElementById('main_palette_2_info').innerHTML = image_information[seriesUID][orig_image_num];
if(negate_each_series[seriesUID] == 1){
document.getElementById('main_palette_2_info').style.color  = "black";
}else{
document.getElementById('main_palette_2_info').style.color  = "white";
}
}
if( main_palette_id[3] == seriesUID && target_palette_id != 3){
document.main_palette_3_img.src = imagestest[seriesUID][orig_image_num].src;
document.getElementById('main_palette_3_info').innerHTML = image_information[seriesUID][orig_image_num];
if(negate_each_series[seriesUID] == 1){
document.getElementById('main_palette_3_info').style.color  = "black";
}else{
document.getElementById('main_palette_3_info').style.color  = "white";
}
}

}



function catch_scrolling(orig_seriesUID,target_seriesUID,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 ;


if(sync_flag_for_series[target_seriesUID]  == 1){
	switch(selected_table){
	case 'z':
	if(orig_seriesUID != target_seriesUID ) {
		if(sync_defference_val_for_series[orig_seriesUID] != 0){
		value_z = value_z - Math.abs(sync_defference_val_for_series[orig_seriesUID]) ;
		}
		if(sync_defference_val_for_series[target_seriesUID] != 0){
		value_z = value_z - Math.abs(sync_defference_val_for_series[target_seriesUID]) ;
		}
	}
	for(var scroll_count = 0; scroll_count < image_count_max[target_seriesUID] ; scroll_count++){
		abs = tableset_z[target_seriesUID][scroll_count] - 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 = scroll_count;
		now_min_value = abs;
		}
		if(now_min_value < abs){
		break;
		}
	}
	setImagefit(target_seriesUID,now_min_num);
	break;
	case 'y':
	if(orig_seriesUID != target_seriesUID ) {
		if(sync_defference_val_for_series[orig_seriesUID] != 0){
		value_y = value_y - Math.abs(sync_defference_val_for_series[orig_seriesUID]) ;
		}
		if(sync_defference_val_for_series[target_seriesUID] != 0){
		value_y = value_y - Math.abs(sync_defference_val_for_series[target_seriesUID]) ;
		}
	}
	for(var scroll_count = 0; scroll_count < image_count_max[target_seriesUID] ; scroll_count++){
		abs = tableset_y[target_seriesUID][scroll_count] - 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 = scroll_count;
		now_min_value = abs;
		}
		if(now_min_value < abs){
		break;
		}
	}
	setImagefit(target_seriesUID,now_min_num);
	break;
	case 'x':
	if(orig_seriesUID != target_seriesUID ) {
		if(sync_defference_val_for_series[orig_seriesUID] != 0){
		value_x = value_x - Math.abs(sync_defference_val_for_series[orig_seriesUID]) ;
		}
		if(sync_defference_val_for_series[target_seriesUID] != 0){
		value_x = value_x - Math.abs(sync_defference_val_for_series[target_seriesUID]) ;
		}
	}
	for(var scroll_count = 0; scroll_count < image_count_max[target_seriesUID] ; scroll_count++){
		abs = tableset_x[target_seriesUID][scroll_count] - 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 = scroll_count;
		now_min_value = abs;
		}
		if(now_min_value < abs){
		break;
		}
	}
	setImagefit(target_seriesUID,now_min_num);
	break;
	case 'variable':
	for(var scroll_count = 0; scroll_count < image_count_max[target_seriesUID] ; scroll_count++){
		if(tableset_x[target_seriesUID][scroll_count] == value_x && tableset_y[target_seriesUID][scroll_count] == value_y && tableset_z[target_seriesUID][scroll_count] == value_z ){
		now_min_num = scroll_count;
		setImagefit(target_seriesUID,now_min_num);
		break;
		}
	}
	break;
	}
}

}


function setImagefit(target_seriesUID,now_min_num) {

//alert(target_seriesUID);
//alert(target_seriesUID + "_" +now_min_value);



image_count_now[target_seriesUID] = now_min_num;

var target_seriesUID_with_img = target_seriesUID + "_img"

document.getElementById(target_seriesUID_with_img).src = imagestest[target_seriesUID][now_min_num].src;

if( main_palette_id[0] == target_seriesUID){
document.main_palette_0_img.src = imagestest[target_seriesUID][now_min_num].src;document.getElementById('main_palette_0_info').innerHTML = image_information[target_seriesUID][now_min_num];
}
if( main_palette_id[1] == target_seriesUID){
document.main_palette_1_img.src = imagestest[target_seriesUID][now_min_num].src;document.getElementById('main_palette_1_info').innerHTML = image_information[target_seriesUID][now_min_num];
}
if( main_palette_id[2] == target_seriesUID){
document.main_palette_2_img.src = imagestest[target_seriesUID][now_min_num].src;document.getElementById('main_palette_2_info').innerHTML = image_information[target_seriesUID][now_min_num];
}
if( main_palette_id[3] == target_seriesUID){
document.main_palette_3_img.src = imagestest[target_seriesUID][now_min_num].src;document.getElementById('main_palette_3_info').innerHTML = image_information[target_seriesUID][now_min_num];
}




    return false;
}

function setImagedw(target_palette_id) {
		
		var target_id = main_palette_id[target_palette_id];
		active_target_id = target_id;
		
			if (0 == image_count_now[target_id]){
			var next_num = (image_count_max[target_id] - 1);
			}else{
			var next_num = image_count_now[target_id] - 1;
			}
			
			
			if(imagestest[target_id][next_num] && imagestest[target_id][next_num].complete){
			
			if(negate_each_series[target_id] == 0){var negate_check_tag = "";}else{var negate_check_tag = "+P"};
			if(rotate_each_series[target_id] == 0){var rotate_check_tag = "";}else{var rotate_check_tag = "+" + rotate_check};
			
			var compare_tag = negate_each_series[target_id] + ":" + rotate_each_series[target_id] + ":" + power_each_series[target_palette_id] + ":" + wl_from_dicomtag[target_id] + ":" + ww_from_dicomtag[target_id];
			
			
			var path=imagestest[target_id][next_num].src;
			var file=path.substring(path.lastIndexOf('/',path.length)+1,path.length); 
			var path_dicom=dicomset[target_id][next_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_from_dicomtag[target_id] + "_" + wl_from_dicomtag[target_id] +  "_" + power_each_series[target_id] + "_" + negate_check_tag  + "_" + rotate_check_tag + ".jpg";
//			}
			
			
			
			if(file != target_converted_file && multiframe_flag_for_series[target_id] == 0){
			do_convert_for_apply("AJAX",next_num,dicomset[target_id][next_num],wl_from_dicomtag[target_id],ww_from_dicomtag[target_id],negate_each_series[target_id],rotate_each_series[target_id],power_each_series[target_id]);
			
			//alert(log);
			}else{
			
			var next_img = imagestest[target_id][next_num].src;
			
			if(target_palette_id == 0){
			document.main_palette_0_img.src = next_img;
			document.main_palette_0_img_mag.src = next_img;
			document.getElementById('main_palette_0_info').innerHTML = image_information[target_id][next_num];
			}else if(target_palette_id == 1){
			document.main_palette_1_img.src = next_img;
			document.main_palette_1_img_mag.src = next_img;
			document.getElementById('main_palette_1_info').innerHTML = image_information[target_id][next_num];
			}else if(target_palette_id == 2){
			document.main_palette_2_img.src = next_img;
			document.main_palette_2_img_mag.src = next_img;
			document.getElementById('main_palette_2_info').innerHTML = image_information[target_id][next_num];
			}else if(target_palette_id == 3){
			document.main_palette_3_img.src = next_img;
			document.main_palette_3_img_mag.src = next_img;
			document.getElementById('main_palette_3_info').innerHTML = image_information[target_id][next_num];
			}
			
			}
			
			image_count_now[target_id] = next_num;
			if(sync_flag_for_series[target_id] == 1){
			cacth_paging(target_id);
			}
			}

}


function setImageup(target_palette_id) {
		
		var target_id = main_palette_id[target_palette_id] ;
		active_target_id = target_id;
		
			if((image_count_now[target_id] + 1) == image_count_max[target_id]){
			var next_num = 0;
			}else{
			var next_num = image_count_now[target_id] + 1;
			}
			
			if(imagestest[target_id][next_num] && imagestest[target_id][next_num].complete){
			
			
			if(negate_each_series[target_id] == 0){var negate_check_tag = "";}else{var negate_check_tag = "+P"};
			if(rotate_each_series[target_id] == 0){var rotate_check_tag = "";}else{var rotate_check_tag = "+" + rotate_check};
			
			var compare_tag = negate_each_series[target_id] + ":" + rotate_each_series[target_id] + ":" + power_each_series[target_palette_id] + ":" + wl_from_dicomtag[target_id] + ":" + ww_from_dicomtag[target_id];
			
			
			var path=imagestest[target_id][next_num].src;
			var file=path.substring(path.lastIndexOf('/',path.length)+1,path.length); 
			var path_dicom=dicomset[target_id][next_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_from_dicomtag[target_id] + "_" + wl_from_dicomtag[target_id] +  "_" + power_each_series[target_id] + "_" + negate_check_tag  + "_" + rotate_check_tag + ".jpg";
//			}
			
			
			
			if(file != target_converted_file && multiframe_flag_for_series[target_id] == 0){
			do_convert_for_apply("AJAX",next_num,dicomset[target_id][next_num],wl_from_dicomtag[target_id],ww_from_dicomtag[target_id],negate_each_series[target_id],rotate_each_series[target_id],power_each_series[target_id]);
			
			
			//alert(log);
			}else{
			
			var next_img = imagestest[target_id][next_num].src;
			
			
			if(target_palette_id == 0){
			document.main_palette_0_img.src = next_img;
			document.main_palette_0_img_mag.src = next_img;
			document.getElementById('main_palette_0_info').innerHTML = image_information[target_id][next_num];
			}else if(target_palette_id == 1){
			document.main_palette_1_img.src = next_img;
			document.main_palette_1_img_mag.src = next_img;
			document.getElementById('main_palette_1_info').innerHTML = image_information[target_id][next_num];
			}else if(target_palette_id == 2){
			document.main_palette_2_img.src = next_img;
			document.main_palette_2_img_mag.src = next_img;
			document.getElementById('main_palette_2_info').innerHTML = image_information[target_id][next_num];
			}else if(target_palette_id == 3){
			document.main_palette_3_img.src = next_img;
			document.main_palette_3_img_mag.src = next_img;
			document.getElementById('main_palette_3_info').innerHTML = image_information[target_id][next_num];
			}
			
			
			}
			
			if(dist_check_flag == true ){
		 		drawFlg = false;
		 		canvas.clear();
				}
			
			image_count_now[target_id] = next_num;
			if(sync_flag_for_series[target_id] == 1){
			cacth_paging(target_id);
			}
			
			
			}


		
    return false;
}


function active_palette(target_palette_num){
	alert(target_palette_num);
}



// enlarge main palette

var enlarged_palette = "";

function palette_enlarge(target_palette_num){
		//alert(target_palette_num);
		
	if(wlww_flag == 0){
		
		if(enlarge_flag == 0){
			enlarge_palette_num = target_palette_num;
			enlarged_palette = target_palette_num;
			
			var main_field_x = window.innerWidth  - 200;
			var main_field_y = window.innerHeight;
			
			var target_id = main_palette_id[target_palette_num];
			//alert(target_id);
		var target_palette = "\'main_palette_" + target_palette_num + "_img\'";
		var target_palette_info = "\'main_palette_" + target_palette_num + "_info\'";
			
			var target_ratio = x_y_ratio[target_id];
			//alert(target_ratio);
			var ratio_power_x = 1 ;
			var ratio_power_y = 1 ;
			if(target_ratio != 1){
				if(target_ratio > 1){
				ratio_power_x = 1 / target_ratio;
				}else{
				ratio_power_y = target_ratio;
				}
			}
			
			for( var i=0; i < 4; i++){
			if(i != target_palette_num){
			var target_palette_none = "\'main_palette_" + i + "_img\'";
			document.getElementById(eval(target_palette_none)).width= 1;
			document.getElementById(eval(target_palette_none)).height = 1;
			document.getElementById(eval(target_palette_none)).style.display="none";
				}
			}
			
			var new_position_x ="200px";
		document.getElementById(eval(target_palette)).style.left = new_position_x ;
			var new_position_y = "0px";
		document.getElementById(eval(target_palette)).style.top = new_position_y ;
		document.getElementById(eval(target_palette)).style.zIndex = 1 ;
			
			if(main_field_x > main_field_y){
		document.getElementById(eval(target_palette)).width = (main_field_y )  * ratio_power_x;
		document.getElementById(eval(target_palette)).height = (main_field_y )  * ratio_power_y;
			}else{
		document.getElementById(eval(target_palette)).width = (main_field_x)  * ratio_power_x;
		document.getElementById(eval(target_palette)).height = (main_field_x)  * ratio_power_y;
			}
			enlarge_flag = 1;
			set();
			document.getElementById("main_palette_0_img_scale").style.zIndex = 0;
			document.getElementById("main_palette_1_img_scale").style.zIndex = 0;
			document.getElementById("main_palette_2_img_scale").style.zIndex = 0;
			document.getElementById("main_palette_3_img_scale").style.zIndex = 0;
			document.getElementById("main_palette_0_info").style.zIndex = 0;
			document.getElementById("main_palette_1_info").style.zIndex = 0;
			document.getElementById("main_palette_2_info").style.zIndex = 0;
			document.getElementById("main_palette_3_info").style.zIndex = 0;
			
			var new_info_set_margin_x = "210px";
			var new_info_set_margin_y = "10px"
		document.getElementById(eval(target_palette_info)).style.left = new_position_x ;
		document.getElementById(eval(target_palette_info)).style.top = new_position_y ;
		document.getElementById(eval(target_palette_info)).style.zIndex = 1 ;
		}else{
		enlarged_palette = "";
		for( var i=0; i < 4; i++){
				var target_palette = "\'main_palette_" + i + "_img\'";
			document.getElementById(eval(target_palette)).style.display="block";
			}
		enlarge_flag = 0;
		resize_pack();
		//window_size_change();
		max_zindex += 1;
		document.getElementById("main_palette_0_img_scale").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_1_img_scale").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_2_img_scale").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_3_img_scale").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_0_info").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_1_info").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_2_info").style.zIndex = max_zindex;
		max_zindex += 1;
		document.getElementById("main_palette_3_info").style.zIndex = max_zindex;
		
		}
	}
}


function select_function(target_palette,target_function){
	for(var i = 0; i < single_img_function_list.length; i++){
		if(single_img_function_list[i] == target_function){
		single_img(target_palette,target_function);
		break;
		}
	}
	for(var i = 0; i < apply_series_function_list.length; i++){
		if(apply_series_function_list[i] == target_function){
		apply_series(target_palette,target_function);
		break;
		}
	}
	for(var i = 0; i < parameter_function_list.length; i++){
		if(parameter_function_list[i] == target_function){
		parameter_setting(target_palette,target_function);
		break;
		}
	}
	
	for(var i = 0; i < divide_function_list.length; i++){
		if(divide_function_list[i] == target_function){
		divide_setting(target_palette,target_function);
		break;
		}
	}
	
	//alert(target_function);
}


function divide_setting(target_palette,target_function){
	//alert(target_palette + " " + target_function);
	if(target_function == "divide-h"){
	divide_horizontal(target_palette);
	}else if(target_function == "divide-v"){
	divide_vertical(target_palette);
	}
}

function single_img(target_palette,target_function){
	//alert(target_palette + " " + target_function);
	if(target_function == "dicom"){
	echo_dicom(target_palette);
	}
	if(target_function == "report"){
	echo_refer(target_palette);
	}
	if(target_function == "sync"){
	sync_mode_change(target_palette);
	}
	if(target_function == "cine"){
	cine_mode(target_palette);
	}
}


function parameter_setting(target_palette,target_function){
	//alert(target_palette + " " + target_function);
	if(target_function == "sensitive"){
	mouse_move_ratio = "10";
	}else if(target_function == "moderate"){
	mouse_move_ratio = "1";
	}else if(target_function == "blunt"){
	mouse_move_ratio = "0.1";
	}
	//alert(mouse_move_ratio);
}

// divide function

function divide_horizontal(target_palette){
	var divide_num=prompt("please input divide number.","");
	if(divide_num == 0 || divide_num == ""){
	alert("divide numver is 0 or null.");
	divide_horizontal(target_palette)
	}else{
	var seriesUID = main_palette_id[target_palette];
	var target_div = document.getElementById(seriesUID);
	var new_image_max = Math.floor( image_count_max[seriesUID] / divide_num);
	
		for(i=0;i<divide_num;i++){
		
		
		var new_seriesUID = seriesUID + '-' + i;
		var new_seriesUID_img = new_seriesUID + '_img'; 
		var new_seriesUID_param = new_seriesUID + '_param'; 
		var new_div = document.createElement('div');
		var new_param = document.createElement('div');
		var new_img = document.createElement('img');
		
		
		study_series_list.push(new_seriesUID) ;
		
		sync_flag_for_series[new_seriesUID] = 1 ;
		multiframe_flag_for_series[new_seriesUID] = 0 ;
		sync_original_val_for_series[new_seriesUID] = 0 ;
		sync_changed_val_for_series[new_seriesUID] = 0 ;
		sync_target_axis_for_series[new_seriesUID] = 0 ;
		sync_defference_val_for_series[new_seriesUID] = 0 ;
		imagestest[new_seriesUID] = new Object();
		dicomset[new_seriesUID] = new Object(); 
		jpgset[new_seriesUID] = new Object(); 
		referset[new_seriesUID] = new Object(); 
		tableset[new_seriesUID] = new Object(); 
		tableset_x[new_seriesUID] = new Object(); 
		tableset_y[new_seriesUID] = new Object(); 
		image_information[new_seriesUID] = new Object(); 
		tableset_z[new_seriesUID] = new Object(); 
		dicom_path[new_seriesUID] = "" ;
		jpg_path[new_seriesUID] = ""; 
		refer_path[new_seriesUID] = "";
		scale_ratio[new_seriesUID] = "";
		
		image_count_cache[new_seriesUID] = 0 ;
		image_count_max[new_seriesUID] = new_image_max ;
		image_count_now[new_seriesUID] = 0;
		
		target_orientation[new_seriesUID] = target_orientation[seriesUID];
		
		power_each_series[new_seriesUID] = power_each_series[seriesUID];
		negate_each_series[new_seriesUID] = negate_each_series[seriesUID];
		rotate_each_series[new_seriesUID] = rotate_each_series[seriesUID] ;


		col_from_dicomtag[new_seriesUID] = col_from_dicomtag[seriesUID]; 
		row_from_dicomtag[new_seriesUID] = row_from_dicomtag[seriesUID] ; 
		
		wl_from_dicomtag[new_seriesUID] = wl_from_dicomtag[seriesUID]; 
		ww_from_dicomtag[new_seriesUID] = ww_from_dicomtag[seriesUID]; 
		fov[new_seriesUID] = fov[seriesUID]; 
		scale_ratio[new_seriesUID] = scale_ratio[seriesUID]; 
		patient_position[new_seriesUID] = patient_position[seriesUID]; 
		name_in_dicom[new_seriesUID] = name_in_dicom[seriesUID]; 
		id_in_dicom[new_seriesUID] = id_in_dicom[seriesUID]; 
		
		series_description[new_seriesUID] = series_description[seriesUID]; 
		
		array_multiframe_tag[new_seriesUID] = array_multiframe_tag[seriesUID]; 
		
		slice_thick[new_seriesUID] = slice_thick[seriesUID]; 
		slice_between[new_seriesUID] = slice_between[seriesUID]; 
		px_size_x[new_seriesUID] = px_size_x[seriesUID]; 
		px_size_y[new_seriesUID] = px_size_y[seriesUID]; 
		x_y_ratio[new_seriesUID] = x_y_ratio[seriesUID]; 
		matrix[new_seriesUID] = matrix[seriesUID]; 
		
		
		new_div.setAttribute("id",new_seriesUID);
		new_div.setAttribute("title",new_seriesUID);
		new_div.setAttribute("class","draggable");
		new_param.setAttribute("id",new_seriesUID_param);
		new_param.setAttribute("title",new_seriesUID);
		new_img.setAttribute("id",new_seriesUID_img);
		new_img.setAttribute("title",new_seriesUID);
		new_img.setAttribute("class","draggable");
		
		new_div.innerHTML = "aaaa";
		
		new_div.style.position = "absolute";
		new_img.style.position = "absolute";
		new_param.style.position = "absolute";
		
		new_img.src = imagestest[seriesUID][0].src;
		
		document.body.appendChild(new_div);
		target_div = document.getElementById(new_seriesUID);
		target_div.appendChild(new_img);
		document.body.appendChild(new_param);
		
		new_div.style.left = "100px";
		new_param.style.left = "100px";

		new_div.style.width = "100px";
		new_div.style.height = "100px";
		new_img.style.width = "100px";
		
		
		document.getElementById(new_seriesUID).addEventListener("DOMMouseScroll",sumnail_scrool,false);
		document.getElementById(new_seriesUID_img).style.position = "absolute";
		document.getElementById(new_seriesUID).style.border = "2px solid green";
		
		document.getElementById(new_seriesUID_param).style.position = "absolute";
		document.getElementById(new_seriesUID_param).style.color = "yellow";
		document.getElementById(new_seriesUID_img).style.left = "0px";
		
		enableTooltips(new_seriesUID);
		
		
		$('div.draggable').Draggable({zIndex:1000,revert:true , ghosting: true, opacity: 0.8});
		sumnail_fit();
		}
	
	
	// image divide loop
	var divide_loop_tag = 0;
	var new_column_num = 0;
	for(i=0;i<image_count_max[seriesUID];i++){
		if(divide_loop_tag == divide_num){
		divide_loop_tag = 0;
		new_column_num += 1;
		}
		var new_seriesUID = seriesUID + '-' + divide_loop_tag;
		
		imagestest[new_seriesUID][new_column_num] = new Image();
		imagestest[new_seriesUID][new_column_num].src = imagestest[seriesUID][i].src ;
		dicomset[new_seriesUID][new_column_num] = dicomset[seriesUID][i] ;
		jpgset[new_seriesUID][new_column_num] = jpgset[seriesUID][i] ;
		referset[new_seriesUID][new_column_num] = referset[seriesUID][i] ;
		tableset_x[new_seriesUID][new_column_num] = tableset_x[seriesUID][i];
		tableset_y[new_seriesUID][new_column_num] = tableset_y[seriesUID][i];
		tableset_z[new_seriesUID][new_column_num] = tableset_z[seriesUID][i];
		image_information[new_seriesUID][new_column_num]  = series_description[new_seriesUID] + '<br>' + 'x:' + tableset_x[new_seriesUID][new_column_num] + 'y:' + tableset_y[new_seriesUID][new_column_num] + 'z:' + tableset_z[new_seriesUID][new_column_num] + '<br>' + fov[new_seriesUID] +  ' ' + matrix[new_seriesUID] + '<br>' + name_in_dicom[new_seriesUID]  +  '<br>' + id_in_dicom[new_seriesUID]  + '<br>' + (new_column_num + 1) + '/' + new_image_max;
		divide_loop_tag += 1;
		
		if(new_column_num == 0){
		new_seriesUID_img = new_seriesUID + "_img";
		new_seriesUID_param = new_seriesUID + "_param";
		document.getElementById(new_seriesUID_img).src = imagestest[seriesUID][i].src ;
		document.getElementById(new_seriesUID_param).innerHTML = "check";
		}
	}
	
	}
}

function divide_vertical(target_palette){
	var divide_num=prompt("please input divide number.","");
	if(divide_num == 0 || divide_num == ""){
	alert("divide numver is 0 or null.");
	divide_horizontal(target_palette)
	}else{
	var seriesUID = main_palette_id[target_palette];
	var target_div = document.getElementById(seriesUID);
	var new_image_max = Math.floor( image_count_max[seriesUID] / divide_num);
	
		for(i=0;i<divide_num;i++){
		
		
		var new_seriesUID = seriesUID + '-' + i;
		var new_seriesUID_img = new_seriesUID + '_img'; 
		var new_seriesUID_param = new_seriesUID + '_param'; 
		var new_div = document.createElement('div');
		var new_param = document.createElement('div');
		var new_img = document.createElement('img');
		
		
		study_series_list.push(new_seriesUID) ;
		
		sync_flag_for_series[new_seriesUID] = 1 ;
		multiframe_flag_for_series[new_seriesUID] = 0 ;
		sync_original_val_for_series[new_seriesUID] = 0 ;
		sync_changed_val_for_series[new_seriesUID] = 0 ;
		sync_target_axis_for_series[new_seriesUID] = 0 ;
		sync_defference_val_for_series[new_seriesUID] = 0 ;
		imagestest[new_seriesUID] = new Object();
		dicomset[new_seriesUID] = new Object(); 
		jpgset[new_seriesUID] = new Object(); 
		referset[new_seriesUID] = new Object(); 
		tableset[new_seriesUID] = new Object(); 
		tableset_x[new_seriesUID] = new Object(); 
		tableset_y[new_seriesUID] = new Object(); 
		image_information[new_seriesUID] = new Object(); 
		tableset_z[new_seriesUID] = new Object(); 
		dicom_path[new_seriesUID] = "" ;
		jpg_path[new_seriesUID] = ""; 
		refer_path[new_seriesUID] = "";
		scale_ratio[new_seriesUID] = "";
		
		image_count_cache[new_seriesUID] = 0 ;
		image_count_max[new_seriesUID] = new_image_max ;
		image_count_now[new_seriesUID] = 0;
		
		target_orientation[new_seriesUID] = target_orientation[seriesUID];
		
		power_each_series[new_seriesUID] = power_each_series[seriesUID];
		negate_each_series[new_seriesUID] = negate_each_series[seriesUID];
		rotate_each_series[new_seriesUID] = rotate_each_series[seriesUID] ;


		col_from_dicomtag[new_seriesUID] = col_from_dicomtag[seriesUID]; 
		row_from_dicomtag[new_seriesUID] = row_from_dicomtag[seriesUID] ; 
		
		wl_from_dicomtag[new_seriesUID] = wl_from_dicomtag[seriesUID]; 
		ww_from_dicomtag[new_seriesUID] = ww_from_dicomtag[seriesUID]; 
		fov[new_seriesUID] = fov[seriesUID]; 
		scale_ratio[new_seriesUID] = scale_ratio[seriesUID]; 
		patient_position[new_seriesUID] = patient_position[seriesUID]; 
		name_in_dicom[new_seriesUID] = name_in_dicom[seriesUID]; 
		id_in_dicom[new_seriesUID] = id_in_dicom[seriesUID]; 
		
		series_description[new_seriesUID] = series_description[seriesUID]; 
		
		array_multiframe_tag[new_seriesUID] = array_multiframe_tag[seriesUID]; 
		
		slice_thick[new_seriesUID] = slice_thick[seriesUID]; 
		slice_between[new_seriesUID] = slice_between[seriesUID]; 
		px_size_x[new_seriesUID] = px_size_x[seriesUID]; 
		px_size_y[new_seriesUID] = px_size_y[seriesUID]; 
		x_y_ratio[new_seriesUID] = x_y_ratio[seriesUID]; 
		matrix[new_seriesUID] = matrix[seriesUID]; 
		
		
		new_div.setAttribute("id",new_seriesUID);
		new_div.setAttribute("title",new_seriesUID);
		new_div.setAttribute("class","draggable");
		new_param.setAttribute("id",new_seriesUID_param);
		new_param.setAttribute("title",new_seriesUID);
		new_img.setAttribute("id",new_seriesUID_img);
		new_img.setAttribute("title",new_seriesUID);
		new_img.setAttribute("class","draggable");
		
		new_div.innerHTML = "aaaa";
		
		new_div.style.position = "absolute";
		new_img.style.position = "absolute";
		new_param.style.position = "absolute";
		
		new_img.src = imagestest[seriesUID][0].src;
		
		document.body.appendChild(new_div);
		target_div = document.getElementById(new_seriesUID);
		target_div.appendChild(new_img);
		document.body.appendChild(new_param);
		
		new_div.style.left = "100px";
		new_param.style.left = "100px";

		new_div.style.width = "100px";
		new_div.style.height = "100px";
		new_img.style.width = "100px";
		
		
		document.getElementById(new_seriesUID).addEventListener("DOMMouseScroll",sumnail_scrool,false);
		document.getElementById(new_seriesUID_img).style.position = "absolute";
		document.getElementById(new_seriesUID).style.border = "2px solid green";
		
		document.getElementById(new_seriesUID_param).style.position = "absolute";
		document.getElementById(new_seriesUID_param).style.color = "yellow";
		document.getElementById(new_seriesUID_img).style.left = "0px";
		
		enableTooltips(new_seriesUID);
		
		
		$('div.draggable').Draggable({zIndex:1000,revert:true , ghosting: true, opacity: 0.8});
		sumnail_fit();
		}
	
	
	// image divide loop
	var divide_loop_tag = 0;
	var divide_loop_check_tag = 0;
	var new_column_num = 0;
	for(i=0;i<image_count_max[seriesUID];i++){
		divide_loop_tag = Math.floor( i / new_image_max);
		var new_seriesUID = seriesUID + '-' + divide_loop_tag;
		if(divide_loop_check_tag != divide_loop_tag){
		new_column_num = 0;
		}
		divide_loop_check_tag = divide_loop_tag;
		
		imagestest[new_seriesUID][new_column_num] = new Image();
		imagestest[new_seriesUID][new_column_num].src = imagestest[seriesUID][i].src ;
		dicomset[new_seriesUID][new_column_num] = dicomset[seriesUID][i] ;
		jpgset[new_seriesUID][new_column_num] = jpgset[seriesUID][i] ;
		referset[new_seriesUID][new_column_num] = referset[seriesUID][i] ;
		tableset_x[new_seriesUID][new_column_num] = tableset_x[seriesUID][i];
		tableset_y[new_seriesUID][new_column_num] = tableset_y[seriesUID][i];
		tableset_z[new_seriesUID][new_column_num] = tableset_z[seriesUID][i];
		image_information[new_seriesUID][new_column_num]  = series_description[new_seriesUID] + '<br>' + 'x:' + tableset_x[new_seriesUID][new_column_num] + 'y:' + tableset_y[new_seriesUID][new_column_num] + 'z:' + tableset_z[new_seriesUID][new_column_num] + '<br>' + fov[new_seriesUID] +  ' ' + matrix[new_seriesUID] + '<br>' + name_in_dicom[new_seriesUID]  +  '<br>' + id_in_dicom[new_seriesUID]  + '<br>' + (new_column_num + 1) + '/' + new_image_max;
		
		if(new_column_num == 0){
		new_seriesUID_img = new_seriesUID + "_img";
		new_seriesUID_param = new_seriesUID + "_param";
		document.getElementById(new_seriesUID_img).src = imagestest[seriesUID][i].src ;
		document.getElementById(new_seriesUID_param).innerHTML = "check";
		}
		new_column_num += 1;
	}
	
	}
}

// for single image functions

function echo_dicom(target_palette) {
	target_id = main_palette_id[target_palette];
	var now_img_num = image_count_now[target_id];
	var path = dicomset[target_id][now_img_num];
	//alert(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("' + path + '");setTimeout("window.close()","10000")>');
	dicom.document.write('</body></html>');
	dicom.document.close();


}

function echo_refer(target_palette) {
	target_id = main_palette_id[target_palette];
	var now_img_num = image_count_now[target_id];
	var jpg_path = jpgset[target_id][now_img_num];
	var refer_path = referset[target_id][now_img_num];
	jpg_path = jpg_path.replace("+", "%2B");
	refer_path = refer_path.replace("+", "%2B");
		//alert(jpg_path + " " + refer_path + "<?php echo $ruid ?>");
	window.open('marking.php?jpg_file_name=' + jpg_path + '&jpg_file_name_tmp=' + refer_path + '&ruid=<?php echo $ruid ?>&patid=<?php echo @$patid ?>','','toolbar=no,location=no,top=0,left=0,directories=no,status=no,menu=no,scrollbars=yes,resizable=no,width=570,height=730');

}



function sync_mode_change(target_palette) {
	target_id = main_palette_id[target_palette];
	if(sync_flag_for_series[target_id] == 1){
		sync_flag_for_series[target_id] = 0;
		var num = image_count_now[target_id];
		if(target_orientation[target_id] == "x"){
		sync_original_val_for_series[target_id] = tableset_x[target_id][num];
		}else if(target_orientation[target_id] == "y"){
		sync_original_val_for_series[target_id] = tableset_y[target_id][num];
		}else if(target_orientation[target_id] == "z"){
		sync_original_val_for_series[target_id] = tableset_z[target_id][num];
		}
	}else{
		sync_flag_for_series[target_id] = 1;
		var num = image_count_now[target_id];
		if(target_orientation[target_id] == "x"){
		sync_changed_val_for_series[target_id] = tableset_x[target_id][num];
		}else if(target_orientation[target_id] == "y"){
		sync_changed_val_for_series[target_id] = tableset_y[target_id][num];
		}else if(target_orientation[target_id] == "z"){
		sync_changed_val_for_series[target_id] = tableset_z[target_id][num];
		}
		
		sync_defference_val_for_series[target_id] = sync_changed_val_for_series[target_id] - sync_original_val_for_series[target_id] ;
		
		
	}
}




var cine_handle = new Object();
var cine_flag = new Object();
cine_handle[0] = 0;
cine_handle[1] = 0;
cine_handle[2] = 0;
cine_handle[3] = 0;
cine_flag[0] = 0;
cine_flag[1] = 0;
cine_flag[2] = 0;
cine_flag[3] = 0;

function cine_mode(target_palette){
	if(cine_flag[target_palette] == 0){
	cine_flag[target_palette] = 1;
	}else{
	cine_flag[target_palette] = 0;
	}
	
	if(cine_flag[target_palette] == 1){
	//target_id = main_palette_id[target_palette];
	cine_handle[target_palette] = setInterval("setImageup('" + target_palette + "')",60);
	}else{
	clearInterval(cine_handle[target_palette]);
	}
}

// exchnage palette from series list

function exchange_palette(target_palette,exchange_series_id){
	var num = image_count_now[exchange_series_id];
	var next_img = imagestest[exchange_series_id][num].src;
	main_palette_id[target_palette] = exchange_series_id;
	//alert(img);
	//var target_palette = "\'main_palette_" + target_palette + "_img\'";
			if(target_palette == 0){
			document.main_palette_0_img.src = next_img;
			}else if(target_palette == 1){
			document.main_palette_1_img.src = next_img;
			}else if(target_palette == 2){
			document.main_palette_2_img.src = next_img;
			}else if(target_palette == 3){
			document.main_palette_3_img.src = next_img;
			}
			resize_pack();
}

// magnification function

var deb,x,y,mag,baseimg,ritu;

mag=200;
ritu=1.5; 
baseimg="";
x=mag;y=mag;

var max_zindex = 100;

deb=""; 
var width_palette_0;
var height_palette_0;
var Px_palette_0;
var Py_palette_0;

var width_palette_1;
var height_palette_1;
var Px_palette_1;
var Py_palette_1;

var width_palette_2;
var height_palette_2;
var Px_palette_2;
var Py_palette_2;

var width_palette_3;
var height_palette_3;
var Px_palette_3;
var Py_palette_3;

function set(){

Px_palette_0 = parseInt(document.getElementById("main_palette_0_img").style.left);
Py_palette_0 = parseInt(document.getElementById("main_palette_0_img").style.top);
width_palette_0 = document.getElementById("main_palette_0_img").width;
height_palette_0 = document.getElementById("main_palette_0_img").height;

Px_palette_1 = parseInt(document.getElementById("main_palette_1_img").style.left);
Py_palette_1 = parseInt(document.getElementById("main_palette_1_img").style.top);
width_palette_1 = document.getElementById("main_palette_1_img").width;
height_palette_1 = document.getElementById("main_palette_1_img").height;

Px_palette_2 = parseInt(document.getElementById("main_palette_2_img").style.left);
Py_palette_2 = parseInt(document.getElementById("main_palette_2_img").style.top);
width_palette_2 = document.getElementById("main_palette_2_img").width;
height_palette_2 = document.getElementById("main_palette_2_img").height;

Px_palette_3 = parseInt(document.getElementById("main_palette_3_img").style.left);
Py_palette_3 = parseInt(document.getElementById("main_palette_3_img").style.top);
width_palette_3 = document.getElementById("main_palette_3_img").width;
height_palette_3 = document.getElementById("main_palette_3_img").height;

x=mag;y=mag;
}

function big(e){

if(enlarge_flag == 0){

var hit_flag = 0;

// for palette 0

if(hit_flag == 0){
if((e.pageX > width_palette_0+Px_palette_0 || e.pageX < Px_palette_0 || e.pageY > height_palette_0+Py_palette_0 || e.pageY < Py_palette_0) == false){
if(e.pageX > mag+Px_palette_0)x=e.pageX-Px_palette_0;
if(e.pageY > mag+Py_palette_0)y=e.pageY-Py_palette_0;
if(e.pageX > width_palette_0-mag+Px_palette_0)x=width_palette_0-mag;
if(e.pageY > height_palette_0-mag+Py_palette_0)y=height_palette_0-mag;
if(document.getElementById("main_palette_0_img_mag").src !=document.getElementById("main_palette_0_img").src ){
document.getElementById("main_palette_0_img_mag").src = document.getElementById("main_palette_0_img").src;
}
document.getElementById("main_palette_0_img_mag").style.display="block";
document.getElementById("main_palette_0_img_mag").height=height_palette_0*ritu;
document.getElementById("main_palette_0_img_mag").width=width_palette_0*ritu;
document.getElementById("main_palette_0_img_mag").style.left =x-x*ritu+Px_palette_0+"px";
document.getElementById("main_palette_0_img_mag").style.top = y-y*ritu+Py_palette_0+"px";
max_zindex += 1;
document.getElementById("main_palette_0_img_mag").style.zIndex = max_zindex;
hit_flag = 1;


document.getElementById("main_palette_1_img_mag").style.display="none";
document.getElementById("main_palette_2_img_mag").style.display="none";
document.getElementById("main_palette_3_img_mag").style.display="none";

}else{
/*
document.getElementById("main_palette_0_img").style.left = Px_palette_0+"px";
document.getElementById("main_palette_0_img").style.top = Py_palette_0+"px";
window_size_change();
*/
document.getElementById("main_palette_0_img_mag").style.display="none";
}
}

// for palette 1

if(hit_flag == 0){
if( (e.pageX > width_palette_1+Px_palette_1 || e.pageX < Px_palette_1 || e.pageY > height_palette_1+Py_palette_1 || e.pageY < Py_palette_1) == false){
if(e.pageX > mag+Px_palette_1)x=e.pageX-Px_palette_1;
if(e.pageY > mag+Py_palette_1)y=e.pageY-Py_palette_1;
if(e.pageX > width_palette_1-mag+Px_palette_1)x=width_palette_1-mag;
if(e.pageY > height_palette_1-mag+Py_palette_1)y=height_palette_1-mag;
if(document.getElementById("main_palette_1_img_mag").src !=document.getElementById("main_palette_1_img").src ){
document.getElementById("main_palette_1_img_mag").src = document.getElementById("main_palette_1_img").src;
}
document.getElementById("main_palette_1_img_mag").style.display="block";
document.getElementById("main_palette_1_img_mag").height=height_palette_1*ritu;
document.getElementById("main_palette_1_img_mag").width=width_palette_1*ritu;
document.getElementById("main_palette_1_img_mag").style.left =x-x*ritu+Px_palette_1+"px";
document.getElementById("main_palette_1_img_mag").style.top = y-y*ritu+Py_palette_1+"px";
max_zindex += 1;
document.getElementById("main_palette_1_img_mag").style.zIndex = max_zindex;
hit_flag = 1;

document.getElementById("main_palette_0_img_mag").style.display="none";
document.getElementById("main_palette_2_img_mag").style.display="none";
document.getElementById("main_palette_3_img_mag").style.display="none";

}else{
/*
document.getElementById("main_palette_1_img").style.left = Px_palette_1+"px";
document.getElementById("main_palette_1_img").style.top = Py_palette_1+"px";
window_size_change();
*/
document.getElementById("main_palette_1_img_mag").style.display="none";
}
}

// for palette 2

if(hit_flag == 0){
if( (e.pageX > width_palette_2+Px_palette_2 || e.pageX < Px_palette_2 || e.pageY > height_palette_2+Py_palette_2 || e.pageY < Py_palette_2) == false){
if(e.pageX > mag+Px_palette_2)x=e.pageX-Px_palette_2;
if(e.pageY > mag+Py_palette_2)y=e.pageY-Py_palette_2;
if(e.pageX > width_palette_2-mag+Px_palette_2)x=width_palette_2-mag;
if(e.pageY > height_palette_2-mag+Py_palette_2)y=height_palette_2-mag;
if(document.getElementById("main_palette_2_img_mag").src !=document.getElementById("main_palette_2_img").src ){
document.getElementById("main_palette_2_img_mag").src = document.getElementById("main_palette_2_img").src;
}
document.getElementById("main_palette_2_img_mag").style.display="block";
document.getElementById("main_palette_2_img_mag").height=height_palette_2*ritu;
document.getElementById("main_palette_2_img_mag").width=width_palette_2*ritu;
document.getElementById("main_palette_2_img_mag").style.left =x-x*ritu+Px_palette_2+"px";
document.getElementById("main_palette_2_img_mag").style.top = y-y*ritu+Py_palette_2+"px";
max_zindex += 1;
document.getElementById("main_palette_2_img_mag").style.zIndex = max_zindex;
hit_flag = 1;

document.getElementById("main_palette_0_img_mag").style.display="none";
document.getElementById("main_palette_1_img_mag").style.display="none";
document.getElementById("main_palette_3_img_mag").style.display="none";

}else{
/*
document.getElementById("main_palette_2_img").style.left = Px_palette_2+"px";
document.getElementById("main_palette_2_img").style.top = Py_palette_2+"px";
window_size_change();
*/
document.getElementById("main_palette_2_img_mag").style.display="none";
}
}

// for palette 3

if(hit_flag == 0){
if( (e.pageX > width_palette_3+Px_palette_3 || e.pageX < Px_palette_3 || e.pageY > height_palette_3+Py_palette_3 || e.pageY < Py_palette_3) == false){
if(e.pageX > mag+Px_palette_3)x=e.pageX-Px_palette_3;
if(e.pageY > mag+Py_palette_3)y=e.pageY-Py_palette_3;
if(e.pageX > width_palette_3-mag+Px_palette_3)x=width_palette_3-mag;
if(e.pageY > height_palette_3-mag+Py_palette_3)y=height_palette_3-mag;
if(document.getElementById("main_palette_3_img_mag").src !=document.getElementById("main_palette_3_img").src ){
document.getElementById("main_palette_3_img_mag").src = document.getElementById("main_palette_3_img").src;
}
document.getElementById("main_palette_3_img_mag").style.display="block";
document.getElementById("main_palette_3_img_mag").height=height_palette_3*ritu;
document.getElementById("main_palette_3_img_mag").width=width_palette_3*ritu;
document.getElementById("main_palette_3_img_mag").style.left =x-x*ritu+Px_palette_3+"px";
document.getElementById("main_palette_3_img_mag").style.top = y-y*ritu+Py_palette_3+"px";
max_zindex += 1;
document.getElementById("main_palette_3_img_mag").style.zIndex = max_zindex;
hit_flag = 1;

document.getElementById("main_palette_0_img_mag").style.display="none";
document.getElementById("main_palette_1_img_mag").style.display="none";
document.getElementById("main_palette_2_img_mag").style.display="none";


}else{
/*
document.getElementById("main_palette_3_img").style.left = Px_palette_3+"px";
document.getElementById("main_palette_3_img").style.top = Py_palette_3+"px";
window_size_change();
*/
document.getElementById("main_palette_3_img_mag").style.display="none";
}
}

}else{


	//alert(enlarged_palette);
	var target_width_palette_name = "width_palette_" + enlarged_palette;
	var target_height_palette_name = "height_palette_" + enlarged_palette;
	var target_Px_palette_name = "Px_palette_" + enlarged_palette;
	var target_Py_palette_name = "Py_palette_" + enlarged_palette;
	var target_main_palette_img = "main_palette_" + enlarged_palette + "_img_mag";
	var target_width_palette = eval(target_width_palette_name);
	var target_height_palette = eval(target_height_palette_name);
	var target_Px_palette = eval(target_Px_palette_name);
	var target_Py_palette = eval(target_Py_palette_name);
	//var test = eval(target_main_palette_img);
	//alert(target_width_palette + " " + target_height_palette + " " +  target_Px_palette + " " + target_Py_palette);
	

//if( (e.pageX > width_palette_3+Px_palette_3 || e.pageX < Px_palette_3 || e.pageY > height_palette_3+Py_palette_3 || e.pageY < Py_palette_3) == false){

if( (e.pageX > target_width_palette+target_Px_palette || e.pageX < target_Px_palette || e.pageY > target_height_palette + target_Py_palette || e.pageY < target_Py_palette) == false){
if(e.pageX > mag+target_Px_palette)x=e.pageX-target_Px_palette;
if(e.pageY > mag+target_Py_palette)y=e.pageY-target_Py_palette;
if(e.pageX > target_width_palette-mag+target_Px_palette)x=target_width_palette-mag;
if(e.pageY > target_height_palette-mag+target_Py_palette)y=target_height_palette-mag;
document.getElementById(target_main_palette_img).style.display="block";
document.getElementById(target_main_palette_img).height=target_height_palette*ritu;
document.getElementById(target_main_palette_img).width=target_width_palette*ritu;
document.getElementById(target_main_palette_img).style.left =x-x*ritu+target_Px_palette+"px";
document.getElementById(target_main_palette_img).style.top = y-y*ritu+target_Py_palette+"px";
max_zindex += 1;
document.getElementById(target_main_palette_img).style.zIndex = max_zindex;
hit_flag = 1;
}else{
/*
document.getElementById(target_main_palette_img).style.left = target_Px_palette+"px";
document.getElementById(target_main_palette_img).style.top = target_Py_palette+"px";
window_size_change();
*/

}

}

}


function window_size_change(){
	
	var main_field_x = window.innerWidth  - 200;
	var main_field_y = window.innerHeight;
	
	if(enlarge_flag == 1){
		//alert(enlarge_palette_num);
		var target_id = main_palette_id[enlarge_palette_num];
		var target_palette = "\'main_palette_" + enlarge_palette_num + "_img\'";
		
		var target_ratio = x_y_ratio[target_id];
		//alert(target_ratio);
		var ratio_power_x = 1 ;
		var ratio_power_y = 1 ;
		if(target_ratio != 1){
			if(target_ratio > 1){
			ratio_power_x = 1 / target_ratio;
			}else{
			ratio_power_y = target_ratio;
			}
		}
		if((main_field_x * ratio_power_x) > (main_field_y * ratio_power_y)){
		document.getElementById(eval(target_palette)).width = (main_field_y )  * ratio_power_x;
		document.getElementById(eval(target_palette)).height = (main_field_y )  * ratio_power_y;
		}else{
		document.getElementById(eval(target_palette)).width = (main_field_x)  * ratio_power_x;
		document.getElementById(eval(target_palette)).height = (main_field_x)  * ratio_power_y;
		}
		
		
	}else{
	
	for( var i=0; i < 4; i++){

var scale_set_margin_x = 10;
var scale_set_margin_y = 100;

info_set_margin_y
info_set_margin_x

		var target_id = main_palette_id[i];
		//alert(target_id);
		var target_palette = "\'main_palette_" + i + "_img\'";
		var target_palette_scale = "\'main_palette_" + i + "_img_scale\'";
		var target_palette_info = "\'main_palette_" + i + "_info\'";
		
		if(i == 1){
		var new_position_x = Math.floor((main_field_x / 2) + 200) + "px";
		var new_position_x_for_scale = Math.floor((main_field_x / 2) + 200) + scale_set_margin_x + "px";
		var new_position_x_for_info = Math.floor((main_field_x / 2) + 200) + info_set_margin_y + "px";
		document.getElementById(eval(target_palette)).style.left = new_position_x ;
		document.getElementById(eval(target_palette_scale)).style.left = new_position_x_for_scale  ;
		document.getElementById(eval(target_palette_info)).style.left = new_position_x_for_info  ;
		}else if(i == 2){
		var new_position_y = Math.floor((main_field_y  / 2)) + "px";
		var new_position_y_for_scale = Math.floor((main_field_y  / 2)) + scale_set_margin_y + "px";
		var new_position_y_for_info = Math.floor((main_field_y  / 2)) + info_set_margin_y + "px";
		document.getElementById(eval(target_palette)).style.top = new_position_y ;
		document.getElementById(eval(target_palette_scale)).style.top = new_position_y_for_scale ;
		document.getElementById(eval(target_palette_info)).style.top = new_position_y_for_info ;
		}else if(i == 3){
		var new_position_x = Math.floor(((main_field_x) / 2) + 200) + "px";
		var new_position_x_for_scale = Math.floor((main_field_x / 2) + 200) + scale_set_margin_x + "px";
		var new_position_x_for_info = Math.floor((main_field_x / 2) + 200) + info_set_margin_y + "px";
		document.getElementById(eval(target_palette)).style.left = new_position_x ;
		document.getElementById(eval(target_palette_scale)).style.left = new_position_x_for_scale  ;
		document.getElementById(eval(target_palette_info)).style.left = new_position_x_for_info  ;
		var new_position_y = Math.floor((main_field_y  / 2)) + "px";
		var new_position_y_for_scale = Math.floor((main_field_y  / 2)) + scale_set_margin_y + "px";
		var new_position_y_for_info = Math.floor((main_field_y  / 2)) + info_set_margin_y + "px";
		document.getElementById(eval(target_palette)).style.top = new_position_y ;
		document.getElementById(eval(target_palette_scale)).style.top = new_position_y_for_scale ;
		document.getElementById(eval(target_palette_info)).style.top = new_position_y_for_info ;
		}
		
		var target_ratio = x_y_ratio[target_id];
		//alert(target_ratio);
		var ratio_power_x = 1 ;
		var ratio_power_y = 1 ;
		if(target_ratio != 1){
			if(target_ratio > 1){
			ratio_power_x = 1 / target_ratio;
			}else{
			ratio_power_y = target_ratio;
			}
		}
		
		if((main_field_x * ratio_power_x) > (main_field_y * ratio_power_y)){
		document.getElementById(eval(target_palette)).width = (main_field_y ) / 2 * ratio_power_x;
		document.getElementById(eval(target_palette)).height = (main_field_y ) / 2 * ratio_power_y;
		
		document.getElementById(eval(target_palette_scale)).height = (((main_field_y ) / 2 * ratio_power_y) * scale_ratio[target_id] / target_ratio );
		
		
		}else{
		document.getElementById(eval(target_palette)).width = (main_field_x) / 2 * ratio_power_x;
		document.getElementById(eval(target_palette)).height = (main_field_x) / 2 * ratio_power_y;
		
		
		document.getElementById(eval(target_palette_scale)).height = (((main_field_x) / 2 * ratio_power_y) * scale_ratio[target_id] / target_ratio);
		
		
		}
		


		}
	}


}

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

// clear event

function event_clear(){


	 document.getElementById('canvas').style.display="none";
	 document.body.style.cursor = "default";
	 document.onmousemove=null;
	 document.onmousedown=null;
	 document.onmouseup=null;
	 document.ondblclick=null;
	 document.body.style.cursor = "default";

	 wlww_flag = 0;
	 canvas_start_x = "";
	 canvas_start_y = "";
	 dist_check_flag = false;
	 drawFlg = false;
	 canvas.clear();

}




// wlww change 

var wlww_flag = 0;

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.getElementById("main_palette_0_img").ondblclick = apply_setting;
	document.getElementById("main_palette_1_img").ondblclick = apply_setting;
	document.getElementById("main_palette_2_img").ondblclick = apply_setting;
	document.getElementById("main_palette_3_img").ondblclick = apply_setting;
*/
	document.ondblclick=apply_setting;
	wlww_flag = 1;
	//document.ondblclick = apply_setting;
	
	//var block = document.getElementById("wlww_data");
    //block.style.display = "block";
	
    return;
}

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 rightButton = false;
var realtime_ratio = 0.1 ;
var mouse_move_ratio = "1";
var active_target_id  = "";
var active_palette_id  = "";



function mouse_down(evt){

    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
        if ((evt.button && evt.button == 2) || (evt.which && evt.which == 3)) {
            rightButton = true;
        }
        if (rightButton) {
            // process right-click here
            //return false;
        } else {
	pos_down_x = evt.pageX;
	pos_down_y = evt.pageY;
	
	if(enlarged_palette != ""){
	active_target_id = main_palette_id[enlarged_palette];
	}else{
	
	if( (evt.pageX > width_palette_0+Px_palette_0 || evt.pageX < Px_palette_0 || evt.pageY > height_palette_0+Py_palette_0 || evt.pageY < Py_palette_0) == false){
		active_target_id = main_palette_id[0];

	}
	if( (evt.pageX > width_palette_1+Px_palette_1 || evt.pageX < Px_palette_1 || evt.pageY > height_palette_1+Py_palette_1 || evt.pageY < Py_palette_1) == false){
		active_target_id = main_palette_id[1];

	}
	if( (evt.pageX > width_palette_2+Px_palette_2 || evt.pageX < Px_palette_2 || evt.pageY > height_palette_2+Py_palette_2 || evt.pageY < Py_palette_2) == false){
		active_target_id = main_palette_id[2];

	}
	if( (evt.pageX > width_palette_3+Px_palette_3 || evt.pageX < Px_palette_3 || evt.pageY > height_palette_3+Py_palette_3 || evt.pageY < Py_palette_3) == false){
		active_target_id = main_palette_id[3];
	}
	
	}
	
	
	mouse_down_flag = true ;
	jpeg_return_check = true;
	document.onmousemove = realtime_wlww ;
	//document.getElementById('wl_ww_value').innerHTML = 'WL/WW ' + wl + ':' + ww;
	if (evt.preventDefault) {
    evt.preventDefault();
    }
        }
    }
}


function realtime_wlww(evt){

	if((mouse_down_flag == true) && (jpeg_return_check == true)){
	
   var d1 = new Date().getTime();
   var d2 = new Date().getTime();
   while( d2 < d1+1000*realtime_ratio ){    //TÉÃÂÔ¤Ä
       d2=new Date().getTime();
   }
   jpeg_return_check = false ;
   
	evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
        if ((evt.button && evt.button == 2) || (evt.which && evt.which == 3)) {
            rightButton = true;							
        }
        if (rightButton) {
            // process right-click here
            //return false;
        } else {
	pos_up_x = evt.pageX;
	pos_up_y = evt.pageY;
	sub_x = (pos_down_x - pos_up_x) * mouse_move_ratio ;
	sub_y = (pos_down_y - pos_up_y) * mouse_move_ratio ;

	change_x = wl_from_dicomtag[active_target_id] - sub_x ;
	change_y = ww_from_dicomtag[active_target_id] - sub_y ;
	
	
	
	change_x = Math.floor(change_x);
	change_y = Math.floor(change_y);
	
	wl_from_dicomtag[active_target_id] = change_x;
	ww_from_dicomtag[active_target_id] = change_y;

	if(change_y <= 1){
	change_y = 1;
	}
	
	//	document.wlww.wl.value = change_x;
	//	document.wlww.ww.value = change_y;
	
	//document.getElementById('wl_ww_value').innerHTML = 'WL/WW ' + change_x + ':' + change_y;
	
	var num = image_count_now[active_target_id];
	var dicom_path = dicomset[active_target_id][num];
	//alert(dicom_path)
	//alert(change_x + " " + change_y);
	

	do_convert(dicom_path,change_x,change_y,negate_each_series[active_target_id],rotate_each_series[active_target_id],power_each_series[active_target_id]);
	
	pos_down_x = pos_up_x;
	pos_down_y = pos_up_y;
	
        }
	}
	 rightButton = false;
	 
	 }else{
	 return false;
	 }
	
}

function mouse_up(evt){
		mouse_down_flag = false ;
		document.onmousemove = null ;
}

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


	
	converting_flag = 1;
	var num = image_count_now[active_target_id];
	var dicom_path = dicomset[active_target_id][num];


var lower_tag = image_count_now[active_target_id];
var higher_tag = image_count_now[active_target_id];
var target_num = 0;
var toward_flag = false;
var count_num = 0;
var send_dicom = "";
var test_flag = true;
//document.getElementById('create_check_AJAX').innerHTML = "AJAX start"   ;
		while (count_num < image_count_max[active_target_id]) {
		if(toward_flag == false && lower_tag != -1){
		target_num = lower_tag;
		send_dicom = dicomset[active_target_id][target_num];
			do_convert_for_apply("high",target_num,send_dicom,wl_from_dicomtag[active_target_id],ww_from_dicomtag[active_target_id],negate_each_series[active_target_id],rotate_each_series[active_target_id],power_each_series[active_target_id]);
		lower_tag-- ;
		count_num++ ;
		toward_flag = true;
		}else{
		if(higher_tag != (image_count_max[active_target_id] - 1)){
		higher_tag++;
		count_num++;
		target_num = higher_tag;
		send_dicom = dicomset[active_target_id][target_num];
			do_convert_for_apply("high",target_num,send_dicom,wl_from_dicomtag[active_target_id],ww_from_dicomtag[active_target_id],negate_each_series[active_target_id],rotate_each_series[active_target_id],power_each_series[active_target_id]);
		}
		toward_flag = false;
		}
	}
	
	converting_flag = 0;
	
	//alert(send_dicom);
		//document.getElementById('create_check_AJAX').innerHTML = "apply setting"   ;
		//setTimeout("document.getElementById('create_check_AJAX').innerHTML = '';", 3000); 



}

// apply preset values 
function apply_series(target_palette,target_function){
	active_target_id = main_palette_id[target_palette];
	

	
	if(target_function == "head"){
	wl_from_dicomtag[active_target_id] = 80;
	ww_from_dicomtag[active_target_id] = 200;
	apply_setting();
	}else if(target_function == "abd"){
	wl_from_dicomtag[active_target_id] = 40;
	ww_from_dicomtag[active_target_id] = 300;
	apply_setting();
	}else if(target_function == "enhance"){
	wl_from_dicomtag[active_target_id] = 80;
	ww_from_dicomtag[active_target_id] = 300;
	apply_setting();
	}else if(target_function == "bone"){
	wl_from_dicomtag[active_target_id] = 50;
	ww_from_dicomtag[active_target_id] = 1500;
	apply_setting();
	}else if(target_function == "lung"){
	wl_from_dicomtag[active_target_id] = -700;
	ww_from_dicomtag[active_target_id] = 1500;
	apply_setting();
	}else if(target_function == "negate"){
	negate_each_series[active_target_id] = 1;
	apply_setting();
	}else if(target_function == "qualifyx2"){
	power_each_series[active_target_id] = 2;
	apply_setting();
	}else if(target_function == "qualifyx3"){
	power_each_series[active_target_id] = 3;
	apply_setting();
	}else if(target_function == "left"){
	rotate_each_series[active_target_id] = "Rl";
	apply_setting();
	}else if(target_function == "right"){
	rotate_each_series[active_target_id] = "Rr";
	apply_setting();
	}else if(target_function == "topdown"){
	rotate_each_series[active_target_id] = "Rtd";
	apply_setting();
	}else if(target_function == "normal"){
	power_each_series[active_target_id] = 1;
	negate_each_series[active_target_id] = 0;
	rotate_each_series[active_target_id] = 0;
	apply_setting();
	}
	
	
	//alert(target_palette + " " + target_function + " " +active_target_id);
}


// measure distance 

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

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

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;
	document.addEventListener("DOMMouseScroll",flash_canvas,false);
    return;
}

function flash_canvas(evt){
canvas.clear();
document.getElementById('canvas').style.display="none";
}

function start_measure_dist(evt){
	
	
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
        if ((evt.button && evt.button == 2) || (evt.which && evt.which == 3)) {
            rightButton = true;
        }
        if (rightButton) {
            // process right-click here
            //return false;
        } else {
		
	var x = evt.pageX ;
	var y = evt.pageY;
	document.getElementById("canvas").style.display = "block";
	max_zindex += 1;
	document.getElementById("canvas").style.zIndex = max_zindex;
	if (!drawFlg) {
	// start click
		// get active palette
		if( (evt.pageX > width_palette_0+Px_palette_0 || evt.pageX < Px_palette_0 || evt.pageY > height_palette_0+Py_palette_0 || evt.pageY < Py_palette_0) == false){
		active_target_id = main_palette_id[0];
		active_palette_id = 0;
		}
		if( (evt.pageX > width_palette_1+Px_palette_1 || evt.pageX < Px_palette_1 || evt.pageY > height_palette_1+Py_palette_1 || evt.pageY < Py_palette_1) == false){
		active_target_id = main_palette_id[1];
		active_palette_id = 1;
		}
		if( (evt.pageX > width_palette_2+Px_palette_2 || evt.pageX < Px_palette_2 || evt.pageY > height_palette_2+Py_palette_2 || evt.pageY < Py_palette_2) == false){
		active_target_id = main_palette_id[2];
		active_palette_id = 2;
		}
		if( (evt.pageX > width_palette_3+Px_palette_3 || evt.pageX < Px_palette_3 || evt.pageY > height_palette_3+Py_palette_3 || evt.pageY < Py_palette_3) == false){
		active_target_id = main_palette_id[3];
		active_palette_id = 3;
		}
		drawFlg = true;
		canvas_start_x = x ;
		canvas_start_y = y;
		canvas.startLine(x - 200 , y);
	} else {
		canvas.lineTo(x - 200 , y);
		if (!continueFlg) {
		// end click
			drawFlg = false;
			canvas.endLine();
			var pixel_val = Math.sqrt((canvas_start_x - x)*(canvas_start_x - x) + (canvas_start_y - y)*(canvas_start_y - y));
			var mm_val = pixel_to_mm_change(pixel_val);
			//alert(pixel_val + 'px');
			//alert(mm_val +'mm');
			mm_val = Math.round(mm_val);
			alert(mm_val + "mm");
			//document.getElementById('distance').innerHTML = mm_val +'mm' ;
		}
	}
	if (evt.preventDefault) {
    evt.preventDefault();
    }
        }
    }
}

function pixel_to_mm_change(val){
	
	var target_palette = "main_palette_" + active_palette_id + "_img"; 
	var target_palette_width =  document.getElementById(target_palette).width;
	var result = val / ((scale_ratio[active_target_id]) *target_palette_width / 100 );
//	alert(val);
	return result;
}


// evaluate CT value


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 start_evaluate_huv(evt){
	
	
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
        if ((evt.button && evt.button == 2) || (evt.which && evt.which == 3)) {
            rightButton = true;
        }
        if (rightButton) {
            // process right-click here
            //return false;
        } else {
		
	// start click
	var x = evt.pageX ;
	var y = evt.pageY;
	document.getElementById("canvas").style.display = "block";
	max_zindex += 1;
	document.getElementById("canvas").style.zIndex = max_zindex;
	if (!drawFlg) {
	// start click
		// get active palette
		if( (evt.pageX > width_palette_0+Px_palette_0 || evt.pageX < Px_palette_0 || evt.pageY > height_palette_0+Py_palette_0 || evt.pageY < Py_palette_0) == false){
		active_target_id = main_palette_id[0];
		active_palette_id = 0;
		}
		if( (evt.pageX > width_palette_1+Px_palette_1 || evt.pageX < Px_palette_1 || evt.pageY > height_palette_1+Py_palette_1 || evt.pageY < Py_palette_1) == false){
		active_target_id = main_palette_id[1];
		active_palette_id = 1;
		}
		if( (evt.pageX > width_palette_2+Px_palette_2 || evt.pageX < Px_palette_2 || evt.pageY > height_palette_2+Py_palette_2 || evt.pageY < Py_palette_2) == false){
		active_target_id = main_palette_id[2];
		active_palette_id = 2;
		}
		if( (evt.pageX > width_palette_3+Px_palette_3 || evt.pageX < Px_palette_3 || evt.pageY > height_palette_3+Py_palette_3 || evt.pageY < Py_palette_3) == false){
		active_target_id = main_palette_id[3];
		active_palette_id = 3;
		}
		
		
		drawFlg = true;
		canvas_start_x = x;
		canvas_start_y = y;
		canvas.startLine(x - 200 , y);
	} else {
		canvas.lineTo(x - 200, canvas_start_y);
		canvas.lineTo(x - 200, y);
		canvas.lineTo(canvas_start_x - 200, y);
		canvas.lineTo(canvas_start_x - 200, canvas_start_y);
		if (!continueFlg) {
		// end click
			drawFlg = false;
			canvas.endLine();
			
			var num = image_count_now[active_target_id];
			var dicom_path = dicomset[active_target_id][num];
			
			var start_original_x = x_point_calc(canvas_start_x);
			var start_original_y = y_point_calc(canvas_start_y);
			var end_original_x = x_point_calc(x);
			var end_original_y = y_point_calc(y);
			do_get_ct(start_original_x,start_original_y,end_original_x,end_original_y,dicom_path);

		}
	}
	if (evt.preventDefault) {
    evt.preventDefault();
    }
        }
    }
}


function x_point_calc(val){
		var target_palette = "main_palette_" + active_palette_id + "_img"; 
		var target_palette_width =  document.getElementById(target_palette).width;
		var img_position = parseInt(document.getElementById(target_palette).style.left);
		var result = Math.round((val - img_position) * ( col_from_dicomtag[active_target_id] / target_palette_width ));
	return result;
}
function y_point_calc(val){
		var target_palette = "main_palette_" + active_palette_id + "_img"; 
		var target_palette_height =  document.getElementById(target_palette).height;
		var img_position = parseInt(document.getElementById(target_palette).style.top);
		var result = Math.round((val - img_position) * ( row_from_dicomtag[active_target_id] / target_palette_height ));
	return result;
}

// cache check

var finish_flag = 0;

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

function cache_end(){
	finish_flag = 1;
			for(var i=0;i<study_series_list.length;i++){
			var seriesUID = study_series_list[i] ;
			var param_link = study_series_list[i] + "_param";
			document.getElementById(param_link).innerHTML = "complete";
			document.getElementById(param_link).style.color = "yellow";
			document.getElementById(param_link).style.backgroundColor = "transparent";
			
			max_zindex += 1;
			document.getElementById('main_palette_0_img').style.zIndex = max_zindex;
			max_zindex += 1;
			document.getElementById('main_palette_1_img').style.zIndex = max_zindex;
			max_zindex += 1;
			document.getElementById('main_palette_2_img').style.zIndex = max_zindex;
			max_zindex += 1;
			document.getElementById('main_palette_3_img').style.zIndex = max_zindex;
			max_zindex += 1;
			if(enlarge_flag != 1){
			document.getElementById('main_palette_0_info').style.zIndex = max_zindex;
			max_zindex += 1;
			document.getElementById('main_palette_1_info').style.zIndex = max_zindex;
			max_zindex += 1;
			document.getElementById('main_palette_2_info').style.zIndex = max_zindex;
			max_zindex += 1;
			document.getElementById('main_palette_3_info').style.zIndex = max_zindex;
			}
			}
}

function cache_check(){

	for(var i=0;i<study_series_list.length;i++){
		var seriesUID = study_series_list[i] ;
		var param_link = study_series_list[i] + "_param";
		var end_tag = 0;
		for(var count=image_count_cache[seriesUID];count < image_count_max[seriesUID];count++){
			if(imagestest[seriesUID][count] && imagestest[seriesUID][count].complete){
			continue;
			}else {
			image_count_cache[seriesUID] = count;
			var download_percent = (image_count_cache[seriesUID] + 1) / image_count_max[seriesUID] ;
			if(document.getElementById(param_link)){
				
				var color_param = toHex(download_percent * 256);
				var back_color_param = toHex(256 - (download_percent * 256));
				document.getElementById(param_link).style.color = '#' + color_param + color_param + color_param;
				document.getElementById(param_link).style.backgroundColor = '#' + back_color_param + back_color_param + back_color_param;
				
				document.getElementById(param_link).innerHTML = (Math.round(download_percent* 100)  )  + '%' + ' (' + (image_count_cache[seriesUID] + 1) + '/' + image_count_max[seriesUID]  + ')';
			
			
				if((download_percent*100) > 95){
				end_tag += 1;
				document.getElementById(param_link).innerHTML = "complete";
				document.getElementById(param_link).style.color = "yellow";
				}
			}
			
			break;
			}
		}
	}
	
}

function toHex(dec)	{
	hexChar="0123456789ABCDEF"
	if(dec > 255)	{
		return "ff"
	}
	var i = dec % 16
	var j = (dec - i)/16
	result = ""
	result += hexChar.charAt(j)
	result += hexChar.charAt(i)
	return result
}

//sumnail scrool

var sumnail_top_id = 0;

function sumnail_scrool(e){
	  e.preventDefault();
  var delta = e.detail * -1;
  	if(delta <0){
		sumnail_up();
	}else{
		sumnail_down();
	}
}


function sumnail_fit(){
	//sumnail_top_id += 1;
	var top_position = 0;
	if(sumnail_top_id > (study_series_list.length )){
	sumnail_top_id = 0;
	}
	for(var i=sumnail_top_id;i<study_series_list.length;i++){
		var img_link = study_series_list[i] + "_img";
		var param_link = study_series_list[i] + "_param";
		document.getElementById(study_series_list[i]).style.top = top_position + "px";
		document.getElementById(param_link).style.top = top_position + "px";
		top_position += document.getElementById(img_link).height;
	}
	for(var i=0;i<sumnail_top_id;i++){
		var img_link = study_series_list[i] + "_img";
		var param_link = study_series_list[i] + "_param";
		document.getElementById(study_series_list[i]).style.top = top_position + "px";
		document.getElementById(param_link).style.top = top_position + "px";
		top_position += document.getElementById(img_link).height;
	}
}

function sumnail_up(){
	sumnail_top_id += 1;
	var top_position = 0;
	if(sumnail_top_id > (study_series_list.length )){
	sumnail_top_id = 0;
	}
	for(var i=sumnail_top_id;i<study_series_list.length;i++){
		var img_link = study_series_list[i] + "_img";
		var param_link = study_series_list[i] + "_param";
		document.getElementById(study_series_list[i]).style.top = top_position + "px";
		document.getElementById(param_link).style.top = top_position + "px";
		top_position += document.getElementById(img_link).height;
	}
	for(var i=0;i<sumnail_top_id;i++){
		var img_link = study_series_list[i] + "_img";
		var param_link = study_series_list[i] + "_param";
		document.getElementById(study_series_list[i]).style.top = top_position + "px";
		document.getElementById(param_link).style.top = top_position + "px";
		top_position += document.getElementById(img_link).height;
	}
}

function sumnail_down(){
	sumnail_top_id -= 1;
	var top_position = 0;
	if(sumnail_top_id < 0){
	sumnail_top_id = study_series_list.length ;
	}
	for(var i=sumnail_top_id;i<study_series_list.length;i++){
		var img_link = study_series_list[i] + "_img";
		var param_link = study_series_list[i] + "_param";
		document.getElementById(study_series_list[i]).style.top = top_position + "px";
		document.getElementById(param_link).style.top = top_position + "px";
		top_position += document.getElementById(img_link).height;
	}
	for(var i=0;i<sumnail_top_id;i++){
		var img_link = study_series_list[i] + "_img";
		var param_link = study_series_list[i] + "_param";
		document.getElementById(study_series_list[i]).style.top = top_position + "px";
		document.getElementById(param_link).style.top = top_position + "px";
		top_position += document.getElementById(img_link).height;
	}

}

// jquery code 

var preset_flag = 0;

function show_preset(){
	if(preset_flag == 0){
	$('#preset-value').fadeOut();
	preset_flag = 1;
	}else{
	$('#preset-value').fadeIn();
	preset_flag = 0;
	}
}

var filter_flag = 0;

function show_filter(){
	if(filter_flag == 0){
	$('#filter-value').fadeOut();
	filter_flag = 1;
	}else{
	$('#filter-value').fadeIn();
	filter_flag = 0;
	}
}

var values_flag = 0;

function show_values(){
	if(values_flag == 0){
	$('#values-value').fadeOut();
	values_flag = 1;
	}else{
	$('#values-value').fadeIn();
	values_flag = 0;
	}
}

// each function list

function_list.push('report');
function_list.push('dicom');
function_list.push('sync');
function_list.push('cine');
function_list.push('head');
function_list.push('abd');
function_list.push('enhance');
function_list.push('bone');
function_list.push('lung');
function_list.push('negate');
function_list.push('qualifyx2');
function_list.push('qualifyx3');
function_list.push('left');
function_list.push('right');
function_list.push('topdown');
function_list.push('normal');
function_list.push('sensitive');
function_list.push('moderate');
function_list.push('blunt');
function_list.push('divide-h');
function_list.push('divide-v');

single_img_function_list.push('report');
single_img_function_list.push('dicom');
single_img_function_list.push('sync');
single_img_function_list.push('cine');

apply_series_function_list.push('head');
apply_series_function_list.push('abd');
apply_series_function_list.push('enhance');
apply_series_function_list.push('bone');
apply_series_function_list.push('lung');
apply_series_function_list.push('negate');
apply_series_function_list.push('qualifyx2');
apply_series_function_list.push('qualifyx3');
apply_series_function_list.push('left');
apply_series_function_list.push('right');
apply_series_function_list.push('topdown');
apply_series_function_list.push('normal');

parameter_function_list.push('sensitive');
parameter_function_list.push('moderate');
parameter_function_list.push('blunt');

divide_function_list.push('divide-h');
divide_function_list.push('divide-v');


</script>



</head>
<body bgcolor="#000000"  onResize="resize_pack();" onload="cache_end();">


<div id='report'  class="draggable">
<img src="./img/report.gif"/  onmouseover="this.src='./img/report_over.gif'" onmouseout="this.src='./img/report.gif'">
</div>

<div id='dicom'  class="draggable">
<img src="./img/dicom_default.gif"/  onmouseover="this.src='./img/dicom_over.gif'" onmouseout="this.src='./img/dicom_default.gif'">
</div>

<div id='sync'  class="draggable">
<img src="./img/sync.gif"/  onmouseover="this.src='./img/sync_over.gif'" onmouseout="this.src='./img/sync.gif'">
</div>

<div id='cine'  class="draggable">
<img src="./img/cine.gif"/  onmouseover="this.src='./img/cine_over.gif'" onmouseout="this.src='./img/cine.gif'">
</div>


<div id='zoom' >
<a onClick=" event_clear();document.onmousemove=big;"><img src="./img/zoom.gif"/  onmouseover="this.src='./img/zoom_over.gif'" onmouseout="this.src='./img/zoom.gif'"></a>
</div>

<div id='wlww' >
<a onClick=" event_clear();initDrag();"><img src="./img/wlww.gif"/  onmouseover="this.src='./img/wlww_over.gif'" onmouseout="this.src='./img/wlww.gif'"></a>
</div>


<div id='distance'  >
<a onClick=" event_clear();measure_dist();">
<img src="./img/distance.gif"/  onmouseover="this.src='./img/distance_over.gif'" onmouseout="this.src='./img/distance.gif'">
</a>
</div>



<div id='ctval'  >
<a onClick=" event_clear();evaluate_huv();">
<img src="./img/ctval.gif"/  onmouseover="this.src='./img/ctval_over.gif'" onmouseout="this.src='./img/ctval.gif'">
</a>
</div>



<div id='default'  >
<a onClick=" event_clear();"><img src="./img/default.gif"/  onmouseover="this.src='./img/default_over.gif'" onmouseout="this.src='./img/default.gif'"><\a>
</div>



<div id='preset'  >
<a onClick=" show_preset()"><img src="./img/preset.gif"/  onmouseover="this.src='./img/preset_over.gif'" onmouseout="this.src='./img/preset.gif'"></a>
</div>

<div id='preset-value'>

<div id='head'  class="draggable">
<img src="./img/head.gif"/  onmouseover="this.src='./img/head_over.gif'" onmouseout="this.src='./img/head.gif'">
</div>

<div id='abd'  class="draggable">
<img src="./img/abd.gif"/  onmouseover="this.src='./img/abd_over.gif'" onmouseout="this.src='./img/abd.gif'">
</div>

<div id='enhance'  class="draggable">
<img src="./img/enhance.gif"/  onmouseover="this.src='./img/enhance_over.gif'" onmouseout="this.src='./img/enhance.gif'">
</div>

<div id='bone'  class="draggable">
<img src="./img/bone.gif"/  onmouseover="this.src='./img/bone_over.gif'" onmouseout="this.src='./img/bone.gif'">
</div>

<div id='lung'  class="draggable">
<img src="./img/lung.gif"/  onmouseover="this.src='./img/lung_over.gif'" onmouseout="this.src='./img/lung.gif'">
</div>


</div>



<div id='filter' >
<a onClick=" show_filter()"><img src="./img/filter.gif"/  onmouseover="this.src='./img/filter_over.gif'" onmouseout="this.src='./img/filter.gif'"></a>
</div>


<div id='filter-value'>


<div id='negate'  class="draggable">
<img src="./img/negate.gif"/ onmouseover="this.src='./img/negate_over.gif'" onmouseout="this.src='./img/negate.gif'">
</div>


<div id='qualifyx2'  class="draggable">
<img src="./img/qualifyx2.gif"/  onmouseover="this.src='./img/qualifyx2_over.gif'" onmouseout="this.src='./img/qualifyx2.gif'">
</div>

<div id='qualifyx3'  class="draggable">
<img src="./img/qualifyx3.gif"/  onmouseover="this.src='./img/qualifyx3_over.gif'" onmouseout="this.src='./img/qualifyx3.gif'">
</div>

<div id='left'  class="draggable">
<img src="./img/left.gif"/  onmouseover="this.src='./img/left_over.gif'" onmouseout="this.src='./img/left.gif'">
</div>

<div id='right'  class="draggable">
<img src="./img/right.gif"/  onmouseover="this.src='./img/right_over.gif'" onmouseout="this.src='./img/right.gif'">
</div>

<div id='topdown'  class="draggable">
<img src="./img/topdown.gif"/  onmouseover="this.src='./img/topdown_over.gif'" onmouseout="this.src='./img/topdown.gif'">
</div>


<div id='normal'  class="draggable">
<img src="./img/normal.gif"/  onmouseover="this.src='./img/normal_over.gif'" onmouseout="this.src='./img/normal.gif'">
</div>



</div>

<div id='values' >
<a onClick=" show_values()"><img src="./img/values.gif"/  onmouseover="this.src='./img/values_over.gif'" onmouseout="this.src='./img/values.gif'"></a>
</div>


<div id='values-value'>

<div id='sensitive'  class="draggable">
<img src="./img/sensitive.gif"/  onmouseover="this.src='./img/sensitive_over.gif'" onmouseout="this.src='./img/sensitive.gif'">
</div>


<div id='moderate'  class="draggable">
<img src="./img/moderate.gif"/  onmouseover="this.src='./img/moderate_over.gif'" onmouseout="this.src='./img/moderate.gif'">
</div>

<div id='blunt'  class="draggable">
<img src="./img/blunt.gif"/  onmouseover="this.src='./img/blunt_over.gif'" onmouseout="this.src='./img/blunt.gif'">
</div>


</div>


<div id='divide-h'  class="draggable">
<img src="./img/divide_h.gif"/  onmouseover="this.src='./img/divide_h_over.gif'" onmouseout="this.src='./img/divide_h.gif'">
</div>

<div id='divide-v'  class="draggable">
<img src="./img/divide_v.gif"/  onmouseover="this.src='./img/divide_v_over.gif'" onmouseout="this.src='./img/divide_v.gif'">
</div>



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

<script language="JavaScript">

var canvas = buildCanvas(screen.width * 2,screen.height * 2);
document.getElementById("canvas").style.display = "none";


$('div.draggable').Draggable({zIndex:100000000,revert:true , ghosting: true, opacity: 1});;


show_filter();
show_preset();
show_values();


var scale_set_margin_x = 10;
var scale_set_margin_y = 100;


	document.write("<div id='main_palette_0' class='droppable'><img id='main_palette_0_img' onDblClick=palette_enlarge(0);  width=" + window_width_half + "px  src=./img/black.jpg style=' padding:0px;margin:0px;position:absolute;top:0px;left:200px;border-style: solid; border-width: 1px;border-color: green;';></div>");
	
	document.write("<div id='main_palette_1' class='droppable'><img id='main_palette_1_img' onDblClick=palette_enlarge(1); width=" + window_width_half + "px height=" + window_height_half + "px src=./img/black.jpg style='padding:0px;margin:0px;position:absolute;top:0px;left:" + window_width_position + "px;border-style: solid; border-width: 1px;border-color: green; ';></div>");
	
	document.write("<div id='main_palette_2'  class='droppable'><img id='main_palette_2_img' onDblClick=palette_enlarge(2); width=" + window_width_half + "px height=" + window_height_half + "px src=./img/black.jpg style='padding:0px;margin:0px;position:absolute;top:" + window_height_position + "px;left:200px;border-style: solid; border-width: 1px;border-color: green;';></div>");
	
	document.write("<div id='main_palette_3' class='droppable' ><img id='main_palette_3_img' onDblClick=palette_enlarge(3); width=" + window_width_half + "px height=" + window_height_half + "px src=./img/black.jpg style='padding:0px;margin:0px;position:absolute;top:" + window_height_position + "px;left:" + window_width_position + "px;border-style: solid; border-width: 1px;border-color: green; ';></div>");


var pos_main_palette_0_img_scale_top = 0 + scale_set_margin_y;
var pos_main_palette_0_img_scale_left = 200 + scale_set_margin_x;

document.write("<img id='main_palette_0_img_scale' src='./img/scale_img.gif' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_0_img_scale_top + "px;left:" + pos_main_palette_0_img_scale_left + "px;'>");

var pos_main_palette_1_img_scale_top = 0 + scale_set_margin_y;
var pos_main_palette_1_img_scale_left = window_width_position + scale_set_margin_x;
document.write("<img id='main_palette_1_img_scale' src='./img/scale_img.gif' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_1_img_scale_top + "px;left:" + pos_main_palette_1_img_scale_left + "px;'>");

var pos_main_palette_2_img_scale_top = window_height_position + scale_set_margin_y ;
var pos_main_palette_2_img_scale_left = 200 + scale_set_margin_x;
document.write("<img id='main_palette_2_img_scale' src='./img/scale_img.gif' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_2_img_scale_top + "px;left:" + pos_main_palette_2_img_scale_left + "px;'>");

var pos_main_palette_3_img_scale_top = window_height_position + scale_set_margin_y;
var pos_main_palette_3_img_scale_left = window_width_position + scale_set_margin_x;
document.write("<img id='main_palette_3_img_scale' src='./img/scale_img.gif' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_3_img_scale_top + "px;left:" + pos_main_palette_3_img_scale_left + "px;'>");




var info_set_margin_x = 10;
var info_set_margin_y = 10;

var pos_main_palette_0_info_top = 0 + info_set_margin_x;
var pos_main_palette_0_info_left = 200 + info_set_margin_y;

document.write("<div id='main_palette_0_info' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_0_info_top + "px;left:" + pos_main_palette_0_info_left + "px;'></div>");

var pos_main_palette_1_info_top = 0 + info_set_margin_x;
var pos_main_palette_1_info_left = window_width_position + info_set_margin_y;

document.write("<div id='main_palette_1_info' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_1_info_top + "px;left:" + pos_main_palette_1_info_left + "px;'></div>");


var pos_main_palette_2_info_top = window_height_position + info_set_margin_x;
var pos_main_palette_2_info_left = 200 + info_set_margin_y;

document.write("<div id='main_palette_2_info' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_2_info_top + "px;left:" + pos_main_palette_2_info_left + "px;'></div>");


var pos_main_palette_3_info_top = window_height_position + info_set_margin_x;
var pos_main_palette_3_info_left = window_width_position + info_set_margin_y;

document.write("<div id='main_palette_3_info' style='padding:0px;margin:0px;position:absolute;top:" + pos_main_palette_3_info_top + "px;left:" + pos_main_palette_3_info_left + "px;'></div>");




document.write("<IMG src=/img/black.jpg  id='main_palette_0_img_mag' onDblClick=palette_enlarge(0);  style='padding:0px;margin:0px;border-style: solid; border-width: 1px;border-color: red;position:absolute;top:0px;left:0px;display:none;'>")
document.write("<IMG src=/img/black.jpg  id='main_palette_1_img_mag'  onDblClick=palette_enlarge(1);  style='padding:0px;margin:0px;border-style: solid; border-width: 1px;border-color: red;position:absolute;top:0px;left:0px;display:none;'>")
document.write("<IMG src=/img/black.jpg  id='main_palette_2_img_mag' onDblClick=palette_enlarge(2);  style='padding:0px;margin:0px;border-style: solid; border-width: 1px;border-color: red;position:absolute;top:0px;left:0px;display:none;'>")
document.write("<IMG src=/img/black.jpg  id='main_palette_3_img_mag' onDblClick=palette_enlarge(3);  style='padding:0px;margin:0px;border-style: solid; border-width: 1px;border-color: red;position:absolute;top:0px;left:0px;display:none;'>")

	 document.getElementById("main_palette_0_img").addEventListener("DOMMouseScroll",wheel0,false);
	 document.getElementById("main_palette_1_img").addEventListener("DOMMouseScroll",wheel1,false);
	 document.getElementById("main_palette_2_img").addEventListener("DOMMouseScroll",wheel2,false);
	 document.getElementById("main_palette_3_img").addEventListener("DOMMouseScroll",wheel3,false);

	 document.getElementById("main_palette_0_img_mag").addEventListener("DOMMouseScroll",wheel0,false);
	 document.getElementById("main_palette_1_img_mag").addEventListener("DOMMouseScroll",wheel1,false);
	 document.getElementById("main_palette_2_img_mag").addEventListener("DOMMouseScroll",wheel2,false);
	 document.getElementById("main_palette_3_img_mag").addEventListener("DOMMouseScroll",wheel3,false);
	 
	 
	 document.onkeydown = key_press;
	 
	 document.getElementById("main_palette_0_img").addEventListener("mouseover",active_key0,false);
	 document.getElementById("main_palette_1_img").addEventListener("mouseover",active_key1,false);
	 document.getElementById("main_palette_2_img").addEventListener("mouseover",active_key2,false);
	 document.getElementById("main_palette_3_img").addEventListener("mouseover",active_key3,false);
	 
	 document.getElementById("main_palette_0_img_mag").addEventListener("mouseover",active_key0,false);
	 document.getElementById("main_palette_1_img_mag").addEventListener("mouseover",active_key1,false);
	 document.getElementById("main_palette_2_img_mag").addEventListener("mouseover",active_key2,false);
	 document.getElementById("main_palette_3_img_mag").addEventListener("mouseover",active_key3,false);
	 


	 


$('#main_palette_0_img').Droppable(
	{
		accept :		'draggable', 
		ondrop:	function (drag) 
			{
				var function_flag = 0;
				var exchange_series_id = $(drag).attr('id');
				for(var i = 0; i < function_list.length; i++){
					if(function_list[i] == exchange_series_id){
					function_flag = 1;
					break;
					}
				}
				if(function_flag == 1){
				select_function(0,exchange_series_id);
				}else{
				exchange_palette(0,exchange_series_id);
				}
				//exchange_palette(0,exchange_series_id);
				//alert(exchange_series_id); 
				//alert(this); //the droppable
				//alert(drag); //the dragganle
			}
	}
	);


$('#main_palette_1_img').Droppable(
	{
		accept :		'draggable', 
		ondrop:	function (drag) 
			{
				var function_flag = 0;
				var exchange_series_id = $(drag).attr('id');
				for(var i = 0; i < function_list.length; i++){
					if(function_list[i] == exchange_series_id){
					function_flag = 1;
					break;
					}
				}
				if(function_flag == 1){
				select_function(1,exchange_series_id);
				}else{
				exchange_palette(1,exchange_series_id);
				}
				//exchange_palette(0,exchange_series_id);
				//alert(exchange_series_id); 
				//alert(this); //the droppable
				//alert(drag); //the dragganle
			}
	}
	);
$('#main_palette_2_img').Droppable(
	{
		accept :		'draggable', 
		ondrop:	function (drag) 
			{
				var function_flag = 0;
				var exchange_series_id = $(drag).attr('id');
				for(var i = 0; i < function_list.length; i++){
					if(function_list[i] == exchange_series_id){
					function_flag = 1;
					break;
					}
				}
				if(function_flag == 1){
				select_function(2,exchange_series_id);
				}else{
				exchange_palette(2,exchange_series_id);
				}
				//exchange_palette(0,exchange_series_id);
				//alert(exchange_series_id); 
				//alert(this); //the droppable
				//alert(drag); //the dragganle
			}
	}
	);
$('#main_palette_3_img').Droppable(
	{
		accept :		'draggable', 
		ondrop:	function (drag) 
			{
				var function_flag = 0;
				var exchange_series_id = $(drag).attr('id');
				for(var i = 0; i < function_list.length; i++){
					if(function_list[i] == exchange_series_id){
					function_flag = 1;
					break;
					}
				}
				if(function_flag == 1){
				select_function(3,exchange_series_id);
				}else{
				exchange_palette(3,exchange_series_id);
				}
				//exchange_palette(0,exchange_series_id);
				//alert(exchange_series_id); 
				//alert(this); //the droppable
				//alert(drag); //the dragganle
			}
	}
	);
	
</script>




<script language="JavaScript">

<?php




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


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



//patient query
/*
$sql = sql_generator("show_study_patient");
$rtn_patient = $db_handle->prepare($sql);
$rtn_patient->execute(array($_GET['patid']));

print("\n $sql \n");

  while ($patient_row = $rtn_patient->fetch(PDO::FETCH_ASSOC)) {
//	print_r($patient_row);
	$patsex = $patient_row['patsex'] ;
	$patname = $patient_row['patnam'] ;
	$patbirthdate = $patient_row['birthdate'] ;
  }

	$patage = calc_age($patbirthdate);
	$patname = str_replace("^^^" , "", $patname);
	$patname = str_replace("^" , ",", $patname);
	$patname = trim($patname);
	$patname = str_replace(" ","",$patname);
	$patname = str_replace(",","_",$patname);
*/

// prepare studyUID and seriesUID selection

$sql = sql_generator("show_study_studyUID");
$rtn_studyUID = $db_handle->prepare($sql);

$sql = sql_generator("show_study_seriesUID");
$rtn_seriesUID = $db_handle->prepare($sql);



// prepair for sumnail

$sql = sql_generator("show_study_sumnail");
$rtn_sumnail = $db_handle->prepare($sql);


//get study with patientID
$studyUID = $_GET['studyUID'];
$rtn_seriesUID->execute(array($_GET['studyUID']));

$series_count = $rtn_seriesUID->rowCount();
$rtn_series_array = array();
$rtn_image_array = array();
$rtn_image_array_for_col = array();

	$rtn_seriesUID->execute(array($studyUID));
	
	$rtn_series_array = $rtn_seriesUID->fetchAll();
	//print_r($rtn_series_array);
	
	
		
	$max_imanum = 0;
	

	$loop_count = 0;
	
	foreach ($rtn_series_array as $seriesUID_row ) {
	$seriesUID = $seriesUID_row['seriesuid'] ;
	$sernum = $seriesUID_row['sernum'] ;
	$series_mod = $seriesUID_row['mod'] ;
	
	if($max_imanum < $sernum){
	$max_imanum = $sernum;
	}
	
	print("study_series_list.push(\"$seriesUID\") ;\n");
	print("sync_flag_for_series[\"$seriesUID\"] = 1 ;\n");
	print("multiframe_flag_for_series[\"$seriesUID\"] = 0 ;\n");
	print("sync_original_val_for_series[\"$seriesUID\"] = 0 ;\n");
	print("sync_changed_val_for_series[\"$seriesUID\"] = 0 ;\n");
	print("sync_target_axis_for_series[\"$seriesUID\"] = 0 ;\n");
	print("sync_defference_val_for_series[\"$seriesUID\"] = 0 ;\n");
	print("imagestest['$seriesUID'] = new Object(); \n");
	print("dicomset['$seriesUID'] = new Object(); \n");
	print("jpgset['$seriesUID'] = new Object(); \n");
	print("referset['$seriesUID'] = new Object(); \n");
	print("tableset['$seriesUID'] = new Object(); \n");
	print("tableset_x['$seriesUID'] = new Object(); \n");
	print("tableset_y['$seriesUID'] = new Object(); \n");
	print("image_information['$seriesUID'] = new Object(); \n");
	print("tableset_z['$seriesUID'] = new Object(); \n");
	print("dicom_path['$seriesUID'] = \"\" \n;");
	print("jpg_path['$seriesUID'] = \"\"; \n");
	print("refer_path['$seriesUID'] = \"\"; \n");
	print("scale_ratio['$seriesUID'] = \"\"; \n");
	
	print("image_count_cache['$seriesUID'] = 0 ;\n");
	print("image_count_max['$seriesUID'] = $sernum ;\n");
	
	
	//print("$seriesUID $sernum $series_mod \n");
	
		$sql = sql_generator("show_image");
		$rtn_image_array[$seriesUID] = $db_handle->prepare($sql);
		$rtn_image_array[$seriesUID]->bindParam(1, $seriesUID);
		$rtn_image_array[$seriesUID]->bindParam(2, $studyUID);
		
		$rtn_image_array[$seriesUID]->execute();
		
		
		$rtn_image_array_for_col[$seriesUID] = $rtn_image_array[$seriesUID]->fetchAll();
	
	$loop_count = $loop_count + 1;
	
	}

print("var max_imanum = $max_imanum;");

		

print("</script>");

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


// each parameter get loop



		$array_wl_from_dicomtag = Array();
		$array_ww_from_dicomtag = Array();
		$array_fov = Array();
		$array_px_x = Array();
		$array_px_y = Array();
		$array_matrix = Array();
		$array_series_description = Array();
		$array_patient_position = Array();
		$array_scale_ratio = Array();
		$array_image_jpeg = Array();
		$array_image_dicom = Array();
		$array_name_in_dicom = Array();
		$array_id_in_dicom = Array();
		$array_slice_thick = Array();
		$array_slice_between = Array();
		$array_px_size_x = Array();
		$array_px_size_y = Array();
		$array_x_y_ratio = Array();
		$power_each_series = Array();
		$negate_each_series = Array();
		$rotate_each_series = Array();
		$image_count_max = Array();
		$array_multiframe_tag = Array();
		
		
		
foreach ($rtn_series_array as $seriesUID_row ) {
	$seriesUID = $seriesUID_row['seriesuid'] ;
	
	$sernum = $seriesUID_row['sernum'] ;
	$image_count_max[$seriesUID] = $sernum;
	
	if($rtn_image_array_for_col[$seriesUID][0]['path']){
	//add dcm4chee root path
	$path = $rtn_image_array_for_col[$seriesUID][0]['path'];
	$path = $file_root_path . $path;
	
	
	//file path set
    $file_name=basename($path);
    @$target_dir=touch_dir($studyUID,$seriesUID,$wwwl);
    $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);
	}
	
		$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);
		@$series_description = get_dicom_value_parenthetic("0018,1030",$target_file_path);
		$series_description = str_replace(" ","_",trim($series_description));
		
		
		// pixel space
		$ps_origin = get_dicom_value("0028,0030",$target_file_path);
		$p_size = explode("\\",$ps_origin);
		if(count($p_size) == 2){
		$px_size_x = trim($p_size[0]);
		$px_size_y = trim($p_size[1]);
		}else{
		$px_size_x = 1;
		$px_size_y = 1;
		}
		
		if(!$px_size_x or $px_size_x == ""){
		$px_size_x = 1;
		}
		if(!$px_size_y or $px_size_y == ""){
		$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_each_series['$seriesUID'] = $power;");
		}elseif($col <= 320 or $row <= 320){
		$power = 2 ;
		 print("power_each_series['$seriesUID'] = $power;");
		}else{
		$power = 1 ;
		print("power_each_series['$seriesUID'] = $power;");
		}
		
		
		print("negate_each_series['$seriesUID'] = $negate;");
		print("rotate_each_series['$seriesUID'] = $rotate;");
		
		$power_each_series[$seriesUID] = $power;
		$negate_each_series[$seriesUID] = $negate;
		$rotate_each_series[$seriesUID] = $rotate;
		
		@$wl_from_dicomtag = round(get_dicom_value("0028,1050",$target_file_path));
		@$ww_from_dicomtag = round(get_dicom_value("0028,1051",$target_file_path));
		
		//$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 );
		}else{
		$scale_ratio = 1;
		}
		
		@$multiframe_tag = get_dicom_value("0028,0008",$target_file_path);
	
		
		
		$array_wl_from_dicomtag[$seriesUID] = $wl_from_dicomtag;
		$array_ww_from_dicomtag[$seriesUID] = $ww_from_dicomtag;
		$array_fov[$seriesUID] = $fov;
		@$array_px_x[$seriesUID] = $row;
		@$array_px_y[$seriesUID] = $col;
		@$array_matrix[$seriesUID] = $matrix;
		$array_scale_ratio[$seriesUID] = $scale_ratio;
		
		$array_name_in_dicom[$seriesUID] = $name_in_dicom;
		$array_id_in_dicom[$seriesUID] = $id_in_dicom;
		$array_slice_thick[$seriesUID] = $slice_thick;
		$array_slice_between[$seriesUID] = $slice_between;
		$array_patient_position[$seriesUID] = $patient_position;
		
		$array_px_size_x[$seriesUID] = $px_size_x;
		$array_px_size_y[$seriesUID] = $px_size_y;
		
		$x_y_ratio = $row / $col;
		$array_x_y_ratio[$seriesUID] = $x_y_ratio;
		
		$array_series_description[$seriesUID] = $series_description;
		
		$array_multiframe_tag[$seriesUID] = $multiframe_tag;
		
		print("col_from_dicomtag['$seriesUID'] = $col; \n");
		print("row_from_dicomtag['$seriesUID'] = $row; \n");
		
		print("wl_from_dicomtag['$seriesUID'] = $wl_from_dicomtag; \n");
		print("ww_from_dicomtag['$seriesUID'] = $ww_from_dicomtag; \n");
		print("fov['$seriesUID'] = '$fov'; \n");
		print("scale_ratio['$seriesUID'] = $scale_ratio; \n");
		print("patient_position['$seriesUID'] = '$patient_position'; \n");
		print("name_in_dicom['$seriesUID'] = '$name_in_dicom'; \n");
		print("id_in_dicom['$seriesUID'] = '$id_in_dicom'; \n");
		
		print("series_description['$seriesUID'] = '$series_description'; \n");
		
		print("array_multiframe_tag['$seriesUID'] = '$multiframe_tag'; \n");
		

		
		if($slice_thick and ($slice_thick != "")){
		print("slice_thick['$seriesUID'] = $slice_thick; \n");
		}
		if($slice_between and ($slice_between != "")){
		print("slice_between['$seriesUID'] = $slice_between; \n");
		}
		print("px_size_x['$seriesUID'] = $px_size_x; \n");
		print("px_size_y['$seriesUID'] = $px_size_y; \n");
		print("x_y_ratio['$seriesUID'] = $x_y_ratio; \n");
		print("matrix['$seriesUID'] = '$matrix'; \n");
		
	}
}

print("</script>");



// image get loop

$draggable_flag = 0;
$img_top = 0;
$palette_flag = 0;

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

for($i=0;$i<$max_imanum;$i++){
	foreach ($rtn_series_array as $seriesUID_row ) {
	
	
	
		$seriesUID = $seriesUID_row['seriesuid'] ;
			if(@$rtn_image_array_for_col[$seriesUID][$i]['path'] ){
			
			
			
			
			
			$path = $rtn_image_array_for_col[$seriesUID][$i]['path'];
			$path = $file_root_path . $path;
			
				//file path set
			
			$ww = $array_ww_from_dicomtag[$seriesUID];
			$wl = $array_wl_from_dicomtag[$seriesUID];	
			if(!$ww){
			$wwwl = "default";
			}
			if($ww){
			$wwwl = $ww . $wl;
			}
			
			
		    $file_name=basename($path);
		    @$target_dir=touch_dir($studyUID,$seriesUID,$wwwl);
		    $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);
			}
			
		
		$power = $power_each_series[$seriesUID]  ;
		$negate = $negate_each_series[$seriesUID]  ;
		$rotate = $rotate_each_series[$seriesUID]  ;
			
		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['$seriesUID']['$i'] = new Image();");
		
		$jpg_file_name=($target_file_path . "-" . $add_tag . ".jpg");
		
		if($array_multiframe_tag[$seriesUID] and $array_multiframe_tag[$seriesUID] != 0){
		
		}else{
			if(file_exists("$jpg_file_name")){
			}else{
			$jpg_file_name=dcmtojpg2_set_filter($target_file_path,$power,$negate,$rotate,$array_ww_from_dicomtag[$seriesUID],$array_wl_from_dicomtag[$seriesUID]);
			}
		}
		
		$jpg_file_name_tmp = "$jpg_file_name" . ".tmp.jpg" ;
		
		$array_image_jpeg[$seriesUID][$i] = $jpg_file_name;
		
		print("imagestest['$seriesUID']['$i'].src = \"$jpg_file_name\"; \n");
		print("jpgset['$seriesUID']['$i'] = \"$jpg_file_name\"; \n");
		print("referset['$seriesUID']['$i'] = \"$jpg_file_name_tmp\"; \n");
		
		
			//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['$seriesUID']['$i'] = \"$table_array[0]\"; \n");
		print("tableset_y['$seriesUID']['$i'] = \"$table_array[1]\"; \n");
		print("tableset_z['$seriesUID']['$i'] = \"$table_array[2]\"; \n");
		$table_set_x[$seriesUID][$i] = $table_array[0];
		$table_set_y[$seriesUID][$i] = $table_array[1];
		$table_set_z[$seriesUID][$i] = $table_array[2];
		}else{
		print("tableset_x['$seriesUID']['$i'] = \"1\"; \n");
		print("tableset_y['$seriesUID']['$i'] = \"1\"; \n");
		print("tableset_z['$seriesUID']['$i'] = \"1\"; \n");
		$table_set_x[$seriesUID][$i] = 1;
		$table_set_y[$seriesUID][$i] = 1;
		$table_set_z[$seriesUID][$i] = 1;
		$table_array[0] = 1;
		$table_array[1] = 1;
		$table_array[2] = 1;
		}
		
		
		
		print("tableset['$seriesUID']['$i'] = \"$table_position\"; \n");
		//print("dicomset['$seriesUID']['$i'] = '$path'; \n");
		print("dicomset['$seriesUID']['$i'] = '$target_file_path'; \n");
		
		$now_count = $i + 1;
		print("image_information['$seriesUID']['$i'] = '$array_series_description[$seriesUID]' + '<br>' + 'x:' + '$table_array[0]' + 'y:' + '$table_array[1]' + 'z:' + '$table_array[2]' + '<br>' + '$array_fov[$seriesUID]' +  ' ' + '$array_matrix[$seriesUID]' + '<br>' + '$array_name_in_dicom[$seriesUID]' +  '<br>' + '$array_id_in_dicom[$seriesUID]'  + '<br>' + '$now_count' + '/' + '$seriesUID_row[sernum]'  ; ");
		
		
		print("image_count_server['$seriesUID'] = '$i';");
		
		print("</script>");
		print("<script language=\"JavaScript\">");
		
		if($i == 0){
		/*
		print("window.opener.document.getElementById('$seriesUID').patient_position.value = patient_position['$seriesUID'];");
		print("window.opener.document.getElementById('$seriesUID').image_num.value = $i;");
		print("window.opener.document.getElementById('$seriesUID').position_x.value = tableset_x['$seriesUID'][$i];");
		print("window.opener.document.getElementById('$seriesUID').position_y.value = tableset_y['$seriesUID'][$i];");
		print("window.opener.document.getElementById('$seriesUID').position_z.value = tableset_z['$seriesUID'][$i];");
		*/
		if(!@$cache_start_tag){
		print("count_local_cache();");
		$cache_start_tag = 1;
		}
		
		$image_path = $array_image_jpeg[$seriesUID][$i];
		$img_position = $img_top . "px";
		
		if($palette_flag < 4){
		$target_palette = "main_palette_" . $palette_flag . "_img";
		print("document.$target_palette.src = '$image_path'; ");
		$target_palette_id = "main_palette_id[" . $palette_flag . "]";
		print("$target_palette_id = '$seriesUID'; ");
		$palette_info_tag = "main_palette_" . $palette_flag .  "_info";
		print("document.getElementById('$palette_info_tag').innerHTML = image_information['$seriesUID']['$i'];");
		
		$palette_flag += 1;
		print("resize_pack();");
		}
		
		print("</script>");
		
		$id_with_img = $seriesUID . "_img";
		//series_drag
		@$series_description = $array_series_description[$seriesUID];
		if(@$series_description == ""){
		@$series_description = "no series name";
		}
		
		print("<div id=\"$seriesUID\" style='position:absolute;top:$img_position;left:100px;border-style: solid; border-width: 2px;border-color: green;'><div id=\"$seriesUID\" class=\"draggable\"><img src=\"$image_path\" id=\"$id_with_img\"  width=100 title=\"$series_description\" ;></div></div>");
		
		$seriesUID_param = $seriesUID . "_param";
		print("<div id=\"$seriesUID_param\" style='position:absolute;top:$img_position;left:100px;color:orange;'>$seriesUID</div>");
		
		print("<script language=\"JavaScript\">");
		
		print("$('div.draggable').Draggable({zIndex:1000,revert:true , ghosting: true, opacity: 0.8});");
		print("document.getElementById(\"$seriesUID\").addEventListener(\"DOMMouseScroll\",sumnail_scrool,false);");
		
		
		print("enableTooltips(\"$seriesUID\");");
		
//revert:true ,
//fx: 300,
		
		
		
		
		$img_top += $array_x_y_ratio[$seriesUID] * 100;
		print("image_count_now['$seriesUID'] = $i ;");
		
		
		
		}
		
		
		
		if($i == 2){
		
		$canditate_x = 0;
		$canditate_y = 0;
		$canditate_z = 0;
		if($table_set_x[$seriesUID][0] == $table_set_x[$seriesUID][1] and $table_set_x[$seriesUID][0] == $table_set_x[$seriesUID][2]){
			$canditate_x = 1;
		}
		if($table_set_y[$seriesUID][0] == $table_set_y[$seriesUID][1] and $table_set_y[$seriesUID][0] == $table_set_y[$seriesUID][2]){
			$canditate_y = 1;
		}
		if($table_set_z[$seriesUID][0] == $table_set_z[$seriesUID][1] and $table_set_z[$seriesUID][0] == $table_set_z[$seriesUID][2]){
			$canditate_z = 1;
		}
		
		
		
		if(($canditate_x == 1) and ($canditate_y == 1)){
		$variable_orientation = "z";
		//print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		print("target_orientation['$seriesUID'] = '$variable_orientation';");
		}
		elseif(($canditate_y == 1) and ($canditate_z == 1)){
		$variable_orientation = "x";
		//print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		print("target_orientation['$seriesUID'] = '$variable_orientation';");
		}
		elseif(($canditate_z == 1) and ($canditate_x == 1)){
		$variable_orientation = "y";
		//print("window.opener.document.getElementById('$seriesUID').target_orientation.value = '$variable_orientation';");
		print("target_orientation['$seriesUID'] = '$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';");
		print("target_orientation['$seriesUID'] = '$variable_orientation';");
		}
		
		
	//	print("alert(\"$variable_orientation\");");
	
	
	
	}
			
			//image loop for no multiframe
			}
		}
}




?>


</script>

<?php

// multiframe compensate


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

	foreach ($rtn_series_array as $seriesUID_row ) {
	$seriesUID = $seriesUID_row['seriesuid'] ;
	$seriesUID_img = $seriesUID . "_img";
	
	$multiframe_tag = $array_multiframe_tag[$seriesUID];
	$image_num = $image_count_max[$seriesUID];
	
	$image_count = 0;
	$image_start_num = 0;
		if($array_multiframe_tag[$seriesUID] and $array_multiframe_tag[$seriesUID] != 0){
			for($i=0;$i < $image_num;$i++){
			$path = $rtn_image_array_for_col[$seriesUID][$i]['path'];
			$path = $file_root_path . $path;
			
			$file_name=basename($path);
		    @$target_dir=touch_dir($studyUID,$seriesUID,$wwwl);
		    $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 != readlink($target_file_path)){
				system("rm -f $target_file_path");
				symlink($path,$target_file_path);
				}
			}else{
				symlink($path,$target_file_path);
			}
			
			$multiframe_tag = get_dicom_value("0028,0008",$target_file_path);
			//print("alert(\"$multiframe_num[$i]\");");
			
			$fn_addone = trim($multiframe_tag) + 1 ;
			$image_total_num = trim($multiframe_tag);
			$image_count += $image_total_num;
			
			
			$check_cache_flag = 0;
			for ($j=0;$j < $image_total_num;$j++) {
				$jpg_file_name = $target_file_path . "." . $j . ".jpg";
				if(file_exists($jpg_file_name)){
				//print("we have cache $jpg_file_name; \n");
				//cache ok
				}else{
				$check_cache_flag = 1;
				}
			}
			
			if($check_cache_flag == 1){
			$dcm2pnm = "/usr/local/dicom/bin/dcmj2pnm";
			system("$dcm2pnm +Fa +oj  $target_file_path $target_file_path");
			//print("alert(\"start convert\");");
			}
			
			
			$j_min = 1;
				for ($j=0;$j < $image_total_num;$j++) {
				
				$jpg_file_name = $target_file_path . "." . $j . ".jpg";
				$jpg_file_name_tmp = "$jpg_file_name" . ".tmp.jpg" ;
				$imagesrc = "imagesrc" . "$j_min";
				$target_num = $image_start_num + $j;
				$now_locate = $target_num + 1 ;
				
				print("imagestest['$seriesUID']['$target_num'] = new Image(); \n");
			
				print("imagestest['$seriesUID']['$target_num'].src = \"$jpg_file_name\" ;\n");
				print("jpgset['$seriesUID']['$target_num'] = \"$jpg_file_name\";\n");
				
				print("dicomset['$seriesUID']['$target_num'] = \"$target_file_path\";\n");
				print("referset['$seriesUID']['$target_num'] = \"$jpg_file_name_tmp\";\n");
				print("image_information['$seriesUID']['$target_num'] = '$array_series_description[$seriesUID]' + '<br>' + '$array_fov[$seriesUID]' +  ' ' + '$array_matrix[$seriesUID]' + '<br>' + '$array_name_in_dicom[$seriesUID]' +  '<br>' + '$array_id_in_dicom[$seriesUID]'  + '<br>' + '$now_locate' + '/' + '$image_total_num'  ; ");
				//print("image_information['$seriesUID']['$target_num'] = $target_num;\n");
				if($j == 0){
				print("document.getElementById(\"$seriesUID_img\").src = \"$jpg_file_name\";");
				print("
				if(main_palette_id[0] == '$seriesUID'){
				document.getElementById('main_palette_0_img').src = '$jpg_file_name';
		document.getElementById('main_palette_0_img_mag').src = '$jpg_file_name';
				}
				if(main_palette_id[1] == '$seriesUID'){
		document.getElementById('main_palette_1_img').src = '$jpg_file_name';
		document.getElementById('main_palette_1_img_mag').src = '$jpg_file_name';
				}
				if(main_palette_id[2] == '$seriesUID'){
		document.getElementById('main_palette_2_img').src = '$jpg_file_name';
		document.getElementById('main_palette_2_img_mag').src = '$jpg_file_name';
				}
				if(main_palette_id[3] == '$seriesUID'){
		document.getElementById('main_palette_3_img').src = '$jpg_file_name';
		document.getElementById('main_palette_3_img_mag').src = '$jpg_file_name';
				}
				");
				}
				
				}
				$image_start_num += $image_total_num;
			}
		print("image_count_max['$seriesUID'] = $image_count ;\n");
		print("multiframe_flag_for_series['$seriesUID'] = 1 ;\n");
		print("sync_flag_for_series['$seriesUID'] = 0 ;\n");
		}
	}


print("</script>");

?>


</body>
