function getTours(a,b){var c=localStorage,d=function(a){var b=a.rows[0].key[0],d=a.rows[0].value.name,e=[];a.rows.forEach(function(f){var g=f.key[1],h="",i="";g&&e.indexOf(f.key[1])==-1&&(e.push(f.key[1]),a.rows.forEach(function(a){a.key[1]==g&&a.value.broader&&(i=a.value.broader.id)}),a.rows.forEach(function(a){a.key[1]==g&&a.value.name&&(h=a.value.name)}),$("#courses-num").text(parseInt($("#courses-num").text())+1),courseList.add({id:g,"course-title":'Parcours "'+h+'"',"course-vp":"— "+d,broader_id:i,distance:c.getItem(g+"_distance")?c.getItem(g+"_distance")+" km":"NC",locations:0,locations_name:[],vitraux:[],duration:c.getItem(g+"_duration")?c.getItem(g+"_duration"):"NC","items-nb":0,"topic-link":"map.html?viewpoint="+b+"&topic="+g}))})},e=function(a){a.rows.forEach(function(a){if(a.value.topic)for(var b=courseList.get("id",a.value.topic.id)[0];b;)b.values().vitraux.indexOf(a.id)==-1&&(b.values({"items-nb":b.values()["items-nb"]+1}),b.values().vitraux.push(a.id)),b=courseList.get("id",b.values().broader_id)[0]}),a.rows.forEach(function(b){if(b.value.spatial){var c=b.id,d=b.value.spatial;a.rows.forEach(function(a){if(a.value.topic&&a.id==c)for(var b=courseList.get("id",a.value.topic.id)[0];b;)b.values().locations_name.indexOf(d)==-1&&(b.values().locations_name.push(d),b.values({locations:b.values().locations+1})),b=courseList.get("id",b.values().broader_id)[0]})}}),courseList.sort("locations",{order:"desc"})},f=function(){var a={valueNames:["course-title","course-vp","distance","locations","duration","items-nb",{data:["id"]},{attr:"href",name:"topic-link"}],item:'<li class="table-view-cell course" data-id=""><a class="navigate-right topic-link" href="#" data-transition="slide-in" data-ignore="push"><div class="media-body"><div class="course-header"><h2 class="course-title"></h2><p class="course-vp"></p></div><div class="course-abstract clearfix"><div><p><span class="fa fa-flag-o"></span> <span class="distance"></span></p><p><span class="fa fa-map-marker"></span> <span class="locations"></span> Lieu(x)</p></div><div><p><span class="fa fa-clock-o"></span> <span class="duration"></span></p><p><span class="items-nb"></span><span class="item-text"><span> Vitraux</p></div></div></div></a></li>'};courseList=new List("courses",a)};f(),Promise.all(a.map(function(a){return requestFactory("http://argos2.hypertopic.org/viewpoint/"+a,d)}).concat(b.map(function(a){return requestFactory(a)}))).then(function(a){a.map(function(a){a&&e(a)})})}function getCourseMap(a){function b(a){function b(c,d){c.forEach(function(c){c.key[1]==d&&c.value.narrower?(l.push(c.value.narrower.id),b(a.rows,c.value.narrower.id)):c.key[1]==l[0]&&c.value.name&&$(".title").text('Parcours "'+c.value.name+'"')})}b(a.rows,l[0])}function c(a){a.map(function(a){var b=[];a.rows.forEach(function(a){a.value.topic&&l.indexOf(a.value.topic.id)!=-1&&b.push(a.id)}),a.rows.forEach(function(a){a.value.spatial&&b.indexOf(a.id)!=-1&&m.indexOf(a.value.spatial)==-1&&m.push(a.value.spatial)})})}function d(a){var b={zoom:14,center:a,mapTypeId:google.maps.MapTypeId.TERRAIN,maxZoom:20};i=new google.maps.Map(document.getElementById("map"),b)}function e(a,b,c){var d={location:a,radius:500,query:b},e=new google.maps.places.PlacesService(c);return constanteSpatial[b]&&(d.query=constanteSpatial[b]),new Promise(function(a,c){e.textSearch(d,function(c,d){a(d===google.maps.places.PlacesServiceStatus.OK?{waypoint:{location:c[0].formatted_address,stopover:!0},place:b,coordinate:{latitude:c[0].geometry.location.lat(),longitude:c[0].geometry.location.lng()}}:null)})})}function f(a,b){function c(a){return a.map(function(a){return a.coordinate})}function d(a,b){var c=6371;return gammaX=e(a.latitude),gammaY=e(b.latitude),deltaX=e(b.latitude-a.latitude),deltaY=e(b.longitude-a.longitude),calcul=Math.sin(deltaX/2)*Math.sin(deltaX/2)+Math.cos(gammaX)*Math.cos(gammaY)*Math.sin(deltaY/2)*Math.sin(deltaY/2),secondCalcul=2*Math.atan2(Math.sqrt(calcul),Math.sqrt(1-calcul)),(c*secondCalcul).toFixed(3)/1}function e(a){return a*Math.PI/180}var f=navigator.geolocation;"geolocation"in navigator&&f.getCurrentPosition(function(e){var f=c(a).map(function(a){return d(a,e.coords)}),h=f.reduce(function(a,b,c,d){return b<d[a]?c:a},0);return g(a,b,h)})}function g(a,b,c){function d(a,b,c){var d=[],e=0,f=0,g=a.waypoint_order;a.legs.map(function(a,h){if(d.indexOf(a.end_location.toString())==-1){var i=new google.maps.Marker({position:a.end_location,map:b,label:(h+1).toString()}),k=c[g[h]].place,m=new google.maps.InfoWindow({content:'<a href="explore.html?topic='+l[0]+"&viewpoint="+j+"&spatial="+k+'"data-transition="slide-in" data-ignore="push">'+k+"</a>"});e+=a.duration.value,f+=a.distance.value,d.push(a.end_location.toString()),i.addListener("click",function(){m.open(b,i)})}}),h(getCourseDuration(e),Math.ceil(f/1e3))}var e=new google.maps.DirectionsService,f=new google.maps.DirectionsRenderer({map:b,suppressMarkers:!0}),g=a.map(function(a){return a.waypoint}),i={origin:g[0].location,destination:g[0].location,waypoints:g,travelMode:google.maps.DirectionsTravelMode.WALKING,optimizeWaypoints:!0};return new Promise(function(c,g){e.route(i,function(c,e){e==google.maps.DirectionsStatus.OK&&(f.setDirections(c),d(c.routes[0],b,a))})})}function h(a,b){var c=localStorage;$("#duration").text(a),$("#distance").text(b+" km"),c.setItem(l[0]+"_duration",a),c.setItem(l[0]+"_distance",b)}var i,j=getUrlParameter("viewpoint"),k=new google.maps.LatLng(48.2973725,4.0721523),l=[getUrlParameter("topic")],m=[];Promise.all([requestFactory("http://argos2.hypertopic.org/viewpoint/"+j,b)].concat(a.map(function(a){return requestFactory(a)}))).then(function(a){return a=cleanData(a),c(a),d(k),Promise.all(m.map(function(a){return e(k,a,i)}))}).then(function(a){return a=cleanData(a),Promise.all([f(a,i),g(a,i,0)])})}function getLocationVitraux(a,b){function c(a){function b(c,d){c.forEach(function(c){c.key[1]==d&&c.value.narrower?(h.push(c.value.narrower.id),b(a.rows,c.value.narrower.id)):c.key[1]==h[0]&&c.value.name&&$(".title").text('Parcours "'+c.value.name+'"')})}b(a.rows,h[0])}function d(a){a.map(function(a){var b=[];a.rows.forEach(function(a){a.value.topic&&h.indexOf(a.value.topic.id)!=-1&&b.push(a.id)}),a.rows.forEach(function(a){if(b.indexOf(a.id)!=-1&&a.value.spatial&&a.value.spatial==g){var c=a.id,d="description.html?id="+c+"&topic="+h[0]+"&viewpoint="+f+"&spatial="+g,e='<li id="'+c+'" class="table-view-cell"><a class="navigate-right" href="'+d+'" data-transition="slide-in" data-ignore="push"><div class="stained-glass-desc"><h3 class="stained-glass-name"></h3></div><div class="stained-glass-img"><img class="table-img" src="" ></div></a></li>';$("#stainted-glass-windows").append(e),i.push(a.id)}})})}function e(a){a.rows.forEach(function(a){i.indexOf(a.key[1])!=-1&&(a.value.name?$("#"+a.key[1]).find(".stained-glass-name").text(a.value.name):a.value.thumbnail&&$("#"+a.key[1]).find("img").attr("src",a.value.thumbnail))})}var f=getUrlParameter("viewpoint"),g=decodeURIComponent(getUrlParameter("spatial")),h=[getUrlParameter("topic")],i=[];$("#back").attr("href","map.html?+&viewpoint="+f+"&topic="+h[0]),$(".place-name").text(g),Promise.all([requestFactory("http://argos2.hypertopic.org/viewpoint/"+f,c)].concat(a.map(function(a){return requestFactory(a)}))).then(function(a){return a=cleanData(a),d(a),Promise.all(b.map(function(a){return requestFactory(a,e)}))})}function getDescription(a,b){function c(a){var b,c,d;$.each(a.rows,function(a,f){f.key[1]===e&&(f.value.name?b=f.value.name:f.value.thumbnail?c=f.value.thumbnail:f.value.resource&&(d=f.value.resource))}),$(".title").text(b),$("#vitrailModal").find(".title").text(b+" [Zoom]"),$("#vitrailModal").find("a.btn").attr("href",d),$("#vitrail-thumbnail-new").find("img").attr("src",c),$("#vitrail-thumbnail-new").find("img").attr("alt",b),$("#vitrail-resource").attr("src",d),$("#vitrail-resource").attr("alt",b)}function d(a){var b=[],c=[],d=[];$("#description-complete").append('<div id="'+a.rows[0].key[0]+'"><h3>'+a.rows[0].value.name+"</h3></div>"),a.rows.forEach(function(a){a.value.item&&a.value.item.id==e&&c.push(a.key[1])});var f=function(a,b){var c,e="";a.rows.forEach(function(a){a.key[1]==b&&(a.value.name?e=a.value.name:a.value.broader&&(c=a.value.broader))}),d.push(e),c&&f(a,c.id)};c.map(function(c){f(a,c),b.push(d),d=[]}),b.map(function(b){b=b.reverse().join(" > "),$("#"+a.rows[0].key[0]).append('<p class="content-padded description">'+b+"</p>")})}var e=getUrlParameter("id"),f=getUrlParameter("spatial"),g=getUrlParameter("viewpoint"),h=getUrlParameter("topic");$("#back").attr("href","explore.html?topic="+h+"&viewpoint="+g+"&spatial="+f),Promise.all(a.map(function(a){return requestFactory("http://argos2.hypertopic.org/viewpoint/"+a,d)}).concat(b.map(function(a){return requestFactory(a,c)})))}var viewpoints=["56e092d8a6179a788c74b618b29801c0","a76306e4f17ed4f79e7e481eb9a1bd06"],corpusArgos=["http://argos2.hypertopic.org/corpus/Vitraux - Bénel","http://argos2.hypertopic.org/corpus/Vitraux%20-%20Dr.%20Krieger","http://argos2.hypertopic.org/corpus/Vitraux%20-%20Recensement"],corpusSt=["http://steatite.hypertopic.org/corpus/Vitraux - Bénel","http://steatite.hypertopic.org/corpus/Vitraux%20-%20Dr.%20Krieger","http://steatite.hypertopic.org/corpus/Vitraux%20-%20Recensement"],constanteSpatial={Aulnay:"10240,Aulnay",Braux:"10500,Braux","Église Notre-Dame, Villeneuve-l'Archevêque":"89190, Villeneuve-l'Archevêque","Église Saint-Pierre-ès-Liens, Ervy-le-Châtel":"10130, Ervy-le-Châtel",Chatres:"10510,Chatres","Église Saint-Remi, Troyes":"Place Saint-Rémy, 10000, Troyes","Église Saint-Pierre, Trouans":"10700, Trouans",Moussey:"10800, Moussey",Ormes:"10700, Ormes"},loading=function(){var a=$("div.content").attr("id");switch(a){case"courses":getTours(viewpoints,corpusArgos);break;case"map-page":getCourseMap(corpusArgos);break;case"explore-page":getLocationVitraux(corpusArgos,corpusSt);break;case"preview-page":getVitrailId();break;case"description-page":getDescription(viewpoints,corpusSt)}},getCourseDuration=function(a){var b=Math.floor(a/86400);a-=24*b*3600;var c=Math.floor(a/3600);a-=3600*c;var d=Math.floor(a/60);return c+=24*b,c+" heures et "+d+" minutes"},getUrlParameter=function(a){var b,c,d=decodeURIComponent(window.location.search.substring(1)),e=d.split("&");for(c=0;c<e.length;c++)if(b=e[c].split("="),b[0]===a)return void 0===b[1]||b[1]},uniq=function(a){var b={};return a.filter(function(a){return!b.hasOwnProperty(a)&&(b[a]=!0)})},getItemText=function(a,b,c){return 1===a?b:c},cleanData=function(a){return a.filter(function(a){return a})},requestFactory=function(a,b){var c=new Promise(function(c,d){$.ajax({type:"GET",url:a,dataType:"json",success:function(a){c("function"==typeof b?b(a):a)},erreur:function(){d("La requête a échoué")}})});return c};$(document).ready(loading);