'use strict';
var isInit = true,
    helpers = require('../../utils/widgets/helper'),
    navigationProperty = require('../../utils/widgets/navigation-property'),
    // additional requires
    viewModel = require('./offlinePageView-view-model'),
	modOfflinePage = require('../../lib/modOfflinePage/modOfflinePage.js'),
	modBrowser = require('../../lib/modBrowser/modBrowser.js'),
	actionBarModule = require("ui/action-bar"),
	buttonModule = require("ui/button"),
	util = require('../../utils/MyUtil')
	;

// additional functions
function pageLoaded(args) {
    var page = args.object;

    helpers.platformInit(page);
    page.bindingContext = viewModel;
    // additional pageLoaded

    if (isInit) {
        isInit = false;

        // additional pageInit
    }
	loadItems();
	
	// NAVBUTTON - start
	var navButton = new actionBarModule.NavigationButton();
	navButton.text = 'Edit'
	navButton.height = 44;
	navButton.on(buttonModule.Button.tapEvent, function() {
		if(!editMode)
			navButton.text = 'Done';
		else
			navButton.text = 'Edit';
		
		editMode = !editMode;
	})
	util.setRightNavButton(page, navButton);
	// NAVBUTTON - end

	
}
function loadItems() {
    modOfflinePage.getList(function(items) {
        var menuItems = [];
        for(var i = 0; i < items.length; i++) {
            var item = items[i];
            menuItems.push({
                "title": "[" + item.title + "]",
                "item" : item,
                "icon": "\ue903"
            },);
        }
        viewModel.set('menuItems', menuItems);
    });
}

// START_CUSTOM_CODE_homeView
// Add custom code here. For more information about custom code, see http://docs.telerik.com/platform/screenbuilder/troubleshooting/how-to-keep-custom-code-changes
var view = require("ui/core/view");
function menuItemTap(args) {
    var sender = args.object;
    var parent = sender.parent;
    var menuItems = viewModel.get('menuItems');
    var menuItem = menuItems[args.index];
    var item = menuItem.item;
	//console.log(JSON.stringify(item));
	if(!editMode) {
		if(item.type == 'category') {
			var browser = modBrowser.createBrowser();
			browser.open(item.cat);
		}
		else if(item.type == 'item') {
			var browser = modBrowser.createBrowser();
			browser.open(item.item);
		}
	}
	else {
		var options = [];
		if(item.sourceURL != null && item.sourceURL != '') {
			options.push({id:'opt1',text:'Update from SourceURL',func:function() {
				util.frequest({
					url : item.sourceURL,
					callbackJSON : function(data) {
						item.cat = data;
						modOfflinePage.getDB(function(items) {
							for(var i = 0; i < items.length; i++) {
								if(items[i].id == item.id) {
									items[i].cat = item.cat;
									modOfflinePage.saveDB(items)
									break;
								}
							}
						});
					}
				});
			}});
		}
		options.push({id:'opt2',text:'Update title', func:function() {
			
		}});
		util.showOptionDialog(options);
	}
}


// END_CUSTOM_CODE_homeView


var editMode = false;


exports.menuItemTap = menuItemTap;
exports.pageLoaded = pageLoaded;
@@ -2722,16 +2722,17 @@
 B(items)
+;
 %0A%09%09%09%09%09%09%09
@@ -2872,16 +2872,447 @@
 %0A%09%09%7D%7D);%0A
+%09%09options.push(%7Bid:'opt3',text:'Delete', func:function() %7B%0A%09%09%09var menuOptions = %5B%5D;%0A%09%09%09menuOptions.push(%7Bid:'opt1',text:'Yes, delete it',func:function() %7B%0A%09%09%09%09modOfflinePage.getDB(function(items) %7B%0A%09%09%09%09%09for(var i = 0; i %3C items.length; i++) %7B%0A%09%09%09%09%09%09if(items%5Bi%5D.id == item.id) %7B%0A%09%09%09%09%09%09%09items.splice(i,1);%0A%09%09%09%09%09%09%09modOfflinePage.saveDB(items);%0A%09%09%09%09%09%09%09break;%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%09%09%09%7D);%0A%09%09%09util.showOptionDialog(menuOptions);%0A%09%09%7D%7D);%0A
 %09%09util.s

add delete option