﻿var rotator = null;
var intervalID = null;

function initRotator() {
    var c1 = 0;
    var c2 = 0;
    var c3 = 0;
    var r = rotator;

    $$('li.rotator-image').each(function(item) {
        if (c1 == 0)
            item.set('opacity', 1)
        else
            item.set('opacity', 0)

        c1++;
    });

    $$('li.rotator-image').addEvents({
        mouseenter: function() {
            r.pause = true;
        },
        mouseleave: function() {
            r.pause = false;
        }
    });

    $$('li.rotator-caption').each(function(item) {
        if (c2 == 0)
            item.set('opacity', 1)
        else
            item.set('opacity', 0)

        c2++;
    });

    $$('li.rotator-text').each(function(item) {
        if (c3 == 0)
            item.set('opacity', 1)
        else
            item.set('opacity', 0)

        c3++;
    });

    $$('.rotator-nav').addEvents({
        mouseenter: function() {
            dC(true);
        },
        mouseleave: function() {
            dC(false);
        }
    });
    
    eE($('rotator'));
}

function Rotator() {
    this.ms = 0;
    this.increment = 10;
    this.interval = 4000;
    this.auto = true;
    this.pause = false;
    this.activeIndex = 0;
    this.transition = 1500;
    this.busy = false;
    this.count = 0;
}

function doTimer() {
    var r = rotator;
    var index;

    if (r.activeIndex == r.count)
        index = 0;
    else
        index = r.activeIndex + 1;

    if (r.auto) {
        if (r.ms == r.interval) {
            xFade(index);
            r.ms = 0;
        }
        else {
            if (!r.pause)
                r.ms += r.increment;
        }
    }
    else
        clearInterval(intervalID);
}

function xFade(index) {
    var r = rotator;
    var li1;
    var li2;
    var li3;
    var li4;

    if (index == r.activeIndex || r.busy)
        return;
    else
        r.busy = true;

    li1 = $('ri' + r.activeIndex);
    li2 = $('ri' + index);
    li3 = $('rc' + r.activeIndex);
    li4 = $('rc' + index);
    li5 = $('rt' + r.activeIndex);
    li6 = $('rt' + index);

    var fx1 = new Fx.Morph(li1, {
        duration: r.transition
    });
    fx1.start({
        'opacity': [1, 0]
    });

    var fx2 = new Fx.Morph(li2, {
        duration: r.transition
    });
    fx2.start({
        'opacity': [0, 1]
    }).chain(function() { r.busy = false });

    var fx3 = new Fx.Morph(li3, {
        duration: r.transition
    });
    fx3.start({
        'opacity': [1, 0]
    });

    var fx4 = new Fx.Morph(li4, {
        duration: r.transition
    });
    fx4.start({
        'opacity': [0, 1]
    })

    var fx5 = new Fx.Morph(li5, {
        duration: r.transition
    });
    fx5.start({
        'opacity': [1, 0]
    });

    var fx6 = new Fx.Morph(li6, {
        duration: r.transition
    });
    fx6.start({
        'opacity': [0, 1]
    })

    r.activeIndex = index;
}

function kill() {
    rotator.auto = false;
}

function nav(index) {
    var r = rotator;
    var count = r.count;

    if (index > count)
        index = 0;
    else if (index < 0)
        index = count;

    xFade(index);
}

/*-- banner ad rotator ~ begin --*/
var bar = null;
var barInterval = null;

function BannerAdRotator() {
    this.ms = 0;
    this.increment = 1000;
    this.interval = 0;
    this.activeIndex = 0;
    this.count = 0;
}

function doBarTimer() {
    var r = bar;
    var index;

    if (r.ms == r.interval) {   // change the banner ad html
        if (r.activeIndex == r.count) {
            index = 0;
        }
        else {
            index = r.activeIndex + 1;
        }

        loadRotator(index);
        r.interval = arr[index][1];
        r.ms = 0;
        r.activeIndex = index;
    }
    //else {
    r.ms += r.increment;
    //}
}

function loadRotator(index) {
    $('banner-ad-rotator').set('html', arr[index][0]);
}
/*-- banner ad rotator ~ end --*/