﻿/*********************Font Size Change************************/
function FontSizeHelper() {
    this.Init = function (options) {
        this.Step = parseFloat("1");
        this.Index = 0;
        this.Options = options || {};
        this.CSSFilter = this.Options.cssFilter;
        this.StopFlag = false;
    };

    this.saveToCookie = function () {

        $.cookie('font_index', this.Index, {expires:this.Options.cookieExpires, path: '/' });
    }

    this.restoreFromCookie = function () {


        index = parseInt($.cookie('font_index'));
        this.Index = isNaN(index) ? 0 : index;
    }

    this.setFont = function (item, status) {
        var fontSize = $(item).css("font-size");
        var currentFont = parseFloat(fontSize);
        var dimension = fontSize.replace(currentFont, "");

        if (status == 1) {
            currentFont = currentFont + this.Step;
        }
        else
            if (status == -1) {
                currentFont = currentFont - this.Step;
            }
            else
                if (status == 0) {
                    currentFont = currentFont - this.Step * this.Index;
                }
                else {
                    currentFont = currentFont + this.Step * this.Index;
                }
        if (currentFont == 0) {
            this.StopFlag = true;
        }

        if (currentFont >= 0) {
            $(item).css("font-size", currentFont + dimension);
        }
    }

    this.changeFontSize = function(status) {
    var elements = $(document).find(this.CSSFilter);
       /* var arrayList = $.unique($.merge($(elements), $(elements).find("*")));
        
        arrayList.sort(function(a, b) {
            var aCount = $(a).find("*").length;
            var bCount = $(b).find("*").length;
            return aCount - bCount;

        });*/

        $(elements).each(function(index, item) {

        FontChangeHelper.setFont(item, status);
    });
    };

    this.incFontSize = function () {
        this.StopFlag = false;
        this.Index = this.Index + 1;
        this.changeFontSize(1);
        this.saveToCookie();
        return false;
    };

    this.resetFontSize = function () {
        this.StopFlag = false;
        this.changeFontSize(0);
        this.Index = 0;
        this.saveToCookie();
        return false;
    }

    this.decFontSize = function () {
        if (this.StopFlag)
        { return false; }
        this.Index = this.Index - 1;
        this.changeFontSize(-1);
        this.saveToCookie();
        return false;
    };

}
function getFontSelectorSettings() {
    return { cssFilter: 'none', cookieExpires: 0 };
}
var FontChangeHelper;
$(document).ready(function () {
    FontChangeHelper = new FontSizeHelper();
    FontChangeHelper.Init(getFontSelectorSettings())
    FontChangeHelper.restoreFromCookie();
    FontChangeHelper.changeFontSize(2);

});
  

