

var PageSlider = Class.create({

    nextId              : 'paging_next',
    prevId              : 'paging_back',
    pagingContaienerId  : 'slider_controls',
    dataContainerId     : 'elements_slider',
    infoContainerId     : 'paging_info',
    ajaxLoaderId        : 'ajax_loader',
    contentContainerId  : 'content_container',
    slideSpeed          : 0.3,
    ajaxUrl             : null,
    offset              : 0,
    limit               : 8,
    total               : 0,

    lastOffset          : null,
    currentPage         : 1,
    dataHash            : null,

    scrollSemaphore     : 0,

    /**
     * Nurodo kiek pikselių reikia praslinkti juostą.
     */
    slideWidth: 640,


    initialize: function(url, offset, limit, total) {
        this.ajaxUrl    = url;
        this.offset     = parseInt(offset);
        this.limit      = parseInt(limit);
        this.total      = parseInt(total);
        this.addRedirectOnElementsList();
        this.initStartHash();
        this.initSlider();
    },


    /**
     * Elementų sąrašui reikalingi veiksmai.
     */
    addRedirectOnElementsList: function() {
        //this.observeElementClick('#travel_elements div.element');
        //this.observeElementClick('#special_offers div.offer');
    },


    /**
     * Uždeda ant elemento observerį, kad paspaudus ant jo būtų nukreipiamas
     * browseris į elemente esančio <a> tago href reikšmę.
     */
    observeElementClick: function(pattern) {
        $$(pattern).each(function(element) {
            element.stopObserving('click');
            element.observe('click', function(event) {
                window.location = element.down('a').href;
            });
        });
    },


    /**
     * Užsetina objektą į hash'ą.
     */
    initStartHash: function() {
        this.dataHash = new Hash();
        var first = new Object();
        first.offset = this.offset;
        first.limit  = this.limit;
        first.prev   = (this.offset == 0) ? false : true;
        first.next   = (this.offset + this.limit >= this.total) ? false : true;
        first.info   = $(this.infoContainerId).innerHTML;
        this.dataHash.set(this.offset, first);
    },


    initSlider: function() {
        
        if ($(this.nextId)) {
            $(this.nextId).observe('click', function(event) {

                if (this.scrollSemaphore == 0) {
                    this.scrollSemaphore++;
                    this.opacityBusy();
                    var newOffset = (parseInt(this.offset) + parseInt(this.limit));

                    //Jei neturime užsetinę duomenų.
                    if (this.dataHash.get(newOffset) === undefined) {
                        var url = this.ajaxUrl + ',action.getPage,offset.' + newOffset;
                        new Ajax.Request(url, {
                            method: 'get',
                            onSuccess: function(t) {
                                var result = t.responseText.evalJSON();
                                $('offset_' + this.offset).insert({
                                    after : result.data
                                //after: t.responseText
                                });
                            
                                this.moveNext();
                                this.offset = newOffset;
                                result.data = null;
                                this.dataHash.set(newOffset, result);
                                this.updatePaging(newOffset);
                                this.addRedirectOnElementsList();
                                this.scrollSemaphore--;
                                this.opacityReady();
                            }.bind(this)
                        });
                    }
                    else {
                        this.moveNext();
                        this.offset = newOffset;
                        this.updatePaging(newOffset);
                        this.scrollSemaphore--;
                        this.opacityReady();
                    }
                }
            }.bind(this));
        }

        if ($(this.prevId)) {
            $(this.prevId).observe('click', function(event) {
                if (this.scrollSemaphore == 0) {
                    this.scrollSemaphore++;
                    this.opacityBusy();
                    var newOffset = (parseInt(this.offset) - parseInt(this.limit));

                    //Jei neturime užsetinę duomenų.
                    if (this.dataHash.get(newOffset) === undefined) {
                        var url = this.ajaxUrl + ',action.getPage,offset.' + newOffset;
                        new Ajax.Request(url, {
                            method: 'get',
                            onSuccess: function(t) {
                                var result = t.responseText.evalJSON();
                                if ($('offset_' + newOffset)) {
                                    $('offset_' + newOffset).update(result.data);
                                }
                                else {
                                    $('offset_' + this.offset).insert({
                                        before : result.data
                                    });
                                }
                                this.movePrev();
                                this.offset = newOffset;
                                result.data = null;
                                this.dataHash.set(newOffset, result);
                                this.updatePaging(newOffset);
                                this.addRedirectOnElementsList();
                                this.scrollSemaphore--;
                                this.opacityReady();
                            }.bind(this)
                        });
                    }
                    else {
                        this.movePrev();
                        this.offset = newOffset;
                        this.updatePaging(newOffset);
                        this.scrollSemaphore--;
                        this.opacityReady();
                    }
                    
                }
            }.bind(this));
        }
    },


    moveNext: function() {
        new Effect.Move(this.dataContainerId, {
            x: this.slideWidth * -1,
            y: 0,
            mode: 'relative',
            transition: Effect.Transitions.sinoidal,
            duration: this.slideSpeed,
            queue: {
                position: 'end',
                scope: 'slidebar'
            }
        });
    },

    movePrev: function() {
        new Effect.Move(this.dataContainerId, {
            x: this.slideWidth,
            y: 0,
            mode: 'relative',
            transition: Effect.Transitions.sinoidal,
            duration: this.slideSpeed,
            queue: {
                position: 'end',
                scope: 'slidebar'
            }
        });
    },

    opacityBusy: function() {
    /*
        new Effect.Opacity(this.dataContainerId, {
            duration:0.3, from:1.0, to:0.4,
            queue: {
                position: 'end',
                scope: 'slidebar'
            }
        });
        */
    },

    opacityReady: function() {
    /*
        new Effect.Opacity(this.dataContainerId, {
            duration:0.3, from:0.4, to:1.0,
            queue: {
                position: 'end',
                scope: 'slidebar'
            }
        });
        */
    },


    updatePaging: function(id) {
        var item = this.dataHash.get(id);
        if (item !== undefined) {
            var nextDisplay = (item.next == true) ? 'block' : 'none';
            var prevDisplay = (item.prev == true) ? 'block' : 'none';

            if ($(this.nextId)) {
                $(this.nextId).setStyle({
                    display : nextDisplay
                });
            }
            if ($(this.prevId)) {
                $(this.prevId).setStyle({
                    display : prevDisplay
                });
            }
            if ($(this.infoContainerId)) {
                $(this.infoContainerId).update(item.info);
            }
        }
    },


    setItemsPerPage: function(offset) {
        var url = this.ajaxUrl + ',action.setPaging';
        new Ajax.Request(url, {
            method: 'post',
            parameters: this.getScreenResolution(),
            onSuccess: function(transport, json) {
                var url = this.ajaxUrl + ',action.getPage,offset.' + offset;
                new Ajax.Request(url, {
                    method: 'get',
                    onSuccess: function(t) {
                        var result = t.responseText.evalJSON();
                        this.limit = result.limit;
                        $('offset_' + this.offset).update(result.data);
                        this.offset = offset;
                        result.data = null;
                        this.dataHash.set(offset, result);
                        this.updatePaging(offset);
                        $(this.ajaxLoaderId).hide();
                        $(this.contentContainerId).show();
                    }.bind(this)
                });


            }.bind(this)
        });
    },


    /**
     * Grąžina ekrano rezoliuciją.
     */
    getScreenResolution: function() {
        var result = new Hash();
        result.set('width', screen.width);
        result.set('height', screen.height);
        return result;
    }
});