var observable = require("data/observable");
var observableArray = require("data/observable-array");
var frame = require("ui/frame");
var view = require("ui/core/view");

var scrapbook;

function scrapbookPageModel(){
    var model = new observable.Observable({ 
        title: "null", 
        birthDate: null, 
        gender: null, 
        image: null,
        lat: null,
        long: null
    });

    model.calcAge = function(birthDate){
        var now = Date.now();
        var diff = Math.abs(now - birthDate) / 1000 / 31536000;

        return diff.toFixed(1);
    };
    
    return model;
}

exports.onLoaded = function(args) {
    var page = args.object;
    var scrapbook = new observable.Observable({
        pages: new observableArray.ObservableArray(),
        selectedPage: null
    });

    page.bindingContext = scrapbook;
};

exports.onAddTap = function(args) {
    var page = args.object;
    var scrapbook = page.bindingContext;
    
    scrapbook.pages.push(new scrapbookPageModel());
};

function onItemTap (args) {
    var page = args.object;
    var scrapbook = page.bindingContext;
    scrapbook.set("selectedPage", scrapbook.pages.getItem(args.index));
    console.log(JSON.stringify(scrapbook.selectedPage));
}

exports.test = function (args) {
    console.log("testing...");
};
@@ -796,24 +796,559 @@
 ll%0A    %7D);%0A%0A
+    var pages = fileSystemService.fileSystemService.getPages();%0A%0A    if (pages.length !== 0) %7B%0A        pages.forEach(function (item) %7B%0A            var model = new scrapbookPageModel();%0A            %0A            model.id = item.id;%0A            model.title = item.title;%0A            model.gender = item.gender;%0A            model.birthDate = item.birthDate;%0A            model.image = item.image;%0A            model.lat = item.lat;%0A            model.long = item.long;%0A            %0A            scrapbook.pages.push(model);%0A        %7D);%0A    %7D%0A%0A
     page.bin

update tablet on load to get data from filesystem