(function($){
    $(function(){
        var styleSwitcher = {
            p : {
                trigger  : '.styleSwitcher',
                on       : '_on',
                off      : '_off',
                className : '.push',
                addClass : 'push_on'
            },
            init : function(){
                var self = this;
                $(self.p.trigger).click(function(){
                    self.change(this.getAttribute('rel'));
                    return false;
                });
                var c = self.readCookie('style');
                var t = '.styleSwitcher[rel="' + c + '"] img';
                if (c) {
                    self.change(c);
                    self.buttonDown(t, self.p);
                } else {
                    self.buttonDown('.styleSwitcher[rel="medium"] img', self.p);
                }
                
                // fontsizeChanger
                $(self.p.className).click(function(){
                    $.each($(self.p.className), function(k, v){
                        if ($(v).hasClass(self.p.addClass)) {
                            styleSwitcher.buttonUp(this, self.p);
                        }
                    });
                    styleSwitcher.buttonDown(this, self.p);
                });
            },
            change : function(styleName){
                $('link[@rel*=style][title]').each(function(i){
                    this.disabled = true;
                    if (this.getAttribute('title') === styleName) {
                        this.disabled = false;
                    }
                });
                this.createCookie('style', styleName, 365);
            },
            buttonDown : function(target, p){
                $(target).attr('src', $(target).attr('src').replace(p.off, p.on)).addClass(p.addClass);
            },
            buttonUp : function(target, p){
                $(target).attr('src', $(target).attr('src').replace(p.on, p.off)).addClass(p.addClass);
            },
            createCookie : function(name, value, days){
                if (days) {
                    var date = new Date();
                    date.setTime(date.getTime() + (days*24*60*60*1000));
                    var expires = '; expires=' + date.toGMTString();
                } else {
                    var expires = '';
                }
                document.cookie = name + '=' + value + expires + '; path=/';
            },
            readCookie : function(name){
                var nameEQ = name + "=";
                var 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;
            },
            eraseCookie : function(name){
                 this.createCookie(name, '', -1);
            }
        };
        styleSwitcher.init();
    });
})(jQuery);

