﻿function ImageSlider(aSlider, aLeft, aRight, aCenter, aSpeed, aMax, aStart)
{
    var me = this;

    this.slider = $(aSlider);
    this.left = aLeft?$(aLeft):null;
    this.right = aRight?$(aRight):null;
    if(this.slider!=null)
    {
        this.container=null;
        for(var i=0;i<this.slider.childNodes.length;i++)
        {
            var o=this.slider.childNodes[i];
            if(o.tagName=='DIV' && o.className=='container')
            {
                this.container = o;
                break;
            }
        }
    
        if(this.container!=null)
        {
            this.loaded = false;    
            this.deadSpan = aCenter?aCenter:6;
            this.maxSpeed = aSpeed?aSpeed:7;
            this.innerWidth = 0;
            this.sliderWidth = 0;

            // State savers
            this.speed = 0;
            this.direction = 0; // -1: Left; 0: Dead; 1: Right
            this.leftTimer = null;
            this.rightTimer = null;
            
            this.init();
        }
        else { throw 'ImageSlider: Could not find a valid container!'; } 
    }
    else { throw 'ImageSlider: Could not find the ['+aSlider+'] object.'; } 
}

ImageSlider.prototype.init = function()
{
    var me=this;
    
    this.sliderWidth = this.slider.offsetWidth;
    this.innerWidth=this.container.offsetWidth;
    this.container.style.left = "0px";
    if(this.left!=null && this.right!=null)
    {
        this.left.onmouseover=function(e){ me.Slide(e); }
        this.left.onmouseout=function(e){ me.Stop(e); }
        this.right.onmouseover=function(e){ me.Slide(e); }
        this.right.onmouseout=function(e){ me.Stop(e); }
    }
    else
    {
        this.slider.onmousemove=function(e){ me.Slide(e); }
        this.slider.onmouseout=function(e){ me.Stop(e); }
    }
    this.slider.onclick=function(e){ me.Stop(e); }

    this.loaded=true;
}

ImageSlider.prototype.Slide = function(e)
{
    var lSlider = this.leftOffset(this.slider),
        lWindow=(window.pageXOffset)? pageXOffset: document.body.scrollLeft,
        lCursor=window.event? event.clientX : e.clientX? e.clientX: "",
        lLeft=(this.sliderWidth-this.deadSpan)/2,
        lRight=(this.sliderWidth+this.deadSpan)/2;
        
    lCursor-=lSlider-lWindow;
    if (lCursor>lRight && (this.right!=null?_$esrc(e)==this.right:true))
    {
        this.speed=(lCursor-lLeft)/((this.sliderWidth-this.deadSpan)/2) * this.maxSpeed;
        clearTimeout(this.rightTimer);
        if (this.direction!=-1) { this.slideLeft(); }
    }
    else
    if (lCursor<lLeft && (this.left!=null?_$esrc(e)==this.left:true))
    {
        this.speed=(lRight-lCursor)/((this.sliderWidth-this.deadSpan)/2) * this.maxSpeed;
        clearTimeout(this.leftTimer);
        if (this.direction!=1) { this.slideRight(); }
    }
    else
    {
        this.speed=0;
    }
}

ImageSlider.prototype.leftOffset=function(aObject)
{
    var lTotal=aObject.offsetLeft,
        lParent=aObject.offsetParent;
    
    while (lParent!=null)
    {
        lTotal+=lParent.offsetLeft;
        lParent=lParent.offsetParent;
    }
    return lTotal;
}

ImageSlider.prototype.Stop = function(e)
{
    if(!window.opera || (window.opera && e.relatedTarget!==null))
    {
        if( (this.right!=null?_$esrc(e)==this.right:true) || (this.left!=null?_$esrc(e)==this.left:true) || (window.event && !this.slider.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget != e.relatedTarget))
        {
            clearTimeout(this.leftTimer);
            clearTimeout(this.rightTimer);
            this.direction=0;
        }
    }
}

ImageSlider.prototype.slideLeft = function ()
{
    var me = this;
    if(this.loaded)
    {
        this.direction=-1;
        if (parseInt(this.container.style.left)>(this.sliderWidth-this.innerWidth))
        {
            this.container.style.left=parseInt(this.container.style.left)-this.speed+"px";
        }
    }
    this.leftTimer=setTimeout( function() {me.slideLeft()}, 10);
}

ImageSlider.prototype.slideRight = function ()
{
    var me = this;
    if(this.loaded)
    {
        this.direction=1;
        if(parseInt(this.container.style.left)<0)
        {
            this.container.style.left=parseInt(this.container.style.left)+this.speed+"px";
        }
    }
    this.rightTimer = setTimeout(function() {me.slideRight()},10);
}
