/**
 * @author timharwood
 */
var BaseView = new Class({

    Implements: [Events, Options],
    
    options: {
        id: 'BaseView' + new Date().getTime() + "-" + Math.random(),
        viewContainer: null,
        dataProvider: null,
        viewClass: '',
        debug: false
    },
    
    viewContainer: null,
    dataProvider: null,
    
    initialize: function(options){
        this.setOptions(options);
        this.initialiseExtras();
        
        if ((this.options.viewContainer)) {
            this.addViewContainer(this.options.viewContainer);
        }
        if ((this.options.dataProvider)) {
            this.addDataProvider(this.options.dataProvider);
        }
        
    },
    
    initialiseExtras: function(){
        /* stub method */
    },
    
    addViewContainer: function(elem){
        if (elem) {
            this.viewContainer = $(elem);
            if (this.options.viewClass) 
                this.viewContainer.addClass(this.options.viewClass);
            if (this.dataProvider) 
                this.build();
        }
    },
    
    addDataProvider: function(data){
        if (this.dataProvider) {
            this.removeDataListeners();
        }
        
        this.dataProvider = data;
        this.addDataListeners();
        if (this.viewContainer) 
            this.build();
    },
    
    removeDataListeners: function(){
        /* stub method */
    },
    
    addDataListeners: function(){
        /* stub method */
    },
    
    build: function(){
        /* stub method */
    }
    
});

//-------------------

var SelectableItemView = new Class({

    Extends: BaseView,
    Implements: [Events, Options],
    
    options: {
        id: 'SelectableItemView' + new Date().getTime() + "-" + Math.random(),
        viewContainer: null,
        viewClass: null,
        dataProvider: null,
        debug: false
    },
    
    onDataProviderSelect: null,
    onDataProviderDeleselect: null,
    
    initialize: function(options){
        this.parent(options);
    },
    
    initialiseExtras: function(){
    
        this.parent();
        
        this.onDataProviderSelect = function(e){
            this.dataProviderSelect(e);
        }.bind(this);
        
        this.onDataProviderDeleselect = function(e){
            this.dataProviderDeselect(e);
        }.bind(this);
        
    },
    
    removeDataListeners: function(){
        this.parent();
        this.dataProvider.removeEvent('select', this.onDataProviderSelect);
        this.dataProvider.removeEvent('deselect', this.onDataProviderDeleselect);
    },
    
    addDataListeners: function(){
        this.parent();
        this.dataProvider.addEvent('select', this.onDataProviderSelect);
        this.dataProvider.addEvent('deselect', this.onDataProviderDeleselect);
    },
    
    dataProviderSelect: function(e){
        /* stub method */
    },
    
    dataProviderDeselect: function(e){
        /* stub method */
    }
    
});

//-------------------

var GalleryView = new Class({

    Extends: SelectableItemView,
    Implements: [Events, Options],
    
    options: {
        id: 'GalleryView' + new Date().getTime() + "-" + Math.random(),
        viewContainer: null,
        viewClass: '',
        dataProvider: null,
        debug: false
    },
    
    views: [],
    onItemsIndexUpdate: null,
    onItemsPassBeginning: null,
    onItemsPassEnd: null,
    
    initialize: function(options){
        this.parent(options);
    },
    
    initialiseExtras: function(){
    
        this.parent();
        
        this.onItemsIndexUpdate = function(e){
            this.itemsIndexUpdate(e);
        }.bind(this);
        
        this.onItemsPassBeginning = function(e){
            this.itemsPassBeginning(e);
        }.bind(this);
        
        this.onItemsPassEnd = function(e){
            this.itemsPassEnd(e);
        }.bind(this);
        
        
    },
    
    addDataListeners: function(){
        this.dataProvider.itemsCollection.addEvent('onIndexChange', this.onItemsIndexUpdate);
        this.dataProvider.itemsCollection.addEvent('onPassBeginning', this.onItemsPassBeginning);
        this.dataProvider.itemsCollection.addEvent('onPassEnd', this.onItemsPassEnd);
    },
    
    removeDataListeners: function(){
        this.dataProvider.itemsCollection.removeEvent('onIndexChange', this.onItemsIndexUpdate);
        this.dataProvider.itemsCollection.removeEvent('onPassBeginning', this.onItemsPassBeginning);
        this.dataProvider.itemsCollection.removeEvent('onPassEnd', this.onItemsPassEnd);
    },
    
    itemsIndexUpdate: function(e){
    },
    
    itemsPassEnd: function(e){
    },
    
    itemsPassBeginning: function(e){
    }
    
});






var Gallery = new Class({

    Implements: [Events, Options],
    
    options: {
        id: new Date().getTime() + "-" + Math.random(),
        source: 'imgsrc',
        sourceElement: 'img',
        period: 5000,
        autoplay: false,
        loop: true,
        randomise: true,
        debug: false
    },
    
    itemsCollection: null,
    imageSource: null,
    
    initialize: function(options){
    
        window.gallery = this;
        
        this.setOptions(options);
        
        this.itemsCollection = new TimerplayCollection({
            delay: this.options.period,
            loop: this.options.loop,
            autoplay: this.options.autoplay
        });
        
        this.itemsCollection.addEvent('onIndexChange', this.itemsIndexUpdate.bind(this));
        this.itemsCollection.addEvent('onPassEnd', this.itemsPassEnd.bind(this));
        this.itemsCollection.addEvent('onPassBeginning', this.itemsPassBeginning.bind(this));
        
        this.source = $(this.options.source);
        this.prepSourceElements();
        
    },
    
    prepSourceElements: function(){
        this.source.getChildren(this.options.sourceElement).each(function(elem, index){
            var ci = new SelectableCollectionItem({
                content: elem.clone()
            });
            this.itemsCollection.addItem(ci);
        }.bind(this));
        
        this.source.empty();
        
        if (this.options.randomise) {
            this.itemsCollection.randomiseItems()
        }
        this.itemsCollection.setCurrentIndex(0);
        
    },
    
    play: function(){
        this.itemsCollection.play();
    },
    
    stop: function(){
        this.itemsCollection.stop();
    },
    
    itemsIndexUpdate: function(e){
        var f = this.fireEvent('onItemsIndexChange', {
            type: 'onItemsIndexChange',
            target: this,
            index: this.itemsCollection.getCurrentIndex(),
            hasPrev:this.itemsCollection.hasPrev(),
            hasNext:this.itemsCollection.hasNext()
        });
    },
    
    itemsPassEnd: function(e){
        var f = this.fireEvent('onItemsPassEnd', {
            type: 'onItemsPassEnd',
            target: this
        });
    },
    
    itemsPassBeginning: function(e){
        var f = this.fireEvent('onItemsPassBeginning', {
            type: 'onItemsPassBeginning',
            target: this
        });
    }
    
});


