var qilroy=qilroy?qilroy:new Object();qilroy.location=new function(){var a={callback:null,errorCallback:null,locationMode:0,userLocationSet:false,postLocationSet:false,cachedLocation:false,locationEnabled:false,manualLocation:false,namedLocation:false,userLat:-1000,userLong:-1000,userLocationId:-1,userAddress:null,userDisplay:null,postLat:-1000,postLong:-1000,postLocationId:-1,postAddress:null,postDisplay:null,timestamp:-1000,geocoder:null,gearsGeocoder:null,geoNagActive:false,locationTags:null,locationRefresh:false,locationStrict:false,sponsorSet:false,sponsorImage:null,sponsorBodyColor:null,sponsorBgColor:null,sponsorHeaderColor:null,sponsorHeaderFontColor:null,init:function(c,b){debugOut("location init");if(qilroy.location.postLocationSet==true){$j(".postLocationRequired").show();if(qilroy.location.userLocationSet==true){$j(".userLocationRequired").show();qilroy.location.postCallback(c,false,false);if(qilroy.location.checkForRefresh(qilroy.location.timestamp)==true){qilroy.location.initGeoApi(function(){$j(".userLocationRequired").show()},function(){})}}else{qilroy.location.postCallback(c,false,false);qilroy.location.initGeoApi(function(){$j(".userLocationRequired").show();qilroy.location.postCallback(c,true,false)},function(){})}}else{if(qilroy.location.userLocationSet==true){qilroy.location.setPostLocation(qilroy.location.userLat,qilroy.location.userLong,qilroy.location.userLocationId,qilroy.location.userAddress);$j(".userLocationRequired").show();$j(".postLocationRequired").show();qilroy.location.postCallback(c,false,true)}else{debugOut("initGeoApi");qilroy.location.initGeoApi(function(){qilroy.location.reportLocation();$j(".userLocationRequired").show();$j(".postLocationRequired").show();qilroy.location.postCallback(c,true,true)},function(){if(b){b()}})}}},postCallback:function(e,b,d){if(e){var c=new Object();c.userSet=qilroy.location.userLocationSet;c.postSet=qilroy.location.postLocationSet;c.userChanged=b;c.postChanged=d;e(c)}},setUserLocation:function(c,d,b,g,f){try{qilroy.location.userLat=c;qilroy.location.userLong=d;qilroy.location.userLocationId=b;qilroy.location.userAddress=g;qilroy.location.locationEnabled=true;qilroy.location.userLocationSet=true;$j(".userLocationRequired").show();qilroy.location.timestamp=f?f:new Date().getTime();qilroy.location.userDisplay=qilroy.location.parseLocation(g);if(qilroy.location.postLocationSet==false){qilroy.location.setPostLocation(c,d,b,g)}}catch(h){debugOut("setUserLocation: "+h)}},setPostLocation:function(c,d,b,f){try{qilroy.location.postLat=c;qilroy.location.postLong=d;qilroy.location.postLocationId=b;qilroy.location.postAddress=f;qilroy.location.postLocationSet=true;$j(".postLocationRequired").show();qilroy.location.postDisplay=qilroy.location.parseLocation(f)}catch(g){debugOut("setPostLocation: "+g)}},initGeoApi:function(c,b){debugOut("initGeoApi");if(geo_position_js.init()){debugOut("geo init complete");qilroy.location.geoNagActive=true;window.setTimeout("qilroy.location.showGeoNag()",5000);qilroy.location.callback=c;qilroy.location.errorCallback=b;window.setTimeout("qilroy.location.checkNavigatorCheck()",15000);geo_position_js.getCurrentPosition(function(d){debugOut("geo location complete");qilroy.location.hideGeoNag();qilroy.location.locationMode=2;qilroy.location.locationEnabled=true;qilroy.location.decodeLocation(d.coords.latitude,d.coords.longitude,function(e){if(e){qilroy.location.setGeoLocation(d,e.address)}else{qilroy.location.setGeoLocation(d,qilroy.location.userLat+", "+qilroy.location.userLong)}if(c){c()}})},function(){debugOut("geo location error");qilroy.location.initIpApi(c,b)},null)}else{qilroy.location.initIpApi(c,b)}},checkNavigatorCheck:function(){debugOut("navigator check: "+qilroy.location.locationEnabled);if(qilroy.location.locationEnabled==false){qilroy.location.initIpApi(qilroy.location.callback,qilroy.location.errorCallback)}},initIpApi:function(c,b){google.load("maps","2",{callback:function(){if(google.loader.ClientLocation){qilroy.location.locationEnabled=true;qilroy.location.locationMode=3;var d="Near "+google.loader.ClientLocation.address.city+" "+google.loader.ClientLocation.address.region;qilroy.location.setGeoLocation(google.loader.ClientLocation,d);if(c){c()}}else{qilroy.location.loadHotSpot(c,b)}}})},getGeocoder:function(){if(qilroy.location.geocoder==null){qilroy.location.geocoder=new GClientGeocoder()}return qilroy.location.geocoder},getGearsGeocoder:function(){if(qilroy.location.gearsGeocoder==null){qilroy.location.gearsGeocoder=google.gears.factory.create("beta.geolocation")}return qilroy.location.gearsGeocoder},setPostAddress:function(b){qilroy.location.postAddress=b;qilroy.location.postDisplay=qilroy.location.parseLocation(b)},getPostAddress:function(){return qilroy.location.postDisplay},restoreUserLocation:function(b){if(qilroy.location.userLat>-1000){qilroy.location.setPostLocation(qilroy.location.userLat,qilroy.location.userLong,qilroy.location.userLocationId,qilroy.location.userAddress);qilroy.location.setLocationDetail(null);if(b){b()}}},setManualLocation:function(d,e,c){var b=$j(d).val();if(b&&b!=""){qilroy.location.setLocationDetail(null);qilroy.location.encodeLocation(b,e,c)}else{c("please enter a valid address")}},setLocationDetail:function(b){if(b){qilroy.location.locationTags=b.location.tags;qilroy.location.locationRefresh=b.location.autoRefresh;qilroy.location.locationStrict=b.location.strictRadius;if(b.sponsor){qilroy.location.sponsorSet=true;qilroy.location.sponsorBgColor=b.sponsor.backgroundColor;qilroy.location.sponsorHeaderColor=b.sponsor.headerColor;qilroy.location.sponsorHeaderFontColor=b.sponsor.headerFontColor;qilroy.location.sponsorBodyColor=b.sponsor.bodyColor;qilroy.location.sponsorImage=b.sponsor.image}else{qilroy.location.sponsorSet=false;qilroy.location.sponsorBgColor=null;qilroy.location.sponsorHeaderColor=null;qilroy.location.sponsorHeaderColor=null;qilroy.location.sponsorBodyColor=null;qilroy.location.sponsorImage=null}}else{qilroy.location.locationTags=null;qilroy.location.locationRefresh=false;qilroy.location.locationStrict=false;qilroy.location.sponsorSet=false;qilroy.location.sponsorBgColor=null;qilroy.location.sponsorHeaderColor=null;qilroy.location.sponsorHeaderColor=null;qilroy.location.sponsorBodyColor=null;qilroy.location.sponsorImage=null}},setNamedLocation:function(c,e,b){var d=qilroy.appContext+"/location/loadLocation/"+c;$j.ajax({type:"GET",url:d,cache:false,dataType:"json",success:function(g){if(g.status==1){var f=g.data;if(f){qilroy.location.setPostLocation(f.location.centerLat,f.location.centerLong,c,f.location.title);qilroy.location.setLocationDetail(f)}if(e){e()}}else{debugOut("json error: "+g.message);if(b){b(g.message)}}},error:function(f){debugOut("error: "+f);if(b){b(f)}}})},setGeoLocation:function(c,b){var e;var d;if(c&&c.coords){e=c.coords.latitude;d=c.coords.longitude}else{if(c&&c.latitude){e=c.latitude;d=c.longitude}}qilroy.location.setUserLocation(e,d,-1,b)},setNoLocation:function(b){debugOut("location error");if(b){b()}},decodeLocation:function(b,c,d){google.load("maps","2",{callback:function(){var e=new GLatLng(b,c);qilroy.location.getGeocoder().getLocations(e,function(f){if(!f||f.Status.code!=200){d(null)}else{var g=f.Placemark[0];d(g)}})}})},encodeLocation:function(b,d,c){google.load("maps","2",{callback:function(){qilroy.location.getGeocoder().getLatLng(b,function(e){if(!e){if(c){c("could not find address")}}else{debugOut("location: "+e.lat()+" "+e.lng());qilroy.location.setPostLocation(e.lat(),e.lng(),-1,b);qilroy.location.manualLocation=true;if(d){d()}}})}})},serverDecodeLocation:function(c){var b=qilroy.appContext+"/location/setLocation?userLat="+qilroy.location.userLat+"&userLong="+qilroy.location.userLong+"&timestamp="+qilroy.location.timestamp;$j.ajax({type:"GET",url:b,cache:false,dataType:"json",success:function(d){if(d.status==1){c(d.data)}else{debugOut("json error: "+d.message)}},error:function(d){debugOut("error: "+d)}})},loadHotSpot:function(d,b){var c=qilroy.appContext+"/home/hotSpots?perPage=1";$j.ajax({type:"GET",url:c,cache:false,dataType:"json",success:function(f){if(f.success==true){if(f.hotSpots&&f.hotSpots.results&&f.hotSpots.results.length>0){var e=f.hotSpots.results[0];qilroy.location.setPostLocation(e.location.centerLat,e.location.centerLong,locationId,e.location.title);d()}}else{qilroy.location.setNoLocation(b);debugOut("json error: "+f.message)}},error:function(e){qilroy.location.setNoLocation(b);debugOut("error: "+e)}})},getAllLocationParams:function(){var b="userLat="+qilroy.location.userLat+"&userLong="+qilroy.location.userLong+"&postLat="+qilroy.location.postLat+"&postLong="+qilroy.location.postLong+"&timestamp="+qilroy.location.timestamp+"&userAddress="+qilroy.location.userAddress+"&postAddress="+qilroy.location.postAddress+"&userLocationId="+qilroy.location.userLocationId+"&postLocationId="+qilroy.location.postLocationId;if(qilroy.location.locationStrict==true){b=b+"&strictRadius=true"}return b},getUserLocationParams:function(){return"userLat="+qilroy.location.userLat+"&userLong="+qilroy.location.userLong+"&timestamp="+qilroy.location.timestamp+"&userAddress="+qilroy.location.userAddress+"&userLocationId="+qilroy.location.userLocationId},getPostLocationParams:function(){return"postLat="+qilroy.location.postLat+"&postLong="+qilroy.location.postLong+"&timestamp="+qilroy.location.timestamp+"&postAddress="+qilroy.location.postAddress+"&postLocationId="+qilroy.location.postLocationId},addAllLocationParams:function(b){b.userLat=qilroy.location.userLat;b.userLong=qilroy.location.userLong;b.timestamp=qilroy.location.timestamp;b.userAddress=qilroy.location.userAddress;b.userLocationId=qilroy.location.userLocationId;b.postLat=qilroy.location.postLat;b.postLong=qilroy.location.postLong;b.postAddress=qilroy.location.postAddress;b.postLocationId=qilroy.location.postLocationId;return b},addUserLocationParams:function(b){b.userLat=qilroy.location.userLat;b.userLong=qilroy.location.userLong;b.timestamp=qilroy.location.timestamp;b.userAddress=qilroy.location.userAddress;b.userLocationId=qilroy.location.userLocationId;return b},addPostLocationParams:function(b){b.postLat=qilroy.location.postLat;b.postLong=qilroy.location.postLong;b.timestamp=qilroy.location.timestamp;b.postAddress=qilroy.location.postAddress;b.postLocationId=qilroy.location.postLocationId;return b},reportLocation:function(d,b){var c=qilroy.appContext+"/location/setLocation?"+qilroy.location.getAllLocationParams();$j.ajax({type:"GET",url:c,cache:false,dataType:"json",success:function(e){if(d){d(e)}},error:function(e){if(b){b(e)}}})},parseLocation:function(f){try{var c=f;if(f){var b=f.split(",");if(b.length==1||b.length==2){return c}else{if(b.length==3){c=qilroy.location.parseAddress(b[0]);c=c+","+b[1];c=c+", "+qilroy.location.parseState(b[2])}else{c=qilroy.location.parseAddress(b[0]);c=c+","+b[1];c=c+", "+qilroy.location.parseState(b[2])}}}return c}catch(d){return f}},parseAddress:function(e){var d=e;if(d){var b=d.split(" ");if(b.length==1){return d}else{d="";for(var c=1;c<b.length;c++){d=d+b[c];if(c!=(b.length-1)){d=d+" "}}}}return d},parseState:function(d){var c=d;if(c){c=c.trim();var b=c.split(" ");if(b.length==1){return c}else{return b[0]}}return c},showGeoNag:function(){if(qilroy.location.geoNagActive==true){var b='<div class="geoNag"><span style="padding-left:20px;">Please share your location to allow qilroy to work</span></div>';$j("body").append(b);window.setTimeout("qilroy.location.hideGeoNag()",5000)}},hideGeoNag:function(){qilroy.location.geoNagActive=false;$j(".geoNag").hide()},checkForRefresh:function(b){return(new Date().getTime()-b>3600000)}};return a};
