var FeatureRotator = Class.create({
    Version : '0.1',
    Index : location.href.slice(location.href.indexOf('?')).toQueryParams().n || 0,
    Counter : 0,
    
    initialize : function(target, indicators, generator, interval) {
        //console.log('initialize');
        this.target = target;
//        this.indicators = indicators;
        this.generator = generator;
        this.interval = interval;
        this.pe;
        this.featureStack = [];
//        this.indicatorStack = [];
        this.populateStack();
//        this.indicatorStack[0].src = '/images/' + this.indicators + 'Progress.gif';
        this.start();
    },
    
    populateStack : function() {
        //console.log('populateStack');
//        this.indicatorStack = $A($(this.indicators).getElementsByTagName('img'));
        for (i = 0; i < 5; i++) { this.requestFeature(i); }
   },
    
    requestFeature : function(i) {
        //console.log('requestFeature');
        new Ajax.Request(this.generator + '?n=' + i + '&JSON=1',
            {
                method:'get',
                onSuccess: function(transport) {
                    var response = transport.responseText || "no response text";
                    if (response.isJSON()) {
                        response = response.evalJSON();
                        this.featureStack[i] = response;
                        preloadImages(response.feature.image);
                    }
                }.bind(this)
            }
        );
    },
    
    start : function() {
        //console.log('start');
        this.pe = new PeriodicalExecuter(this.displayNextFeature.bind(this), this.interval);
    },
    
    displayNextFeature : function() {
        //console.log('displayNextFeature');
        if (this.featureStack[(parseInt(this.Index) + 1) % 5]) {
//            this.indicatorStack[this.Index].src = '/images/' + this.indicators + 'Next.gif';
//            this.indicatorStack[(parseInt(this.Index) + 1) % 5].src = '/images/' + this.indicators + 'Progress.gif';
        
            this.Index++;
            this.Index = this.Index % 5;

            var imageTarget = $(this.target).getElementsByTagName('img')[0];
            Effect.Fade(imageTarget, { afterFinish: function() {
                    imageTarget.addClassName('hide');
                    imageTarget.src = this.featureStack[this.Index].feature.image;
                    imageTarget.removeClassName('hide')
                    Effect.Appear(imageTarget);
                }.bind(this)}
            );

            imageTarget.parentNode.href = this.featureStack[this.Index].feature.link;
            imageTarget.alt = this.featureStack[this.Index].feature.head;
        } else { // in case the db barfed
//            this.indicatorStack[0].src = '/images/' + this.indicators + 'Next.gif';
        }
        this.Counter++;
        if (parseFloat(navigator.userAgent.indexOf('6.0')) > 0 && navigator.appName == "Microsoft Internet Explorer" && this.Counter == 5) { this.exit(); }
        else if (this.Counter == 25) { this.exit(); }
    },
    
    displayArbitraryFeature : function(f) {
        //console.log('displayArbitraryFeature');
//        this.indicatorStack[this.Index].src = '/images/' + this.indicators + 'Next.gif';
//        try { this.indicatorStack[this.Index].blur(); }
//        catch(err) { }
        this.Index = f == 0 ? 4 : (f - 1) % 5;
        this.pe.stop();
        this.displayNextFeature();
        this.start();
    },
    
    exit : function() {
//        this.indicatorStack[0].src = '/images/' + this.indicators + 'Next.gif';
        this.pe.stop();
    }
});

var UpdateStamper = Class.create({
    Version : '0.2',
    initialize : function(generator) {
        //console.log('initialize');
        this.generator = generator;
        this.updates;
        this.getAllUpdates();
    },
    getAllUpdates : function() {
        //console.log('getAllUpdates');
        new Ajax.Request(this.generator,
            {
                method:'get',
                onSuccess: function(transport) {
                    var response = transport.responseText || "no response text";
                    if (response.isJSON()) {
                        this.updates = response.evalJSON();
                        this.insertStamps();
                    }                        
                }.bind(this)
            }
        );
    },
    insertStamps : function() {
        //console.log('insertStamps');
        $A(this.updates).each( function(u) {
            a = $(u.element).getElementsByTagName('a')[0];
            a.innerHTML = '<span class="updated" title="' + u.posted + '" style="left: ' + u.left + 'px; top: ' + 
                u.top + 'px; filter: alpha(opacity=' + u.opacityIE + '); -moz-opacity: ' + 
                u.opacity + '; opacity: ' + u.opacity + ';"></span>' + a.innerHTML;
        });
    }
});

document.observe('dom:loaded', function() {
    // Fire up the feature rotator, if necessary.
    if ($('splash')) {
        var featureRotator = new FeatureRotator('splash', 'featureIndicators', '/inc/generateFeature.php', 10);

//        $A($('featureIndicators').getElementsByTagName('a')).each(
//            function(s) {
//                Event.observe(s, 'click', function(e) {
//                        featureRotator.displayArbitraryFeature(parseInt(s.href.slice(s.href.indexOf('?')).toQueryParams().n));
//                        Event.stop(e);
//                    });
//            }
//        );
    }
});
