"use strict";
/**
 *
 */
App.ApplicationController = Ember.ArrayController.extend(
{
  needs: ['user', 'application', 'category', 'products', 'pairing',
          'action', 'program', 'sync'],

  glomeid: false,
  password: '',
  loggedin: false,
  inProgress: false,
  notification: false,
  token: false,

  actions:
  {
    /**
     * should be called from the application route before model
     */
    connect: function()
    {
      console.log('here we start?');
      if (! window.localStorage.getItem('loggedin'))
      {
        window.localStorage.setItem('loggedin', false);
      }

      if (window.localStorage.getItem('glomeid') && typeof window.localStorage.getItem('glomeid') != 'undefined')
      {
        console.log('glomeid exists: ' + window.localStorage.getItem('glomeid'));
        this.set('glomeid', window.localStorage.getItem('glomeid'));
        console.log('lets login');
        this.get('controllers.user').send('auth', this.get('glomeid'), this.get('password'));
      }
      else
      {
        if (App.apiKey)
        {
          this.send('generateGlomeId');
        }
        else
        {
          console.log('It is not a Glome enabled application.');
        }
      }
    },
    loadCategories: function(category)
    {
      var self = this;
      var controller = this.get('controllers.products');
      // load categories
      controller.set('categories', this.store.find('category', { display: 'tree', filter: 'all', maxlevel: 1 }));
      // map out categories
      controller.get('categories').then(function(data)
      {
        data.content.forEach(function(item, index, enumerable)
        {
          controller.get('categoryMap')[item.get('urlName')] = item;
          item.get('subcategories').forEach(function(_item, _index, _enum)
          {
            controller.get('categoryMap')[_item.get('urlName')] = _item;
          }, item);
        });

        console.log('category: ' + category);
        if (category)
        {
          controller.set('currentCategory', controller.get('categoryMap')[category]);
        }

        console.log('loaded categories');

        var prevT = self.get('controllers.application').get('previousTransition');
        if (prevT)
        {
          console.log('go to');
          console.log('target:');
          console.log(prevT.targetName);
          console.log('params:');
          console.log(prevT.params);

          switch (prevT.targetName)
          {
            case 'index':
              self.transitionToRoute(prevT.targetName);
              break;
            case 'products.index':
              self.transitionToRoute(prevT.targetName, prevT.params.category);
              break;
            case 'products.show':
              self.transitionToRoute(prevT.targetName, prevT.params.category, prevT.params.product_id);
              break;
          }
        }
      });

      return controller.get('categories');
    },
    /**
     * Shows a notification message
     */
    notify: function(message)
    {
      this.set('notification', message);
      Ember.$('div.server.notifications').fadeIn(400, function()
      {
        Ember.$('div.server.notifications').fadeOut(5000);
      });
    },
    /**
     * Set some variables used in all view
     */
    setGlobals: function()
    {
      console.log('start here ---');
      this.set('inProgress', false);
      this.set('glomeid', window.localStorage.getItem('glomeid'));
      this.set('loggedin', window.localStorage.getItem('loggedin') == 'true');
      console.log('globals: glomeid: ' + this.get('glomeid') + ', loggedin: ' + this.get('loggedin'));
    },
    open: function()
    {
      console.log(this);
    },
    /**
     * Requests a new Glome ID from the server
     */
    generateGlomeId: function()
    {
      console.log('user:generateGlomeId');
      if (! App.apiKey || App.apiKey == '') return;
      var self = this;
      var url = App.apiHost + App.generateGlomeIdPost;
      var data =
      {
        application:
        {
          uid: App.apiHost,
          apikey: App.apiKey
        }
      };

      return Ember.$.ajax(
      {
        url: url,
        data: data,
        type: 'post',
        dataType: 'json',
        xhrFields: { withCredentials: true }
      }).then(function(data)
      {
        console.log('glome id created: ' + data.glomeid);
        window.localStorage.setItem('glomeid', data.glomeid);
        self.get('controllers.application').send('setGlobals');
        self.get('controllers.user').send('auth', data.glomeid, '');
      });
    }
  }
});

@@ -1686,36 +1686,25 @@
 ap')
-%5Bitem.get('urlName')%5D = 
+.pushObject(
 item
+)
 ;%0A  
@@ -1833,70 +1833,461 @@
 ap')
-%5B_item.get('urlName')%5D = _item;%0A          %7D, item);%0A        %7D)
+.pushObject(_item);%0A          %7D, item);%0A        %7D);%0A%0A        controller.get('categoryMap').forEach(%0A          function(item, index, enumerable)%0A          %7B%0A            // fetch all programs who have content in this category%0A            var vars =%0A            %7B%0A              catid: item.get('id'),%0A              application:%0A              %7B%0A                master_uid: App.apiHost,%0A                master_apikey: App.apiKey%0A              %7D%0A            %7D
 ;%0A%0A 
@@ -2297,45 +2297,120 @@
     
+ 
-console.log('category: ' + category);
+   controller.get('categoryMap').objectAt(index)%5B'programs'%5D = self.store.find('program', vars);%0A          %7D);%0A
 %0A   
@@ -2515,17 +2515,35 @@
 ap')
-%5B
+.findBy('urlName', 
 category
 %5D);%0A
@@ -2538,17 +2538,17 @@
 category
-%5D
+)
 );%0A     

use the enumerable array properly