/*
 * MJL
 */
MJL.event.add(window, "load", function() {
    MJL.enable.flash("flash");
    MJL.enable.window('popup-01', {width : 820, height : 750});
    MJL.enable.window('popup-02', {width : 780, height : 750});
    MJL.enable.heightEqualizer('list-index-02', {groupBy : 3});
    MJL.enable.heightEqualizer('box-index-01', {groupBy : 2});
    MJL.enable.heightEqualizer('box-index-02', {groupBy : 3});
    MJL.enable.heightEqualizer('box-index-04', {groupBy : 2});
    MJL.enable.heightEqualizer('box-index-05', {groupBy : 2});
    MJL.enable.heightEqualizer('box-index-06', {groupBy : 2});
    MJL.enable.heightEqualizer('lyt-col2', {
        groupBy : 2,
        collect : function(parent) {
            return $('.list-contact-01 > dd', parent);
        }
    });
    MJL.enable.heightEqualizer('lyt-col3', {
        groupBy : 3,
        collect : function(parent) {
            return $('.list-define-03 > dd', parent);
        }
    });
    MJL.enable.rollover("roll", {disable : "unroll"});
});


/*
 * jQuery
 */
$(function(){
    // CSS Support
    $('> li:first-child', '#nav-01').addClass('first-child');
    $('> li:first-child', '#nav-local').addClass('first-child');
    $('.banner li:first-child', '#extra').addClass('first-child');
    $('> dd:last-child', '.list-define-01').addClass('last-child');
    $('> dd:last-child', '.list-define-02').addClass('last-child');
    $('tr:nth-child(even)', '.tbl-news-01').addClass('even');
    $('tr:nth-child(even)', '.tbl-news-02').addClass('even');
    $('li:nth-child(even)', '.list-qa-01').addClass('even');
    if ($.browser.msie && $.browser.version < 7){
        $('div:nth-child(3n):last-child', '.box-index-02').css('margin-bottom', '0');
        $('div:nth-child(3n):last-child', '.box-index-01').css('margin-bottom', '0');
    }

    // Add Icon
    $('a[href$=".pdf"]').not(':has(img)').after('<span class="icon-pdf">&nbsp;</span>');
    $('a[href$=".xls"]').not(':has(img)').after('<span class="icon-excel">&nbsp;</span>');
    $('a[target$=blank]').not('a[href$=".pdf"], a[href$=".xls"], :has(img), #nav-global a[target$=blank], #nav-local a[target$=blank]').after('<span class="icon-external">&nbsp;</span>');
    $('#nav-global a[target$=blank], #nav-local a[target$=blank]').append('<span class="icon-external">&nbsp;</span>');

    // Drop Down Menu
    (function(){
        function mouseOver(){
            $(this).find(".nav-sub").stop(true, false).fadeTo('fast', 1).show();
        }
        function mouseOut(){ 
            $(this).find(".nav-sub").stop(true, false).fadeTo('fast', 0, function() {
                $(this).hide(); 
            });
        }
        var config = {    
            sensitivity: 1,
            interval: 20,
            over: mouseOver,
            timeout: 50,
            out: mouseOut
        };
        $("#nav-global > ul > li").hoverIntent(config);
    })();

    // Image Layout
    (function(){
        function getImgParallelStyler(side) {
            return function() {
                var obj = $(this);
                var imgBlock = $('> .img', obj);
                var imgWidth = $('img', imgBlock).width();
                imgBlock.css('width', imgWidth+'px');
                $('> .text', obj).css('margin-'+side, imgBlock[0].offsetWidth+'px');
            };
        }
        $('.lyt-image-01.image-left').each(getImgParallelStyler('left'));
        $('.lyt-image-01.image-right').each(getImgParallelStyler('right'));
    })();

    // Global Navigation
    (function(){
        $('#nav-global a[href]').each(function(){
            var _self = $(this);
            var _href = _self.attr('href').replace(/index\.html$/, '');
            if (0 <= location.href.search(_href)){ 
                _self.find('img').each(function(){
                    var _img = $(this);
                    var _src = _img.attr('src');
                    var _currentSrc = _src.replace(/\.gif$/, '_o.gif');
                    _img.attr('src', _currentSrc);
                });
                $(this).parent('li').addClass('unroll');
            }
        });
    })();

    // Local Navigation
    (function(){
        $('#nav-local a[href]').each(function(){
            var _self = $(this);
            var _href = _self.attr('href').replace(/index\.html$/, '');
            if(0 <= location.href.search(_href)){
                _self.parents().show();
                _self.siblings().show();
                _self.parents('li').addClass('current');
                $('#nav-local .nav-hidden').hide();
            }
        });
    })();
});

