﻿	    
    var Navigation = Class.create();
    Navigation.prototype = 
    {
        initialize: function(htmlElement, pages, pageDimensions)
        {
            this.container = $(htmlElement);
            /* задаем высоту и ширину страницы...*/
            this.pageWidth = pageDimensions.width;
            this.pageHeight = pageDimensions.height;
            this.offsetLeft = 0;
            this.offsetTop = 0;
            this.pageLoadedCallback = null;
            this._movingStep = 0;

            /* инициализируем страницы*/
            this.pages = pages;            
            this.currentPage = this.pages[7];
            /* текущая точка - нулевая страница*/
            this.currentPoint = { x: this.currentPage.x, y: this.currentPage.y };           
            /* вычисляем отступы*/
            this.getPositionOffset();
            /* перемещаемся в начальную точку*/
            this.positionToAbsolute(this.currentPoint);
            /* отключаем скроллинг (везде, кроме IE)*/
            document.body.style.overflow = "hidden";
            /* включаем буферизацию (только IE)*/
            window.offscreenBuffering = true;
            
            this.navigator = 
            {
                path: [],
                step: 0  
            };
            this.timer = 0;
            this.disableElementScroll();
        },
        positionToAbsolute: function(point)
        {
            this.container.scrollLeft = point.x;
            this.container.scrollTop = point.y;
            
            var div = $("divSplash");
            if(div != null)
               document.body.removeChild(div);            
        },
        onWindowResize: function()
        {
            this.getPositionOffset();
            /* если не выполняется переход*/
            if(this.navigator.path.length < 1)
                this.positionToAbsolute(this.currentPage);
        },
        getPositionOffset: function()
        {
            var availWidth = Screen.getClientWidth();
            var availHeight = Screen.getClientHeight();
         
            this.offsetLeft = Math.ceil((availWidth - this.pageWidth) / 2);
            this.offsetTop = Math.ceil((availHeight - this.pageHeight) / 2);
            
            this.offsetLeft = (this.offsetLeft > 0) ? this.offsetLeft : 0;
            this.offsetTop = (this.offsetTop > 0) ? this.offsetTop : 0;
            
            this.container.style.paddingLeft = this.offsetLeft + "px";
            /* раскомментировать, если нужно выставлять страницу по центру вертикали
            this.container.style.paddingTop = this.offsetTop + "px";*/
        },
        navigateImmediately: function(pageIndex)
        {
            this.currentPage = this.pages[pageIndex];
            var div = $("divSplash");
            if(div != null)
               document.body.removeChild(div);
            
            this.enableElementScroll();      
            this.positionToAbsolute(this.currentPage);
            this.disableElementScroll();
            
            this.currentPoint = { x: this.currentPage.x, y: this.currentPage.y };      
            
            if(this.pageLoadedCallback != null)
               this.pageLoadedCallback.call(this, this.currentPage);
        },
        disableElementScroll: function()
        {
            this.container.style.overflow = "hidden";  
        },
        enableElementScroll: function()
        {
            this.container.style.overflow = "auto";
        },
        navigateToPage: function(pageIndex)
        {
            if(this.currentPage == this.pages[pageIndex])
                return;
                
            window.clearTimeout(this.timer);
            this._movingStep = 0;
            /* добавляем слой, не позволяющий выполнять 
             действия во время перехода*/
            var div = document.createElement("div");
            div.id = "divSplash";
            div.style.position = "absolute";
            div.style.top = "0px";
            div.style.left = "0px";
            div.style.width = "100%";
            div.style.height = "100%";
            div.style.zIndex = "350";         
            /* следующую строку можно закомментировать, 
             тогда слой не будет отображен*/
            document.body.appendChild(div);
            
            this.enableElementScroll();   
            this.navigator.path = [ this.currentPage, this.pages[pageIndex] ];
            this.navigator.step = 1;
            
            this.nextMove();
        },
        onPageLoaded: function(eventArgs)
        {
            this.navigator.step++;
            if(this.navigator.step < this.navigator.path.length)
                this.nextMove();
            else
            {
                this.currentPage = eventArgs;
                this.positionToAbsolute(this.currentPage);
                this.navigator = {
                    path: [],
                    step: 0
                };
                
                this.disableElementScroll();
                /* убиваем слой*/
                var div = $("divSplash");
                if(div != null)
                    document.body.removeChild(div);

                if(Screen.getClientHeight() < 600 && this.currentPage.id == 5)    
                    this.enableElementScroll();
                
                if(this.pageLoadedCallback != null)
                    this.pageLoadedCallback.call(this, this.currentPage);
            }
        },
        nextMove: function()
        {
            var steps = [ 1, 2, 3, 5, 15, 25, 40, 70, 255, 70, 40, 25, 15, 5, 3, 2, 1 ];
            var offset = function(array, max){ 
                    var s = 0;
                    for(var i = 0; i < array.length; i++) 
                        if(array[i] == max)
                            break;
                        else
                            s += array[i];
                    return s;
                  }.call(this, steps, 255);
            
            /* получаем предыдущую и страницу, к которой движемся*/
            var nextPage = this.navigator.path[this.navigator.step];
            var prevPage = this.navigator.path[this.navigator.step - 1];
            
            /* двигаемся вниз или вверх*/
            var directionY = (nextPage.y > this.currentPoint.y);
            /* двигаемся направо или налево*/
            var directionX = (nextPage.x > this.currentPoint.x);                     	     
            var doNextStep = true;
                    
            var step = steps[this._movingStep];
            /* Вычисление новой координаты */
            if(directionX && directionY)
            {                    
                if(this.currentPoint.x < nextPage.x)
                    this.currentPoint.x += (step == 255) ? 
                        Math.abs(nextPage.x - this.currentPoint.x) - offset : step;
                else if(this.currentPoint.y < nextPage.y)
                    this.currentPoint.y += (step == 255) ? 
                        Math.abs(nextPage.y - this.currentPoint.y) - offset : step;
                else 
                {
                    this.onPageLoaded(nextPage);
                    doNextStep = false;
                }
            }  
            else if(!directionX && !directionY)
            {
                if(this.currentPoint.x > nextPage.x + step)
                    this.currentPoint.x -= (step == 255) ? 
                        Math.abs(nextPage.x - this.currentPoint.x) - offset : step;
                else if(this.currentPoint.y > nextPage.y + step)
                    this.currentPoint.y -= (step == 255) ? 
                        Math.abs(nextPage.y - this.currentPoint.y) - offset : step;
                else 
                {
                    this.onPageLoaded(nextPage);
                    doNextStep = false;
                }
            } 
            else if(directionX && !directionY)
            {
                if(this.currentPoint.y > nextPage.y + step)
                    this.currentPoint.y -= (step == 255) ? 
                        Math.abs(nextPage.y - this.currentPoint.y) - offset : step;
                else if(this.currentPoint.x < nextPage.x - step)
                    this.currentPoint.x += (step == 255) ? 
                        Math.abs(nextPage.x - this.currentPoint.x) - offset : step;
                else 
                {
                    this.onPageLoaded(nextPage);
                    doNextStep = false;
                }
            }
            else
            {
                if(this.currentPoint.y < nextPage.y - step)
                    this.currentPoint.y += (step == 255) ? 
                        Math.abs(nextPage.y - this.currentPoint.y) - offset : step;
                else if(this.currentPoint.x > nextPage.x + step)
                    this.currentPoint.x -= (step == 255) ? 
                        Math.abs(nextPage.x - this.currentPoint.x) - offset : step;                  
                else 
                {
                    this.onPageLoaded(nextPage);
                    doNextStep = false;
                }
            }             
            /* перемещаемся в точку*/
            this.positionToAbsolute(this.currentPoint);
            /* если движение не окончено, вызываем себя еще раз...*/
            if(doNextStep)
                this.timer = window.setTimeout(this.nextMove.bind(this), 5);
            
            this._movingStep++;
        }
    }

