var SiteRotator = new Class({
    initialize: function(obj){
        this.obj = obj;
        this.timer = obj.timer;
        this.FX = null;
        this.currentImg = null;
        this.currentDescription = null;
        this.nextImg = null;
        this.nextDescription = null;
        this.Images = new Array();
        this.Descriptions = new Array();
        this.registerImages();
        this.start();
    },
    
    registerImages: function(){
        this.obj.images.each(function(imgObj){
            this.Images.push(this.createImage(imgObj));
            this.Descriptions.push(imgObj.description);
        }, this);
    },
    
    createImage: function(imgObj){
        var newEl = new Element('img', {
            'styles': {
                'position': 'absolute',
                'display': 'block',
                'border': '0px solid black',
                'z-index': 0,
                'opacity': 1
            },
            'class': this.obj.imageClass,
            'description': imgObj.description,
            'src': this.obj.imagePathPrefix + imgObj.name
        });
        
        if(imgObj.path!=null){
            newEl.setStyle('cursor', 'pointer');
            newEl.set('path', imgObj.path);
            newEl.addEvent('click', this.click.bindWithEvent(this, newEl));
            newEl.addEvent('mouseenter', this.over.bindWithEvent(this, newEl));
            newEl.addEvent('mouseleave', this.out.bindWithEvent(this, newEl));
        }
        
        return newEl;
    },
    
    click: function(){
        //TCSite.loadPage(arguments[1].get('path'));
        TCSite.redirect(arguments[1].get('path'));
    },
    
    over: function(){
        
    },
    
    out: function(){
        //if(this.FX) this.FX.cancel();
        //this.FX = new Fx.Morph(arguments[1], {duration: 300, transition: Fx.Transitions.Circ.easeIn}).start({
        //    'width': 100,
        //    'height': 100
        //});
    },
    
    fade: function(img){
        if(this.FX) this.FX.cancel();
        this.FX = new Fx.Morph(img, {duration: this.obj.fadeTime, transition: Fx.Transitions.Circ.easeIn}).start({
            'opacity': 0
        });
    },
    
    fadeComplete: function(){
            this.currentImg.setStyle('z-index', 0 );
            this.setCurrentImage(this.nextImg);
            this.setCurrentDescription(this.nextDescription);
            this.loadDescription(this.currentDescription);
    },
    
    setCurrentImage: function(img){
        this.currentImg = img;
        this.currentImg.setStyle('z-index', 20 );
        this.currentImg.setStyle('opacity', 1 );
    },

    setCurrentDescription: function(description){
        this.currentDescription = description;
    },
    
    loadImage: function(img){
        if(!$(this.obj.imageTargetID).hasChild(img)){
            $(this.obj.imageTargetID).grab(img);
        }
    },

    loadDescription: function(description){
        //if(!$(this.obj.descriptionTargetID).hasChild(description)){
            $(this.obj.descriptionTargetID).innerHTML = "<b>" + description + "</b>";
        //}
    },
    
    setNext: function(){
        
        if(this.Images.length > this.Images.indexOf(this.currentImg)+1 && this.Images.indexOf(this.currentImg)+1 != -1 ){
            this.nextImg = this.Images[(this.Images.indexOf(this.currentImg) + 1)];
            this.nextDescription = this.Descriptions[(this.Images.indexOf(this.currentImg) + 1)];
        }else{
            this.nextImg = this.Images[0];
            this.nextDescription = this.Descriptions[0];
        }
        this.nextImg.setStyle('z-index', 10 );
        this.nextImg.setStyle('opacity', 1 );
        this.loadImage(this.nextImg);
        //this.loadDescription(this.nextDescription);
    },
            
    showNext: function(){
        this.fade(this.currentImg);
        this.setNext(); 
        this.fadeComplete.delay(this.obj.fadeTime, this);
    },
    
    start: function(){
        this.loadImage(this.Images[0]);
        this.loadDescription(this.Descriptions[0]);
        this.setCurrentImage(this.Images[0]);
        this.setCurrentDescription(this.Descriptions[0]);
        this.setNext();
        this.timer.start();
    }
    
});

