/**
 * 
 * 
 */
function picturePlayerCarouselWidget(eleFrame,elePane, eleButtonLeft, eleButtonRight)
{ 
    var Me = this;
    
    this.uid = picturePlayerCarouselWidget._widgets.length;
    picturePlayerCarouselWidget._widgets.push(this);
    
    dojo.require("dojo.fx");
    dojo.require("dojo.fx.easing");
    
    this._eleFrame       = eleFrame;
    this._elePane        = elePane
    this._eleButtonLeft  = eleButtonLeft;
    this._eleButtonRight = eleButtonRight
    
    this._frameWidth = this._eleFrame.offsetWidth;
    
    this._scrollStep = this._frameWidth;
    
    var lastItem = this._elePane.lastChild;
    while (lastItem.nodeType !== 1 && lastItem){lastItem = lastItem.previousSibling;}

    this._paneWidth = lastItem.offsetLeft + lastItem.offsetWidth;
    
    this._scrollMax   = Math.abs(this._frameWidth - this._paneWidth);
    
    this._eleButtonRight.onclick = function()
    {
        Me.scrollClick('left');
    }
    this._eleButtonRight.onmouseover = function()
    {
        Me.autoScrollStart('left');
        Me._enableMakeVisible = false;
    }
    this._eleButtonRight.onmouseout = function()
    {
        Me.autoScrollEnd();
        Me._enableMakeVisible = true;
    }
    
    this._eleButtonLeft.onclick = function()
    {
        Me.scrollClick('right');
    }
    this._eleButtonLeft.onmouseover = function()
    {
        Me.autoScrollStart('right');
        Me._enableMakeVisible = false;
    }
    this._eleButtonLeft.onmouseout = function()
    {
        Me.autoScrollEnd();
        Me._enableMakeVisible = true;
    }
    
    this._eleFrame.onmouseover = function()
    {
        Me._enableMakeVisible = false;
    }
    this._eleFrame.onmouseout = function()
    {
        Me._enableMakeVisible = true;
    }
    
    this._inited = true;  
};

picturePlayerCarouselWidget._widgets = [];
picturePlayerCarouselWidget.get = function(uid)
{
        var widget = null;
        for( var i=0; i<this._widgets.length; i++ )
        {
            if( this._widgets[i].uid == uid )
            {
                widget = this._widgets[i];
                break;
            }
        }
        return widget; 
}

picturePlayerCarouselWidget.prototype.uid;
picturePlayerCarouselWidget.prototype._eleFrame;
picturePlayerCarouselWidget.prototype._elePane;
picturePlayerCarouselWidget.prototype._eleButtonLeft;
picturePlayerCarouselWidget.prototype._eleButtonRight;
picturePlayerCarouselWidget.prototype._inited = false;

picturePlayerCarouselWidget.prototype._scrollMax = 0;
picturePlayerCarouselWidget.prototype._paneWidth = 0;
picturePlayerCarouselWidget.prototype._frameWidth = 0;
picturePlayerCarouselWidget.prototype._scrollStep = 100;

picturePlayerCarouselWidget.prototype._scroll = function(direction, animate, step)
{
    if( arguments.length < 2 ){animate = true;}
    if( arguments.length < 3 ){step    = this._scrollStep;}

    var scrollPos   = this._elePane.offsetLeft;

    if( isNaN(scrollPos) ){scrollPos = 0;}

    if( this._paneWidth > this._frameWidth )
    {
        var newPos  = direction=='left' ? scrollPos - step : scrollPos+step ;

        this._movePane(newPos,animate);
    }
};          
picturePlayerCarouselWidget.prototype._movePane = function(newPos,animate)
{
    if( arguments.length < 2 ){animate = false;}
    
    if(newPos > 0 ){ newPos = 0; }
    
    if(  Math.abs(newPos) > this._scrollMax ) { newPos = 0 - this._scrollMax; }
    
    if( animate )
    {
        dojo.animateProperty({
                  node: this._elePane,
                  easing: dojo.fx.easing['linear'],
                  duration: 100,
                  properties: { left: newPos }
                }).play();
    }
    else
    {
        this._elePane.style.left = newPos + 'px';//anim nelkul
    }
};
picturePlayerCarouselWidget.prototype._enableMakeVisible = true;
picturePlayerCarouselWidget.prototype.makeItemVisible = function(eleItem)
{
    if( !this._enableMakeVisible || this._autoScrollState  ){ return; }
    
    if( eleItem.offsetLeft < Math.abs(this._elePane.offsetLeft)  )
    {
        this._movePane( (0-eleItem.offsetLeft) ,true);
    }
    if( (eleItem.offsetLeft+eleItem.offsetWidth) > this._frameWidth+this._elePane.offsetLeft )
    {
        this._movePane( (0-(eleItem.offsetLeft+eleItem.offsetWidth-this._frameWidth)) ,true);
    }
};
picturePlayerCarouselWidget.prototype._autoScrollState = false;
picturePlayerCarouselWidget.prototype._autoScrollDirection = '';
picturePlayerCarouselWidget.prototype._autoScrollTimer = null;
picturePlayerCarouselWidget.prototype.scrollClick = function(direction)
{
    this.autoScrollEnd();
    this._scroll(direction);
};
picturePlayerCarouselWidget.prototype.autoScrollStart = function(direction)
{
    this._autoScrollState     = true; 
    this._autoScrollDirection = direction;
    this._autoScrollSlide();
};
picturePlayerCarouselWidget.prototype.autoScrollEnd = function()
{
    this._autoScrollState     = false; 
    clearTimeout(this._autoScrollTimer);
};     
picturePlayerCarouselWidget.prototype._autoScrollSlide = function()
{
    var Me = this;
 
    if( this._autoScrollState == false ){return;}

    this._scroll(this._autoScrollDirection, false, 2);

    this._autoScrollTimer = setTimeout( function(){ Me._autoScrollSlide() },10);
};
