function tools() {};
tools.prototype = {
    body_id: $('body').attr('id'),
    body_class: $('body').attr('class'),
    sec: {
        scroll_to_pagetop: 500
    },
    fontsize: {
        target: 'body',
        cookie: {
            name: 'fontsize',
            expires: 365
        },
        default_size: 'medium',
        classes: {
            small  : 'font_small',
            medium : 'font_medium',
            large  : 'font_large'
        }
    },
    
    // Image rollover
    imageRollover: function(obj) {
        $(obj).hover(function() {
            $(this).attr('src', $(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
        }, function() {
            $(this).attr('src', $(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, '$1$2'));
        }).each(function() {
            $(this).click(function() {
                $(this).attr('src', $(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, '$1$2'));
            });
            $('<img>').attr('src', $(this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
        });
    },
    
    
    // CSS background image preload
    backgroundImagePreload: function(selector) {
        $(selector).each(function() {
            var base_url = $(this).css('background-image').replace(/^url\((\"|)(.+?)(\"|)\)$/, '$2');
            var over_url = base_url.replace(/^(.+)(\.[a-z]+)$/, '$1_on$2');
            $('<img>').attr('src', over_url);
        });
    },
    
    
    // Fontsize switcher
    fontsizeSwitcher: function(selector) {
        var settings = this.fontsize;
        var class_name = $.cookie(settings.cookie.name);
        if (class_name == null) {
            class_name = settings.classes[settings.default_size];
        }
        this._switchFontsize(selector, settings, class_name);
        
        $(selector).click(function() {
            var class_name = $(this).attr('class');
            var local_tools = new tools();
            local_tools.setFontsize(settings);
            local_tools._switchFontsize(selector, settings, class_name);
            return false;
        });
    },
    
    // Switch fontsize
    _switchFontsize: function(selector, settings, class_name) {
        var settings = this.fontsize;
        for (var key in settings.classes) {
            $(settings.target).removeClass(settings.classes[key]);
        }
        $(settings.target).addClass(class_name);
        $.cookie(settings.cookie.name, class_name, {expires: settings.cookie.expires});
    },
    
    
    // Uniform block height
    uniformHeight: function(selector) {
        var maxheight = 0;
        $(selector).each(function(){
            if ($(this).height() > maxheight) {
                maxheight = $(this).height();
            }
        });
        $(selector).each(function(){
            $(this).css('min-height', maxheight);
        });
    },
    
    
    // Scroll to pagetop
    scrollToPagetop: function(selector) {
        var sec = this.sec.scroll_to_pagetop;
        $(selector).click(function() {
            $(window).scrollTo($(this).attr('href'), sec);
            return false;
        });
    },
    
    
    // Search box
    searchBox: function(selector) {
        $(selector).removeClass('focus');
        if ($(selector).val() != '') {
            $(selector).addClass('focus');
        }
        
        $(selector).focus(function() {
            $(selector).addClass('focus');
        });
        $(selector).blur(function() {
            $(selector).removeClass('focus');
            if ($(selector).val() != '') {
                $(selector).addClass('focus');
            }
        });
    },
    
    
    // Global Navigation
    globalNavigation: function(selector) {
        $(selector).each(function() {
            var local_tools = new tools();
            if ($(this).hasClass(local_tools.body_id)) {
                $('a img', this).attr('src', $('a img', this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
            } else {
                local_tools.imageRollover($('a img', this));
            }
        });
    },
    
    
    // Local Navigation
    localNavigation: function(selector) {
        $(selector).each(function() {
            var local_tools = new tools();
            var classes = local_tools.body_class.split(' ');
            var current = false;
            for (var i = 0; i < classes.length; i++) {
                if ($(this).hasClass(classes[i])) {
                    if (classes[i] == 'news') {
                        $('a img', this).attr('src', $('a img', this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_open$2'));
                        $('div.content_navigation', this).show();
                        $('div.content_navigation ul li', this).each(function() {
                            if ($('body').hasClass($(this).attr('class'))) {
                                $(this).addClass('on');
                            }
                        });
                    } else {
                        $('a img', this).attr('src', $('a img', this).attr('src').replace(/^(.+)(\.[a-z]+)$/, '$1_on$2'));
                    }
                    current = true;
                }
            }
            if (current == false) {
                local_tools.imageRollover($('a img', this));
            }
        });
    },
    
    setTargetBlank: function(selector) {
        $(selector).attr('target', '_blank');
    },
    
    /* Setter */
    setScrollToPagetopSec: function(sec) {
        this.sec.scroll_to_pagetop = sec;
    },
    
    setFontsize: function(obj) {
        this.fontsize = obj;
    },
    
    initialize: function() {
        
    }
}

var t = new tools();
/* Initialize */
jQuery(function() {
    t.initialize();
    t.imageRollover($('img.rollover'));
    t.backgroundImagePreload('.bgpreload');
    t.scrollToPagetop('div#content_footer p a');
    t.scrollToPagetop('p.pageUp a');
    t.fontsizeSwitcher('div#header_tools div.font_size ul li');
    t.searchBox('div#header_tools input#search_txt');
    t.globalNavigation('div#global_navigation ul li');
    t.localNavigation('div#local_navigation > ul > li');
    t.setTargetBlank('a.blank');
});

