var HomePageBanner = new Class({
    initialize: function(link_div, banners, target){
        this.link_div = link_div;
        if (banners.length < 2) {
            /*Stop any processing, there's nothing to rotate*/
            return;
        }
        this.updating = false;
        this.id = 0;
        this.addControlLinks();
        this.banners = banners;
        this.target = target;
        this.elements = new Array();
        this.interval = null;
        this.ems = {
            'previous': function(e){e.stop(); this.prev();}.bind(this),
            'next': function(e){e.stop(); this.next();}.bind(this),
            'play': function(e){e.stop(); this.play();}.bind(this),
            'pause': function(e){e.stop(); this.pause();}.bind(this)
        }
        for(var i=0; i < this.banners.length; i++){
            this.elements.push(this.generateElement(i));   
        };
        this.addEvents();
        this.next();
        var base_img = this.target.getElement('img');
        this.target.set('styles', {'height': 273, 'width': 941});
        base_img.dispose();
        this.updateBanner(this.id);
        this.play();
    },
    
    addControlLinks: function(){
        this.links = new Array();
        var pause = new Element('a', {'href':'#', 'class':'pause'}).set('text','Pause');
        pause.injectInside(this.link_div);
        var previous = new Element('a', {'href':'#', 'class':'previous'}).set('text','Previous');
        previous.injectInside(this.link_div);
        var next = new Element('a', {'href':'#', 'class':'next'}).set('text','Next');
        next.injectInside(this.link_div);
        this.links.push(pause);
        this.links.push(previous);
        this.links.push(next);
    },
    
    addImageEvent: function(img){
        img.addEvent('mouseover', function(e){this.softPause();}.bind(this));
        img.addEvent('mouseout', function(e){this.softPlay();}.bind(this));
    },
    
    addEvents: function(){
        this.links.each(function(e){
            e.addEvent('click', this.ems[e.className]); 
        }.bind(this));
    },
    
    generateElement: function(id){
        var data = this.banners[id];
        var a = new Object();
        a['img'] = new Element('img', {'src':data.src, 'alt':data.alt, 'usemap':'#imagemap'});
        this.addImageEvent(a['img']);
        a['area'] = new Element('area', {'shape':'rect', 'coords':data.hotspot_coords, 'href':data.hotspot_url, 'alt':data.hotspot_alt});
        return a;
    },
    
    updateBanner: function(id){
        this.updating = true;
        var e = this.generateElement(id);
        var i = this.target.getElement('img');                
        var map = this.target.getElement('map');
        replace = this.elements[id];
        map.empty();
        map.adopt(replace.area);
        replace.img.set('styles', {'position': 'absolute'});
        replace.img.fade('show');
        replace.img.injectTop(this.target);
        if (i){
            i.get('tween').addEvent('complete', function(e){
                var img = this.target.getElements('img')[1];
                if (img && img.getStyle('visibility') == 'hidden') {
                    img.dispose();
                    this.updating = false;
                }
            }.bind(this));
            i.fade('out');
        }
    },
    
    next: function(){
        if(!this.updating){
            this.id = (this.id + 1) % this.elements.length;
            this.updateBanner(this.id);
        }
    },
    
    prev: function(){
        if(!this.updating){
            this.id = (this.id - 1) % this.elements.length;
            if(this.id < 0) this.id += this.elements.length;
            this.updateBanner(this.id);
        }
    },
    
    softPlay: function(){
        if(!this.interval){
            this.interval = setInterval(this.next.bind(this), 5000);
            this.togglePlay(true);
        }    
    },
    
    play: function(){
        this.softPlay();
    },
    
    softPause: function(){
        if(this.interval){
            clearInterval(this.interval);
            this.interval = null;
        }
    },
    
    pause: function(){
        if(this.interval){
            this.softPause();
            this.togglePlay(false);
        }
    },
    
    togglePlay: function(play){
        this.links.each(function(e){
            if(e.className == 'play' || e.className == 'pause'){
                if(play && e.className == 'play'){
                    e.className = 'pause';
                    e.removeEvents();
                    e.addEvent(this.ems[e.className]);
                }else if(!play && e.className != 'play'){
                     e.className = 'play';
                    e.removeEvents();
                    e.addEvent(this.ems[e.className]);
                }
            }
            e.addEvent('click', this.ems[e.className]); 
        }.bind(this));
    }
});

