$(document).ready(function () {
    var b = $('.headlines .itemList a[class!=all]');
    b.bind('mouseenter', function (a) {
        if (parseInt(a.clientX) > 0) {
            $('.headlines').data('hovered', true)
        }
    });
    b.bind('mouseleave', function () {
        $('.headlines').data('hovered', false)
    })
});
$(window).load(function () {
    var d = $('.headlines');
    if (!d) {
        return false
    }
    var e = $('li', d),
        $current = $('.itemList a.current', d),
        $bullets = $('.itemList a[class!=all]', d),
        $title = $('.title', d),
        interval = 3000;
    e.css({
        'position': 'absolute',
        'left': 0,
        'opacity': 0,
        'top': 0,
        'z-index': 1
    });
    e.each(function (i, a) {
        var b = $(a),
            $img = b.find('img');
        if ($img && (b.width() - $img.width() > 20) && !d.hasClass('sideSlide')) {
            $gradient = $('<div class="pngfix">');
            $gradient.css({
                'background': 'transparent ' + b.css('background-image') + ' repeat-y 0 0',
                'height': b.height(),
                'left': $img.width() - 40,
                'position': 'absolute',
                'top': 0,
                'width': 40
            });
            b.append($gradient)
        }
    });
    e.eq(parseInt($current.text()) - 1).css({
        'opacity': 1,
        'z-index': 2
    });
    $bullets.bind('mouseenter', function (a) {
        if (parseInt(a.clientX) > 0 && d.data('timer')) {
            clearInterval(d.data('timer'))
        }
        var b = parseInt($(this).text()) - 1,
            currentNo = parseInt($current.text()) - 1,
            zIndex = parseInt(e.eq(currentNo).css('z-index'));
        if (zIndex > 3) {
            e.css('z-index', 1);
            zIndex = 1
        }
        e.eq(b).stop(true).css({
            'opacity': 0,
            'z-index': zIndex + 1
        }).animate({
            'opacity': 1
        }, 750);
        $title.attr('href', e.eq(b).find('a').attr('href')).text(e.eq(b).find('img').attr('alt'));
        $title.stop(true).css({
            'height': 'auto',
            'opacity': 0,
            'overflow': 'visible'
        });
        var c = $title.height();
        $title.css({
            'height': 0,
            'overflow': 'hidden'
        }).animate({
            'height': c,
            'opacity': 1
        }, 500);
        $current.removeClass('current');
        $(this).stop(true).addClass('current');
        $current = $(this)
    });
    $bullets.bind('mouseleave', function (a) {
        clearInterval(d.data('timer'));
        d.data('timer', setInterval(f, interval))
    });
    var f = function () {
            if (!$('.headlines').data('hovered')) {
                var a = parseInt($current.text());
                var b = (a === $bullets.length) ? 0 : a;
                $bullets.eq(b).trigger('mouseenter')
            }
        };
    d.data('timer', setInterval(f, interval))
});
