/**
 *  ABook Viewer for WEB
 *  ZoomDetector Library
 *  Copyright (C) Agentec Co, Ltd. All rights reserved.
*/

//グローバルの名前空間用のオブジェクトを用意する
var ZOOM_DETECTOR = {};

ZOOM_DETECTOR.zoom_ratioPre = 1;
ZOOM_DETECTOR.zoom_ratio = 1;
ZOOM_DETECTOR.zoom_timer;
ZOOM_DETECTOR.zoom_continue = false;
ZOOM_DETECTOR.zoom_callbackFunction;
ZOOM_DETECTOR.zoom_miliSeconds = 1000;  // Default is 1 second
ZOOM_DETECTOR.zoom_oldW = -1;
ZOOM_DETECTOR.zoom_oldH = -1;

ZOOM_DETECTOR.calculateZoomLevel = function() {
    ZOOM_DETECTOR.zoom_ratioPre = ClientData.zoom_ratioPre();
    if (ZOOM_DETECTOR.zoom_timer) {
        clearTimeout(ZOOM_DETECTOR.zoom_timer);
        ZOOM_DETECTOR.zoom_timer = null;
    }
    
    ZOOM_DETECTOR.zoom_ratio = document.documentElement.clientWidth / window.innerWidth;
    if (ZOOM_DETECTOR.zoom_ratioPre != ZOOM_DETECTOR.zoom_ratio) {
        if (ZOOM_DETECTOR.zoom_oldW == -1) {
            ZOOM_DETECTOR.zoom_oldW = document.documentElement.clientWidth;
        }
        if (ZOOM_DETECTOR.zoom_oldH == -1) {
            ZOOM_DETECTOR.zoom_oldH = document.documentElement.clientWidth;
        }
        if (ZOOM_DETECTOR.zoom_callbackFunction) {
            ZOOM_DETECTOR.zoom_callbackFunction(ZOOM_DETECTOR.zoom_ratioPre, ZOOM_DETECTOR.zoom_ratio, ZOOM_DETECTOR.zoom_oldW, ZOOM_DETECTOR.zoom_oldH, window.innerWidth, window.innerHeight);
        }
        ZOOM_DETECTOR.zoom_ratioPre = ZOOM_DETECTOR.zoom_ratio;
        ClientData.zoom_ratioPre(ZOOM_DETECTOR.zoom_ratioPre);
        ZOOM_DETECTOR.zoom_oldW = window.innerWidth;
        ZOOM_DETECTOR.zoom_oldH = window.innerHeight;
    }
    if (ZOOM_DETECTOR.zoom_continue == true) {
        ZOOM_DETECTOR.zoom_timer = setTimeout("ZOOM_DETECTOR.calculateZoomLevel();", ZOOM_DETECTOR.zoom_miliSeconds);
    }
};

ZOOM_DETECTOR.stopDetectZoom = function() {
    ZOOM_DETECTOR.zoom_continue = false;
};

ZOOM_DETECTOR.startDetectZoom = function(params) {
    ZOOM_DETECTOR.zoom_continue = true;
    if (params.callbackFunction) {
        ZOOM_DETECTOR.zoom_callbackFunction = params.callbackFunction;
    }
    if (params.time) {
        ZOOM_DETECTOR.zoom_miliSeconds = params.time;
    }
    ZOOM_DETECTOR.zoom_timer = setTimeout("ZOOM_DETECTOR.calculateZoomLevel();", ZOOM_DETECTOR.zoom_miliSeconds);
};
