// Utils

try {
    $ // Test if it is alread used
} catch(e) {
    $ = function(id){
        return document.getElementById(id)
    };
}

// Acceleration Handling

var accelX = 0;
var accelY = 0;
var accelZ = 0;

function gotAcceleration(x,y,z){
	x = eval(x);
	y = eval(y);
	z = eval(z);
	if ((!isNaN(x)) && (!isNaN(y)) && (!isNaN(z))) {
		accelX = x;
		accelY = y;
		accelZ = z;
	}
	return x + " " + y + " " + z;
}

//Device token
var deviceToken;
function gotDeviceToken(token){

deviceToken=token;

}



// A little more abstract

var DEBUG = true;
if (!window.console || !DEBUG) {
    console = {
        log: function(){
        },
        error: function(){
        }
    }
}

var Device = {  
    available: false,
    model: "",
    version: "",
	  uuid: "",
    isIPhone: null,
    isIPod: null,
    isAndroid: null, 

    init: function(model, version) {
        try {
            if (window.DroidGap.exists() )
            {                
                Device.available = true;
                Device.isAndroid = true;
                Device.uuid = window.DroidGap.getUuid();
                Device.geoVersion = window.DroidGap.getVersion();
            }
            else
            {          
                Device.available = __geo;
                Device.model = __geo_device_model;
                Device.version = __geo_device_version;
                Device.geoVersion = __geo_version;
			          Device.uuid = __geo_device_uniqueid;
            }
        } catch(e) {            
            alert("GeoID application is required\nPlease download it from Apple iTune")
        } 
    },
   
    exec: function(command) {
        if (Device.available) {
            try {
                document.location = "geo:" + command;
            } catch(e) {
                console.log("Command '" + command + "' has not been executed, because of exception: " + e);
                alert("Error executing command '" + command + "'.")
            }
        }
    },

    Location: {
        // available: true,
        
        lon: null,
        lat: null,
        //
        speed: null,
        altitude: null,
        heading: null,
        
        //
        callback: null,
        
        init: function() {
            if (Device.isAndroid)
            {
               /*
                * TO-DO: Add support for multiple location providers
                * GPS is only one of many ways to do this
                */
               window.DroidGap.getLocation("gps"); 
            }
            else
            {
                Device.exec("getloc");
            }
        },
        
        set: function(lat, lon, speed, altitude, heading) {
            Device.Location.lat = lat;
            Device.Location.lon = lon;
              Device.Location.speed = speed;
            Device.Location.altitude = altitude;
            Device.Location.heading = heading;
            
            if(Device.Location.callback != null) {
                Device.Location.callback(lat, lon, speed, altitude, heading)
                Device.Location.callback = null;
            }
        },

        wait: function(func) {
            Device.Location.callback = func
            Device.exec("getloc");
        }
        
    },

    Image: {

        //available: true,

		callback: null,
		
        getFromPhotoLibrary: function() {
            return Device.exec("getphoto" + ":" + Device.Image.callback)
        },
        
        getFromCamera: function() {
            return Device.exec("getphoto" + ":" + Device.Image.callback)
        },
        
        getFromSavedPhotosAlbum: function() {
            return Device.exec("getphoto" + ":" + Device.Image.callback)
        }

    },

    vibrate: function() {
        if (Device.isAndroid)
        {
          window.DroidGap.vibrate(10);
        }
        else
        {
          return Device.exec("vibrate");
        }
    }

}

function gotLocation(lat, lon, speed, altitude, heading) {
    return Device.Location.set(lat, lon, speed, altitude, heading)
}
