Spry.Widget.TelekomCarousel = function(element, img, title, description, idx) {

    var options = {};
    if (idx) {
        options.defaultPanel = idx;
    } 
    Spry.Widget.TelekomCarousel.baseConstructor.call(this, element, options);
    
    this.image = img;
    this.title = title;
    this.description = description;
    
    if (idx) {
        this.imageIndex = idx;
    } else {
        this.imageIndex = 0;
    }
}

KevLinDev.extend(Spry.Widget.TelekomCarousel, Spry.Widget.SlidingPanels);

Spry.Widget.TelekomCarousel.prototype.setUrlArray = function(uA) {
    this.urlArray = uA;
}

Spry.Widget.TelekomCarousel.prototype.setImageIndex = function(newIndex)
{
    this.imageIndex = newIndex;
    var selectedImage = this.urlArray[this.imageIndex];
    this.image.src = selectedImage.url; 
    this.title.innerHTML = selectedImage.title;
    this.description.innerHTML = selectedImage.description;
    this.showPanel(this.imageIndex); 
};

Spry.Widget.TelekomCarousel.prototype.incImageIndex = function()
{
    var numPanels = this.getContentPanelsCount();
    
    if(this.imageIndex < numPanels - 1)
    {
        this.setImageIndex(this.imageIndex + 1);
    }
};

Spry.Widget.TelekomCarousel.prototype.descImageIndex = function()
{
    if(this.imageIndex > 0)
    {
        this.setImageIndex(this.imageIndex - 1);
    }
};


/**
 * Ugyanaz, mint a showPanel, csak 1-gyel eltolja az indexet balra, kivéve elsot, amikor nem, és 
 * az utolsót, amikor meg 2-vel.
 */
Spry.Widget.TelekomCarousel.prototype.showPanel=function(elementOrIndex)  {

    var pIndex = -1;
    
    if (typeof elementOrIndex == "number")
        pIndex = elementOrIndex;
    else // Must be the element for the content panel.
        pIndex = this.getContentPanelIndex(elementOrIndex);

    var numPanels = this.getContentPanelsCount();
    if (numPanels > 0)
        pIndex = (pIndex >= numPanels) ? numPanels - 1 : pIndex;
    else
        pIndex = 0;

    var panel = this.getContentPanels()[pIndex];
    var contentGroup = this.getContentGroup();

    if (panel && contentGroup)
    {
        if (this.currentPanel)
            this.removeClassName(this.currentPanel, this.currentPanelClass);
        this.currentPanel = panel;

        var refPanel;
        
        if(pIndex == 0)
            refPanel = this.getContentPanels()[pIndex];
        else if(pIndex == numPanels - 1)
            refPanel = this.getContentPanels()[pIndex - 2];
        else 
            refPanel = this.getContentPanels()[pIndex - 1];
            
        var nx = - refPanel.offsetLeft;
        var ny = - refPanel.offsetTop;

        if (this.enableAnimation)
        {
            if (this.animator)
                this.animator.stop();
            var cx = contentGroup.offsetLeft;
            var cy = contentGroup.offsetTop;
            if (cx != nx || cy != ny)
            {
                var self = this;
                this.addClassName(this.element, this.animatingClass);
                this.animator = new Spry.Widget.SlidingPanels.PanelAnimator(contentGroup, cx, cy, nx, ny, { duration: this.duration, fps: this.fps, transition: this.transition, finish: function()
                {
                    self.removeClassName(self.element, self.animatingClass);
                    self.addClassName(panel, self.currentPanelClass);
                } });
                this.animator.start();
            }
        }
        else
        {
            contentGroup.style.left = nx + "px";
            contentGroup.style.top = ny + "px";
            this.addClassName(panel, this.currentPanelClass);
        }
    }

    return panel;
};

/**
 * Mivel utolsónál 2-vel van eltolva, így ott a showPrev eseményre 2-t kell görgetni. 
 */
Spry.Widget.TelekomCarousel.prototype.showPreviousPanel = function()
{
    var index = this.getContentPanelIndex(this.currentPanel);
    var numPanels = this.getContentPanelsCount();
    if (index == numPanels - 1)
        {
            index--;
        }
    return this.showPanel(--index);
};

/**
 * Mivel elsonél nincs eltolva, így ott 2-t kell görgetni. 
 */
Spry.Widget.TelekomCarousel.prototype.showNextPanel = function()
{
    var index = this.getContentPanelIndex(this.currentPanel);
    var numPanels = this.getContentPanelsCount();
        
    if (index == 0)
    {
        index++;
    }
    return this.showPanel(++index);
};




