﻿<krpano h="1">

<action name="dothejob">
trace('In Edit Mode / No Google nav displayed')
</action>

<action name="followmouse_init">
trace('In Edit Mode / No follow mouse effect')
</action>
//CONSERVER FICHIER XML ET URL

<action name="store_xml_file" autorun="preinit">
copy(xml_file,xml.content);
copy(xml_url,xml.url);
</action>


//TOOLBOX

<plugin name="toolbox"
	url="toolbox/toolboxV2.js"
	preload="true"
	keep="true"

	poly_bgcolor="0x336699"
	dot_color="0xffff00"
	dot_size="12"
	active_dot_color="0x00ff00"
	active_dot_size="12"

	keycode_to_home="77"
	keycode_to_activate="84"
	keycode_to_log="76"
	keycode_to_numbers="78"
	keycode_to_distorted="72"
	keycode_to_poly="80"
	keycode_to_grid="71"
	keycode_to_stickies="74"
	keycode_to_colorpicker="75"
	keycode_to_info="73"

	decimals_for_numbers="3"
	decimals_for_dhe="2"

	basecolor_for_colorpicker="0xff0000"

	/>

//TOOLS

<layer name="note_tools_container" type="container" align="top" width="455" height="40" bgcolor="0x000000" bgalpha="0.7" bgroundedge="10 10 0 0" y="80" bgcapture="true" ondown="draglayer();" keep="true">
<layer name="tools_move" url="note_tool_assets/move.png" x="25" scale="0.4" align="left" edge="center" alpha="0.7" enabled="false"/>
<layer name="tools_line1" style="tool_line" x="45"/>
<layer name="note_add_hs" url="note_tool_assets/hotspot.png" x="75" style="tool" onclick="edit_addHS('myhotspot')" onhover="showtext(add nav hotspot)"/>
<layer name="note_add_hs_up" url="note_tool_assets/floor_direction.png" x="120" style="tool" rotate="180" onclick="edit_addFloorHS_up('floor_direction')" onhover="showtext(add level up hotspot)"/>
<layer name="note_add_hs_down" url="note_tool_assets/floor_direction.png" x="165" style="tool" onclick="edit_addFloorHS_down('floor_direction')" onhover="showtext(add level down hotspot)"/>
<layer name="note_add_hs_shop" url="note_tool_assets/shop_hs.png" x="210" style="tool" onclick="edit_addHS_shop('shop')" onhover="showtext(add shop hotspot)"/>
<layer name="note_add_hs_callout" url="note_tool_assets/hotspot_red.png" x="255" style="tool" onclick="edit_addHS_callout('callout')" onhover="showtext(add callout hotspot)"/>
<layer name="note_set_view" url="note_tool_assets/set_view.png" x="300" style="tool" onclick="edit_set_initial_view()" onhover="showtext(set scene view)"/>
<layer name="note_set_startview" url="note_tool_assets/set_startview.png" x="345" style="tool" onclick="edit_set_start_view()" onhover="showtext(set start scene)"/>
<layer name="note_set_thumbnail" url="note_tool_assets/camera.png" x="390" style="tool" onclick="edit_make_thumbnail()" onhover="showtext(generate thumbnail)"/>
<layer name="tools_line2" style="tool_line" x="415"/>
<layer name="tools_send" url="note_tool_assets/send.png" x="440" scale="0.4" align="left" edge="center" alpha="0.7" onclick="edit_save_my_xml()" onover="tween(alpha,1)" onout="tween(alpha,0.7)" onhover="showtext(Save Tour)"/>
</layer>

<style name="tool" scale="0.7" align="left" edge="center" alpha="0.5" onover="tween(alpha,1,0.2)" onout="tween(alpha,0.5,0.2)" />
<style name="tool_line" type="container" width="1" height="40%" align="left" edge="center" bgcolor="0xdddddd" bgalpha="0.6"/>

//LIST ALL SCENES

<events name="update_scene_list" onnewscene="layer[cbscenes].selectItemByName(calc(scene[get(xml.scene)].name));set(scene_to_link,calc(scene[get(xml.scene)].name)));" keep="true"/>
<include url="%VIEWER%/plugins/combobox.xml" />


<combobox name="cbscenes" design="jerome" align="bottom" edge="top" width="calc:layer[note_tools_container].width" onloaded="add_scene_items();" parent="layer[note_tools_container]"/>

	<action name="add_scene_items" scope="local">
		for(set(i,0), i LT scene.count, inc(i),
			caller.addNameditem(calc(scene[get(i)].name),calc('[img src=[dq]' + scene[get(i)].thumburl +  '[dq] style=[dq]border:1px solid rgba(255,255,255,1);width:160px;height:80px;vertical-align:middle;margin-right:8px;object-fit: cover;[dq]/] '+scene[get(i)].title), calc('set(scene_to_link,'+scene[get(i)].name+');') );
		);
		layer[cbscenes].item.sortby(name);
	</action>

	<combobox_design name="jerome" margin="4" open_close_speed="0.25">
		<style name="combobox_container_style" bgalpha="0.7" bgcolor="0x000000" bgborder="0" bgroundedge="0 0 10 10"/>
		<style name="combobox_marker_style" css="color:#FFFFFF;" bg="false" txtshadow="0 0 2 0x000000 1" />
		<style name="combobox_item_style" css="color:#FFFFFF;" padding="10" bg="false" bgcolor="0xFFFFFF" bgalpha="0.5" bgroundedge="0" txtshadow="0 0 2 0x000000 1" />
	</combobox_design>

//KEYBOARD ACTIONS

<control usercontrol="mouse"/>
<events onkeydown="trace(keycode);if(keycode == 13,load_selected_scene());if(keycode == 37,rotatenorthleft());if(keycode == 39,rotatenorthright());if(keycode == 65,edit_loop_all_scenes_north());copy(pressedkey, keycode);" onkeyup="edit_update_prealign();delayedcall(0.2,set(pressedkey,0));" onnewscene="if(image.prealign,txtsplit(image.prealign, |, ix, iy, iz),copy(image.prealign,'0|0|0');set(ix,0);set(iy,0);set(iz,0);)"/>
<action name="load_selected_scene">
loadscene(get(scene_to_link));
</action>
<action name="rotatenorthleft">
add(iy,0.8);
calc(image.prealign,ix+'|'+iy+'|'+iz);
</action>
<action name="rotatenorthright">
sub(iy,0.8);
calc(image.prealign,ix+'|'+iy+'|'+iz);
</action>

//NORTH LINE
<layer name="linenorthtop" type="container" bgalpha="1" bgcolor="0xffffff" width="2" height="200" parent="hotspot[northdirection]" align="top" edge="bottom" keep="true"/>
<hotspot name="northdirection" type="text" bg="false" bgborder="2 0xffffff 1" bgroundedge="25" width="50" height="50" vcenter="true" html="N" css="text-align:center;color:#fff;font-size:30px" ath="0" atv="0" keep="true" enabled="false"/>
<layer name="linenorthbottom" type="container" bgalpha="1" bgcolor="0xffffff" width="2" height="200" parent="hotspot[northdirection]" align="bottom" edge="top" keep="true"/>

//HORIZON LINE

<events name="horizon" onviewchanged="tween(hotspot[horizon_line].ath|hotspot[horizon_line2].ath|hotspot[horizon_line3].ath,get(view.hlookat),0.01)" keep="true"/>
<hotspot name="horizon_line" atv="0" width="calc:stagewidth" height="3" type="text" bgcolor="0xff0000" keep="true" enabled="false"/>
<hotspot name="horizon_line2" atv="4.66" width="calc:stagewidth/2" height="2" type="text" bgcolor="0x5de1f8" keep="true" enabled="false"/>
<hotspot name="horizon_line3" atv="26.24" width="calc:stagewidth/2" height="2" type="text" bgcolor="0x5de1f8" keep="true" enabled="false"/>

//INTIAL VIEW SET

<action name="show_view_set">
tween(layer[viewset].alpha,1,,,
delayedcall(2,tween(layer[viewset].alpha,0))
)
</action>

<layer name="viewset" type="text" html="Initial view set" align="center" css="color:#fff;font-size:18px;font-family:Primary" backgroundcolor="0x000000" backgroundalpha="0.8" roundedge="8" padding="6 12" keep="true" alpha="0" autoalpha="true"/>

//START VIEW SET

<action name="show_startview_set">
tween(layer[startviewset].alpha,1,,,
delayedcall(2,tween(layer[startviewset].alpha,0))
)
</action>

<layer name="startviewset" type="text" html="Start scene set" align="center" css="color:#fff;font-size:18px;font-family:Primary" backgroundcolor="0x000000" backgroundalpha="0.8" roundedge="8" padding="6 12" keep="true" alpha="0" autoalpha="true"/>


//ALL SCENES NORTH SET

<action name="show_north_set">
tween(layer[north_set].alpha,1,,,
delayedcall(2,tween(layer[north_set].alpha,0))
)
</action>

<layer name="north_set" type="text" html="All scenes are north oriented with the same direction" align="center" css="color:#fff;font-size:18px;font-family:Primary" backgroundcolor="0x000000" backgroundalpha="0.8" roundedge="8" padding="6 12" keep="true" alpha="0" autoalpha="true"/>

//GENERATE HOTSPOTS

<!-- <hotspot name="active_zone" keep="true" type="text" bgborder="2 0xffffff 1" bgcolor="0x000000" bgalpha="0.3" enabled="false" width="600" height="600" bgroundedge="300" visible="false" distorted="true" scale="0.5"/>
<action name="display_active_zone" scope="local">
copy(hotspot[active_zone].ath, caller.ath);
copy(hotspot[active_zone].atv, caller.atv);
calc(hotspot[active_zone].rx, -90 - caller.atv);
set(hotspot[active_zone].visible, true);
</action> -->


<style name="myhotspot" url="note_tool_assets/hotspot_green.png" visible="true" edge="bottom"
	   ondown="edit_drag_spots();"
	   onup="edit_updateHSpos();"
	   downx="0"
       downy="0"
       clicks="0"
       onsingleclick="load3d"
       ondoubleclick="edit_removeHS();"
       onclick="doubleclick_style_onclick();"
	   onhover="showtext(get(scene[get(linkedscene)].name));display_active_zone()"
	   onout="set(hotspot[active_zone].visible, false);"
	   onloaded="if(contains(get(linkedscene),'3d'),set(url,%SWFPATH%/note_tool_assets/hotspot_red.png))"
	   />

<style name="floor_direction" url="note_tool_assets/floor_direction.png" visible="true"
	   onloaded="if(!down,set(rotate,180))"
	   ondown="edit_drag_spots();"
	   onup="edit_updateHSpos();"
	   downx="0"
       downy="0"
       clicks="0"
       onsingleclick="load3d"
       ondoubleclick="edit_removeHS();"
       onclick="doubleclick_style_onclick();"
	   onhover="showtext(get(scene[get(linkedscene)].name));"
	   />

<style name="shop" visible="true"
	   ondown="edit_drag_shop_spots();"
	   onup="edit_updateHSpos();"
	   downx="0"
       downy="0"
       clicks="0"
       onsingleclick="if(pressedkey == 17,popup(get(name)),edit_unit_key());"
       ondoubleclick="edit_remove_shop_HS();"
       onclick="doubleclick_style_onclick();"
	   onhover="showtext(get(unit_key))"
	   />

<style name="callout"
	   url="note_tool_assets/hotspot_red.png"
	   edge="bottom"
	   ondown="edit_drag_spots();"
	   onup="edit_updateHSpos();"
	   downx="0"
       downy="0"
       clicks="0"
       onsingleclick="edit_updateHStooltip();"
       ondoubleclick="edit_removeHS();"
       onclick="doubleclick_style_onclick();"
	   onhover="showtext(get(title))"
	   onloaded=""
	   rotate="0"
	   />

	<action name="draglayer" protect="true">
		copy(drag_currentx, x);
		copy(drag_currenty, y);
		copy(drag_stagex, mouse.stagex);
		copy(drag_stagey, mouse.stagey);
		indexoftxt(align_contains_right, get(align), 'right');
		indexoftxt(align_contains_bottom, get(align), 'bottom');
		calc(drag_align_x, align_contains_right GE 0 ? -1 : +1);
		calc(drag_align_y, align_contains_bottom GE 0 ? -1 : +1);
		asyncloop(pressed,
			calc(x, drag_currentx + (mouse.stagex - drag_stagex)*drag_align_x);
			calc(y, drag_currenty + (mouse.stagey - drag_stagey)*drag_align_y);
		  );
	</action>


<action name="edit_drag_spots" protect="true">
	if(%1 != dragging,
		spheretoscreen(ath, atv, hotspotcenterx, hotspotcentery);
		sub(drag_adjustx, mouse.stagex, hotspotcenterx);
		sub(drag_adjusty, mouse.stagey, hotspotcentery);
		edit_drag_spots(dragging);
		,
		if(pressed,
			sub(dx, mouse.stagex, drag_adjustx);
			sub(dy, mouse.stagey, drag_adjusty);
			screentosphere(dx, dy, ath, atv);
			delayedcall(0, edit_drag_spots(dragging) );
			);
		);
</action>

<action name="edit_drag_shop_spots" protect="true">
	if(%1 != dragging,
		spheretoscreen(ath, atv, hotspotcenterx, hotspotcentery);
		sub(drag_adjustx, mouse.stagex, hotspotcenterx);
		sub(drag_adjusty, mouse.stagey, hotspotcentery);
		edit_drag_shop_spots(dragging);
		,
		if(pressed,
			sub(dx, mouse.stagex, drag_adjustx);
			sub(dy, mouse.stagey, drag_adjusty);
			screentosphere(dx, dy, ath, atv);
			screentosphere(dx, dy, hotspot[get(myicon)].ath, hotspot[get(myicon)].atv);
			screentosphere(dx, dy, hotspot[get(mytext)].ath, hotspot[get(mytext)].atv);
			screentosphere(dx, dy, hotspot[get(mypulse)].ath, hotspot[get(mypulse)].atv);
			delayedcall(0, edit_drag_shop_spots(dragging) );
			);
		);
</action>

//DOUBLE CLICK ACTION

<action name="doubleclick_style_onclick">
    inc(clicks);
    if(clicks == 2,
        set(clicks,0);
        stopdelayedcall(doubleclickdetector);
        if((((mouse.x-downx)^2 + (mouse.y-downy)^2)^0.5) LT (device.touch ? 10 : 2),
            ondoubleclick();
          ,
            onsingleclick();
            onsingleclick();
        );
      ,
        copy(downx, mouse.x);
        copy(downy, mouse.y);
        delayedcall(doubleclickdetector, 0.3,
            set(clicks,0);
            onsingleclick();
        );
    );
</action>


// MANIPULATE XML ACTIONS

<action name="edit_addHS" scope="local">
if(scene_to_link,
addhotspot(auto, dhs);
dhs.loadstyle(%1);
set(dhs,
	ath=calc(roundval(view.hlookat,2)),
	atv=calc(roundval(view.vlookat,2)),
	onfirstloaded=edit_addHStoscene
	);
copy(dhs.linkedscene,scene_to_link);
callwith(dhs,onfirstloaded);
	);
</action>

<action name="edit_addFloorHS_up" scope="local">
if(scene_to_link,
addhotspot(auto, fhs);
fhs.loadstyle(%1);
set(fhs,
	ath=calc(roundval(view.hlookat,2)),
	atv=calc(roundval(view.vlookat,2)),
	down=false,
	rotate=180,
	onfirstloaded=edit_addFloorHStoscene
	);
copy(fhs.linkedscene,scene_to_link);
callwith(fhs,onfirstloaded);
	);
</action>

<action name="edit_addFloorHS_down" scope="local">
if(scene_to_link,
addhotspot(auto, fhs);
fhs.loadstyle(%1);
set(fhs,
	ath=calc(roundval(view.hlookat,2)),
	atv=calc(roundval(view.vlookat,2)),
	down=true,
	onfirstloaded=edit_addFloorHStoscene
	);
copy(fhs.linkedscene,scene_to_link);
callwith(fhs,onfirstloaded);
	);
</action>

<action name="edit_addHS_shop" scope="local">
jsget(unitkey, prompt('Shop unit-key', '') );
if(unitkey,
addhotspot(auto, shs);
shs.loadstyle(%1);
set(shs,
	ath=calc(roundval(view.hlookat,2)),
	atv=calc(roundval(view.vlookat,2)),
	onfirstloaded=edit_addShopHStoscene
	);
copy(shs.unit_key,unitkey);
callwith(shs,onfirstloaded);
);
</action>

<action name="edit_addHS_callout" scope="local">
jsget(infotext, prompt('Callout text', '') );
if(infotext,
addhotspot(auto, chs);
chs.loadstyle(%1);
set(chs,
	ath=calc(roundval(view.hlookat,2)),
	atv=calc(roundval(view.vlookat,2)),
	onfirstloaded=edit_addCalloutHStoscene
	);
copy(chs.title,infotext);
callwith(chs,onfirstloaded);
	);
</action>

<action name="edit_updateHStooltip" type="Javascript" protect="true"><![CDATA[
let scene = krpano.get("scene[get(xml.scene)].content");
let newtooltip = prompt('Callout Text', caller.title);

let namelet = scene.indexOf('name="'+caller.name+'"');
let debut = scene.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scene.indexOf("/"+String.fromCharCode(62), namelet);

let hotspot = scene.substring(debut, fin+2);
let hotspot2 = hotspot;
let arg_i = hotspot.indexOf("title");
let gui1 = hotspot.indexOf('"', arg_i);
let gui2 = hotspot.indexOf('"', gui1+1);
let arg_i_var = hotspot.substring(gui1+1, gui2);

if(newtooltip){
hotspot = hotspot.replace(arg_i_var, newtooltip);
krpano.set("hotspot["+caller.name+"].title", newtooltip);
scene = scene.replace(hotspot2, hotspot);
krpano.set("scene[get(xml.scene)].content",scene);
}
]]></action>

<action name="edit_set_initial_view" type="Javascript" protect="true"><![CDATA[
let scenecontent = krpano.get("scene[get(xml.scene)].content");
let namelet = scenecontent.indexOf('view');
let debut = scenecontent.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scenecontent.indexOf("/"+String.fromCharCode(62), namelet);
let view = scenecontent.substring(debut, fin+2);
let view2 = view;

let hlookat = view.indexOf("hlookat");
let hlookat1 = view.indexOf('"', hlookat);
let hlookat2 = view.indexOf('"', hlookat1+1);
hlookat_val = krpano.get("view.hlookat");
view = view.replace(view.substring(hlookat, hlookat2+1), 'hlookat="'+parseFloat(hlookat_val).toFixed(2)+'"');

let vlookat = view.indexOf("vlookat");
let vlookat1 = view.indexOf('"', vlookat);
let vlookat2 = view.indexOf('"', vlookat1+1);
vlookat_val = krpano.get("view.vlookat");
view = view.replace(view.substring(vlookat, vlookat2+1), 'vlookat="'+parseFloat(vlookat_val).toFixed(2)+'"');

let fov = view.indexOf("fov=");
let fov1 = view.indexOf('"', fov);
let fov2 = view.indexOf('"', fov1+1);
fov_val = krpano.get("view.fov");
view = view.replace(view.substring(fov, fov2+1), 'fov="'+parseFloat(fov_val).toFixed(2)+'"');

scenecontent = scenecontent.replace(view2, view);
krpano.set("scene[get(xml.scene)].content",scenecontent);
krpano.call("show_view_set");
]]></action>

<action name="edit_set_start_view" type="Javascript" protect="true"><![CDATA[
let scenecontent = krpano.get("scene[get(xml.scene)].content");
let namelet = scenecontent.indexOf('view');
let debut = scenecontent.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scenecontent.indexOf("/"+String.fromCharCode(62), namelet);
let view = scenecontent.substring(debut, fin+2);
let view2 = view;

let hlookat = view.indexOf("hlookat");
let hlookat1 = view.indexOf('"', hlookat);
let hlookat2 = view.indexOf('"', hlookat1+1);
hlookat_val = krpano.get("view.hlookat");
view = view.replace(view.substring(hlookat, hlookat2+1), 'hlookat="'+parseFloat(hlookat_val).toFixed(2)+'"');

let vlookat = view.indexOf("vlookat");
let vlookat1 = view.indexOf('"', vlookat);
let vlookat2 = view.indexOf('"', vlookat1+1);
vlookat_val = krpano.get("view.vlookat");
view = view.replace(view.substring(vlookat, vlookat2+1), 'vlookat="'+parseFloat(vlookat_val).toFixed(2)+'"');

let fov = view.indexOf("fov=");
let fov1 = view.indexOf('"', fov);
let fov2 = view.indexOf('"', fov1+1);
fov_val = krpano.get("view.fov");
view = view.replace(view.substring(fov, fov2+1), 'fov="'+parseFloat(fov_val).toFixed(2)+'"');

scenecontent = scenecontent.replace(view2, view);
krpano.set("scene[get(xml.scene)].content",scenecontent);

var xml_file = krpano.get("xml_file");
let startscene = xml_file.indexOf("startscene");
let startscene1 = xml_file.indexOf('"', startscene);
let startscene2 = xml_file.indexOf('"', startscene1+1);
let newstartscene = krpano.get("scene[get(xml.scene)].name");
console.log(startscene);
xml_file = xml_file.replace(xml_file.substring(startscene, startscene2+1), 'startscene="'+newstartscene+'"');

krpano.set("xml_file",xml_file);
krpano.call("show_startview_set");
]]></action>

<action name="edit_update_prealign" type="Javascript" protect="true"><![CDATA[
let scenecontent = krpano.get("scene[get(xml.scene)].content");
let valy = krpano.get("iy");
let valx = krpano.get("ix");
let valz = krpano.get("iz");
let val = valx+"|"+valy+"|"+valz;
let debut = scenecontent.lastIndexOf("prealign");
if(debut == -1){
	val = '<image prealign="'+val+'">';
	scenecontent = scenecontent.replace("<image>", val);
}else{
	debut +=10;
	let fin = scenecontent.indexOf('"', debut);
	let oldval = scenecontent.substring(debut,fin);
	scenecontent = scenecontent.replace(oldval, val);
}
krpano.set("scene[get(xml.scene)].content",scenecontent);
]]></action>

<action name="edit_addHStoscene" type="Javascript" protect="true"><![CDATA[
	const lat1 = krpano.get('scene[get(xml.scene)].lat');
	const lon1 = krpano.get('scene[get(xml.scene)].lng');
	const lat2 = krpano.get('scene['+caller.linkedscene+'].lat');
	const lon2 = krpano.get('scene['+caller.linkedscene+'].lng');
	const R = 6371e3;

	const φ1 = lat1 * Math.PI/180;
	const φ2 = lat2 * Math.PI/180;
	const λ1 = lon1 * Math.PI/180;
	const λ2 = lon2 * Math.PI/180;
	const Δφ = (lat2-lat1) * Math.PI/180;
	const Δλ = (lon2-lon1) * Math.PI/180;

	const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
			  Math.cos(φ1) * Math.cos(φ2) *
			  Math.sin(Δλ/2) * Math.sin(Δλ/2);
	const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

	const d = R * c;

let scene = krpano.get("scene[get(xml.scene)].content");
var hotspot = '<hotspot name="'+caller.name+'" style="'+caller.style+'" ath="'+caller.ath+'" atv="'+caller.atv+'"  distance="'+d.toFixed(2)+'" linkedscene="'+caller.linkedscene+'"/>';
scene = scene + hotspot + "\n";
krpano.set("scene[get(xml.scene)].content",scene);
]]></action>

<action name="edit_addFloorHStoscene" type="Javascript" protect="true"><![CDATA[
let scene = krpano.get("scene[get(xml.scene)].content");
var hotspot = '<hotspot name="'+caller.name+'" style="'+caller.style+'" ath="'+caller.ath+'" atv="'+caller.atv+'" linkedscene="'+caller.linkedscene+'" down="'+caller.down+'"/>';
scene = scene + hotspot + "\n";
krpano.set("scene[get(xml.scene)].content",scene);
]]></action>

<action name="edit_addShopHStoscene" type="Javascript" protect="true"><![CDATA[
let scene = krpano.get("scene[get(xml.scene)].content");
var hotspot = '<hotspot name="'+caller.name+'" style="'+caller.style+'" ath="'+caller.ath+'" atv="'+caller.atv+'" unit_key="'+caller.unit_key+'" />';
scene = scene + hotspot + "\n";
krpano.set("scene[get(xml.scene)].content",scene);
krpano.call("set_hotspot_data");
]]></action>

<action name="edit_addCalloutHStoscene" type="Javascript" protect="true"><![CDATA[
let scene = krpano.get("scene[get(xml.scene)].content");
var hotspot = '<hotspot name="'+caller.name+'" style="'+caller.style+'" ath="'+caller.ath+'" atv="'+caller.atv+'" title="'+caller.title+'"/>';
scene = scene + hotspot + "\n";
krpano.set("scene[get(xml.scene)].content",scene);
]]></action>

<action name="edit_removeHS" type="Javascript" protect="true"><![CDATA[
actions.removehotspot(caller.name);
let scene = krpano.get("scene[get(xml.scene)].content");
let namelet = scene.indexOf('name="'+caller.name+'"');
let debut = scene.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scene.indexOf(String.fromCharCode(62), namelet);
let hotspot = scene.substring(debut, fin+1);
scene = scene.replace(hotspot+"\n", "");
scene = scene.replace("\n"+hotspot, "");
krpano.set("scene[get(xml.scene)].content",scene);
]]></action>

<action name="edit_remove_shop_HS" type="Javascript" protect="true"><![CDATA[
actions.removehotspot(caller.name);
actions.removehotspot(caller.myicon);
actions.removehotspot(caller.mytext);
actions.removehotspot(caller.mypulse);
let scene = krpano.get("scene[get(xml.scene)].content");
let namelet = scene.indexOf('name="'+caller.name+'"');
let debut = scene.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scene.indexOf(String.fromCharCode(62), namelet);
let hotspot = scene.substring(debut, fin+1);
scene = scene.replace(hotspot+"\n", "");
scene = scene.replace("\n"+hotspot, "");
krpano.set("scene[get(xml.scene)].content",scene);
]]></action>

<action name="edit_unit_key" type="Javascript" protect="true"><![CDATA[
let scene = krpano.get("scene[get(xml.scene)].content");
let newunitkey = prompt('Shop unit key', caller.unit_key);

let namelet = scene.indexOf('name="'+caller.name+'"');
let debut = scene.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scene.indexOf("/"+String.fromCharCode(62), namelet);

let hotspot = scene.substring(debut, fin+2);
let hotspot2 = hotspot;
let arg_i = hotspot.indexOf("unit_key");
let gui1 = hotspot.indexOf('"', arg_i);
let gui2 = hotspot.indexOf('"', gui1+1);
let arg_i_var = hotspot.substring(gui1+1, gui2);

if(newunitkey){
hotspot = hotspot.replace(arg_i_var, newunitkey);
krpano.set("hotspot["+caller.name+"].unit_key", newunitkey);
scene = scene.replace(hotspot2, hotspot);
krpano.set("scene[get(xml.scene)].content",scene);
krpano.call("set_hotspot_data");
}
]]></action>

<action name="edit_updateHSpos" type="Javascript" protect="true"><![CDATA[
let scenecontent = krpano.get("scene[get(xml.scene)].content");
let namelet = scenecontent.indexOf('name="'+caller.name+'"');
let debut = scenecontent.lastIndexOf(String.fromCharCode(60), namelet);
let fin = scenecontent.indexOf("/"+String.fromCharCode(62), namelet);
let hotspot = scenecontent.substring(debut, fin+2);
let hotspot2 = hotspot;
let ath_i = hotspot.indexOf("ath");
let gui1 = hotspot.indexOf('"', ath_i);
let gui2 = hotspot.indexOf('"', gui1+1);
hotspot = hotspot.replace(hotspot.substring(ath_i, gui2+1), 'ath="'+parseFloat(caller.ath).toFixed(2)+'"');
let atv_i = hotspot.indexOf("atv");
gui1 = hotspot.indexOf('"', atv_i);
gui2 = hotspot.indexOf('"', gui1+1);
hotspot = hotspot.replace(hotspot.substring(atv_i, gui2+1), 'atv="'+parseFloat(caller.atv).toFixed(2)+'"');
scenecontent = scenecontent.replace(hotspot2, hotspot);
krpano.set("scene[get(xml.scene)].content",scenecontent);
]]></action>

<action name="edit_updateSCENEcontent" type="Javascript" protect="true"><![CDATA[
let scene = krpano.get("scene["+args[1]+"].content");
var xml_file = krpano.get("xml_file");
let namelet = xml_file.indexOf('name="'+args[1]+'"');
let debut = xml_file.indexOf(String.fromCharCode(62), namelet);
let fin = xml_file.indexOf(String.fromCharCode(60)+"/scene"+String.fromCharCode(62), namelet);
let str = xml_file.substring(debut+1, fin);
xml_file = xml_file.replace(str, scene);
krpano.set("xml_file",xml_file);
]]></action>



<action name="edit_loop_one_scenes_north" type="Javascript" protect="true"><![CDATA[
let scenecontent = krpano.get("scene["+args[1]+"].content");
let valy = krpano.get("iy");
let valx = krpano.get("ix");
let valz = krpano.get("iz");
let val = valx+"|"+valy+"|"+valz;
let debut = scenecontent.lastIndexOf("prealign");
if(debut == -1){
	val = '<image prealign="'+val+'">';
	scenecontent = scenecontent.replace("<image>", val);
}else{
	debut +=10;
	let fin = scenecontent.indexOf('"', debut);
	let oldval = scenecontent.substring(debut,fin);
	scenecontent = scenecontent.replace(oldval, val);
}
krpano.set("scene["+args[1]+"].content",scenecontent);
krpano.call("show_north_set()");
]]></action>

<action name="edit_loop_all_scenes_north" protect="true">
for(set(i,0),i LT scene.count, inc(i),
	edit_loop_one_scenes_north(get(scene[get(i)].name));
	);
</action>
//BOUTON SAUVEGARDER

<action name="edit_save_my_xml" protect="true">
for(set(i,0),i LT scene.count, inc(i),
	edit_updateSCENEcontent(get(scene[get(i)].name));
	if(i == scene.count -1,edit_send_to_local(get(xml_url),get(xml_file)))
	);
</action>


<action name="edit_send_to_console" type="Javascript" protect="true"><![CDATA[
console.log(args[2]);
]]></action>

<action name="edit_send_to_local" type="Javascript"><![CDATA[
var fileNameToSaveAs = args[1];
var xmlcontent = args[2];
var textFileAsBlob = new Blob([xmlcontent], {type:'text/xml;charset=utf-8;'});

var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Hidden link";

if (window.webkitURL != null)
	{
	downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
	}
	else
	{
	downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
	downloadLink.onclick = destroyClickedElement;
	downloadLink.style.display = "none";
	document.body.appendChild(downloadLink);
	}

downloadLink.click();

function destroyClickedElement(event)
	{
	document.body.removeChild(event.target);
	}
	]]>
</action>

<action name="edit_send_to_server" type="Javascript" protect="true"><![CDATA[
var data = "url=" + args[1] + "&xml=" + args[2];

var request = false;
if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    request = new ActiveXObject('Microsoft.XMLHTTP');
}

function send_ajax() {
    if (request) {
        request.open('POST', 'save_xml.php', true);
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                alert('Tour saved');
            }
        }
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(data);
    }
}

send_ajax();
]]></action>

//GENERATE THUMBNAIL


	<action name="makescreenshot_init" type="Javascript" autorun="onstart"><![CDATA[

		// Load the FileSaver.js script for saving files locally cross browser.
		// Source: https://github.com/eligrey/FileSaver.js/
		krpano.loadFile("%VIEWER%/note_tool_assets/FileSaver.js", function(file)
		{
			// remove the 'export ' part from the code to be able to 'eval' it
			eval(file.data.replace("export ",""));

			// store the 'saveAs' function from the 'FileSaver.js' in krpano
			krpano.screenshotSaveAs = saveAs;
		});

		// create an empty Object as makeScreenshot cache
		var makeScreenshotCache = {};


		// add a 'makescreenshot' action to krpano
		krpano.edit_make_thumbnail = function(screenshotwidth,screenshotheight)
		{
			// if there is already a screenshot layer, remove it now
			krpano.call("removelayer(screenshot,true)");

			// make a screenshot as canvas
			var sizeinfo = {w:0, h:0};
			var screenshotcanvas = krpano.webGL.makeScreenshot(240, 120, false, "canvas", 0, null, sizeinfo, makeScreenshotCache);
			let thumburl = krpano.get('scene[get(xml.scene)].name');
			if (screenshotcanvas)
			{
				screenshotcanvas.toBlob(function(blob){ krpano.screenshotSaveAs(blob, thumburl); }, "image/jpeg", 0.95);
			}
		}

	]]></action>

</krpano>
