/*
    Behaviours for the Auction Calendar Page 
*/

Site.Pages.Auction_Results = Class.create
(
    Site.Page,
    {
        initialize: function($super)
        {
            $super();
            
            this.addBlocks("richSelectOnChange");
            //this.addBlocks("sendRequestOnComplete", "richSelectOnChange", "richCheckboxOnClick", "_sortData");
            //this.addObservers("clearAllLocationsOnClick");
            
        },
        
        calendarOnDayChange: function(day)
        {
            //this.setDay(day);
        },
          
        domOnLoad: function($super, event)
        {
            $super(event);

            
            
            this.collapsePanelController = new Site.Controllers.CollapsePanel($('criteria'));
         
            //this.listLocations = new Site.Widgets.RichCheckboxList($('list-locations'), { richCheckboxOnClick: this.blocks.richCheckboxOnClick } );

           // addEvent($('clear-all-locations'), "click", this.observers.clearAllLocationsOnClick);
            
            //this.list = $('list-items');
            
            if ($('filter-by-month'))
                this.selectFilterByLocation = new Site.Widgets.RichSelect($('filter-by-month'), { width: 100, optionsWidth: 100, onChange: this.blocks.richSelectOnChange } );
            
            if ($('filter-by-year'))
                this.selectFilterBySales = new Site.Widgets.RichSelect($('filter-by-year'), { width: 100, optionsWidth: 100, onChange: this.blocks.richSelectOnChange } );

            

        },
        
        richSelectOnChange: function(select)
        {
            //alert(select.value)
            document.location = select.value;
            
            //this.sendRequest();
        },
        
        richCheckboxOnClick: function()
        {
            //this.sendRequest();
        },
        
        clearAllCategoriesOnClick: function(event)
        {
            //this.listCategories.uncheckAll();
            //this.sendRequest();

            Event.stop(event);
        },
        
        clearAllLocationsOnClick: function(event)
        {
            //this.listLocations.uncheckAll();
            //this.sendRequest();

            Event.stop(event);
        },
        
        updateSaleTooltips: function()
        {
            this.saleTooltips.update
            (
                this.data.sales.inject
                (
                    "",
                    function(html, sale)
                    {
                        return Site.Pages.Auction_Results.TEMPLATES.SALE_TOOLTIP.interpolatep
                        (
                            {
                                saleNumber: sale.saleNumber,
                                
                                auctionTimes: sale.auctionTimes.inject
                                (
                                    "", 
                                    function(html, auctionTime)
                                    {
                                        return html + Site.Pages.Auction_Results.TEMPLATES.SALE_TOOLTIP_AUCTION_TIME.interpolatep(auctionTime);
                                    },
                                    this
                                ),
                                  
                                viewingTimes: sale.viewingTimes.inject
                                (
                                    "", 
                                    function(html, viewingTime)
                                    {
                                        return html + Site.Pages.Auction_Results.TEMPLATES.SALE_TOOLTIP_VIEWING_TIME.interpolatep(viewingTime);
                                    },
                                    this
                                )   
                            }
                        );
                    },
                    this
                )
            );
        },
        
        sendRequest: function()
        {
            // here, would gather the values of form elements together and prepate an appropriate request to
            // a web service
            
            //var categories = this.listCategories.getValue();
            //var locations = this.listLocations.getValue();
            
            this.request = new Ajax.Request
            (
                "js/sample_data/calendar.js?t=" + (new Date()).getTime(),
                {
                    method: "get",
                    onComplete: this.blocks.sendRequestOnComplete
                }
            );

            this.request = null;
        },
        
        sendRequestOnComplete: function(transport)
        {
            this.data = transport.responseText.evalJSON();
            this.updateElements();
        },
        
        updateElements: function()
        {
            this.updateList();

            //this.destroyTooltips();
            //this.setupTooltips();
        },

        
        /*
            Note:
                This method simulates the expected behaviour - the problem is that data on any subsequent pages is not regarded
                What is really needed is for this to send an AJAX request with a current day value set and to place the auctions up the top
                
        */
        
        setDay: function(day)
        {
            this.day = day;
            
            // sort the data so that any auction in the currently selected range is shown at the top
            this.data.auctions = this.data.auctions.sort(this.blocks._sortData);
            this.updateList();
            //this.destroyTooltips();
            //this.setupTooltips();
        },
        
        _sortData: function(a, b)
        {
            
            if (!this.day)
            {
                 return a.dateLow > b.dateLow ? 1 : -1;
            }
            else
            {
                if (a.dateLow <= this.day && a.dateHigh >= this.day && b.dateLow <= this.day && b.dateHigh >= this.day)
                {
                    return a.dateLow > b.dateLow ? 1 : -1;
                }
                else if (a.dateLow <= this.day && a.dateHigh >= this.day)
                {
                    return -1;
                }
                else if (b.dateLow <= this.day && b.dateHigh >= this.day)
                {
                    return 1;
                }
            }
            
            return a.dateLow > b.dateLow ? 1 : -1;            
        },
        
        updateList: function()
        {
            this.list.update
            (
                this.data.auctions.inject
                (
                    "",
                    function(html, auction, index)
                    {
                        return html + this.templateListItem.evaluatep( { "className": index % 2 == 0 ? "" : "alternate", featuredSaleButton: auction.feature ? Site.Pages.Auction_Results.TEMPLATES.LIST_ITEM_FEATURED_SALE_BUTTON : '' } ).interpolatep(auction)
                    },
                    this
                )
            );
        },
        
        
        destroyTooltips: function()
        {
           //this.saleTooltipsController.destroy();
            
            /*
            $H(this.tooltips).each
            (
                function(pair)
                {
                    pair.value.destroy();
                }
            );
            */        
        },
        
        setupTooltips: function()
        {
            this.saleTooltipsController = new Site.Controllers.SaleTooltips(this.list.select("a.viewing-times"));
        },
       
        
        destroy: function($super)
        {
            //this.saleTooltipsController.destroy();
            
            //removeEvent($('clear-all-categories'), "click", this.observers.clearAllCategoriesOnClick);
            //removeEvent($('clear-all-locations'), "click", this.observers.clearAllLocationsOnClick);

            $super();
        }
                  
    }
);
