//"use strict";
console.log('context.js loaded');
var data = [];
var GUID;
var CAPTUREDATA = true;
var SEQ = 0;

// Timer stuff  ----------------------------------------------
function captureTimer() {
    if (CAPTUREDATA) {
        if (SEQ < 20) {
            setTimeout("captureHandler()", 1500);
        }
        //console.log('Capture timer initiated');
    }
}

function submitTimer() {
    if (CAPTUREDATA) {
        if (SEQ < 20) {
            setTimeout("submitHandler()", 6000);
        }
        //console.log('Submit timer initiated');
    }
}


function captureHandler() {
    geo_position_js.getCurrentPosition(successCallback, errorCallback);
    captureTimer();
}


// Send the data to the server
function submitHandler() {
    var encodedData = 'datapoints=' + encodeURIComponent(JSON.stringify(data)) + '&GUID=' + GUID;
    console.log('Submit handler, data is ', encodedData);

    if (XHRSupported) {
        try {
            console.log('encodedData=', encodedData);
            xmlhttp = new XMLHttpRequest();
            var url = 'http://cremin.mobi/context/context.py/post/';
            xmlhttp.open('POST', url);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send(encodedData);
        } catch(e) {
            console.log('Problem submitting data', e);
        }
    }
    submitTimer();
} 


function successCallback(p) {
    //alert('lat='+p.coords.latitude.toFixed(2)+';lon='+p.coords.longitude.toFixed(2));
    var datapoint = {};
    datapoint.lat = p.coords.latitude.toFixed(8);
    datapoint.lon = p.coords.longitude.toFixed(8);
    datapoint.orientation = window.orientation;
    datapoint.seq = SEQ;

    var d = new Date();
    datapoint.datetime = Math.round(d.getTime() / 100);
    console.log('Data point captured', datapoint.datetime, datapoint);
    data.push(datapoint);
    SEQ += 1; 
    //console.log('data is now', data);
}


function errorCallback(p) {
    //alert('error=' + p.code);
    console.log('GEO error=' + p.code);
}



// Some cookie handling utilities ----------------------------------------------

function checkCookie(cName) {
    var val = getCookie(cName);
    if (val != null && val != "") {
        return val;
    } else {
        return false;
    }
}

function getCookie(cName) {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g, '');
        if (x==cName) {
            return unescape(y);
        }
    }
}


function setCookie(cName, value, exdays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var cValue = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=cName + "=" + cValue;
}



function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}



function stopCapture() {
    CAPTUREDATA = false;
    console.log('Stopping data capture');
}


// Point to native JSON object or new object if not available
var JSON = JSON || {};

// check if XHR is supported
var XHRSupported = (typeof XMLHttpRequest === 'function') || (typeof XMLHttpRequest === 'object');

// Implement JSON.stringify serialization, if neccessary
JSON.stringify = JSON.stringify || function (obj) {
    var t = typeof (obj);
    if (t !== "object" || obj === null) {
        // simple data type
        if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    }
    else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n];t = typeof(v);
            if (t == "string") v = '"'+v+'"';
            else if (t == "object" && v !== null) v = JSON.stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
};


/*!
 * geo-location-javascript v0.4.3
 * http://code.google.com/p/geo-location-javascript/
 *
 * Copyright (c) 2009 Stan Wiechers
 * Licensed under the MIT licenses.
 *
 * Revision: $Rev: 68 $: 
 * Author: $Author: whoisstan $:
 * Date: $Date: 2010-02-15 13:42:19 +0100 (Mon, 15 Feb 2010) $:    
 */
var bb_successCallback;
var bb_errorCallback;
var bb_blackberryTimeout_id=-1;

function handleBlackBerryLocationTimeout() {
	if(bb_blackberryTimeout_id!=-1)
	{
		bb_errorCallback({message:"Timeout error", code:3});
	}
}

function handleBlackBerryLocation() {
    clearTimeout(bb_blackberryTimeout_id);
    bb_blackberryTimeout_id=-1;
    if (bb_successCallback && bb_errorCallback) {
        if(blackberry.location.latitude==0 && blackberry.location.longitude==0) {
            //http://dev.w3.org/geo/api/spec-source.html#position_unavailable_error
            //POSITION_UNAVAILABLE (numeric value 2)
            bb_errorCallback({message:"Position unavailable", code:2});
        } else {  
            var timestamp=null;
            //only available with 4.6 and later
            //http://na.blackberry.com/eng/deliverables/8861/blackberry_location_568404_11.jsp
            if (blackberry.location.timestamp) {
                timestamp=new Date(blackberry.location.timestamp);
                }
            bb_successCallback({timestamp:timestamp, coords: {latitude:blackberry.location.latitude,longitude:blackberry.location.longitude}});
            }
        //since blackberry.location.removeLocationUpdate();
        //is not working as described http://na.blackberry.com/eng/deliverables/8861/blackberry_location_removeLocationUpdate_568409_11.jsp
        //the callback are set to null to indicate that the job is done

        bb_successCallback = null;
        bb_errorCallback = null;
    }
}

var geo_position_js=function() {

    var pub = {};
    var provider=null;

    pub.getCurrentPosition = function(successCallback,errorCallback,options) {
        provider.getCurrentPosition(successCallback, errorCallback,options);
    }

    pub.init = function() {			
        try {
            if (typeof(geo_position_js_simulator)!="undefined") {
                provider=geo_position_js_simulator;
            }
            else if (typeof(bondi)!="undefined" && typeof(bondi.geolocation)!="undefined") {
                provider=bondi.geolocation;
            }
            else if (typeof(navigator.geolocation)!="undefined") {
                provider=navigator.geolocation;
                pub.getCurrentPosition = function(successCallback, errorCallback, options) {
                    function _successCallback(p) {
                        //for mozilla geode,it returns the coordinates slightly differently
                        if(typeof(p.latitude)!="undefined") {
                            successCallback({timestamp:p.timestamp, coords: {latitude:p.latitude,longitude:p.longitude}});
                        } else {
                            successCallback(p);
                        }
                    }
                    provider.getCurrentPosition(_successCallback,errorCallback,options);
                }
            }
            else if(typeof(window.google)!="undefined" && typeof(google.gears)!="undefined") {
                provider=google.gears.factory.create('beta.geolocation');
            }
            else if ( typeof(Mojo) !="undefined" && typeof(Mojo.Service.Request)!="Mojo.Service.Request") {
                provider=true;
                pub.getCurrentPosition = function(successCallback, errorCallback, options) {

                    parameters={};
                    if(options) {
                         //http://developer.palm.com/index.php?option=com_content&view=article&id=1673#GPS-getCurrentPosition
                         if (options.enableHighAccuracy && options.enableHighAccuracy==true) {
                            parameters.accuracy=1;
                         }
                         if (options.maximumAge) {
                            parameters.maximumAge=options.maximumAge;
                         }
                         if (options.responseTime) {
                            if(options.responseTime<5) {
                                parameters.responseTime=1;
                            }
                        else if (options.responseTime<20) {
                            parameters.responseTime=2;
                        } else {
                            parameters.timeout=3;
                        }
                     }
                }


                 r=new Mojo.Service.Request('palm://com.palm.location', {
                        method:"getCurrentPosition",
                            parameters:parameters,
                            onSuccess: function(p){successCallback({timestamp:p.timestamp, coords: {latitude:p.latitude, longitude:p.longitude,heading:p.heading}});},
                            onFailure: function(e){
                                if (e.errorCode==1) {
                                    errorCallback({code:3,message:"Timeout"});
                                }
                                else if (e.errorCode==2) {
                                    errorCallback({code:2,message:"Position Unavailable"});
                                }
                                else {
                                    errorCallback({code:0,message:"Unknown Error: webOS-code"+errorCode});
                                }
                            }
                        });
                }

        }
        else if (typeof(device)!="undefined" && typeof(device.getServiceObject)!="undefined") {
            provider=device.getServiceObject("Service.Location", "ILocation");

            //override default method implementation
            pub.getCurrentPosition = function(successCallback, errorCallback, options) {
                function callback(transId, eventCode, result) {
                    if (eventCode == 4) {
                        errorCallback({message:"Position unavailable", code:2});
                    } else {
                        //no timestamp of location given?
                        successCallback({timestamp:null, coords: {latitude:result.ReturnValue.Latitude, longitude:result.ReturnValue.Longitude, altitude:result.ReturnValue.Altitude,heading:result.ReturnValue.Heading}});
                    }
                }
                //location criteria
                var criteria = new Object();
                criteria.LocationInformationClass = "BasicLocationInformation";
                //make the call
                provider.ILocation.GetLocation(criteria,callback);
            }
        }
        else if(typeof(window.blackberry)!="undefined" && blackberry.location.GPSSupported)
        {

            // set to autonomous mode
            if(typeof(blackberry.location.setAidMode)=="undefined") {
                return false;									
            }
            blackberry.location.setAidMode(2);
            //override default method implementation
            pub.getCurrentPosition = function(successCallback,errorCallback,options) {
                //alert(parseFloat(navigator.appVersion));
                //passing over callbacks as parameter didn't work consistently
                //in the onLocationUpdate method, thats why they have to be set
                //outside
                bb_successCallback=successCallback;
                bb_errorCallback=errorCallback;
                //function needs to be a string according to
                //http://www.tonybunce.com/2008/05/08/Blackberry-Browser-Amp-GPS.aspx
                if(options['timeout'])  {
                    bb_blackberryTimeout_id=setTimeout("handleBlackBerryLocationTimeout()",options['timeout']);
                } else //default timeout when none is given to prevent a hanging script
            {
                bb_blackberryTimeout_id=setTimeout("handleBlackBerryLocationTimeout()",60000);
            }										
                blackberry.location.onLocationUpdate("handleBlackBerryLocation()");
                blackberry.location.refreshLocation();
            }
            provider=blackberry.location;				
        }
}
    catch (e){ 
        alert("error="+e);
        if(typeof(console)!="undefined") {
            console.log(e);
        }
        return false;
    }
    return  provider!=null;
}


    return pub;
}();


geo_position_js.init();

GUID = checkCookie('GUID')
if (!GUID) {
    console.log('No GUID cookie found');
    GUID = guid();
    console.log('Generated new GUID', GUID);
    setCookie('GUID', GUID, 31);
    console.log('GUID stored in cookie');
} else {
    console.log('GUID loaded from cookie');
} 


console.log('GUID is', GUID);
captureTimer();
submitTimer();


