/**
 * Created by JetBrains PhpStorm.
 * User: thomaskasper
 * Date: 07.11.11
 * Time: 14:18
 * To change this template use File | Settings | File Templates.
 */
jQuery(function() {
    var thumbSliderHandler = new thumbsSlider();
    thumbSliderHandler.init();
    $(document).bind("referencesOverview-updated",
        function ()
        {
            thumbSliderHandler.init();
        }
    );
});

function thumbsSlider() {

}

thumbsSlider.prototype.init = function()
{
    //container
    this.$container = $('#referenzenOverview .inner');
    this.$containerWidth = this.$container.width();

    //thumbs
    this.$thumb = $('.referenzenThumbnail');
    this.$scrollByWidth = this.$thumb.width();
    this.$countThumbs = $('#referenzenOverview .inner .referenzenThumbnail').size();
    this.$thumbholder = $('#referenzenOverview .inner');
    this.$thumbholderWidth = $('#referenzenOverview .inner').css('width',this.$countThumbs*this.$scrollByWidth);

    //navigation controls
    this.$next = $('span.right');
    this.$prev = $('span.left');

    //Animation
    this.inAnimation = false;
    this.anitime = 250;
    this.fadeSpeed = 250;

    //offset: difference between container mask and thumbholder
    this.$offset = this.$containerWidth - this.$thumbholderWidth.width();

    this.$position = 0;

    this.registerClickHandler();

}


thumbsSlider.prototype.registerClickHandler = function() {

    var _this = this;

    if (this.needsToBeScrolled()){
        $('#referenzenOverview span.right').click(function()
        {
            _this.next();
        });
    }

    $('#referenzenOverview span.left').click(function()
    {
        _this.prev();
    });

    jQuery('.styledSelect').change(function(){
        var _this = this;


        var width = $('.referenzenThumbnail').width();
        var count = $('#referenzenOverview .inner .referenzenThumbnail').size();

        $('#referenzenOverview .inner').width(width*count);
    })
};

thumbsSlider.prototype.next = function()
{
    var _this = this;
    var currentPosition = this.currentPosition();

    if (this.inAnimation) {
        return;
    } else if (currentPosition <= this.$offset) {
        this.$next.hide();
        return;
    }

    this.inAnimation = true;

    this.$container.animate({
        left: '-='+_this.$scrollByWidth
    }, _this.anitime, function() {
        _this.inAnimation = false;
        _this.$position = _this.currentPosition();

        _this.$prev.fadeIn(_this.fadeSpeed);
        if (_this.$position <= _this.$offset) {

            _this.$next.fadeOut(_this.fadeSpeed);

        }
    });
}

thumbsSlider.prototype.prev = function()
{
    var _this = this;
    var currentPosition = this.currentPosition();

    if (this.inAnimation ) {
        return;
    } else if (currentPosition >= 0) {
        this.$prev.hide();
        return;
    }

    this.inAnimation = true;
    this.$container.animate({
        left: '+='+_this.$scrollByWidth
    }, _this.anitime, function() {
        _this.$position = _this.currentPosition();

        if (_this.currentPosition()==0) {
            _this.$prev.fadeOut(_this.fadeSpeed);
            _this.$next.fadeIn(_this.fadeSpeed);
        }
        _this.inAnimation=false;
    });
};

/*
 * Check wheter thumbcontainer has to be scrolled
 */
thumbsSlider.prototype.needsToBeScrolled = function()
{
   var _this = this;


   if (_this.$containerWidth < _this.$thumbholder.width() ) {
       this.$next.fadeIn(_this.fadeSpeed);
       return true;
   } else {
       return false;
   }
}


/**
 * get the current position of the thumbholder and trim the 'px'
 */
thumbsSlider.prototype.currentPosition = function()
{
    var currentPosition = this.$thumbholder.css('left');
    currentPosition = Number(currentPosition.slice(0, currentPosition.indexOf('px')));
    return currentPosition;
}

thumbsSlider.prototype.setThumbContainerWidth = function()
{
    this.$countThumbs = $('#referenzenOverview .inner .referenzenThumbnail').size();
    this.$thumbholderWidth = $('#referenzenOverview .inner').css('width',this.$countThumbs*this.$scrollByWidth);
}

//var index = jQuery('#controlWrap div').index(this);

/*

 var _this = this;
 var currentPosition = this.currentPosition();

 if (this.inAnimation ) {
 return;
 }

 this.inAnimation = true;
 this.$container.animate({
 left: '+='+_this.$scrollByWidth
 }, _this.anitime, function() {
 _this.$position = _this.currentPosition();

 if (_this.currentPosition()==0) {

 _this.inAnimation=true;
 _this.$prev.fadeOut(_this.fadeSpeed);
 }
 _this.inAnimation=false;
 });

    */
