define(['jquery', 'underscore', 'backbone', 'router', 'classes/profile', 'classes/cache', 'libs/fastclick'], function
        ($, _, Backbone, Router, Profile, Cache, FastClick) {
    '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 = "";

                    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;
                    }
                    Profile._loadProfile(id, function (retVal) {
                        if (retVal === false)
                        {
                            Profile.createProfile(
                                    id,
                                    auth.network,
                                    displayName,
                                    email,
                                    gender,
                                    function () {
                                        window.location.href = "";
                                    }
                            );
                        }
                        else
                            window.location.href = "";
                    });
                });
            }
        });

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

    return {
        initialize: init
    };
});