﻿var intervalID = 0;
var period = 12000;

function makeActive(next, active, time) {
    active.stop().addClass('last-active');
    var c = next.attr('class');

    next.css({ opacity: 0.0 })
            .addClass('active')
            .stop()
            .animate({ opacity: 1.0 }, time, function () {
                var cont = jQuery('#RotatingHead div.rotating_img_text');

                active.removeClass('active last-active');

                jQuery('div.' + active.attr('class') + ' a', cont).removeClass('active');
                jQuery('div.' + c.replace("last-active", "").replace("active", "") + ' a', cont).addClass('active');
            });
}

function slideSwitch() {
    var container = jQuery('#RotatingHead .images');

    var jQueryactive = jQuery('IMG.active', container);

    if (jQueryactive.length == 0)
        jQueryactive = jQuery('IMG:last', container);

    // use this to pull the images in the order they appear in the markup
    var jQuerynext = jQueryactive.next().length
            ? jQueryactive.next()
            : jQuery('IMG:first', container);

    makeActive(jQuerynext, jQueryactive, 1500);
}


function Activate(active) {
    var container = jQuery('#RotatingHead .images');

    var jQueryactive = jQuery('IMG.active', container);

    if (!jQueryactive.hasClass(active)) {
        clearInterval(intervalID);

        makeActive(jQuery('.' + active, container), jQueryactive, 1500);

        intervalID = setInterval("slideSwitch()", period);
    }
}

jQuery(function() {
    intervalID = setInterval("slideSwitch()", period);
    jQuery(".images img").each(function() {
        var img = jQuery(this);
        img.attr("src", img.attr("id"));
    });
});
