﻿/*
Created by Branislav Abadjimarinov
*/
function ScrollControl(containerDivName, scrollSpeed, elementsCount, elementWidth, elementHeight, elementsVisible)
{
    this.previousAnim = null;
    this.nextAnim = null;
    this.scrollSpeed = scrollSpeed;
    this.containerDivName = containerDivName;           
    this.containerDiv = document.getElementById(this.containerDivName);
    this.elementWidth = elementWidth;
    this.elementHeight = elementHeight;
    this.elementsCount = elementsCount;
    this.elementsVisible = elementsVisible;
}
ScrollControl.prototype.scrollNRight = ScrollNRight;
ScrollControl.prototype.scrollNLeft = ScrollNLeft;

ScrollControl.prototype.scrollNUp = ScrollNUp;
ScrollControl.prototype.scrollNDown = ScrollNDown;

ScrollControl.prototype.scrollOneRight = ScrollOneRight;
ScrollControl.prototype.scrollOneLeft = ScrollOneLeft;

ScrollControl.prototype.endlessScrollRight = EndlessScrollRight;
ScrollControl.prototype.endlessScrollLeft = EndlessScrollLeft;

ScrollControl.prototype.instantScrollLeft = InstantScrollLeft;
ScrollControl.prototype.instantScrollRight = InstantScrollRight;

ScrollControl.prototype.stopPrevAnimation = StopPrevAnimation;
ScrollControl.prototype.scrollAnimation = ScrollAnimation;

ScrollControl.prototype.endlessScrollUp = EndlessScrollUp;
ScrollControl.prototype.endlessScrollDown = EndlessScrollDown;

ScrollControl.prototype.instantScrollUp = InstantScrollUp;
ScrollControl.prototype.instantScrollDown = InstantScrollDown;


function ScrollNRight(N) {
  var anim = this.scrollAnimation(this.containerDiv.scrollLeft + this.elementWidth * N, 0, this.scrollSpeed * N);

    var currentScroll = this;

    this.stopPrevAnimation();
    anim.animate();
}

function ScrollNLeft(N){
  var anim = this.scrollAnimation(this.containerDiv.scrollLeft - this.elementWidth * N, 0, this.scrollSpeed * N);
    
    var currentScroll = this; 
    
    this.stopPrevAnimation();
    anim.animate();
}

function ScrollNUp(N) {
  var anim = 
  this.scrollAnimation(0,
                       this.containerDiv.scrollTop - this.elementHeight * N,
                       this.scrollSpeed * N
                       );

  var currentScroll = this;

  this.stopPrevAnimation();
  anim.animate();
}
  
function ScrollNDown(N) {
  var anim = 
  this.scrollAnimation(0,
                       this.containerDiv.scrollTop + this.elementHeight * N,
                       this.scrollSpeed * N
                      );

  var currentScroll = this;

  this.stopPrevAnimation();
  anim.animate();
}

function ScrollOneRight()
{
    var anim = this.scrollAnimation(this.containerDiv.scrollLeft + this.elementWidth, 0, this.scrollSpeed / 2);
    
    var currentScroll = this;
    anim.onComplete.subscribe(function(){currentScroll.endlessScrollRight();});
    
    this.stopPrevAnimation();
    anim.animate();
}

function ScrollOneLeft()
{
    var anim = this.scrollAnimation(this.containerDiv.scrollLeft - this.elementWidth, 0, this.scrollSpeed);
    
    var currentScroll = this; 
    anim.onComplete.subscribe(function(){currentScroll.endlessScrollRight();});
    
    this.stopPrevAnimation();
    anim.animate();
}

function EndlessScrollUp() {
    var anim =
    this.scrollAnimation(0,
                         0,
                         this.containerDiv.scrollTop / this.elementHeight * this.scrollSpeed
                        );

    var currentScroll = this;
    anim.onComplete.subscribe(function() { currentScroll.instantScrollDown(); });

    this.stopPrevAnimation();
    anim.animate();
}
function EndlessScrollDown() {
    var anim =
    this.scrollAnimation(0,
                        (this.elementsCount - this.elementsVisible) * this.elementHeight,
                        ((this.elementsCount - this.elementsVisible) - (this.containerDiv.scrollTop / this.elementHeight)) * this.scrollSpeed
                        );

    var currentScroll = this;
    anim.onComplete.subscribe(function() { currentScroll.instantScrollUp(); });

    this.stopPrevAnimation();
    anim.animate();
}
function InstantScrollUp() {
    var anim = this.scrollAnimation(0, 0, 0);

    var currentScroll = this;

    this.stopPrevAnimation();
    anim.animate();
}
function InstantScrollDown() {
    var anim = this.scrollAnimation(0,
                                    (this.elementsCount - this.elementsVisible) * this.elementWidth, 
                                    0);

    var currentScroll = this;
    anim.onComplete.subscribe(function() { currentScroll.endlessScrollUp(); });

    this.stopPrevAnimation();
    anim.animate();
}
function EndlessScrollRight()
{          
    var anim = 
    this.scrollAnimation((this.elementsCount - this.elementsVisible) * this.elementWidth, 
                        0, 
                        ((this.elementsCount - this.elementsVisible) - (this.containerDiv.scrollLeft / this.elementWidth)) * this.scrollSpeed
                        );
                        
    var currentScroll = this;
    anim.onComplete.subscribe(function(){currentScroll.instantScrollLeft();});
    
    this.stopPrevAnimation();
    anim.animate();
}
function EndlessScrollLeft()
{
    var anim = this.scrollAnimation(0, 0, this.containerDiv.scrollLeft / this.elementWidth * this.scrollSpeed);
    
    var currentScroll = this;
    anim.onComplete.subscribe(function() { currentScroll.instantScrollRight(); });
    
    this.stopPrevAnimation();
    anim.animate();
}
function InstantScrollLeft()
{
    var anim = this.scrollAnimation(0, 0, 0);
    
    var currentScroll = this;
    anim.onComplete.subscribe(function(){currentScroll.endlessScrollRight();});
    
    this.stopPrevAnimation();
    anim.animate();
}
function InstantScrollRight() {
    var anim = this.scrollAnimation((this.elementsCount - this.elementsVisible) * this.elementWidth, 0, 0);

    var currentScroll = this;
    anim.onComplete.subscribe(function() { currentScroll.endlessScrollLeft(); });

    this.stopPrevAnimation();
    anim.animate();
}
function ScrollAnimation(x, y, duration)
{
    var attributes = {
        scroll: { to: [x, y] }
    };
        var anim = new YAHOO.util.Scroll(this.containerDiv, attributes);
        anim.duration = duration;
        this.nextAnim = anim;
        return anim;
}
function StopPrevAnimation()
{
    if(this.previousAnim != null)
    {
        this.previousAnim.onComplete = null;
        this.previousAnim.stop();
    }
    this.previousAnim = this.nextAnim;
} 