Catalogue = new Class({
    show_by: ['all','nv','vintage'],
    browser_active: false,
    current_vintage: 'all',
    
    initialize: function () {
        this.winelist = $('winelist-wrapper');
        this.vintage_browser = $('catalogue-browser').getFirst('ul');
        
        this.region_slideshow();
        this.catalogue_decade_browser();
        this.show_events();
        this.vintage_events();
        this.activate_winelist();
        this.pagination_events();
        
//        new PDF();
    },
    
    set_active_show: function (which) {
        this.show_by.each(function(v){ $(v).removeClass('active'); });
        $(which).addClass('active');
    },
    
    toggle_browser: function(to) {
        if (to=='show') { 
            this.vintage_browser.setStyle('overflow','visible').morph({'height':43,'margin-top':15}).fade('in');
            this.browser_active = true;
        } else if (to=='hide') {
            this.vintage_browser.setStyle('overflow','hidden').morph({'height':0,'margin-top':0}).fade('out');
            $('catalogue-browser').getFirst('h3').set('html', 'browse catalogue:')
            this.browser_active = false;
        }
    },
    
    show_events: function () {
        this.vintage_browser.setStyles({'opacity':0,'height':0,'margin-top':0,'overflow':'hidden'});
        
        $('vintage').addEvent('click', function(e){
            e.stop(); this.set_active_show('vintage'); if (!this.browser_active) this.toggle_browser('show');
        }.bind(this));
        
        $('all').addEvent('click', function(e){
            e.stop(); this.set_active_show('all'); this.current_vintage = 'all'; this.retrieve_wines(1); if (this.browser_active) this.toggle_browser('hide');
        }.bind(this));
        
        $('nv').addEvent('click', function(e){
            e.stop(); this.set_active_show('nv'); this.current_vintage = 'nv';
            this.retrieve_wines(1);
            if (this.browser_active) this.toggle_browser('hide');
        }.bind(this));
    },
    
    vintage_events: function() {
        $$('div.vintages a').each(function(a){
            a.addEvent('click',function(e){
                e.stop();
                
                $('catalogue-browser').getFirst('h3').set('html', 'browse catalogue: <span>'+a.get('id')+'</span>');
                this.current_vintage = a.get('id');
                
                this.retrieve_wines(1);
            }.bind(this));
        }.bind(this));
    },
    
    pagination_event: function (direction) {
        if ($(direction)) {
            this.next = $(direction);
            this.next.addEvent('click',function(e){
                e.stop();
                this.retrieve_wines(e.target.get('href').substr(1));
            }.bind(this));
        }
    },
    
    pagination_events: function () {
        this.pagination_event('next');
        this.pagination_event('previous');
    },
    
    retrieve_wines: function (page) {
        new Request({
                url: "/price-list/api/wines/",
                onRequest: function(){
                    this.winelist.setStyle('background','url(/media/img/loader.gif) no-repeat center');
                    $('winelist').fade('out');
                    $('pager').fade('out');
                }.bind(this),
                onSuccess: function(html){ 
                    (function(){
                    this.winelist.set('html',html).setStyle('background','none');
                    this.pagination_events();
                    this.activate_winelist();
                    $('winelist').fade('hide').fade('in');
                    $('pager').fade('hide').fade('in');
                    }.bind(this)).delay(500);
                }.bind(this)
            }).get({'page':page,'vintage':this.current_vintage,'region':$('catalogue-browser').get('class')})
    },
    
    activate_winelist: function () {
        $$('#winelist tbody tr.details').each(function(row){ new WineInfo(row); });
    },
    
    catalogue_decade_browser: function() {
        browser = $('catalogue-browser');
        browser.getFirst('ul').getChildren('li').each(function(decade, index){
            decade.addEvents({
                mouseover: function () {
                    decade.getFirst('div').setStyle('background-position','left bottom').getNext('div').setStyle('display','block');
                },
                mouseout: function () {
                    decade.getFirst('div').setStyle('background-position','left top').getNext('div').setStyle('display','none');
                }
			})
		});
	},
	
	region_slideshow: function () {
	    if ($('slideshow')!=null) new SlideShow('slideshow',{delay: 8000,transition: 'fade', duration: 500,autoplay: true });
	}
});

