var app = app || {};
var requester, selector, userModel, postModel, homeViewBag, postViewBag,
	loginViewBag, addPostBag, homeController, postController, userController,
	loginController, addPostController, registerViewBag, registerController,
	contactViewBag, contactController;

//include navigation on site
(function() {
	$.get('templates/navigation.html', function(template) {
		var rendered = Mustache.render(template);
		$('header').html(rendered);
	})
})();

(function () {
    app.router = Sammy(function () {
        requester = app.requester.config('kid_b1HhUaVLJb', '916fc5a992ee453780b58f565e2b50ad');
        selector = '#wrapper';

        userModel = app.userModel.load(requester);
        postModel = app.postModel.load(requester);

        homeViewBag = app.homeViews.load();
        postViewBag = app.postViews.load();
		loginViewBag = app.loginViews.load();
		addPostBag = app.addPostViews.load();
		registerViewBag = app.registerViews.load();
		contactViewBag = app.contactViews.load();

        homeController = app.homeController.load(homeViewBag);
        postController = app.postController.load(postModel, postViewBag);
        userController = app.userController.load(userModel, postViewBag);
		loginController = app.loginController.load(loginViewBag);
		addPostController = app.addPostController.load(addPostBag);
		registerController = app.registerController.load(registerViewBag);
		contactController = app.contactController.load(contactViewBag);

        this.get('#/home', function () {
            userController.login('test', 'test');
            homeController.loadHomePage('article');
            //postController.getAllPosts('article');
        });

        this.get('#/posts', function () {
			if (userController.checkActiveUser()) {
				postController.getAllPosts('article');
			} else {
				this.redirect('#/login');
			}
        });

        this.get('#/addpost', function () {
			if(userController.checkActiveUser()) {
				addPostController.loadAddPostPage('article');
			} else {
				this.redirect('#/login');
			}
        });

        this.get('#/login', function () {
            loginController.loadLoginPage('article');
        });

		this.get('#/auto-login', function () {
			userController.login('test', 'test');
		});

		this.get('#/register', function () {
            //userModel.signUp("ivaylo", 'ivanov', 'ivaylo@abv.bg');
            registerController.loadRegisterPage('article');
        });

        this.get('#/contact', function () {
            contactController.loadContactPage('article');
        });

		this.get('#/logout', function () {
			userController.logout();
		})
    });

    app.router.run('#/home');
})();
@@ -1510,28 +1510,22 @@
 on () %7B%0A
-            
+%09%09%09if(
 userCont
@@ -1535,43 +1535,33 @@
 ler.
-login('test', 'test');%0A            
+checkActiveUser()) %7B%0A%09%09%09%09
 home
@@ -1600,60 +1600,54 @@
 ');%0A
-            //postController.getAllPosts('article');
+%09%09%09%7D else %7B%0A%09%09%09%09this.redirect('#/login');%0A%09%09%09%7D
 %0A   
@@ -2082,28 +2082,107 @@
 on () %7B%0A
-            
+%09%09%09if(userController.checkActiveUser()) %7B%0A%09%09%09%09this.redirect('#/register');%0A%09%09%09%7D else %7B%0A%09%09%09%09
 loginCon
@@ -2207,32 +2207,37 @@
 age('article');%0A
+%09%09%09%7D%0A
         %7D);%0A%0A%09%09t
@@ -2551,65 +2551,146 @@
 ) %7B%0A
-            contactController.loadContactPage('article');
+%09%09%09if(userController.checkActiveUser()) %7B%0A%09%09%09%09contactController.loadContactPage('article');%0A%09%09%09%7D else %7B%0A%09%09%09%09this.redirect('#/login');%0A%09%09%09%7D
 %0A   

create private blog - for error in console when is not login in system