﻿/// <reference path="jquery-1.5.min.js"/>
windowSize = (function () {
    var onChangedCallBack = [];

    // cross browser functions
    function f_clientWidth() {
        return f_filterResults(window.innerWidth ? window.innerWidth : 0, document.documentElement ? document.documentElement.clientWidth : 0, document.body ? document.body.clientWidth : 0);
    }

    function f_clientHeight() {
        return f_filterResults(window.innerHeight ? window.innerHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0, document.body ? document.body.clientHeight : 0);
    }

    function f_filterResults(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
            n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    }
    $(function () {
        $(
        "<table id='mainTable' style='margin: 0; padding: 0;' border='0' cellspacing='0' cellpadding='0'>" +
            "<tr style='margin: 0; padding: 0;' border='0'>" +
                "<td style='margin: 0; padding: 0;' border='0'>" +
                    "<img id='imgHeight' src='http://web.ischool.com.tw/pixel.gif' height='1' width='0'" +
                        "border='0' style='margin: 0; padding: 0;' />" +
                "</td>" +
            "</tr>" +
        "</table>").appendTo('body');
        setInterval("windowSize.checkSize();", 500);
    });
    return {
        width: 0,
        height: 0,
        checkSize: function () {
//            var overflow = $('body').css("overflow");
//            var overflowx = $('body').css("overflow-x");
//            var overflowy = $('body').css("overflow-y");
//            $('body').css("overflow-x", "hidden");
//            $('body').css("overflow-y", "hidden");
            $('#imgHeight').height($(document).height());
            var height = f_clientHeight();
            var width = f_clientWidth();
            $('#imgHeight').height(0);
            if (windowSize.height != height || windowSize.width != width) {
                windowSize.height = height;
                windowSize.width = width;
                for (var i = 0; i < onChangedCallBack.length; i++) {
                    onChangedCallBack[i]({ height: height, width: width });
                }
            }
//            $('body').css("overflow", overflow);
//            $('body').css("overflow-x", overflowx);
//            $('body').css("overflow-y", overflowy);
        },
        onChanged: function (fn) {
            onChangedCallBack.push(fn);
            fn({
                height: windowSize.height,
                width: windowSize.width
            });
        }
    };
} ());
