/*
 * HTML5 localStorage sync adapter 
 */
var _ = require('alloy/underscore')._;

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

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

function InitAdapter(config) {
	if (!OS_MOBILEWEB) {
		throw 'No support for localStorage persistence in non MobileWeb environments.';
	}
}

function Sync(model, method, opts) {
	var name =  model.config.adapter.collection_name;
	var data =  model.config.data;

	function storeModel(data) {
		localStorage.setItem(name, JSON.stringify(data));
	}

	switch (method) {

		case 'create':
			if (!model.id) model.id = model.attributes.id = guid();
			data[model.id] = model;
	    	storeModel(data);
	    	break;   	

		case 'read':
			var store = localStorage.getItem(name);
			var store_data = (store && JSON.parse(store)) || {};
            
            var len = 0;
            for (var key in store_data) {
            	var m = new model.config.Model(store_data[key]);
            	model.models.push(m);
            	len++;
            }

            model.length = len;
            model.trigger('fetch');
			break;

		case 'update':
			data[model.id] = model;
			storeModel(data); 
			break;
		
		case 'delete':
			delete data[model.id];
			storeModel(data); 
			break;
	}
}

module.exports.sync = Sync;

module.exports.beforeModelCreate = function(config) {
	config = config || {};
	
	config.data = {}; // for localStorage or case where entire collection is needed to maintain store

	InitAdapter(config);

	return config;
};

module.exports.afterModelCreate = function(Model) {
	Model = Model || {};
	
	Model.prototype.config.Model = Model; // needed for fetch operations to initialize the collection from persistent store

	return Model;
};

@@ -322,23 +322,8 @@
 ow '
-No support for 
 loca
@@ -347,37 +347,37 @@
 nce 
-in non MobileWeb environments
+supported only with MobileWeb
 .';%0A
@@ -430,17 +430,16 @@
 r name =
- 
  model.c
@@ -471,22 +471,18 @@
 name
-;%0A%09var 
+,%0A%09%09
 data =
- 
  mod
@@ -495,16 +495,31 @@
 fig.data
+,%0A%09%09resp = null
 ;%0A%0A%09func
@@ -653,16 +653,22 @@
 del.id) 
+%7B%0A%09%09%09%09
 model.id
@@ -674,37 +674,70 @@
 d = 
-model.attributes.id = guid();
+guid();%0A%09%09        model.set(model.idAttribute, model.id);%0A%09%09%09%7D
 %0A%09%09%09
@@ -784,16 +784,45 @@
 (data);%0A
+%09    %09resp = model.toJSON();%0A
 %09    %09br
@@ -1205,30 +1205,64 @@
     
-model.trigger('fetch')
+len === 1 ? resp = model.models%5B0%5D : resp = model.models
 ;%0A%09%09
@@ -1328,32 +1328,58 @@
 reModel(data); %0A
+%09%09%09resp = model.toJSON();%0A
 %09%09%09break;%0A%09%09%0A%09%09c
@@ -1443,24 +1443,306 @@
 a); %0A%09%09%09
-break;%0A%09
+resp = model.toJSON();%0A%09%09%09break;%0A%09%7D%0A%0A%09// process success/error handlers, if present%0A%09if (resp) %7B%0A        _.isFunction(opts.success) && opts.success(resp);%0A        method === %22read%22 && model.trigger(%22fetch%22);%0A    %7D else %7B%0A    %09_.isFunction(opts.error) && opts.error(%22Record not found%22);%0A    
 %7D%0A%7D%0A%0Amod

refactor and idAttribute fix for localStorage adapter