/*
    Creates the sliding content object
*/
var ContentCarosel = new Class({
    initialize : function (main, titleSelector, panesContainerSelector, itemSelector) {
        
        this.panes = main.getElements(itemSelector);
        this.selectedPane;
        
        var c = main.getElement(panesContainerSelector);
        var title = main.getElement(titleSelector);
        var nav = new Element('div', {'class': 'contentCarosel_nav' });
        title.adopt(nav);
        title.setStyles({'position': 'relative', 'top' : '0', 'left': '0'});
        
        this.paneContainer = new Element('div'); 
// ADDED Z-INDEX JAN 8 2009        
        c.setStyles({'position':'relative', 'top':'0', 'left':'0', 'z-index': '1', 'overflow': 'hidden', 'height': '100%'});
        c.adopt(this.paneContainer);
        
        this.paneContainer.setStyles({'position':'absolute', 'top':'0'});
        var targetWidth = c.getCoordinates().width;
        this.scrollAnim = new Fx.Style(this.paneContainer, 'left', {'duration': 700, 'wait': false, 'transition': Fx.Transitions.Cubic.easeInOut});
// these are two functions that are used by the buttons        
        var disableButton = function () {this.addClass('disabled');};
        var enableButton = function () {this.removeClass('disabled');};
// create and add the "previous" button        
        this.prevButton = new Element('span', {'class':'contentCarosel_prevButton'});
        nav.adopt(this.prevButton );
        this.prevButton.addEvent('click', this.doButtonClick.pass(['prev'], this));
        
        this.prevButton.disable = disableButton;
        this.prevButton.enable = enableButton;
        
        var selectedItem = null, maxHeight = 0, currLoc = 0;
        for (var i = 0, l= this.panes.length; i<l;i++) {
            var p = this.panes[i];
            var isSelected = p.hasClass('contentCarosel_selected');
            p.selectionIndex = i;
            p.leftLoc = currLoc;
            p.setStyles({'position': 'absolute', 'top':'0', 'left': p.leftLoc+'px', 'width': targetWidth +'px', 'display': 'block'});
            var coords = p.getCoordinates();
            currLoc = currLoc + coords.width + 10;
            p.paneIndex = i+1;
            this.paneContainer.adopt(p);
// selectes the first item, or which ever is set to 'contentCarosel_selected'
            if (isSelected || i == 0) { this.selectedItem = p; }
            if (coords.height > maxHeight) { maxHeight = coords.height; }
        }
// IE6 adds some extra space for some reason, so this takes it out
        if (window.ie6) { maxHeight = maxHeight - 22; }
        
        this.locationIndecator = new Element('span', {'class':'contentCarosel_locationIndecator'});
        this.locationIndecator.setText(' X / ' + this.panes.length + ' ');
        nav.adopt(this.locationIndecator);
// create and add the "next" button    
        this.nextButton = new Element('span', {'class':'contentCarosel_nextButton'});
        nav.adopt(this.nextButton);
        this.nextButton.disable = disableButton;
        this.nextButton.enable = enableButton;
        this.nextButton.addEvent('click', this.doButtonClick.pass(['next'], this));
// set to the selected item        
        this.scrollAnim.set('-' + this.selectedItem.leftLoc);
        this.locationIndecator.setText(this.selectedItem.paneIndex +  ' / '  + this.panes.length);
        
        this.setButtonStates();
        main.setStyle('height', ( maxHeight + title.getCoordinates().height )+'px');
// ADDED BLOCK UNTILL END OF FUNCTION JAN 8, 2009
        this.blockAutoForward = false;
        this.timer = new Timer(9000);
        this.timer.addEvent('onExpire', this.autoForward.bind(this));
        main.addEvent('mouseover', this.timer.stop.bind(this.timer));
        main.addEvent('mouseout', this.timer.start.bind(this.timer));
        var ref = this;
        this.paneContainer.addEvent('click', function(){ref.blockAutoForward = true;});
        
        this.mask = new Element('div');
        this.mask.setStyles({'display': 'none', 'background': '#fff', 'position': 'relative', 'top':'0', 'left':'0', 'z-index': '2', 'width': '100%', 'height':'100%'})            
        c.adopt(this.mask);
        this.fadeAnim = new Fx.Style(this.mask, 'opacity', {'duration': 250});
        this.fadeAnim.set(0);
        this.timer.start();
    },
    
// NEW FUNCTION JAN 8, 2009
    autoForward : function () {
        if (this.blockAutoForward == false) {
            if (this.selectedItem.selectionIndex == (this.panes.length - 1)) {
                this.reset();
            } else {
                this.showSlide(this.panes[this.selectedItem.selectionIndex + 1]);
                this.timer.start();
            }
        }        
    },
// NEW FUNCTION JAN 8, 2009    
    reset : function () {
        this.mask.setStyle('display', 'block');
        this.fadeAnim.start(0,1);
        this.showSlide.delay(250, this, this.panes[0]);
        this.fadeAnim.start.delay(900, this.fadeAnim, [1,0]);
        this.mask.setStyle.delay(1100, this.mask, ['display', 'none']);
        this.timer.start.delay(1150, this.timer);
    },
    showSlide : function (selectedItem)  {
// progress the item to the 'selected' item's position
        this.scrollAnim.start('-' + selectedItem.leftLoc);
        this.selectedItem = selectedItem;
        for (var i = 0, l= this.panes.length; i<l;i++) {    
            var p = this.panes[i];
            if (p == selectedItem) {
                this.locationIndecator.setText(this.selectedItem.paneIndex +  ' / '  + this.panes.length);
            }
        }
        this.setButtonStates();
    },
    
    setButtonStates: function () {
// manages the whether the prev/next buttons are enabled
        if (this.selectedItem.selectionIndex > 0 && this.panes.length > 1) {
            this.prevButton.enable();
        } else  {
            this.prevButton.disable();
        }
        if (this.selectedItem.selectionIndex + 1 < this.panes.length) {
            this.nextButton.enable();
        } else  {
            this.nextButton.disable();
        }
    },
    doButtonClick : function (dir) {
// manages the button clicks 
        if (dir == 'prev' && this.prevButton.hasClass('disabled') == false) {
            this.showSlide(this.panes[this.selectedItem.selectionIndex - 1]    );
        } else if (dir == 'next' && this.nextButton.hasClass('disabled') == false) {
            this.showSlide(this.panes[this.selectedItem.selectionIndex + 1]);
        }
        // ADDED LINE JAN 8, 2009;
        this.blockAutoForward = false;
    }
});
// NEW CLASS JAN 8, 2009
Timer = new Class({
    msecs: 1000,
    onExpire : Class.empty,
    isRunning: false,
    initialize : function(msecs) {
        ($defined(msecs))? this.msecs = msecs: null;
    },
    start : function () {
        var ref = this;
        this.isRunning = true;
        this.interval = setTimeout(function() {ref.fireExpire();}, this.msecs );
        return true;
    },
    stop : function () {
        this.isRunning = false;
        clearTimeout(this.interval);
        return true;    
    }, 
    restart : function () {
        this.stop();
        this.start();
        return true;
    }, 
    fireExpire : function () {
        this.stop();
        this.fireEvent('onExpire');
        return true;
    }
});
Timer.implement(new Events);