/*
 * wq.app 0.8.1-dev - wq/photos.js
 * Helpers for working with Cordova photo library
 * (c) 2012-2015, S. Andrew Sheppard
 * https://wq.io/license
 */

/* global Camera */

define(['jquery', './template', './store', './spinner'],
function($, tmpl, ds, spin) {

var LOCALFORAGE_PREFIX = '__lfsc__:blob~~local_forage_type~image/jpeg~';

var photos = {};

photos.init = function() {
    tmpl.setDefault('image_url', function() {
        try {
            return this.body && _getUrl(this.body);
        } catch (e) {
            // Image will be blank, but at least template won't crash
        }
    });
};

photos.preview = function(imgid, file) {
    $('#'+imgid).attr('src', _getUrl(file));
};

function _getUrl(file) {
    var URL = window.URL || window.webkitURL;
    return URL && URL.createObjectURL(file);
}

photos.take = function(input, preview) {
    var options = $.extend({
        sourceType: Camera.PictureSourceType.CAMERA,
        correctOrientation: true,
        saveToPhotoAlbum: true
    }, _defaults);
    _start(options, input, preview);
};

photos.pick = function(input, preview) {
    var options = $.extend({
        sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM
    }, _defaults);
    _start(options, input, preview);
};

function _start(options, input, preview) {
    navigator.camera.getPicture(
        function(data) {
            load(data, input, preview);
        },
        function(msg) {
            error(msg);
        },
    options);
}

function load(data, input, preview) {
    spin.start('Loading image...');
    // localforageSerializer is defined by localstorage.js, but might not be
    // loaded already - so use async require.
    require(['localforageSerializer'], function(serializer) {
        var blob = serializer.deserialize(LOCALFORAGE_PREFIX + data);
        var number = Math.round(Math.random() * 1e10);
        var name = $('#' + input).val() || ('photo' + number + '.jpg');
        var file = {
            'name': name,
            'type': 'image/jpeg',
            'body': blob
        };
        ds.set(name, file).then(function() {
            $('#' + input).val(name);
            spin.stop();
            photos.preview(preview, blob);
        });
    });
}

function error(msg) {
    spin.start("Error Loading Image: " + msg, 1.5, {
        "theme": $.mobile.pageLoadErrorMessageTheme,
        "textonly": true
    });
}

var _defaults = {
    quality: 75,
    destinationType: 0 //Camera.DestinationType.DATA_URL
};

return photos;
});

@@ -186,16 +186,33 @@
 jquery',
+ 'jquery.mobile',
  './temp
@@ -254,16 +254,21 @@
 ction($,
+ jqm,
  tmpl, d
@@ -635,41 +635,541 @@
 tos.
-preview = function(imgid, file) %7B
+run = function() %7B%0A    var $page = jqm.activePage;%0A    $page.find('input%5Btype=file%5D').on('change', photos.preview);%0A    $page.find('button%5Bdata-wq-action=take%5D').on('click', photos.take);%0A    $page.find('button%5Bdata-wq-action=pick%5D').on('click', photos.pick);%0A%7D;%0A%0Aphotos.preview = function(imgid, file) %7B%0A    if (typeof imgid !== 'string' && !file) %7B%0A        imgid = $(this).data('wq-preview');%0A        if (!imgid) %7B%0A            return;%0A        %7D%0A        file = this.files%5B0%5D;%0A        if (!file) %7B%0A            return;%0A        %7D%0A    %7D
 %0A   
@@ -1542,33 +1542,44 @@
 lts);%0A    _start
-(
+.call(this, 
 options, input, 
@@ -1748,25 +1748,36 @@
 ;%0A    _start
-(
+.call(this, 
 options, inp
@@ -1833,24 +1833,244 @@
  preview) %7B%0A
+    if (typeof input !== 'string' && !preview) %7B%0A        input = $(this).data('wq-input');%0A        if (!input) %7B%0A            return;%0A        %7D%0A        preview = jqm.activePage.find('#' + input).data('wq-preview');%0A    %7D%0A
     navigato
@@ -2923,16 +2923,47 @@
 stop();%0A
+            if (preview) %7B%0A    
         
@@ -2997,16 +2997,30 @@
  blob);%0A
+            %7D%0A
         
@@ -3130,16 +3130,11 @@
 e%22: 
-$.mobile
+jqm
 .pag

make wq/photos.js work as a wq/app.js plugin