$(document).ready(function () {
    resizeSpacer();
    $(window).resize(resizeSpacer);

    // Drop down menus
    if ($.browser.msie && $.browser.version < 8) {
        $('ul.sf-menu li li ul').css('opacity', '0.8');
    }
    else {
        $('ul.sf-menu li li').css('opacity', '0.85'); //works w ie8, not ie7
    }

    $("ul.sf-menu").supersubs({ minWidth: 10, maxWidth: 11, extraWidth: 1 }).superfish({ speed: 'fast', autoArrows: false, animation: { height: 'show'} }).find('ul').bgIframe({ opacity: true });
    $('#innerRegion .smallshadow').css('margin-top', $('#cr0101c').height() - 6 + 'px');

    // Text over text boxes
    $('label').labelOver('over-apply');

    //default button setting
    // do we need to increment .length manually?
    $btn = $('.gelbtn34d'); $btn.add($('.gelbtn150d'));
    var $form = $btn.parents('.form');
    $form.keypress(function (e) {
        if (e.which == 13 && e.target.type != 'textarea') {
            if ($btn[0].type == 'submit')
                $btn[0].click();
            else
                eval($btn[0].href);
            return false;
        }
    });

    //Stock Ticker
    jQuery('#jcarousel1').jcarousel({
        scroll: 2,
        auto: 2,
        wrap: 'circular',
        animation: 3500,
        easing: 'swing'
    });

    //Side Menu
    var queryString = window.location.search.toLowerCase();
    $('#sideMenu li a').parent().append('<div class="arrows"></div>');
    $('#sideMenu ul li span').css('padding-right', '15px');
    $('#sideMenu li a').hover(function () { if ($(this).attr('href').toLowerCase() != queryString) $(this).parent().find('.arrows').animate({ width: '12px' }, 150); },
                              function () { if ($(this).attr('href').toLowerCase() != queryString) $(this).parent().find('.arrows').animate({ width: '0px' }, 150); });
    $('#sideMenu li a').each(function () {
        if ($(this).attr('href').toLowerCase() == queryString) { $(this).css('font-weight', 'bold').parent().find('.arrows').css('width', '12px'); }
        else $(this).css('font-weight', 'normal').parent().find('.arrows').css('width', '0px');
    });

    //JBA
    if ($.query.get('i') == 'jba') {
        var bgcolor = '#00437E';
        $('.footer').css('background-color', bgcolor).css('background-image', 'url(/app_themes/lea/images/bluebarweightmed.png)');
        $('#cr0100').css('background-color', bgcolor).css('background-image', 'url(/app_themes/lea/images/bluebarweight.png)');
        $('.newsHeader').css('background-color', bgcolor).css('background-image', 'url(/app_themes/lea/images/bluebarweightsmall.png)');
    }

    $('#innerRegion').localScroll({ axis: 'xy', queue: true, hash: false, duration: 1000 });
    $('.expandable').append('<hr>');
    $('.expandable .content').toggle();
    $('.expandable .content').append('<span class="toggler">Collapse</span>');
    $('.expandable').click(function (e) { if ($(e.target).children('.content').length > 0) { $(e.target).children('.content').toggle('fast'); } else { $(e.target).parent().children('.content').toggle('fast'); } });
    $('.toggler').click(function (e) { if ($(e.target).parent().children('.content').length > 0) { $(e.target).parent().children('.content').toggle('fast'); } else { $(e.target).parent().parent().children('.content').toggle('fast'); $.scrollTo($(this).parent(), 1700, { offset: { top: -60} }); } });

    var newsItem = $('#' + $.query.get('newsitem'));
    if (newsItem.length != 0) { newsItem.click().scrollTo(1700, { offset: { top: -60} }); }
});

function jcarousel1_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function () {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function () {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        carousel.stopAuto();
    }, function () {
        carousel.startAuto();
    });
};

function resizeSpacer() {
    if ($('.contentSpacer').length != 0) {
        var space = 124;
        if ($(window).width() <= (960 + $('.contentSpacer.l').width() + $('.contentSpacer.r').width())) { space = ($(window).width() - 960) / 2; }
        if (space < 1) { space = 0; }
        $('.contentSpacer').css('width', space + 'px');
        $('#contentWrapper').css({ 'margin-left': space + 'px', 'margin-right': space + 'px' });
        $('#outerregion01').css('width', (960 + space + space) + 'px');
    }
}
