"use strict";
/**
 * Load and initialise skrollrjs, based on:
 * {@link http://stackoverflow.com/questions/23715337/integrating-skrollr-w-angularjs-single-page-app}
 * @author SOON_
 * @module sn.skrollr
 * @class  snSkrollrService
 */
angular.module("sn.skrollr").service("snSkrollrService", [
    "$document",
    "$q",
    "$rootScope",
    "$window",
    /**
     * @constructor
     * @param   {Object}  $document  angular wrapper for document
     * @param   {Service} $q         promise service
     * @param   {Object}  $rootScope data on rootScope
     * @param   {Object}  $window    angular wrapper for window object
     */
    function($document, $q, $rootScope, $window){

        var defer = $q.defer();

        function onScriptLoad() {
            // Load skrollr.js in the browser
            $rootScope.$apply(function() {
                var s = $window.skrollr.init({
                        forceHeight: false,
                        smoothScrolling: true,
                        mobileDeceleration: 0.004
                    });
                defer.resolve(s);
            });
        }

        /**
         * Create a script tag with skrollr as the source
         * and call our onScriptLoad callback when it
         * has been loaded
         */
        var scriptTag = $document[0].createElement("script");
        scriptTag.type = "text/javascript";
        scriptTag.async = true;
        scriptTag.src = "components/skrollr/dist/skrollr.min.js";

        scriptTag.onreadystatechange = function () {
            if (this.readyState === "complete") {
                onScriptLoad();
            }
        };

        scriptTag.onload = onScriptLoad;

        var s = $document[0].getElementsByTagName("body")[0];
        s.appendChild(scriptTag);

        return {
            skrollr: function() { return defer.promise; }
        };

    }
 ]);

@@ -18,18 +18,9 @@
 %0A * 
-Load and i
+I
 niti
@@ -38,122 +38,8 @@
 lrjs
-, based on:%0A * %7B@link http://stackoverflow.com/questions/23715337/integrating-skrollr-w-angularjs-single-page-app%7D
 %0A * 
@@ -169,25 +169,8 @@
 , %5B%0A
-    %22$document%22,%0A
     
@@ -240,74 +240,8 @@
 tor%0A
-     * @param   %7BObject%7D  $document  angular wrapper for document%0A
     
@@ -440,19 +440,8 @@
 ion(
-$document, 
 $q, 
@@ -503,16 +503,35 @@
 %0A       
+ var onScriptLoad =
  functio
@@ -923,406 +923,45 @@
     
-/**%0A         * Create a script tag with skrollr as the source%0A         * and call our onScriptLoad callback when it%0A         * has been loaded%0A         */%0A        var scriptTag = $document%5B0%5D.createElement(%22script%22);%0A        scriptTag.type = %22text/javascript%22;%0A        scriptTag.async = true;%0A        scriptTag.src = %22components/skrollr/dist/skrollr.min.js%22;%0A%0A        scriptTag.onreadystatechange =
+angular.element($window).bind('load',
  fun
@@ -969,68 +969,13 @@
 tion
- 
 () %7B%0A
-            if (this.readyState === %22complete%22) %7B%0A    
     
@@ -998,20 +998,16 @@
 Load();%0A
-    
         
@@ -1011,156 +1011,8 @@
    %7D
-%0A        %7D;%0A%0A        scriptTag.onload = onScriptLoad;%0A%0A        var s = $document%5B0%5D.getElementsByTagName(%22body%22)%5B0%5D;%0A        s.appendChild(scriptTag
 );%0A%0A

remove loading from SkrollrService