$(document).ready(function () {
    carousel();
    newsCycle();
});

function carousel() {
    var cl = 1;
    var $first = $('#slideshow a:first').attr('href');
    $('<img src="' + $first + '" />').load(start);
    function start() {
        $('#slideshow a').each(function() {
            var $lia = this.href;
            if (cl == 1) {
                $("#slideshow").append('<img alt="' + $lia + '" class="active" src="' + $lia + '" />')

            } else {
                $("#slideshow").append('<img alt="' + $lia + '" class="' + $(this).attr('class') + '" />')
            }
            cl += 1;
        })
        if (cl > 2) { setInterval("slideSwitch()", 5000); }
    }
}

function slideSwitch() {
    var $active = $('#slideshow img.active');
    if ($active.length == 0) $active = $('#slideshow img.active');
    var $next = $active.next().length ? $active.next() : $('#slideshow img:first');
    $('<img src="' + $next.attr('alt') + '" />').load(nextImg);
    function nextImg() {
        $next.attr('src', $next.attr('alt'));
        $active.addClass('last-active');
        $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1500, function() {
            $active.removeClass('active last-active');
        })
    }
}

function newsCycle() {
    $('#homeNews').before('<div id="cyclePager" class="pager">').cycle({
    fx: 'fade',
    speed: 400,
    timeout: 5000,
    pager: '#cyclePager',
    cleartype: true, cleartypeNoBg: true //ie7/8 bug
});
    $("#cyclePager a").html("&nbsp;");
    $("#cyclePager a").click(function () {
        $('#ctl00_Zone2_16_dlNews').cycle('pause');
    })
}
