
App.ready(function()
{App.getGlobalED().on("open-directions-dialog",directionsPanel,this);App.getGlobalED().on("dir-print-link",composeDirPrintLink,this);App.getGlobalED().on("dir-email-link",composeDirEmailLink,this);openDirectionsDialogIfInUrl();var inputFieldListener=new YAHOO.util.KeyListener('inputAddress',{keys:13},getDirections(),'keydown');inputFieldListener.enable();});var map=null;var map_print=null;var Dom=YAHOO.util.Dom;var centered=false;function directionsPanel(e,el)
{YAHOO.util.Event.stopEvent(e);this.directionsPanel=new App.Ui.AjaxDialog(null,{srcUrl:el.href,invisMask:false,close:true,zIndex:0,events:{onDialogInit:function()
{if(document.forms["directionsForm"]!=undefined){App.log("DIRECTIONS PANEL LOADED");document.forms["directionsForm"]["inputAddress"].focus();var key_collection=[];key_collection.push(13);var inputAddressListener=new YAHOO.util.KeyListener('inputAddress',{keys:key_collection},getDirections);inputAddressListener.enable();App.log('inputAddressListener %o',inputAddressListener);App.getGlobalED().on("dialog-close",markCentered(false));App.getGlobalED().on("container-close",markCentered(false));}}}});this.directionsPanel.show();}
function getDirections()
{centerFrame();hideChangeListingIfShown();var reverse=getReverse();var roundtrip=getRoundtrip();var walking=getWalking();var listingXCoord=getListingXCoord();var listingYCoord=getListingYCoord();var inputAddress=getInputAddress(reverse);if(inputAddress===""){showError();return;}
else{YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('changeListing'),'hide','show');YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('dir_table'),'center','search_left');YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('show_listingAddress_normal'),'show_listingAddress_normal_center','show_listingAddress_normal_left');YAHOO.util.Dom.removeClass('options','hide');showResults();}
map=new VEMap('dir_map');map.LoadMap(new VELatLong(listingYCoord,listingXCoord),10);map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);App.log("map : %o",map);var options=new VERouteOptions();App.log("options: %o",options);options.ShowDisambiguation=false;options.ShowErrorMessages=false;options.DistanceUnit=VERouteDistanceUnit.Kilometer;if(walking=="true"){options.RouteMode=VERouteMode.Walking;}
else{options.RouteMode=VERouteMode.Driving;}
options.RouteCallback=onGotRoute;var routeArray=getRouteArray(reverse,roundtrip,listingXCoord,listingYCoord,inputAddress);App.log("route: %o",routeArray);App.log("map : %o",map);map.GetDirections(routeArray,options);}
function onGotRoute(route)
{if(route==null){openNoRouteFoundDialog();return;}
var legs=route.RouteLegs;var time_text="";var time_hours=Math.floor(route.Time/3600);var time_min=Math.floor((route.Time%3600)/60);if(time_hours>0){time_text=time_hours+" h ";}
time_text+=time_min+" min";var reverse=getReverse();var roundtrip=getRoundtrip();var walking=getWalking();var optionSpans="<span id='routeTextImg' class='routeTextImg'></span>";if(reverse=="true"){optionSpans+="<span id='routeTextImg' class='routeTextImg routeReverse'></span>";}
if(roundtrip=="true"){optionSpans+="<span id='routeTextImg' class='routeTextImg routeTextRoundTrip'></span>";}
if(walking=="true"){optionSpans+="<span id='routeTextImg' class='routeTextImg routeTextWalk'></span>";}
if(walking=="false"){optionSpans+="<span id='routeTextImg' class='routeTextImg routeTextCar'></span>";}
var turns="<br /><span style='font-size: 18px;'><strong>"+route.Distance.toFixed(1)+" km, "+time_text+"</strong>&nbsp;&nbsp;"+optionSpans+"</span><br/>"+"<table style='font-size: 11px;'>";var numTurns=0;var leg=null;App.log("route: %o",route);App.log("legs: %o",legs);for(var i=0;i<legs.length;i++)
{leg=legs[i];var turn=null;for(var j=0;j<leg.Itinerary.Items.length;j++)
{turn=leg.Itinerary.Items[j];if(j==0||j==(leg.Itinerary.Items.length-1)){turns+="<tr><td width=26>&nbsp;</td><td width=25 valign=top color=#005ed6></td><td width=250><strong>"+turn.Text+"</strong></td><td width=45></td><td></td></tr>";}
else{turns+="<tr><td width=26>&nbsp;</td><td width=25 valign=top color=#005ed6>"+numTurns+"</td><td width=250>"+turn.Text+"</td><td width=45></td><td>"+turn.Distance.toFixed(1)+"km</td></tr>";}
numTurns++;}}
turns+="</table>";document.getElementById('dir_routeText').innerHTML=turns;setMapHeight();}
function getRouteArray(reverse,roundtrip,listingXCoord,listingYCoord,inputAddress)
{var routeArray=new Array();if(reverse=="true")
{routeArray[0]=new VELatLong(listingYCoord,listingXCoord);routeArray[1]=inputAddress;if(roundtrip=="true"){routeArray[2]=new VELatLong(listingYCoord,listingXCoord);}}
else
{routeArray[0]=inputAddress;routeArray[1]=new VELatLong(listingYCoord,listingXCoord);if(roundtrip=="true"){routeArray[2]=inputAddress;}}
return routeArray;}
function changeToWalking()
{document.forms["directionsForm"]["walking"].value=true;YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('car'),'car_active','car');YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('walk'),'walk','walk_active');getDirections();}
function changeToDriving()
{document.forms["directionsForm"]["walking"].value=false;YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('car'),'car','car_active');YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.get('walk'),'walk_active','walk');getDirections();}
function changeReverse()
{if(document.forms["directionsForm"]["reverse"].value==true){document.forms["directionsForm"]["reverse"].value=false;document.forms["directionsForm"]["inputAddress"].value=document.forms["directionsForm"]["inputAddressReverse"].value;document.getElementById("inputs_reverse").style.display='none';document.getElementById("inputs_normal").style.display='block';}
else{document.forms["directionsForm"]["reverse"].value=true;document.forms["directionsForm"]["inputAddressReverse"].value=document.forms["directionsForm"]["inputAddress"].value;document.getElementById("inputs_normal").style.display='none';document.getElementById("inputs_reverse").style.display='block';}
getDirections();}
function changeRoundtrip()
{if(document.forms["directionsForm"]["roundtrip"].value==true){document.forms["directionsForm"]["roundtrip"].value=false;YAHOO.util.Dom.addClass('one-way','hide');YAHOO.util.Dom.removeClass('roundTripCell','hide');}
else{document.forms["directionsForm"]["roundtrip"].value=true;YAHOO.util.Dom.addClass('roundTripCell','hide');YAHOO.util.Dom.removeClass('one-way','hide');}
getDirections();}
function hideErrorMessageIfShown()
{document.getElementById('dir_error').style.display='none';}
function hideChangeListingIfShown()
{if(document.getElementById('dir_changeListing').style.display=='block')
{document.getElementById('dir_routeText').style.display='block';document.getElementById('dir_changeListing').style.display='none';}}
function showChangeListing()
{document.getElementById('dir_routeText').style.display='none';document.getElementById('dir_changeListing').style.display='block';}
function changeListing(listingId,listingXCoord,listingYCoord,street,houseNumber,postalCode,city,busName)
{document.forms["directionsForm"]["chosenListingId"].value=listingId;document.getElementById('dir_changeListing').style.display='none';document.getElementById('dir_routeText').style.display='block';document.forms["directionsForm"]["listingXCoord"].value=listingXCoord;document.forms["directionsForm"]["listingYCoord"].value=listingYCoord;var show_listingAddress="<span style='font-weight: bold;'>"+busName+"<br/></span>"+street+", "+houseNumber+"<br/>"+postalCode+" "+city;document.getElementById('show_listingAddress_normal').innerHTML=show_listingAddress;document.getElementById('show_listingAddress_reverse').innerHTML=show_listingAddress;getDirections();}
function composeDirEmailLink(e,el)
{YAHOO.util.Event.stopEvent(e);var reverse=getReverse();var roundtrip=getRoundtrip();var walking=getWalking();var inputAddress=escape(getInputAddress(reverse));var listingId=getListingId();if(!listingId||!inputAddress)return;App.log("inputAddress: "+inputAddress);var link=el.href;App.log("link: "+link);link=replaceParam(link,"address1",listingId);link=replaceParam(link,"address2",inputAddress);link=replaceParam(link,"reverse",reverse);link=replaceParam(link,"roundtrip",roundtrip);link=replaceParam(link,"walking",walking);App.log("link: "+link);this.emailPanel=new App.Ui.AjaxDialog(null,{srcUrl:link,invisMask:false,close:true,zIndex:999,events:{onDialogInit:function()
{App.log("onDialogInit() start send directions email ...");},onDialogResponse:function(o)
{App.log("onDialogResponse(%o)",o);var html=o.responseText;},onDialogSubmit:function(o){App.log("onDialogSubmit(%o) FORM SUBMIT ",o);}}});this.emailPanel.show();}
function replaceParam(text,param,newValue)
{var RE=new RegExp("&"+param+"=");return text.replace(RE,"&"+param+"="+newValue);}
function replaceAll(Source,stringToFind,stringToReplace){var temp=Source;var index=temp.indexOf(stringToFind);while(index!=-1){temp=temp.replace(stringToFind,stringToReplace);index=temp.indexOf(stringToFind);}
return temp;}
function removeWhiteSpaces(text)
{return replaceAll(text," ","");}
function composeDirPrintLink(e,el)
{YAHOO.util.Event.stopEvent(e);var reverse=getReverse();var roundtrip=getRoundtrip();var walking=getWalking();var inputAddress=escape(getInputAddress(reverse));var listingId=getListingId();if(!listingId||!inputAddress)return;var link=el.href;link=replaceParam(link,"address1",listingId);link=replaceParam(link,"address2",inputAddress);link=replaceParam(link,"reverse",reverse);link=replaceParam(link,"roundtrip",roundtrip);link=replaceParam(link,"walking",walking);App.log("link: "+link);window.open(link);}
function getReverse()
{return document.forms["directionsForm"]["reverse"].value;}
function getRoundtrip()
{return document.forms["directionsForm"]["roundtrip"].value;}
function getWalking()
{return document.forms["directionsForm"]["walking"].value;}
function getInputAddress(reverse)
{if(reverse=="true")
{document.forms["directionsForm"]["inputAddress"].value=document.forms["directionsForm"]["inputAddressReverse"].value;return document.forms["directionsForm"]["inputAddressReverse"].value;}
else
{document.forms["directionsForm"]["inputAddressReverse"].value=document.forms["directionsForm"]["inputAddress"].value;return document.forms["directionsForm"]["inputAddress"].value;}}
function getListingXCoord()
{return document.forms["directionsForm"]["listingXCoord"].value;}
function getListingYCoord()
{return document.forms["directionsForm"]["listingYCoord"].value;}
function getListingId()
{return document.forms["directionsForm"]["chosenListingId"].value;}
function showError()
{document.getElementById('dir_result').style.display='none';document.getElementById('dir_map').style.display='none';document.getElementById('dir_error').style.display='block';}
function showResults()
{document.getElementById('dir_error').style.display='none';document.getElementById('dir_result').style.display='block';document.getElementById('dir_map').style.display='block';}
function openDirectionsDialogIfInUrl(){if(App.getRequestParameter('directions')=="true"){var urlEl=document.getElementById('directionsUrlLink');var address1=getRequestParam('address1');var address2=escape(decodeURI(getRequestParam('address2')));var reverse=getRequestParam('reverse');var roundtrip=getRequestParam('roundtrip');var walking=getRequestParam('walking');var link=urlEl.href;link+="&address1="+address1+"&address2="+address2+"&reverse="+reverse+"&roundtrip="+roundtrip+"&walking="+walking;App.log("link: "+link);this.directionspanel=new App.Ui.AjaxDialog(null,{srcUrl:link,invisMask:false,close:true,zIndex:999,events:{onDialogInit:function()
{App.log("onDialogInit() start directions from email link ...");getDirections();},onDialogResponse:function(o)
{App.log("onDialogResponse(%o)",o);var html=o.responseText;},onDialogSubmit:function(o){App.log("onDialogSubmit(%o) FORM SUBMIT ",o);}}});this.directionspanel.show();}}
function getRequestParam(param)
{return App.getRequestParameter(param);}
function openNoRouteFoundDialog()
{var link=Dom.get('directionsErrorLink').href;this.noRouteFoundPanel=new App.Ui.AjaxDialog(null,{srcUrl:link,invisMask:false,close:true,zIndex:0,events:{onDialogInit:function()
{}}});this.noRouteFoundPanel.show();}
function centerFrame(){if(!centered){var container=YAHOO.util.Dom.getElementsByClassName('yui-panel-container'),popupID=container[0].id;var top=YAHOO.util.Dom.getY(popupID);App.log('offsettop: ',top);var popupHeight=document.getElementById(popupID).offsetHeight;App.log("popupHeight: ",popupHeight);var newPosition=top-popupHeight/2+'px';App.log("newTop: ",newPosition);YAHOO.util.Dom.setStyle(popupID,'top',newPosition);markCentered(true);}}
function markCentered(b){App.log("B: %o",b);b?centered=true:centered=false;}
function setMapHeight(){var dirTable=document.getElementById('dir_table'),dirTableHeight=dirTable.offsetHeight;App.log("dirTableHeight: ",dirTableHeight);document.getElementById('dir_map').style.height=dirTableHeight+'px';}
