var VGEO = function() {
 return {
   init : function() {
  try {
    var url = "http://veloroutes.org/m/";
   var icon = "http://veloroutes.org/favicon.ico";
   var name = "My Elevation by veloroutes.org";
   var msg = "Google Gears is required by this veloroutes.org application.";
   this.eleMsg= "feet";
   this.highAcc = false; //high accuracy
   VGEO.isApple = VGEO.getIsApple();
   VGEO.isBerry = VGEO.getIsBerry();
    if (!VGEO.isBerry && !VGEO.isApple && (!window.google || !google.gears)) {
	var agent = VGEO.getUA();
	var type = '';
	if(agent){if(agent.match(/android|linux/i)){type='linux'}
	 else if(agent.match(/mac[\s\S]+safari/i)){type='mac-safari'}
         else if(agent.match(/mac[\s\S]+firefox/i)){type='mac-firefox'}
         else if(agent.match(/iemobile/i)){type='wince'}
	 else if(agent.match(/windows/i)){type='win'}
	}
	
	VGEO.doUrch('/outgoing/googlegears/'+type);
	if(type != ''){
		location.href="http://gears.google.com/?platform="+type;
	}
	else {
        location.href = "http://gears.google.com/?action=install" +
         "&return="+escape(url) + "&icon_src="+escape(icon) + "&name="+escape(name);//+"&message="+escape(msg);
	}
    }

    if(VGEO.isApple) {
      var ele = VGEO.getElem('acc');
      if(ele){ele.disabled=true;}
      VGEO.geo = navigator.geolocation ? navigator.geolocation : window.geolocation;
    }
    else if (VGEO.isBerry) {
      var ele = VGEO.getElem('acc');
      if(ele){ele.disabled=true;}
    }
    else if(!this.geo && google && google.gears){
	VGEO.geo = google.gears.factory.create('beta.geolocation');
    }
  }catch(e){alert("ERROR: " + e); VGEO.doUrch('/m/ele/init/error');}
}, //init
toggleAcc : function() {
   this.highAcc = !this.highAcc;
},
getIsBerry: function() {
 return(window.blackberry);
},
getIsApple: function() {
  var ret = false;
  var agent = VGEO.getUA();
  if(agent.match(/iphone/i) || agent.match(/ipod/i)) { ret = true; }
  return ret;
},
getUA:function(){
 if(navigator){return navigator.userAgent}
 return '';
},
doUrch : function(url) {
 urchinTracker(url);
},
getMyElevation : function() {
   try {
    var geo = VGEO.geo;
     var fb = VGEO.getElem("feedback");
    if(fb){fb.innerHTML="Acquiring location...";}

    //if(!VGEO.isBerry && !VGEO.isApple && !geo){alert("Hmm, there's an issue with Google Gears. Is it installed?");return;}
    //else if(!VGEO.isBerry && !geo){alert("Hmm, there's a geolocation issue on your device.");return;}

    function handleError(err) {
      alert("Error during geolocation: " + err.message);
    }

    function updateLocation(pos) {
      if(fb){fb.innerHTML="Acquiring elevation...";}
      if(VGEO.isBerry) { 
        if(!blackberry.location.latitude || !blackberry.location.longitude) { alert("Error: could not get GPS location."); }
        VGEO.getElevation(blackberry.location.latitude+","+blackberry.location.longitude,'-',150);
        blackberry.location.removeLocationUpdate();
	return;
      }
      var lat = pos.coords.latitude;
      var lon = pos.coords.longitude;
      VGEO.getElevation(lat+","+lon,pos.gearsAddress,pos.coords.accuracy);
     }
 
    var opts = {gearsRequestAddress:true,enableHighAccuracy:this.highAcc}; 
    if(VGEO.isApple){
     if(geo){
      geo.getCurrentPosition(updateLocation, handleError);
     }
    } else if (VGEO.isBerry) {
      if(!blackberry.location.GPSSupported) { alert("Error: Your BlackBerry's GPS functionality must be enabled.");return; }
       blackberry.location.onLocationUpdate(updateLocation);
       blackberry.location.setAidMode(2);
       blackberry.location.refreshLocation();
    } else {
     if(geo){
     geo.getCurrentPosition(updateLocation, handleError,opts);
     }
    }
  }
  catch(e){alert("Error updating location: " + e); VGEO.doUrch('/m/ele/updateLocation/error');}
},
getElevation : function(pos,addr,accuracy) {
  try{
    if(!pos){return;}
    var request = VGEO.getXmlReq();
    request.open('GET', '/cgi-bin/get_elevation.cgi?m=1&latLong='+pos);
    request.onreadystatechange = function() {
    if (request.readyState == 4) {
	var fb = VGEO.getElem("feedback");
	if(fb){fb.innerHTML='';}
        var resp = request.responseText;
        if(resp) {
	resp = parseInt(resp);
         VGEO.setElevation(resp,pos,addr,accuracy);
        }
    }};

    request.send();
   }catch(e){alert("Error getting elevation: " + e);VGEO.doUrch('/m/ele/getEle/error');}
},
getXmlReq : function() {
  if(VGEO.isBerry || VGEO.isApple){return new XMLHttpRequest();}
  return google.gears.factory.create('beta.httprequest');
},
formatMsg : function(ele) {
 return ele + " " + this.eleMsg;
},
setElevation:function(ele,loc,addr,accuracy) {
   try {
        var elem = VGEO.getElem("elevation");
        if(elem){elem.innerHTML=VGEO.formatMsg(ele);}

	if(loc) {     
	 elem = VGEO.getElem("hid");
	 elem.innerHTML = '<iframe src="/m/'+loc+'/static" border="no" width="280" height="270"></iframe>';
        }

 	elem = VGEO.getElem("location");
        if(addr) { elem.innerHTML = addr.streetNumber + " " + addr.street + ", " + addr.city + ", " + addr.region + " ("+loc+")" }
        else {
	 if(elem){elem.innerHTML=loc;}
        }

        elem = VGEO.getElem("accuracy");
        if(accuracy != '') {
	 elem.innerHTML = VGEO.translateAcc(accuracy) + " (" + accuracy + ")";
        }
  }catch(e){alert("Error: " + e);VGEO.doUrch('/m/ele/setElevation/error');}
},
translateAcc : function(acc) {
 acc = parseInt(acc);
 if(!acc || acc <= 0){return "None"}
 if(acc < 150) { return "Poor" }
 if(acc >= 150) { return "Good" }
 return '';
},
isDef : function(type) {
  return type != 'undefined' && type != 'unknown';
},
getElem : function(id) {
  if (VGEO.isDef(typeof document.getElementById)) {
     return document.getElementById(id);
  } else if (VGEO.isDef(typeof document.all)) {
     return document.all[id];
  } else {
    throw new Error("Can not find a method to locate DOM element.");
    return null;
  }
}
}//return
}();//VGEO
//eof
