﻿var accessWidget = null;

window.addEvent('domready', function() {
    accessWidget = new AccessWidget();
});

AccessWidget = new Class ({
    initialize : function() {
    
        this.fontSizes = {
            normal: "0.9em",
            bigger: "1.2em",
            biggest: "1.4em"
        }
    
        try {
            // load cookies
            var cookieFontSizeVal = Cookie.read("currFontSize");
            var cookieThemeVal = Cookie.read("currentTheme");
            
            // load previous font size
            if (typeof(cookieFontSizeVal) != 'undefined' && cookieFontSizeVal != 'null') {                
                this.currentFontSize = cookieFontSizeVal;
                this.setFontSize(this.currentFontSize);
            }
            
            if (typeof(cookieThemeVal) != 'undefined' && cookieThemeVal != 'null') {                
                this.currentTheme = cookieThemeVal;
                this.setTheme(this.currentTheme);
            }
            
            // add widget event listeners
            if ($('access-widget')) {
                var slide = new Fx.Slide('access-widget-slide');
                slide.hide();
                //slide.setStyle('display', 'none');
                var wgt = $('access-widget');                
                $('access-options-trigger').setStyle('display','block');
                $('access-options-trigger').addEvent('click', function() {
                    slide.toggle();                    
                    //wgt.setStyle('display', (wgt.getStyle('display') == 'none') ? 'block' : 'none');
                });
                $('awd-close').addEvent('click', function() {
                    slide.toggle();
                    //wgt.setStyle('display', 'none');
                });
                $('awd-font-normal').addEvent('click', this.handleChangeFontSize);
                $('awd-font-bigger').addEvent('click', this.handleChangeFontSize);
                $('awd-font-biggest').addEvent('click', this.handleChangeFontSize);
                $('awd-theme-default').addEvent('click', this.handleClickTheme);
                $('awd-theme-1').addEvent('click', this.handleClickTheme);
                $('awd-theme-2').addEvent('click', this.handleClickTheme);
            }

            // unhide the page after the theme has been applied            
            this.showPage();
    
        }
        catch(e) {
            this.showPage();
        }
    },
    handleChangeFontSize : function(e) {
        var fontSizeVal = accessWidget.fontSizes[e.target.id.split('-')[2]];        
        accessWidget.setFontSize(fontSizeVal);
        
        // save font size in cookie for future references
        Cookie.dispose('currFontSize');
        Cookie.write("currFontSize", fontSizeVal, {duration: 365, path: '/'});
    },
    handleClickTheme : function(e) {
               
        // save value in cookie
        Cookie.dispose('currentTheme');
        Cookie.write("currentTheme", e.target.id, {duration: 365, path: '/'});
        
        accessWidget.setTheme(e.target.id);
        
        // refresh the page after the theme has been saved
        history.go(0);
    },
    setTheme : function(theme) {
        this.currentTheme = theme;

        if (theme != 'awd-theme-default') {
            var accSheet = document.createElement('link');
		    accSheet.type = 'text/css';
		    accSheet.rel = 'stylesheet'
		    accSheet.href = "/css/" + theme + ".css";
		    accSheet.media = 'screen';
		    document.getElementsByTagName("head")[0].appendChild(accSheet);
        }        
    },
    setFontSize : function(size) {
        this.currentFontSize = size;
        $(document.body).setStyle('font-size', size);
    },
    showPage : function() {
        $(document.body).setStyle('display', 'block');
    }
});

