window.addEvent('domready', function() {
    
if ($('headlines')) {
    var myHeadlines = new rotator({ imageContainers: $$('#photos li'), text: $$('#text li'), numbers: $$('#numbers li a') });
} var gallery = $$('.gallery a');
});

var rotateImg;



var Asset = { javascript: function(f, d) {
    d = $extend({ onload: $empty, document: document, check: $lambda(true) }, d);
    var b = new Element("script", { src: f, type: "text/javascript" });
    var e = d.onload.bind(b), a = d.check, g = d.document;
    delete d.onload;
    delete d.check;
    delete d.document;
    b.addEvents({ load: e, readystatechange: function() {
        if (["loaded", "complete"].contains(this.readyState)) {
            e();
        } 
    } 
}).set(d);
if (Browser.Engine.webkit419) {
    var c = (function() {
        if (!$try(a)) {
            return;
        } $clear(c);
        e();
    }).periodical(50);
} return b.inject(g.head);
}, css: function(b, a) {
    return new Element("link", $merge({ rel: "stylesheet", media: "screen", type: "text/css", href: b }, a)).inject(document.head);
}, image: function(c, b) {
    b = $merge({ onload: $empty, onabort: $empty, onerror: $empty }, b);
    var d = new Image();
    var a = document.id(d) || new Element("img");
    ["load", "abort", "error"].each(function(e) {
        var f = "on" + e;
        var g = b[f];
        delete b[f];
        d[f] = function() {
            if (!d) { return; } if (!a.parentNode) {
                a.width = d.width; a.height = d.height;
            } d = d.onload = d.onabort = d.onerror = null;
            g.delay(1, a, a);
            a.fireEvent(e, a, 1);
        };
    });
    d.src = a.src = c;
    if (d && d.complete) {
        d.onload.delay(1);
    } return a.set(b);
}, images: function(d, c) {
    c = $merge({ onComplete: $empty, onProgress: $empty, onError: $empty, properties: {} }, c);
    d = $splat(d);
    var a = [];
    var b = 0;
    return new Elements(d.map(function(e) {
        return Asset.image(e, $extend(c.properties, { onload: function() {
            c.onProgress.call(this, b, d.indexOf(e));
            b++;
            if (b == d.length) {
                c.onComplete();
            } 
        }, onerror: function() {
            c.onError.call(this, b, d.indexOf(e));
            b++;
            if (b == d.length) {
                c.onComplete();
            } 
        } 
    }));
}));
} 
};
var rotator = new Class({ Implements: [Options, Events], options: { currentImg: 0, swapCycle: 3, imageContainers: [], text: [], numbers: [] }, initialize: function(options) {
    this.setOptions(options);
    this.protoTimer = new Hash({ counter: 0 });
    $$(this.options.text).fade('hide');
    $$(this.options.imageContainers).fade('hide');
    this.imageList = this.options.imageContainers.map(function(item, index) {
        return $(item).getFirst().get('rel');
    });
    this.preloadImages(this.imageList);
    $$(this.options.numbers).addEvent('mouseover', function(e) {
        index = this.options.numbers.indexOf(e.target);
        this.swapImage(index);
    } .bind(this), this);
}, preloadImages: function(imageList) {
    this.loadedImages = new Asset.images(imageList, { onProgress: function(counter, index) {
        this.loadedImages[index].inject(this.options.imageContainers[index].getFirst());
        if (counter == 0) this.swapImage(index);
    } .bind(this), onComplete: function() {
        $('photos').removeClass('spinner');
        this.timer('start');
    } .bind(this)
});
}, fadeImage: function(fadeType) {
    number = this.options.currentImg; this.options.text[number].fade(fadeType);
    this.options.imageContainers[number].fade(fadeType);
    if (fadeType == 'in') {
        this.options.numbers[number].addClass('active');
    } else {
        this.options.numbers[number].removeClass('active');
    } 
}, swapImage: function(newImage) {
    this.fadeImage('out');
    if ($chk(newImage)) {
        this.options.currentImg = newImage;
    } else {
        this.options.currentImg = (this.options.currentImg == (this.options.text.length - 1)) ? 0 : this.options.currentImg + 1;
    } this.fadeImage('in');
    this.timer('reset');
}, timer: function(action) {
    simpleTimer = function() {
        currentTime = this.protoTimer.counter++;
        if (currentTime >= this.options.swapCycle) { this.swapImage(); } 
    } .bind(this);
    if (action == 'start') this.buttonTimer = simpleTimer.periodical(1000, this.protoTimer);
    else if (action == 'reset') this.protoTimer.set('counter', 0);
                    }});
