var _siteID;var _lastTide,_lastCurrent;var _stations=[];var _num_tides_found=0;GMarker.prototype.setInfo=function(type,name,distance){this.type=type;this.name=name;this.distance=distance;};function initMap(){if(GBrowserIsCompatible()){_map=new GMap2($("map_canvas"));_map.setUIToDefault();var ui=_map.getDefaultUI();ui.maptypes.hybrid=false;ui.maptypes.physical=false;ui.controls.maptypecontrol=false;ui.controls.menumaptypecontrol=false;ui.controls.smallzoomcontrol3d=false;_map.setUI(ui);GEvent.addListener(_map,"click",map_click);}else{alert("Your browser is not compatible with Google Maps.  Please try Internet Explorer, FireFox, Opera, Chrome or Safari.");}};function map_click(overlay,latlng,overlaylatlnt){if(overlay){if("Tide"==overlay.type){$("spnTideStation").innerHTML=overlay.name;if(_lastTide){_lastTide.setImage("/images/map_tide.png");}overlay.setImage("/images/map_tide_selected.png");_lastTide=overlay;}else if("Current"==overlay.type){$("spnCurrentStation").innerHTML=overlay.name;if(_lastCurrent){_lastCurrent.setImage("/images/map_current.png");}overlay.setImage("/images/map_current_selected.png");_lastCurrent=overlay;}}};function addMarker(lat,lng,type){var marker=new GMarker(new GLatLng(lat,lng),{icon:createIcon(type)});_map.addOverlay(marker);return marker;};function addStation(lat,lng,type,name,distance){var marker=addMarker(lat,lng,type);marker.setInfo(type,name,distance);return marker;};function getDate(id){var ret;try{ret=new Date(Date.parse($(id).value));}catch(e){ret=null;}return ret;};function getCorrection(){var s=$('txtCorrection').value;var correction;if(s){if(isNaN(s)){$('txtCorrection').focus();throw new Error('\''+s+'\' is not a valid correction value.\n\n'+'Enter a numerical correction value from -180 to 180 minutes.');}else{correction=parseInt(s);}if(180<Math.abs(correction)){$('txtCorrection').focus();throw new Error('Corrections beyond 180 minutes are not allowed.');}}else{correction=0;}return correction;};function formatDate(d){return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();};function getGraphSrc(start,end,numDays,correction){return encodeURI('/Tides/'+'?Tides='+$('spnTideStation').innerHTML+'&Currents='+$('spnCurrentStation').innerHTML+'&Start='+formatDate(start)+'&End='+formatDate(end)+'&Width=775'+'&Height='+(100+(numDays*225))+'&Correction='+correction+'&Format=Full');};function generateGraphs(){var start,end,correction,numDays,i;try{dp_Hide();if(0==_siteID){throw new CustomError('You must select a dive site first.');}start=getDate('txtDate');if(null==start){throw new CustomError('Enter a valid date.');}numDays=parseInt(getListValue($('selNumDays')));end=new Date(start.getTime()+((numDays-1)*86400000));correction=getCorrection();swap(false);$('imgGraphs').src=getGraphSrc(start,end,numDays,correction);showGraph(true);}catch(e){showError(e);}};function showBestTides(){var start,end,correction,i,html,type,c,speed,ary,startMin,endMin;try{dp_Hide();if(1>_siteID){throw new Error('You must select a dive site first.');}correction=getCorrection();start=getDate('txtSearchFrom');end=getDate('txtSearchTo');if(null==start||null==end){throw new Error('Enter valid search dates.');}if(start>end){throw new Error('The start date cannot be after the end date.');}var days=(end-start)/(1000*60*60*24);if(365<days){throw new Error('Specify a date range that is no greater than 365 days.');}var weekdays='';for(i=1;i<8;i++){if($('weekday'+i).checked){weekdays+=$('weekday'+i).value;}}if(!weekdays){throw new Error('Select at least one day of the week to check tides for.');}var slacks='';if($('slackFlood').checked){slacks+='flood';}if($('slackEbb').checked){slacks+='ebb';}if(!slacks){throw new Error('Select either flood or ebb (or both) for the slack setting.');}startMin=parseInt(getListValue($('selTideStart')));endMin=parseInt(getListValue($('selTideEnd')));if(startMin>=endMin){throw new Error('Select a start time that is before the end time.');}hideObj('divSettings');showObj('divBestTides');$('divTideListing').innerHTML='Searching...';new Ajax.Request('/AJAX/GetBestTides3.ashx',{parameters:{site:_siteID,start:formatDate(start),end:formatDate(end),correction:correction,station:$('spnCurrentStation').innerHTML,weekdays:weekdays,slacks:slacks,startMin:startMin,endMin:endMin},onSuccess:function(transport){try{var json=transport.responseText.evalJSON();_num_tides_found=json.tides.length;html='<a href="javascript:hideTideListing()">Change settings and search again</a><br /><br />';html+='<b>'+json.site.name+'</b><br/>'+'Station: '+json.station+' (correction: '+json.correction+' minutes)<br/><br/>';if(5<json.tides.length){html+='<a id="link_toggle_best_days" href="javascript:toggleBestDays()">Show all '+_num_tides_found+' entries</a><br/><br/>';}html+='<table id="best_tides" width="750" cellpadding="0" cellspacing="0">';for(i=0;i<json.tides.length;i++){if(json.tides[i].isVeryBest){html+='<tr class="veryBestTide">';}else if(json.tides[i].isBest){html+='<tr class="bestTide">';}else{html+='<tr class="notBestTide" style="display:none">';}if('MaxEbb'==json.tides[i].type){type='Slack/Flood';}else{type='Slack/Ebb';}var timeClass='smallText time';var speedClass='';if(json.tides[i].isVeryBest){timeClass+=' veryBest';speedClass='veryBest';}html+='<td width="14%" class="tideDay"><b>'+json.tides[i].weekday+'</b><br/>'+json.tides[i].preDate+'</td>';html+='<td width="14%" class="tideDay">'+type+'</td>';html+='<td width="18%"><span class="'+timeClass+'">'+json.tides[i].preDate+'<br/>'+json.tides[i].preTime+'</span><br/><span class="'+speedClass+'">'+json.tides[i].preSpeed+' kt</span></td>';if(json.tides[i].slackDate){if(!json.tides[i].slackSpeed){speed='Slack'}else{speed=json.tides[i].slackSpeed+' kt';}html+='<td width="18%"><span class="'+timeClass+'">'+json.tides[i].slackDate+'<br/>'+json.tides[i].slackTime+'</span><br/><span class="'+speedClass+'">'+speed+'</span></td>';}else{html+='<td width="18%"><span class="'+timeClass+'">No data<br/>available<br/>&nbsp;</td>';}if(json.tides[i].postDate){html+='<td width="18%"><span class="'+timeClass+'">'+json.tides[i].postDate+'<br/>'+json.tides[i].postTime+'</span><br/><span class="'+speedClass+'">'+json.tides[i].postSpeed+' kt</span></td>';}else{html+='<td width="18%"><span class="'+timeClass+'">No data<br/>available<br/>&nbsp;</td>';}var d=new Date(Date.parse(json.tides[i].slackDate));html+='<td width="18%"><a href="/NewEvent.aspx'+'?type=1'+'&date='+encodeURIComponent(json.tides[i].preDate)+'&time='+encodeURIComponent(json.tides[i].preTime)+'&sitename='+encodeURIComponent(json.site.name)+'&siteid='+_siteID+'">Plan a dive</a>'+'<br /><a href="'+getGraphSrc(d,d,1,correction)+'" rel="lightbox['+i+']">View graph</a></td>';html+='</tr>';}html+='</table>';$('divTideListing').innerHTML=html;var tbl=$('best_tides');for(i=0;i<tbl.rows.length;i++){for(c=0;c<tbl.rows[i].cells.length;c++){tbl.rows[i].cells[c].style.borderTop='1px solid #ccc';}}}catch(e){showError(e);}},on400:function(transport){var json=transport.responseText.evalJSON();$('divTideListing').innerHTML='<a href="javascript:hideTideListing()">Change settings and search again</a><br /><br />'+json.message;},onFailure:function(transport){$('divTideListing').innerHTML='<a href="javascript:hideTideListing()">Change settings and search again</a><br /><br />'+'An unexpected problem occurred while attempting to communicate with the PyD servers.  Please try again.';},onComplete:function(transport){showLoader(false);}});}catch(e){showError(e);}};function hideTideListing(){$('divBestTides').style.display='none';$('divSettings').style.display='';};function toggleBestDays(){var tbl,lnk,on,i;tbl=$('best_tides');lnk=$('link_toggle_best_days');on=('Show only best entries'==lnk.innerHTML);for(i=0;i<tbl.rows.length;i++){if('notBestTide'==tbl.rows[i].className){if(on){tbl.rows[i].style.display='none';}else{tbl.rows[i].style.display='';}}}if(on){lnk.innerHTML='Show all '+_num_tides_found+' entries';}else{lnk.innerHTML='Show only best entries';}};function swap(on){if(on){showAddToSite(0);initLinks();$("imgLoading").style.display="none";$("divGraphs").style.display="";}else{$("imgLoading").style.display="";$("divGraphs").style.display="none";}};function showGraph(on){if(on){$("divSettings").style.display="none";$("divGraph").style.display="";}else{$("divSettings").style.display="";$("divGraph").style.display="none";}};function diveSiteSelected(ctl,site){hideObj('siteSelector');ctl.textbox.value="";$('spnSiteName').innerHTML=site.name;_siteID=site.id;loadSite();};function setSelectSiteLink(on){if(!on){hideObj('changeSiteLink');}else{if(0<_siteID){$('changeSiteLink').innerHTML='(change)';}else{$('changeSiteLink').innerHTML='(select)';}showObj('changeSiteLink');}};function loadSite(){try{if(0<_siteID){var params=[];params[0]="gettidestationinfo";params[1]=_siteID;showLoader("Loading tide stations...");new PyDajax(params,true,this,loadSiteCallback).send();}else{_map.clearOverlays();showLoader("No dive site specified");$("spnSiteName").innerHTML="Enter the name of the dive site";$("spnTideStation").innerHTML="None selected";$("spnCurrentStation").innerHTML="None selected";showObj("siteSelector");setSelectSiteLink(false);$("txtSite").focus();}}catch(e){showError(e);}};function loadSiteCallback(xml){var bounds,node,marker,type,i;try{showLoader();_lastTide=null;_lastCurrent=null;_stations=[];_map.clearOverlays();if(xml){$("spnSiteName").innerHTML=xml.documentElement.getAttribute("SiteName");bounds=new GLatLngBounds(new GLatLng(xml.documentElement.getAttribute("SWLat"),xml.documentElement.getAttribute("SWLng")),new GLatLng(xml.documentElement.getAttribute("NELat"),xml.documentElement.getAttribute("NELng")));_map.setCenter(bounds.getCenter());fit(_map,bounds);for(i=0;i<xml.documentElement.childNodes.length;i++){node=xml.documentElement.childNodes[i];type=node.getAttribute("Type");marker=addStation(node.getAttribute("Lat"),node.getAttribute("Lng"),type,node.getAttribute("Name"),node.getAttribute("Distance"));if("Tide"==type&&null==_lastTide){_lastTide=marker;}else if("Current"==type&&null==_lastCurrent){_lastCurrent=marker;}}addMarker(xml.documentElement.getAttribute("SiteLat"),xml.documentElement.getAttribute("SiteLng"),"site");map_click(_lastTide);map_click(_lastCurrent);showObj('clickMapNote');setSelectSiteLink(true);}}catch(e){showError(e);}};function noSiteSelected(ctl){hideObj('clickMapNote');};function showSiteInstructions(div){showBubble(div,"<span class='smallText'>As you type the name of the dive site, a list will appear below the input box.  Use your mouse to select a dive site from the list.",300,12);};function positionLoader(){var ldr=$("map_canvas_loading");var map=$("map_canvas");var orig=getAbsolutePos(map);ldr.style.left=orig.x+"px";ldr.style.top=orig.y+"px";ldr.style.width=map.style.width;ldr.style.height=map.style.height;};function showLoader(msg){if(msg){$("map_loader_message").innerHTML=msg;positionLoader();showObj("map_canvas_loading");}else{hideObj("map_canvas_loading");}};function showAddToSite(on){if(1==on){hideObj("addToSiteQ");showObj("addToSiteA");}else{hideObj("addToSiteA");showObj("addToSiteQ");}};function isDateValid(id){if(!Date.parse($(id))){return false;}else{return true;}};function loadTimeDropDown(list,min){var opt,h,m,hs,ms,tt,timeStr,timeVal;tt='AM';for(h=0;h<25;h++){if(11<h){tt='PM';}if(0==h){hs='12';}else if(12<h){hs=(h-12).toString();}else{hs=h.toString();}for(m=0;m<60;m+=15){if(0==m){ms='00';}else{ms=m.toString();}timeVal=(h*60)+m;if(24==h&&0==m){timeStr='Midnight';}else if(12==h&&0==m){timeStr='Noon';}else{timeStr=hs+':'+ms+' '+tt;}opt=document.createElement('option');opt.value=timeVal;opt.text=timeStr;list.options[list.options.length]=opt;if(((h*60)+m)==min){opt.selected=true;}if(h>23){break;}}}};function initLinks(){var img=$("imgGraphs");$("directLink").value=img.src;$("embedHTML").value="<img src='"+img.src+"' height='"+img.height+"' width='"+img.width+"' alt='Tides and currents on PlanYourDive.com' />";$("bbcode").value="[img]"+img.src+"[/img]";showAddToSite(0);};
