dojo.require("dojo.fx");

var videoSoulSlider = false;

function tty11OnLoad()
{
    videoSoulSlider = new tty11Slider("video_soul", {direction: 2, width: 111, widthVisible: 111});
}

dojo.addOnLoad(tty11OnLoad);



/******************/
/* Basique Slider */
/******************/
function tty11Slider (id, option) 
{
    this.id = id;
    this.nbItems = 0; /* nombre de li caluler a l'init */
    this.initMiddlePosition = false;
    this.blockWidthVisible = option.widthVisible;
    this.blockWidth = 0; /* largeur total du ul calculer au premier apel */
    this.blockWidthLeft = 0; /* offset de depart */
    this.itemWidth = option.width;
    this.duration = 5; //4500 /* dure de defilement pour un pixel des elements du ul */
    this.durationArrow = 500; /* dure de disparition et d'apparition des fleches  de defilement */
    this.currentAnimation = null;
    this.arrowLeftDisplay = 1;
    this.arrowRightDisplay = 1;
    this.noSlide = false;
    this.direction = option.direction;

    if (this.direction == 2)
    {
	this.positionRef = 'top';
    }
	

    if (dojo.byId(this.id + "_ul") == undefined)
    {
	return false;
    }
    

    this.myinit = function () {
	if (!dojo.byId(this.id + "_ul"))
	{
	    return (false);
	}
	var children = dojo.byId(this.id + "_ul").childNodes
	for(var i = 0; i < children.length; i++){
	    if (children[i].nodeName == 'LI')
		this.nbItems++;
	}
	this.blockWidth = this.nbItems * this.itemWidth;
	if (this.initMiddlePosition && this.blockWidth > this.blockWidthVisible)
	    dojo.byId(this.id + "_ul").style[this.positionRef] = "-" + parseInt((this.blockWidth - this.blockWidthVisible) / 2) + "px";

	if (this.blockWidth < this.blockWidthVisible)
	{
	    this.noSlide = true;
	    if (this.initMiddlePosition)
		dojo.byId(this.id + "_ul").style[this.positionRef] = parseInt((this.blockWidthVisible - this.blockWidth) / 2) + "px";
	}
	this.displayArrow(true);
    }

    this.SlideNext = function () {
	this.resumeAnimation();
	this.Slide(1);
	return (false);
    }

    this.SlidePrev = function () {
	this.resumeAnimation();
	this.Slide(2);
	return (false);
    }

    this.SlidePageNext = function () {
	this.Slide(-1);
	return (false);
    }

    this.SlidePageLeft = function () {
	this.Slide(-2);
	return (false);
    }

    this.Slide = function (index) {
	if (this.blockWidth == 0)
	    this.myinit();

	if (this.noSlide)
	    return false;

	if(this.currentAnimation!= null && this.currentAnimation && this.currentAnimation.status() != "stopped"){
	    return;//do not interrupt a running animation
	}

	if (dojo.byId(this.id + "_ul").style[this.positionRef] == undefined || dojo.byId(this.id + "_ul").style[this.positionRef] == "")
	    currentLeftPosition = 0;
	else
	    currentLeftPosition = parseInt(dojo.byId(this.id + "_ul").style[this.positionRef]);

	if (index < 0)
	{
	    var scrollindex = -(this.itemWidth - currentLeftPosition);
	    var scrollindexleft = currentLeftPosition + this.itemWidth;
	    var slideRight = false;
	    var slideLeft = false;
	    if (-scrollindex < this.blockWidth)
		slideRight = dojo.fx.slideTo({ node: this.id + "_ul", left: scrollindex, top:"0", unit:"px", duration: this.duration * this.blockWidthVisible});
	    if (scrollindexleft <= 0)
  		slideLeft = dojo.fx.slideTo({ node: this.id + "_ul", left: scrollindexleft, top:"0", unit:"px", duration: this.duration * this.blockWidthVisible});
	}
	else
	{
	    var scrollindex = -this.blockWidth + this.blockWidthVisible;
	    var scrollindexleft = this.blockWidthLeft;
	    if (this.direction == 2)
	    {
		var slideRight = dojo.fx.slideTo({ node: this.id + "_ul", top: scrollindex, left:"0", unit:"px", duration: this.duration * (this.blockWidth + currentLeftPosition - this.blockWidthVisible)});
  		var slideLeft = dojo.fx.slideTo({ node: this.id + "_ul", top: scrollindexleft, left:"0", unit:"px", duration: this.duration * (-currentLeftPosition)});
	    } 
	    else 
	    {
		var slideRight = dojo.fx.slideTo({ node: this.id + "_ul", left: scrollindex, top:"0", unit:"px", duration: this.duration * (this.blockWidth + currentLeftPosition - this.blockWidthVisible)});
  		var slideLeft = dojo.fx.slideTo({ node: this.id + "_ul", left: scrollindexleft, top:"0", unit:"px", duration: this.duration * (-currentLeftPosition)});
	    }
	}
	console.log(scrollindexleft + ' - ' + scrollindex);
	switch(index) {
	case 1:
	case -1:
            this.currentAnimation = slideRight;
	    dojo.fadeIn({node: this.id + '_imgprev',duration: this.durationArrow}).play();
	    this.arrowLeftDisplay = 1;
            break;
	case 2: 
	case -2: 
            this.currentAnimation = slideLeft;
	    dojo.fadeIn({node: this.id + '_imgnext',duration: this.durationArrow}).play();
	    this.arrowRightDisplay = 1;
            break;
	}
	
	if (this.currentAnimation) {
	    //Play the animation. Without this call, it will not run.
	    var onEnd = dojo.connect(this.currentAnimation, "onEnd", dojo.hitch(this,function(){
		delete this.currentAnimation;
		this.currentAnimation = null;
		this.displayArrow();
	    }));
	    this.currentAnimation.play();
	}
	
    }
    
    this.displayArrow = function (noduration) {
	if (noduration != undefined && noduration)
	    duration = 1;
	else
	    duration = this.durationArrow;

	offsetLeft = parseInt(dojo.byId(this.id + "_ul").style[this.positionRef]);
	if (!offsetLeft)
	    offsetLeft = 0;
	if (offsetLeft == -(this.blockWidth - this.blockWidthVisible) || this.noSlide)
	  {
	    if (this.arrowRightDisplay)
	      {
		dojo.fadeOut({node: this.id + '_imgnext', duration: duration}).play();
		this.arrowRightDisplay = 0;
	      }
	  }
	else
	  {
	    if (!this.arrowRightDisplay)
	      {
		dojo.fadeIn( {node: this.id + '_imgnext', duration: duration}).play();
		this.arrowRightDisplay = 1;
	      }
	  }
	if (offsetLeft == 0 || this.noSlide)
	  {
	    if (this.arrowLeftDisplay)
	      {
		dojo.fadeOut({node: this.id + '_imgprev',  duration: duration}).play();
		this.arrowLeftDisplay = 0;
	      }
	  }
	else
	  {
	    if (!this.arrowLeftDisplay)
	      {
		dojo.fadeIn( {node: this.id + '_imgprev',  duration: duration}).play();
		this.arrowLeftDisplay = 1;
	      }
	  }
	return (false);
    }
  
    this.pauseAnimation = function (){
	if(this.currentAnimation && this.currentAnimation.status() == "playing"){
	    this.currentAnimation.pause();
	    this.currentAnimation = null;
	    this.displayArrow();
	}
    }
  
    this.resumeAnimation = function (){
	if(this.currentAnimation && this.currentAnimation.status() == "paused"){
	    this.currentAnimation.play();
	}
    }

    this.myinit();

}

