define(['jquery', 'underscore', 'backbone', 'router', 'classes/profile', 'classes/cache', 'libs/fastclick', 'classes/debug'], function
        ($, _, Backbone, Router, Profile, Cache, FastClick, Debug) {
    'use strict';
    var init = function () {
        //create backbone router 
        var router = new Router();
		
        Backbone.history.start();
        Backbone.emulateJSON = true;


        hello.init({
            facebook: '1466942266882168',
            google: '319140243944-qk0lfhgmi8shi4go97r83l69f1k0gdf4.apps.googleusercontent.com'
        }, {display: 'page', scope: 'email'});


        //console.log("Backbone history start");
        // Trigger 'route' event on router instance.
        router.on('route', function (name, args) {
            //alert(name);
        });
		
        Cache.checkCacheFromServer();

        if (Profile.isConnected()) {
            Profile.loadProfile();
        }

        hello.on('auth.login', function (auth) {
            if (!Profile.isConnected())
            {
                hello(auth.network).api("me").then(function (json) {
                    var id = "";
                    var displayName = "";
                    var email = "";
                    var gender = "unknown";

                    if (auth.network === "google")
                    {
                        id = json.id;
                        displayName = json.displayName;
                        email = json.email;
                        gender = json.gender;
                    }
                    else if (auth.network === "facebook")
                    {
                        id = json.id;
                        displayName = json.name;
                        email = json.email;
                        gender = json.gender;
                    }

                    if(gender === undefined)
                        gender = "unknown";

                    if(email === undefined)
                        email = "";

                    Debug.writeMessage('Your name is ' + json.name + ' in the social ' + auth.network + ", ID=" + id + ", DisplayName: "+ displayName + ", email:"+ email);

                    Profile._loadProfile(id, function (retVal) {
                        if (retVal === false)
                        {
                            Debug.writeMessage("Profilo non trovato, ne creo uno nuovo");
                            Profile.createProfile(
                                    id,
                                    auth.network,
                                    displayName,
                                    email,
                                    gender,
                                    function () {
                                        Debug.writeMessage("Profilo nuovo creato");
                                        window.location.href = "";
                                    }
                            );
                        }
                        else
                            window.location.href = "";
                    });
                },
                function(e){                                        
                    Debug.writeMessage('Whoops! ' + e.error.message);                    
                });
            }
        });

        $('body').css('background-color', '');
        $('#loadingFrm').remove();
    };

    return {
        initialize: init
    };
});