(function($){$.fn.geoWidget=function(l){var m={ip:'193.109.119.24',title:'Position ',textFieldStyle:'width:25em',mapStyle:'width:25em; height:25em'};var n=$.extend(m,l);var o=$(this).attr('id');var p=o+'_address';var q=o+'_loader';var r=o+'_valid';var s=o+'_invalid';var t=o+'_map';var u;var v;var w='MISSING_IPINFODB_KEY';var x='UNKNOWN_IP';var y='ZERO_RESULTS';var z='OVER_QUERY_LIMIT';var A="<div>"+n.title+"<input id='"+p+"' style='"+n.textFieldStyle+"'/>";if(n.imageAjaxLoader!='')A=A+"<img id='"+q+"' align='top' src='"+n.imageAjaxLoader+"' style='display:none;'/>";if(n.imageAddressValid!='')A=A+"<img id='"+r+"' align='top' src='"+n.imageAddressValid+"' style='display:none;'/>";if(n.imageAddressInvalid!='')A=A+"<img id='"+s+"' align='top' src='"+n.imageAddressInvalid+"' style='display:none;'/>";A=A+"</div><div id='"+t+"' style='"+n.mapStyle+"'></div>";$('#'+o).empty().append(A);$('#'+p).keyup(function(e){if(e.keyCode==13){var a=$('#'+p).val();if((a!='')&&(a!=n.address)){if(n.geoprovider==undefined)geogoogle(a);else if(n.geoprovider=='google')geogoogle(a);else if(n.geoprovider=='yahoo')geoyahoo(a);else if(n.geoprovider=='twitter')geotwitter(a);else alert('Unknown geoprovider '+n.geoprovider)}}});if(n.address==''||n.latitude==''||n.longitude==''){iplocate()}else{$('#'+p).val(n.address);$('#'+r).show();n.callback(n.address,n.latitude,n.longitude,null,null)}function iplocate(){$('#'+q).show();$('#'+r).hide();$('#'+s).hide();$.ajax({dataType:'jsonp',url:'http://api.ipinfodb.com/v2/ip_query.php',data:'key='+n.ipinfodbKey+'&ip='+n.ip+'&output=json&timezone=false',success:function(a,b,c){var d;var e;var f;var g;var h;var i=a.Status;if(i=='OK'){e=a.Latitude;f=a.Longitude;d=a.City;if(d!=undefined&&a.CountryName!=undefined)d=d+', '+a.CountryName;$('#'+r).show();$('#'+p).val(d);var j=new google.maps.LatLng(e,f);var k={zoom:15,center:j,mapTypeId:google.maps.MapTypeId.ROADMAP};u=new google.maps.Map(document.getElementById(t),k);v=new google.maps.Marker({map:u,position:j,title:d})}else{if(i=='MISSING API KEY'||n.ipinfodbKey==''){g=w;h='Missing ipinfodb API key, please register one for free: http://ipinfodb.com/'}else if(i.substr(0,12)=='IP NOT FOUND'){g=x;h='ipinfodb service cound not found IP '+n.ip}else{g=i;h='ipinfodb service was not successful for IP '+n.ip}$('#'+s).show()}$('#'+q).hide();n.callback(d,e,f,g,h)}})}function geogoogle(h){$('#'+q).show();$('#'+r).hide();$('#'+s).hide();var i=new google.maps.Geocoder();i.geocode({'address':h},function(a,b){var c;var d;var e;var f;if(b==google.maps.GeocoderStatus.OK){h=a[0].formatted_address;var g=a[0].geometry.location;c=g.lat();d=g.lng();$('#'+r).show();$('#'+p).val(h);u.setCenter(a[0].geometry.location);v.setMap(null);v=new google.maps.Marker({map:u,position:a[0].geometry.location,title:h})}else{e=b;switch(e){case'ZERO_RESULTS':f='No results for address '+h;break;case'OVER_QUERY_LIMIT':f='Usage limits for geolocation, see http://code.google.com/intl/fr/apis/maps/documentation/geocoding/index.html#Limits';break;default:f='Geocode was not successful for address '+h;break}$('#'+s).show();v.setMap(null)}$('#'+q).hide();n.callback(h,c,d,e,f)})}function geoyahoo(h){$('#'+q).show();$('#'+r).hide();$('#'+s).hide();$.ajax({dataType:'jsonp',url:'http://where.yahooapis.com/geocode',data:'location='+h+'&flags=J&appid=[yourappidhere]',success:function(a,b,c){var d;var e;var f;var g;var f=a.ResultSet.Error;if(f==0){d=a.ResultSet.Results[0].latitude;e=a.ResultSet.Results[0].longitude;$('#'+r).show();$('#'+p).val(h)}else{g=a.ResultSet.ErrorMessage;$('#'+s).show()}$('#'+q).hide();n.callback(h,d,e,f,g)}})}function geotwitter(k){$('#'+q).show();$('#'+r).hide();$('#'+s).hide();$.ajax({dataType:'jsonp',url:'http://api.twitter.com/1/geo/search.json',data:'query='+k,success:function(a,b,c){var d;var e;var f;var g;var h=a.result.places[0].name;var i=a.result.places[0].attributes.street_address;var j=a.result.places[0].country;k=h;if(k!=undefined&&i!=undefined)k=k+', '+i;if(k!=undefined&&j!=undefined)k=k+', '+j;d=a.result.places[0].bounding_box.coordinates[0][0][1];e=a.result.places[0].bounding_box.coordinates[0][0][0];$('#'+r).show();$('#'+p).val(k);$('#'+q).hide();n.callback(k,d,e,f,g)}})}return $(this)}})(jQuery);
