// Define libraries
require.config({
	baseUrl: 'static/js/',
	paths: {
		jquery: 'lib/jquery-1.7.1.min',
        ember: 'lib/ember-0.9.8.1.min',
        mobile: 'lib/jquery.mobile-1.1.0.min',
		text: 'lib/require/text',
		mocha: 'lib/mocha',
		chai: 'lib/chai'
	}
});

// Load our app
define( 'app', [
	'jquery',
    'app/controllers/backends',
    'app/controllers/confirmation',
    'app/controllers/notification',
    'app/controllers/machine_add',
    'app/controllers/select_machines',
    'app/views/count',
    'app/views/backend_button',
    'app/views/edit_backend',
    'app/views/machine_list_item',
    'app/views/image_list',
    'app/views/enable_backend_button',
    'app/views/machine_add_dialog',
    'app/views/machine',
    'app/views/confirmation_dialog',
    'app/views/machine_actions_dialog',
    'app/views/single_machine_actions_dialog',
	'ember',
	'mobile',
	], function($, BackendsController, ConfirmationController, 
			NotificationController, MachineAddController,
			SelectMachinesController,
			Count, BackendButton, EditBackend, MachineList, ImageList,
			EnableBackendButton, MachineAddDialog, MachineView, ConfirmationDialog,
			MachineActionsDialog,
			SingleMachineActionsDialog) {
		var App = Ember.Application.create({

			VERSION: '0.3-ember',

			// Sets up mocha to run some integration tests
			specsRunner: function( chai ) {
				// Create placeholder for mocha output
				$( document.body ).before( '<div id="mocha"></div>' );

				// Setup mocha and expose chai matchers
				window.expect = chai.expect;
				mocha.setup('bdd');

				// Load testsuite
				require([
					'app/specs/templates/basic_acceptance'
				], function() {
						mocha.run().globals( [ '$', 'Ember', 'Mist' ] );
					}
				);
			},

			// Constructor
			init: function() {
				this._super();

				this.set(
					'backendsController',
					BackendsController.create()
				);
				
				this.set(
						'confirmationController',
						ConfirmationController.create()
					);
				
				this.set(
						'notificationController',
						NotificationController.create()
					);

				this.set(
						'machineAddController',
						MachineAddController.create()
					);
				
				this.set(
						'selectMachinesController',
						SelectMachinesController.create()
					);
				
				// Run specs if asked
				if ( location.hash.match( /specs/ ) ) {
					require( [ 'chai', 'mocha' ], this.specsRunner );
				}
				
				
				
				

				setTimeout(function(){
					if($('.ui-page-active').attr('id') == 'splash'){
						$.mobile.changePage('#one', {
							transition: 'fade',
						});
					} else {
						$.mobile.changePage('#splash', {
							transition: 'fade',
						});
						setTimeout(function(){
							$.mobile.changePage('#one', {
									transition: 'fade',
							});
						}, 2000);
					}
				}, 1000);
				
				
			}
		});
		
		
		
		$(document).on( 'pagebeforeshow', '#machines', function(){
		    $('#machines-list').listview('refresh');
		});
		
		App.Select = Ember.Select.extend({
		    attributeBindings: ['name', "data-theme", "data-icon",
                "data-native-menu", 'disabled'],
		
            didInsertElement: function(){
	    	
			    var that = this;
			
	    	    //Em.run.next(function() {
	    	    //		that.get('parentView').$().trigger('create');	
	    	    
	    		//});
	        },
		});
		
		App.TextField = Ember.TextField.extend({
		    attributeBindings: ['name', "data-theme"]
		});
		
		App.Checkbox = Ember.Checkbox.extend({
		    attributeBindings: ['name', "id"]
		});
		
		App.CountView = Count;
		App.BackendButtonView = BackendButton;
		App.EditBackendView = EditBackend;
		App.MachineListView = MachineList;
		App.ImageListView = ImageList;
		App.EnableBackendButtonView = EnableBackendButton;
		App.onOff = ['on', 'off'];
		
		var addDialog = MachineAddDialog.create();
		addDialog.append();
		
		var machineView = MachineView.create();
		machineView.append();
		
		var confirmationDialog = ConfirmationDialog.create();
		confirmationDialog.append();
		
		var machineActionsDialog = MachineActionsDialog.create();
		machineActionsDialog.append();
		var dialog = SingleMachineActionsDialog.create();
		dialog.append();
		
		$(document).on( 'pagebeforeshow', '#dialog-add', function(){
		    $('#dialog-add').trigger('create');
		});
		
		// Expose the application globally
		return window.Mist = App;
	}
);

@@ -2414,24 +2414,50 @@
 %09%09%0A%09%09%09%09%0A%09%09%09%09
+location.hash = '#splash';
 %0A%09%09%09%09%0A%0A%09%09%09%09s
@@ -2615,229 +2615,17 @@
 %09%09%09%7D
- else %7B%0A%09%09%09%09%09%09$.mobile.changePage('#splash', %7B%0A%09%09%09%09%09%09%09transition: 'fade',%0A%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%09setTimeout(function()%7B%0A%09%09%09%09%09%09%09$.mobile.changePage('#one', %7B%0A%09%09%09%09%09%09%09%09%09transition: 'fade',%0A%09%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%09%7D, 2000);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D, 1
+%0A%09%09%09%09%7D, 2
 000)

change the hash, cleaner