
﻿var debug=false;var map;var geocoder;var pIcon;var mainMarker
var closestGarage=null;var closestGarageGLatLng=null;var doNotLoad=false;var profile;var mainZoom=17;var iPhoneZoom=16;var getLocInt;var getLocState=false;var isLocDisallowed=false;var theCity="New York, NY";function findCityByIP(ip){$.get('/Park/Tools/GetCityFromID',{ip:ip},function(curAddr){if(curAddr!="")$("#address").val(curAddr);},"text");}
function changeCity(city){theCity=city;var initPoint=getCityLatLng();map.setCenter(initPoint,mainZoom);mainMarker.setLatLng(initPoint);}
function getCityLatLng(){if(theCity=="New York, NY")
return new GLatLng(40.7489381,-73.9918181);else if(theCity=="San Francisco, CA")
return new GLatLng(37.775222,-122.419188);else if(theCity=="Chicago, IL")
return new GLatLng(41.851662,-87.632103);else if(theCity=="Boston, MA")
return new GLatLng(42.35892,-71.05781);else if(theCity=="Los Angeles, CA")
return new GLatLng(34.052187,-118.243425);}
function initMap(p){profile=p||'website';iPhone=(profile=='iPhone');if(iPhone)mainZoom=iPhoneZoom;geocoder=new GClientGeocoder();var initPoint=getCityLatLng();if(debug)initPoint=new GLatLng(40.7501025,-73.9749366);map=new GMap2($("#map").get(0));map.setUIToDefault();map.setCenter(initPoint,mainZoom);map.enableContinuousZoom();if(iPhone)map.disableDoubleClickZoom();GEvent.addListener(map,"dragstart",function(){if(getLocState)toggleLocationRefresh(false);});GEvent.addListener(map,"zoomstart",function(){if(getLocState)toggleLocationRefresh(false);});GEvent.addListener(map,"click",function(overlay,latlng,overlaylatlng){if(!overlay)$("#parkingInfo").hide();});GEvent.addListener(map,"moveend",function(){var center=map.getCenter();mainMarker.setLatLng(center);if(!iPhone)
geocoder.getLocations(center,function(response){$("#address").attr("value",response.Placemark[0].address);});if(!doNotLoad)findParking(center);});var blueIcon=new GIcon(G_DEFAULT_ICON);blueIcon.image="http://checkparking.com/images/blue-dot.png";blueIcon.shadow="http://checkparking.com/images/shadow-blue-dot.png";blueIcon.iconSize=new GSize(32.0,32.0);mainMarker=new GMarker(initPoint,{draggable:true,bouncy:true,icon:blueIcon});GEvent.addListener(mainMarker,"dragstart",function(){map.closeInfoWindow();});GEvent.addListener(mainMarker,"dragend",function(latlng){if(map.getZoom()!=mainZoom){map.setZoom(mainZoom);map.setCenter(latlng);}
else
map.panTo(latlng);if(!iPhone)geocoder.getLocations(latlng,function(response){$("#address").attr("value",response.Placemark[0].address);});});map.clearOverlays();map.addOverlay(mainMarker);pIcon=new GIcon();pIcon.image="http://checkparking.com/Park/images/parking.png";pIcon.shadow="http://checkparking.com/Park/images/shadow-parking.png";pIcon.iconSize=new GSize(35.0,38.0);pIcon.shadowSize=new GSize(51.0,37.0);pIcon.iconAnchor=new GPoint(16.0,37.0);pIcon.infoWindowAnchor=new GPoint(16.0,18.0);}
function updateMapByAddress(){var address=$("#address").attr("value");if(getLocState)toggleLocationRefresh(false);if(address.length<10){var parts=address.split(",");if(parts.length==2){address=parts[0]+" St. and "+parts[1]+" Ave., New York, NY";$("#address").attr("value",address);}}
geocoder=new GClientGeocoder();geocoder.getLocations(address,function(response){if(!response.Placemark){if(iPhone)
alert("Problem finding the address. Try again");else
$("#addressError").show();return;}
else
$("#addressError").hide();latlng=new GLatLng(response.Placemark[0].Point.coordinates[1],response.Placemark[0].Point.coordinates[0]);mainMarker.setLatLng(latlng);if(map.getZoom()!=mainZoom){map.setZoom(mainZoom);findParking(latlng);}
map.setCenter(latlng,mainZoom);});return false;}
function findParking(latlng){var monthly="";var dur="";if(iPhone){var s=$('#durDDL').val();if(s=="Monthly"){monthly="on";dur=1;}
else{monthly="";dur=s.split(" ")[0];}}
else{monthly=$("#monthly").attr("checked")?"on":"";dur=$("#duration").val();}
$.get('/Park/Rate/BestRate',{devkey:"271A62BC74DB4e8a8BA598786F3E81F3",request:"point",format:"json",curLat:latlng.lat(),curLng:latlng.lng(),dur:dur,vehicle:$("#vehicle").val(),monthly:monthly},function(data,textStatus){map.clearOverlays();map.addOverlay(mainMarker);if(data.length>0){closestGarage=data[0];closestGarageGLatLng=new GLatLng(closestGarage.info.Lat,closestGarage.info.Lng);if(!map.getBounds().containsLatLng(closestGarageGLatLng)){if(iPhone&&getLocState)toggleLocationRefresh(false);$('#closestDistance').html("<a href='#' onclick='map.setCenter(closestGarageGLatLng, mainZoom)'>Take me to the closest garage</a> ("+closestGarage.distance.toFixed(2)+" miles or "+(closestGarage.distance*1.609344).toFixed(2)+" Km away)");$('#noresults').show().center();}
else{$('#noresults').hide();}}
$.each(data,function(index,item){addParkingMarker(item.info,item.rate.toString());});},"json");}
function UpdateCouponDiv(){if(hasCoupon(curRate,curParking)){var content="<p><span style='font-size: 2em'>$"+curParking.CouponValue+" off</span> discount coupons available!</p>";var userEmail=getCookie("UserEmail");var userCode=getCookie("UserCode");content+="<p>Your email: <input type='text' id='userEmail' value='"+userEmail+"' /> <a href='#' onclick='setUserEmail($(\"#userEmail\").val());'>Update</a></p>";if(userEmail==""){content+="<p>Need an email to generate your coupon</p>";}
else if(userCode==null||userCode==""){UC();}
else if(couponCode==null||couponCode==""){content+="<p><b>DO NOT</b> generate your coupon until you are arrive at this garage. <br /> You can only generate <b>one coupon</b> per garage per day.</p><center><a href='#' onclick='CC()'>Generate Your Coupon</a></center>";}
else{content+="<p><center style='padding: 20px; background: white'><img src='/Park/Rate/MakeBarcode?c="+couponCode+"' /></center></p>";content+="<p><center style='font-size: 3em'>"+couponCode+"</center></p>";}
$('#topCouponDiv').show().html(content).attr("class","la").attr("style","background:Green; color: White; padding: 10px; width: 280px;");}
else{$('#topCouponDiv').html('No coupons available at this time but mention <b>CheckParking</b> to the parking attendents for possible discounts.');}}
function setUserEmail(email){if(!isEmailValid(email))
alert('Email is not valid');else
setCookie("UserEmail",email,10000);UpdateCouponDiv();}
function isEmailValid(email){return!(email.length<4||email.indexOf('@')<-1);}
function UC(){$.get('/Park/Tools/GetGUID',{},function(guid){setCookie("UserCode",guid,10000);UpdateCouponDiv();},"text");}
function CC(){$.post('/Park/Parking/CC',{parkingID:curParking.ParkingID,userCode:getCookie("UserCode"),userEmail:getCookie("UserEmail")},function(cc){couponCode=cc;UpdateCouponDiv();},"text");}
var curParking=null;var curRate=null;var couponCode="";function sendCommments(){var comments=$("#comments").val().trim();var email=$("#userEmail2").val();if(email.length>0){if(!isEmailValid(email)){alert('Email is not valid. Please remove if you prefer not to provide.');return;}
else
setCookie("UserEmail",email,10000);}
if(comments.length==0){alert("Please type your comments before submitting");}
else{$.post('/Park/Tools/email',{subject:"ParkingID: "+curParking.ParkingID,body:escape("<a href='http://checkparking.com/Rate/Index/"+curParking.ParkingID+"'>Parking Link</a><br />Sender: "+email+"<br />"+comments)},function(cc){if(cc=="")
$("#divComment").html("<b>Thank you for your input</b>");else
$("#divComment").html("Sorry; there was a problem:<br />"+cc);},"text");}}
function addParkingMarker(parking,rate){setPIcon(rate,parking);var marker=new GMarker(new GLatLng(parking.Lat,parking.Lng),{icon:pIcon});if(iPhone){GEvent.addListener(marker,"click",function(latlng){var top=0;var left=0;curParking=parking;curRate=rate;$("#parkingInfo").show().html("<input type='button' onclick=\"$('#parkingInfo').hide(); couponCode='';\" value='Back to Map' /><br /><br />"
+parking2html(parking)
+"<br /><br /><div id='topCouponDiv'></div><br /><br /><div id='divComment'>Kindly share with us any incorrect rates, how much you paid or any comments about the parking"
+"<br /><textarea rows='6' cols='30' id='comments'></textarea>"
+"<br />Your email (optional): <input type='text' id='userEmail2' />"
+"<br /><input type='button' onclick=\"sendCommments();\" value='Submit to CheckParking' /></div>").css({top:top,left:left,padding:"10px",width:"100%",height:"480px"});$("#userEmail2").val(getCookie("UserEmail"));UpdateCouponDiv();});}
else{GEvent.addListener(marker,"click",function(latlng){$('#simpleOptions').show();});GEvent.addListener(marker,"mouseover",function(latlng){var a=map.fromLatLngToContainerPixel(latlng);var top=a.y+$("#map").offset().top+20;var left=a.x+$("#map").offset().left+20;var dur=$("#monthly").attr("checked")?" a month":$("#duration").val()+" hour(s)";content="<center><span style='font-size: 0.8em; color: Gray;'>To change, use options under the search box</span></center>";content+="Rate for a <b>"+$("#vehicle").val()+"</b> parked at";content+="<br />"+parking.BldgNo+" "+parking.Street+" for <b>"+dur+"</b>";content+=getParkingPhone(parking);$("#parkingInfo").show().html(content).css({top:top,left:left,padding:"10px"});});GEvent.addListener(marker,"mouseout",function(latlng){$("#parkingInfo").hide();});}
map.addOverlay(marker);}
function parking2html(p){return"<b>"+p.BldgNo+" "+p.Street
+"</b><br />Type: "+p.ParkingType
+getParkingPhone(p);}
function getParkingPhone(p){return((iPhone)?"<br />Tel: <a href='tel:"+formatTel(p.Tel)+"'>"+formatTel(p.Tel)+"</a>":"<br />Tel: "+formatTel(p.Tel));}
function parking2address(p){return p.BldgNo+" "+p.Street+", "+p.Location+", "+p.State+" "+p.Zip}
function formatTel(c){if(c.length<10)return c;if(c.substring(0,1)=="(")return c;return c.substring(0,3)+"-"+c.substring(3,6)+"-"+c.substring(6,10);}
function hasCoupon(rate,parking){return(parking.RemainingCoupon!=null&&parking.RemainingCoupon!=0&&(rate-parking.CouponValue)>0);}
function setPIcon(rate,parking){var icon=hasCoupon(rate,parking)?"blankSD":"blank";if(rate==""){pIcon.image="http://checkparking.com/Park/images/parking.png";}
else{var sp="";if(rate.length==1)sp="  ";else if(rate.length==2)sp=" ";pIcon.image='/Park/Rate/MakeMapMarker?icon='+icon+'&rate='+sp+'$'+rate;}}