﻿var Catalog = {

    Buttons: {
        Next: null,
        Previous: null
    },

    Catalog: null,
    Category: undefined,
    IsLoading: false,
    IsScrolling: false,
    Page: 0,
    Pages: 0,
    PagesLoaded: -1,

    FixButtons: function(num) {

        if (!num) {
            num = 0;
        }

        if (this.Buttons.Next) {
            this.Buttons.Next.style.display = this.Pages > this.Page ? 'block' : 'none';
        }

        if (this.Buttons.Previous) {
            this.Buttons.Previous.style.display = this.Page > 1 || (this.Page > 0 && !this.IsLoading && (!this.IsScrolling || num < 0)) ? 'block' : 'none';
        }
    },

    OnLoaded: function() {
        this.IsLoading = false;
        setTimeout("Catalog.Scroll();", 10);
    },

    Scroll: function() {

        if (!this.Catalog) {
            this.IsScrolling = false;
            this.FixButtons();
            return;
        }

        var x = this.Page * this.Catalog.offsetWidth;

        if (this.Catalog.scrollLeft < x) {

            this.Catalog.scrollLeft += 25;

            if (this.Catalog.scrollLeft > x) {
                this.Catalog.scrollLeft = x;
            }
        }
        else if (this.Catalog.scrollLeft > x) {

            this.Catalog.scrollLeft -= 25;

            if (this.Catalog.scrollLeft < x) {
                this.Catalog.scrollLeft = x;
            }
        }

        this.IsScrolling = !(this.Catalog.scrollLeft <= 0 || this.Catalog.scrollLeft == x || this.Catalog.scrollLeft >= this.Catalog.scrollWidth - this.Catalog.offsetWidth);

        this.FixButtons(x - this.Catalog.scrollLeft);

        if (!this.IsScrolling) {
            return;
        }

        setTimeout("Catalog.Scroll();", 10);
    },

    SetPages: function(num) {
        this.Pages = num;
        this.FixButtons();
    },

    Update: function(num) {

        if (this.IsLoading) {
            return;
        }

        if (this.IsScrolling) {
            return;
        }

        if (!num) {
            num = 0;
        }

        num = parseInt(num);

        this.Page += num;

        if (this.Page < 0) {
            this.Page = 0;
        }

        if (this.Page > this.Pages) {
            this.Page = this.Pages;
        }

        if (this.Page > this.PagesLoaded) {

            this.IsLoading = true;

            this.FixButtons();

            var id = 'Catalog_Page' + this.Page;

            this.PagesLoaded = this.Page;

            if (this.Page > 0) {

                var e = document.getElementById('Catalog_Pages');

                var c = e.insertCell(e.cells.length);

                c.className = 'Page';
                c.id = id;
            }
            else {
                id = 'Catalog_MainOffer';
            }

            if (this.Category != undefined) {
                new Ajax.Updater(id, '/Ajax/Catalog/' + this.Category + '/' + this.Page + '/', { evalScripts: true, onComplete: function(transport) { Catalog.OnLoaded(); } });
            }
            else {
                new Ajax.Updater(id, '/Ajax/Catalog/' + this.Page + '/', { evalScripts: true, onComplete: function(transport) { Catalog.OnLoaded(); } });
            }
        }
        else {
            this.Scroll();
        }
    }
}

//////////////////////////////////////////////////

function NextPage() {
    Catalog.Update(1);
}

function PreviousPage() {
    Catalog.Update(-1);
}

function UpdateCatalog() {
    Catalog.Update(0);
}

//////////////////////////////////////////////////

window.onload = function() {

    Catalog.Buttons.Next = document.getElementById('Catalog_Next');
    Catalog.Buttons.Previous = document.getElementById('Catalog_Previous');

    Catalog.FixButtons();

    Catalog.Catalog = document.getElementById('Catalog');

    Catalog.Update();
}