/*global DW_LIVEEDIT_CONSTANTS */
function DevicePreviewHandler() {
    'use strict';
    this.prevScrollX = 0;
    this.prevScrollY = 0;
    
    this.initialize = function () {
        this.initScrollListener();
    };
    
    this.initScrollListener = function () {
        var self  = this;
        document.addEventListener('scroll', function (evt) {
            
            if (window.scrollX === self.prevScrollX && window.scrollY === self.prevScrollY) {
                return;
            } else {
                self.prevScrollX = window.scrollX;
                self.prevScrollY = window.scrollY;
            }
            
            var yPercentageScrolled = (document.body.scrollTop / (document.body.scrollHeight - window.innerHeight)) * 100,
                xPercentageScrolled = (document.body.scrollLeft / (document.body.scrollWidth - window.innerWidth)) * 100,
                argsObj = {
                    "xPercentageScrolled": Math.round(Number(xPercentageScrolled)),
                    "yPercentageScrolled": Math.round(Number(yPercentageScrolled))
                };
            window.DWLECallJsBridgingFunction(DW_LIVEEDIT_CONSTANTS.DevicePreview, 'updateScrollParams', argsObj);
        }, true);
    };
    
    this.initializeIfDevicePreviewEnabled = function () {
        var argsObj = {},
            self = this;
        
        argsObj.callbackFunc = function (isEnabled) {
            if (isEnabled) {
                self.initialize();
            }
        };
        
        window.DWLECallJsBridgingFunction(DW_LIVEEDIT_CONSTANTS.DevicePreview, 'isDevicePreviewEnabled', argsObj);
    };
}

window.devicePreviewHandler = window.devicePreviewHandler || new DevicePreviewHandler();
window.devicePreviewHandler.initializeIfDevicePreviewEnabled();
    