

var Button = new Class({
    
    
    Implements: Options,
    
    
    options: {
        
        element: false,
        target: false,
        
        active: false,
        
        normal_src: '',
        mouseover_src: '',
        mousedown_src: ''
        
    },
    
    
    
    initialize: function(options) {
        
        this.setOptions(options);
        this.addEvents();
        
    },
    
    
    
    addEvents: function() {
        
        if(this.options.mouseover_src != '' && Rollover_Util.fileExists(this.options.mouseover_src)) {
            new Asset.image(this.options.mouseover_src);
            this.options.element.addEvent('mouseenter', this.mouseover.bind(this));
            this.options.element.addEvent('mouseleave', this.mouseout.bind(this));
        }
        
        if(this.options.mousedown_src != '' && Rollover_Util.fileExists(this.options.mousedown_src)) {
            new Asset.image(this.options.mousedown_src);
            this.options.element.addEvent('mousedown', this.mousedown.bind(this));
            this.options.element.addEvent('click',     this.mousedown.bind(this));
        }
        
        if(window.location.href.contains(this.options.element.href) && !this.options.element.hasClass('no_active')) {
            this.options.element.fireEvent('click');
        }
        
    },
    
    
    
    removeEvents: function() {
        this.options.element.removeEvents('mouseleave');
        this.options.element.removeEvents('mouseenter');
        this.options.element.blur();
    },
    
    
    
    swapSource: function(src) {
        this.options.target.setProperty('src', src);
    },
    
    
    
    mouseover: function() {
        this.swapSource(this.options.mouseover_src);
    },
    
    
    
    mouseout: function() {
        this.swapSource(this.options.normal_src);
    },
    
    
    
    mousedown: function() {
        this.swapSource(this.options.mousedown_src);
        this.removeEvents();
    }
    
    
    
});




var CSS_Button = new Class({
    
    
    Implements: Options,
    
    
    options: {
        
        element: false,
        
        target: false,
        
        active: false,
        
        normal_class: '',
        mouseover_class: '',
        mousedown_class: ''
        
    },
    
    
    
    initialize: function(options) {
        
        this.setOptions(options);
        this.addEvents();
        
    },
    
    
    
    addEvents: function() {
        
        if(this.options.mouseover_class != '') {
            this.options.element.addEvent('mouseenter', this.mouseover.bind(this));
            this.options.element.addEvent('mouseleave', this.mouseout.bind(this));
        }
        
        if(this.options.mousedown_class != '') {
            this.options.element.addEvent('mousedown', this.mousedown.bind(this));
            this.options.element.addEvent('click',     this.mousedown.bind(this));
        }
        
        if(window.location.href.contains(this.options.target.href)) {
            this.options.element.fireEvent('click');
        }
        
    },
    
    
    
    removeEvents: function() {
        this.options.element.removeEvents('mouseleave');
        this.options.element.removeEvents('mouseenter');
        this.options.element.blur();
    },
    
    
    mouseover: function() {
        this.options.element.addClass(this.options.mouseover_class);
    },
    
    
    mouseout: function() {
        this.options.element.removeClass(this.options.mouseover_class);
    },
    
    
    mousedown: function() {
        this.options.element.removeClass(this.options.mouseover_class);
        this.options.element.addClass(this.options.mousedown_class);
        this.removeEvents();
    }
    
    
    
});




var Rollover_Util = {
    
    
    fileExists: function(file) {
        var request_obj = this.requestObject();
        
        if(request_obj != false) {
            try {
                request_obj.open('HEAD', file, false);
                request_obj.send(null);
                return (request_obj.status == 200) ? true : false;
            } catch(e) {
                return false;
            }
        }
    },
    
    
    
    requestObject: function() {
        var request_obj;
        
        try {
            request_obj = new XMLHttpRequest();
        } catch(e) {
            if(!window['ActiveXObject']) return false;
            try {
                request_obj = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    request_obj = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    return false;
                }
            }
            return request_obj;
        }
        
        return request_obj;
    }
    
}





var CSS_Rollovers = new Class({
   
   
   Implements: Options,
   
   
   options: {
        elements: '',
        normal_class: '',
        mouseover_class: 'mouseover',
        mousedown_class: 'mousedown'
   },
   
   
   initialize: function(options) {
        this.setOptions(options);
        
        if(this.options.elements == '') {
            return;
        }
        
        $$(this.options.elements).each(function (element) {
            
            links = element.getElements('a');
            
            if(links.length > 0) {
                var link = links[0];
            }
            
            else {
                var link = element;
            }
            
            new CSS_Button({
                'element': element,
                'target': link,
                'normal_class': this.options.normal_class,
                'mouseover_class': this.options.mouseover_class,
                'mousedown_class': this.options.mousedown_class
            });
            
        }, this);
   }
    
    
});



var Rollovers = new Class({
    
    
    Implements: Options,
    
    
    options: {
        elements: '',
        normal_suffix: '_0',
        mouseover_suffix: '_1',
        mousedown_suffix: '_2'
    },
    
    
    initialize: function(options){
        this.setOptions(options);
        
        if(!Browser.Engine.trident || (Browser.Engine.trident && Browser.Engine.version > 4)) {
            
            if(this.options.elements == '') {
                return;
            }
            
            $$(this.options.elements).each(function (element) {
                
                var button      = {}
                var images      = element.getElements('img');
                
                if(images.length > 0) {
                    var target = images[0];
                }
                
                else {
                    var target = element;
                }
                
                var normal_src = target.getProperty('src');
                var image_file_extension = normal_src.substr(normal_src.lastIndexOf('.'));
                var mouseover_src = normal_src.substr(0, normal_src.lastIndexOf('.') - this.options.normal_suffix.length) + this.options.mouseover_suffix + image_file_extension;
                var mousedown_src = normal_src.substr(0, normal_src.lastIndexOf('.') - this.options.normal_suffix.length) + this.options.mousedown_suffix + image_file_extension;
                
                new Button({
                    
                    'element': element,
                    'target': target,
                    
                    'normal_src': normal_src,
                    'mouseover_src': mouseover_src,
                    'mousedown_src': mousedown_src
                    
                });
                
            }, this);
        }
    }
    
    
});



