var ChangeFontSize = {

    /**
    * 是否使用Cookie儲存字型設定資料
    * @type Boolean
    */
    isUseCookie: true,

    /**
    * Cookie索引名稱
    * @type String
    */
    cookieName: 'CurrFontSize',

    /**
    * 是否改變圖片狀態
    * @type Boolean
    */
    isChangeImage: true,

    /**
    * 文字大小尺寸陣列
    * @type Array
    */
    fontSize: ['90%', '100%', '125%', '135%'],

    /**
    * 圖片id陣列
    * @type Array
    */
    imgID: ['fontSize1', 'fontSize2', 'fontSize3', 'fontSize4'],

    /**
    * 普通狀態圖片CSS陣列
    * @type Array
    */
    normalCSS: ['FuncBar_Font1', 'FuncBar_Font2', 'FuncBar_Font3', 'FuncBar_Font4'],

    /**
    * 滑鼠覆蓋狀態圖片CSS陣列
    * @type Array
    */
    overCSS: ['FuncBar_Font1_ov', 'FuncBar_Font2_ov', 'FuncBar_Font3_ov', 'FuncBar_Font4_ov'],

    /**
    * 背景圖檔
    * @type String
    */
    backImage: '',

    /**
    * 語系
    * @type String
    */
    lang: '0',

    /**
    * 網頁載入完畢初始讀取
    */
    OnLoad: function () {
        var index = this.ReadCookie(this.cookieName) || 1;
        try {
            //查詢Cookie是否使用Cookie
            if (this.isUseCookie) {
                document.body.style.fontSize = this.fontSize[parseInt(index)] || this.fontSize[1];
            }
            //解析網址語系
            if (document.location.href.match(new RegExp("(&|\\u003F)lang=([^&]*)(&|$)"))) {
                this.lang = RegExp.$2;
            }
            if (this.lang != '0' && this.lang != '1')
                this.backImage = 'FuncBar_icon_ej';

            this.Update(index);
        }
        catch (e) {
            return;
        }
        finally {
            index = null;
        }
    },

    /**
    * 立即改變字型大小
    */
    Immediate: function () {
        var i, currSize;

        try {
            //檢查陣列資料
            if (this.fontSize == null || this.fontSize.length == 0) throw new Error('大小索引錯誤。');

            //讀取目前尺寸或預設值
            currSize = document.body.style.fontSize || '100%';

            for (i = 1; i <= this.fontSize.length; i++) {
                if (this.fontSize[i - 1] == currSize) {
                    $('div.main_main').css('font-size', (i == 4 ? this.fontSize[0] : this.fontSize[i]));
                    //document.body.style.fontSize = (i == 4 ? this.fontSize[0] : this.fontSize[i]);

                    //假如啟用圖片轉換
                    if (this.isChangeImage) {
                        //重製圖片並且更改選取字型的圖片顏色
                        this.ResetImg();
                        this.ImgClick((i == 4 ? 0 : i));
                    }
                    //假如有使用Cookie則寫入Cookie
                    if (this.isUseCookie) this.CreateCookie(this.cookieName, (i == 4 ? 0 : i), 1);
                }
            }
        }
        catch (e) {
            alert(e.Message);
            return;
        }
        finally {
            i = null;
            currSize = null;
        }
    },

    /**
    * 依照索引改變文字大小
    * @param {number} fontIndex -陣列索引
    */
    Update: function (fontIndex) {
        var index;
        try {
            index = parseInt(fontIndex);
            if (this.fontSize == null || this.fontSize.length < (index + 1)) throw new Error('大小索引錯誤。');

            $('div.main_main').css('font-size', this.fontSize[index]);
            //document.body.style.fontSize = this.fontSize[index];

            if (this.isChangeImage) this.ImgClick(fontIndex);
            if (this.isUseCookie) this.CreateCookie(this.cookieName, (fontIndex), 1);
        }
        catch (e) {
            alert(e.Message);
            return;
        }
        finally {
            index = null;
        }
    },

    /**
    * 選擇字型按下改變圖片樣式
    * @param {number} idIndex -陣列索引
    */
    ImgClick: function (idIndex) {
        var el, index;

        try {
            index = parseInt(idIndex);
            if (this.imgID == null || this.imgID.length < (index + 1)) throw new Error('圖片id索引錯誤。');
            if (this.normalCSS == null || this.normalCSS.length < (index + 1)) throw new Error('CSS索引錯誤。');
            if (this.overCSS == null || this.overCSS.length < (index + 1)) throw new Error('CSS索引錯誤。');

            this.ResetImg();
            el = document.getElementById(this.imgID[idIndex]);
            el.className = this.overCSS[idIndex] + " " + this.backImage;
        }
        catch (e) {
            return;
        }
        finally {
            el = null;
            index = null;
        }
    },

    /**
    * 重置圖片
    */
    ResetImg: function () {
        var i, el;
        try {
            if (this.isChangeImage) {
                for (i = 1; i <= this.imgID.length; i++) {
                    el = document.getElementById(this.imgID[i - 1]);
                    if (el == null) continue;
                    el.className = this.normalCSS[i - 1] + " " + this.backImage;
                }
            }
        }
        catch (e) {
            return;
        }
        finally {
            i = null;
            el = null;
        }
    },

    /**
    * 建立Cookie
    * @param {string} name -索引名稱
    * @param {string} value -值
    * @param {number} days	-天數
    */
    CreateCookie: function (name, value, days) {
        var date, expires;
        try {
            if (days) {
                date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = '; expires='.concat(date.toGMTString());
            }
            else expires = '';
            document.cookie = name.concat('=', value, expires, '; path=/');
        }
        catch (e) {
            alert('Cooke建立失敗：'.concat(e.Message));
        }
        finally {
            date = null;
            expires = null;
        }
    },

    /**
    * 讀取Cookie的值
    * @param {string} name -索引名稱
    * @return {string}
    */
    ReadCookie: function (name) {
        var nameEQ, ca;

        try {
            nameEQ = name.concat('=');
            ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
        catch (e) {
            alert('Cookie讀取失敗：'.concat(e.Message));
            return null;
        }
        finally {
            nameEQ = null;
            ca = null;
        }
    }
};
