var ImageSlider = Class.create();
ImageSlider.getLog = function()
{
	return nulllog;
	//return jslog;
}
ImageSlider.Transition = function(dist) 
{	

    if(dist > 400)
    {
        dist = 400;
    } 
    if(dist < -400)
    {
        dist = -400;
    }

	return dist * 0.05;
}
ImageSlider.PageTransition = function(dist) 
{	
	return dist * 0.23;
}


ImageSlider.DefaultOptions = 
{
	numSteps:10,
	fps:16,
	direction:"H",
	btnLeft:"btnLeft",
	btnRight:"btnRight",
	transition:ImageSlider.Transition
}
ImageSlider.Instance = null;

ImageSlider.prototype =
{

    initialize: function(slidingArea, options) {
        this.random = Math.random();

        if (ImageSlider.Instance != null) {
            ImageSlider.Instance.dispose();
        }
        ImageSlider.Instance = this;

        this.log = ImageSlider.getLog();
        this.log.info("initializing Slider " + this.random);

        this.options = Object.extend(ImageSlider.DefaultOptions, options);
        this.slidingArea = $(slidingArea);
        this.slidingContent = this.slidingArea.down();
        this.transition = this.options.transition;
        this.areaWidth = this.slidingArea.getStyle("width").replace("px", "");

        this.totalWidth = this.slidingContent.getStyle("width").replace("px", "");

        this.initialPosition = 0;
        this.currentPosition = this.initialPosition;

        this.currentInterval = 0;

        this.currentX = 0;

        this.finalPosition = this.totalWidth - this.areaWidth;
        this.moveInterval = this.finalPosition / this.options.numSteps;

        this.log.debug("finalPosition:" + this.finalPosition);
        this.log.debug("moveInterval:" + this.moveInterval);

        var interval = 1000 / this.options.fps;
        this.log.debug("Setting interval of " + interval);

        this.intervalID = window.setInterval((function() { this.enterFrame(); }).bind(this), interval);

        this.isInitialized = true;


        $(this.options.btnLeft).observe("mouseover", this.moveRight.bind(this));
        $(this.options.btnRight).observe("mouseover", this.moveLeft.bind(this));


        $(this.options.btnLeft).observe("mouseout", this.stopMoving.bind(this));
        $(this.options.btnRight).observe("mouseout", this.stopMoving.bind(this));
        //Event.observe('window', "mouseup", (function(){this.stopMoving()}).bind(this) );

    },
    enterFrame: function() {

        if (this.isInitialized) {
            this.currentPosition += 1;
        }

        this.currentPosition += this.currentInterval;

        this.currentPosition = this.currentPosition > this.finalPosition ? this.finalPosition : this.currentPosition;
        this.currentPosition = this.currentPosition < this.initialPosition ? this.initialPosition : this.currentPosition;

        var dist = this.currentPosition - this.currentX;

        var step = 0;
        step = this.transition(dist);


        this.currentX += Math.floor(step);
        this.slidingContent.setStyle({ left: -this.currentX + "px", position: "relative" });

    },
    moveLeft: function() {
        this.isInitialized = false;
        this.log.info("Start moving left");
        this.currentInterval = this.moveInterval;
        this.transition = ImageSlider.Transition;
    },
    moveRight: function() {
        this.isInitialized = false;
        this.log.info("Start moving right");
        this.currentInterval = -this.moveInterval;
        this.transition = ImageSlider.Transition;
    },
    stopMoving: function() {
        this.isInitialized = false;
        this.log.info("Stop moving");
        this.currentInterval = 0;

        var dist = this.currentPosition - this.currentX;
        if (dist > 400) this.currentPosition = this.currentX + 400;
        if (dist < -400) this.currentPosition = this.currentX - 400;
        this.transition = ImageSlider.Transition;

    },
    nextPage: function() {
        this.log.info("NextPage: " + this.random);
        this.currentPosition += (this.areaWidth - (this.areaWidth / this.options.numSteps));
        this.transition = ImageSlider.PageTransition;
    },
    previousPage: function() {
        this.log.info("PrevPage: " + this.random);
        this.currentPosition -= (this.areaWidth - (this.areaWidth / this.options.numSteps));
        this.transition = ImageSlider.PageTransition;
    },
    dispose: function() {

        Event.stopObserving(this.options.btnLeft, 'mouseover', this.moveRight.bind(this));
        Event.stopObserving(this.options.btnLeft, 'mouseover', this.moveLeft.bind(this));
        Event.stopObserving(this.options.btnLeft, 'mouseout', this.stopMoving.bind(this));
        Event.stopObserving(this.options.btnLeft, 'mouseout', this.stopMoving.bind(this));
        window.clearInterval(this.intervalID);
    }
};


