﻿var rotator = null;
var intervalID = null;

window.addEvent('domready', function() {
//    var el = $('rotator');
//    
//	var req = new Request.HTML({url:'tml/ajax/GetRotator1.aspx', 
//		onSuccess: function(html) {
//			el.set('text', '');
//			el.adopt(html);
//			initRotator();
//		},
//		onFailure: function() {
//			el.set('text', 'The request failed.');
//		}
//	});
//	
//    req.send();
});

function initRotator() {
    var c1 = 0;
    var c2 = 0;
    var r = rotator;
    
	$$('li.ri').each(function(item){
	    if (c1 == 0)
	        item.set('opacity', 1)
	    else
	        item.set('opacity', 0)
	        
	    c1++;
	});
	
	$$('li.ri').addEvents({
		mouseenter: function(){
		    r.pause = true;
		},
		mouseleave: function(){
		    r.pause = false;
		}
	});

	$$('li.rc').each(function(item) {
	    if (c2 == 0)
	        item.set('opacity', 1)
	    else
	        item.set('opacity', 0)

	    c2++;
	});

	$$('li.rt').addEvents({
		mouseenter: function(){
		    dC(true);
		},
		mouseleave: function(){
		    dC(false);
		}
	});
//	
//    $$('div.rotator-nav').addEvent('click', function(e) {
//        e.stop();
//        
//    });
}

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 = 3;
}

function doRotatorTimer()
{
    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 ri1;
    var ri2;
    var rc1
    var rc2;

    if (index == r.activeIndex || r.busy)
        return;
    else
        r.busy = true;
    
    ri1 = $('ri'+r.activeIndex);
    ri2 = $('ri'+index);
    rc1 = $('rc'+r.activeIndex);
    rc2 = $('rc'+index)
    
    var fx1 = new Fx.Morph(ri1, {
	    duration: r.transition
    });
    fx1.start({
	    'opacity':[1,0]
	}).chain(function() { r.busy = false });
    
    var fx2 = new Fx.Morph(ri2, {
	    duration: r.transition
    });
    fx2.start({
	    'opacity':[0,1]
    });

    var fx3 = new Fx.Morph(rc1, {
	    duration: r.transition
    });
    fx3.start({
        'opacity':[1,0]
    });

    var fx4 = new Fx.Morph(rc2, {
	    duration: r.transition
    });
    fx4.start({
        'opacity':[0,1]
    });

    r.activeIndex = index;
}

function kill()
{
    rotator.auto = false;
}

rotator = new Rotator();
intervalID = setInterval('doRotatorTimer()', rotator.increment);
