﻿var sl=sl?sl:{};sl.maps=sl.maps?sl.maps:{};sl.maps.earthInstance=null; sl.maps.tour={detectKmlTourEnd:function(g,j){if(j!=null&&g!=null&&g.getTourPlayer()!=null&&g.getTourPlayer().getCurrentTime()!=null&&g.getTourPlayer().getDuration()!=null)g.getTourPlayer().getCurrentTime()<g.getTourPlayer().getDuration()?setTimeout(function(){sl.maps.tour.detectKmlTourEnd(g,j)},1E3):j.playListItemStatusUpdate()},Tour:function(g,j){this.gex=null;this.playList=g;this.playListCounter=0;this.repeat=true;this.currentItem=null;this.running=false;this.i18n={lengthText:j};this.getEarthInstance= function(){sl.maps.earthInstance==null&&g_map.getEarthInstance(this.onEarthInstanceCallback);return sl.maps.earthInstance};this.onEarthInstanceCallback=function(a){sl.maps.earthInstance=a;setTimeout(function(){},1500);sl.maps.earthInstance.getOptions().setAtmosphereVisibility(true);sl.maps.earthInstance.getNavigationControl().setVisibility(sl.maps.earthInstance.VISIBILITY_HIDE);g_Tour.createNativeHTMLButton("helpButtonShim",11,11,27,18,G_ANCHOR_TOP_RIGHT,"/facelets/rallymap/tour/HelpButtonFacelet.faces"); g_Tour.playTour()};this.addTourItemData=function(a){if(a!=null&&this.playList!=null&&this.playList.length>0)for(var b=0;b<this.playList.length;b+=1)if(this.playList[b].stageid==a.stageid){this.playList[b].kmlData=a.kmlData;return}};this.playIncrement=function(a){a=a===true;if(this.playList!=null&&this.playList.length>0){if(this.currentItem!=null)this.currentItem.currentBoundsIndex=0;if(a===false){this.playListCounter+=1;if(this.playListCounter>=this.playList.length)this.playListCounter=this.playList.length- 1}else{this.playListCounter-=1;if(this.playListCounter<0)this.playListCounter=0}a=this.playList[this.playListCounter];a.currentBoundsIndex=0;this.playTourItem(a);this.requestTourData()}};this.playNextStage=function(){this.playIncrement(false)};this.playPrevStage=function(){this.playIncrement(true)};this.enbableRepeat=function(){this.repeat=true;this.running==false&&this.playTour()};this.disableRepeat=function(){this.repeat=false};this.playTour=function(){g_map.setMapType(G_SATELLITE_3D_MAP);if(this.getEarthInstance()!= null&&this.playList!=null&&this.playList.length>0){this.playList.length>1&&this.createNativeHTMLButton("tourControlShim",11,70,122,29,G_ANCHOR_BOTTOM_LEFT,"/facelets/rallymap/tour/controls.faces");this.playListCounter=0;this.removeAllFeatures();this.playTourItem(this.playList[0])}};this.playTourItem=function(a){if(a!=null){this.running=true;this.currentItem=a;if(a.currentBoundsIndex==undefined||a.currentBoundsIndex>=a.showBounds.length||a.currentBoundsIndex==0){a.currentBoundsIndex=0;var b={"div.rallyName":"rallyName", "div.stageName":"stageName","div.stageLength":this.i18n.lengthText},d={rallyName:a.rallyName,stageName:a.stageName,stageLength:a.stageLength.toFixed(2)};this.createNativeHTMLButton("stageInfoShim",parseInt(jQuery("#map_canvar").width()/2-126),11,275,66,G_ANCHOR_TOP_LEFT,"/facelets/rallymap/tour/touriframe.html",".message",b,d)}g_Tour.removeAllFeatures();if(a.kmlData!=null&&a.parsedTour===undefined){a.parsedTour=this.parseKml(a.kmlData);g_Tour.playList[g_Tour.playListCounter].parsedTour=a.parsedTour}a.parsedTour!= null&&this.getEarthInstance().getFeatures().appendChild(a.parsedTour.kmlObject);if(a.showBounds!=null&&a.showBounds.length>0&&a.showBounds.length>a.currentBoundsIndex&&a.showBounds[a.currentBoundsIndex]!=null)this.flyToBounds(a.showBounds[a.currentBoundsIndex]);else a.parsedTour!=null&&this.playKmlTour(a);a.currentBoundsIndex+=1}};this.requestTourData=function(){if(this.playListCounter+1<this.playList.length){var a=this.playList[this.playListCounter+1];a!=null&&a.kmlData==null&&g_Comm.RequestTourData(a.stageid)}}; this.playListItemDone=function(){this.playListCounter+=1;if(this.playListCounter<this.playList.length){this.playTourItem(this.playList[this.playListCounter]);this.requestTourData()}else this.repeat==true&&this.playTour();this.running=false};this.flyToBounds=function(a){if(this.getEarthInstance()!=null&&a!=null){this.gex=new GEarthExtensions(this.getEarthInstance());google.earth.addEventListener(this.getEarthInstance().getView(),"viewchangeend",g_Tour.onFlyToBoundsDone);a=new geo.Bounds(a.getSouthWest(), a.getNorthEast());a=this.gex.view.createBoundsView(a,{aspectRatio:1});this.getEarthInstance().getView().setAbstractView(a)}};this.parseKml=function(a,b){if(a!=null&&this.getEarthInstance()!=null){var d=this.getEarthInstance().parseKml(a),c=null;walkKmlDom(d,function(){if(this.getType()=="KmlTour")if(b!=null&&this.getName()==b){c=this;return false}else if(b==null||b==""){c=this;return false}});return{kmlObject:d,tourObject:c,rawKml:a}}return null};this.playListItemStatusUpdate=function(){var a=this.currentItem; a!=null&&a.showBounds!=null&&a.showBounds.length>0&&a.showBounds.length>a.currentBoundsIndex?this.playTourItem(a):this.playListItemDone()};this.playKmlTour=function(a){if(a!=null&&this.getEarthInstance()!=null)if(a.parsedTour!=undefined&&a.parsedTour!=null){this.getEarthInstance().getTourPlayer().setTour(null);this.getEarthInstance().getTourPlayer().setTour(a.parsedTour.tourObject);this.getEarthInstance().getTourPlayer().play();sl.maps.tour.detectKmlTourEnd(this.getEarthInstance(),this)}else g_Tour.playListItemStatusUpdate()}; this.onFlyToBoundsDone=function(){if(g_Tour!=null){google.earth.removeEventListener(g_Tour.getEarthInstance().getView(),"viewchangeend",g_Tour.onFlyToBoundsDone);var a=g_Tour.playList[g_Tour.playListCounter];a!=null&&a.rawKml!=null?g_Tour.playKmlTour(a):g_Tour.playListItemStatusUpdate()}};this.removeAllFeatures=function(){if(this.getEarthInstance()!=null)for(var a=this.getEarthInstance().getFeatures();a.getFirstChild();)a.removeChild(a.getFirstChild())};this.getElementRect=function(a){for(var b=a.offsetLeft, d=a.offsetTop,c=a.offsetParent;c&&c!=document.body.parentNode;){if(isFinite(c.offsetLeft)&&isFinite(c.offsetTop)){b+=c.offsetLeft;d+=c.offsetTop}c=c.offsetParent}return{left:b,top:d,width:a.offsetWidth,height:a.offsetHeight}};this.addDomListener=function(a,b,d){if(a.addEventListener)a.addEventListener(b,d,false);else a.attachEvent&&a.attachEvent("on"+b,d)};this.createNativeHTMLButton=function(a,b,d,c,i,h,f,k,l,m){if(a!=undefined&&a!=null&&f!=undefined&&f!=null){b=b==undefined||b==null?0:b;d=d==undefined|| d==null?0:d;c=c==undefined||c==null?75:c;i=i==undefined||i==null?20:i;h=h==undefined||h==null?G_ANCHOR_TOP_LEFT:h;var o=k!=undefined&&l!=undefined&&m!=undefined&&k!=null&&l!=null&&m!=null,e=jQuery("#"+a),n=!o?null:function(){var p=jQuery("#"+a),q=p.contents().find(k);q.length>0&&q.render(m,l);p.css("display","block")};if(e.length==0){e=document.createElement("iframe");e.setAttribute("class","iframeShim");e.setAttribute("id",a);e.frameBorder=0;e.scrolling="no";e.src=f;e.allowtransparency="true";f= this.getElementRect(document.getElementById("map_canvar"));e.style.position="absolute";e.style.top=h==G_ANCHOR_BOTTOM_RIGHT||h==G_ANCHOR_BOTTOM_LEFT?f.top+f.height-i-d+"px":f.top+d+"px";e.style.left=h==G_ANCHOR_BOTTOM_RIGHT||h==G_ANCHOR_TOP_RIGHT?f.left+f.width-c-b+"px":f.left+b+"px";e.style.width=c+"px";e.style.height=i+"px";e.style.zIndex=10;if(o){jQuery(e).load(n);e.style.display="none"}document.body.appendChild(e)}else n!=null&&n()}}}};
