/**
 * @author Alexandr Averyanov (aversan@yandex.ru)
 * @requires jQuery
 *
 * Description:
 * Slider
 */

var Slider = function()	{

	this.mainContainer = null
	this.menuContainer = null
	this.slidersContainer = null
	this.selectedMenuLabel = null
	this.holder = null
	this.currentSlideIndex = null
	this.aSliders = []
	this.animateDuration = 0
	this.onTimer = false
	this.timerInterval = 0
	this.animateDuration = 0
}

Slider.prototype.Init = function(params, sliders) {

	var _this = this

	if (
		!params.elements || !params.elements.mainContainer || !( this.mainContainer = $("#" + params.elements.mainContainer) ) || !this.mainContainer.length
		|| !params.elements.menuContainer || !( this.menuContainer = $("#" + params.elements.menuContainer) ) || !this.menuContainer.length
		|| !params.elements.slidersContainer || !( this.slidersContainer = $("#" + params.elements.slidersContainer) ) || !this.slidersContainer.length
		|| !sliders || !(this.aSliders = sliders) || !this.aSliders.length
	) { return this.OnError() }

	this.selectedMenuLabel = this.menuContainer.find('.option:first-child > LABEL[for!=]').parent().addClass('option_selected').end()
	if ( !this.selectedMenuLabel ) { return this.OnError() }

	this.holder = this.menuContainer.find('.b-sw__i .holder')
	if ( this.holder ) {
		this.holder.css({
			left: this.selectedMenuLabel.parent().position().left,
			top: this.selectedMenuLabel.parent().position().top,
			width: this.selectedMenuLabel.parent().width(),
			display: 'block'
		})
	}

	this.onTimer = params.settings.onTimer || false
	this.timerInterval = parseInt(params.settings.timerInterval) || 3000
	this.animateDuration = parseInt(params.settings.animateDuration) || 800

	this.BindCurrentSlide()

	if ( this.HasSlide() ) {

		this.mainContainer.css('background-color', this.aSliders[this.currentSlideIndex].backgroundColor)
		this.mainContainer.addClass(this.aSliders[this.currentSlideIndex].colorsStyle)
		this.slidersContainer.find('.present-slide.' + this.aSliders[this.currentSlideIndex].slide).addClass('show')
	}

	this.menuContainer.find('.option > LABEL[for!=]').click(function() {

		_this.selectedMenuLabel = $(this)

		_this.BindCurrentSlide()

		if ( _this.HasSlide() ) {

			$(this).parents().filter('.option').siblings().removeClass('option_selected').end()

			_this.mainContainer.stop().animate(
				{
					backgroundColor: _this.aSliders[_this.currentSlideIndex].backgroundColor
				},
				_this.animateDuration,
				'swap1'
			)

			_this.mainContainer
				.removeClass(function() {

					var classes = []

					$.each(_this.aSliders, function(i, v) {
						classes.push(v.colorsStyle)
					})

					classes = classes.join(" ")

					return classes
				})
				.addClass(_this.aSliders[_this.currentSlideIndex].colorsStyle)

			_this.holder.stop().animate(
				{
					left: _this.selectedMenuLabel.parent().position().left,
					top: _this.selectedMenuLabel.parent().position().top,
					width: _this.selectedMenuLabel.parent().width()
				},
				_this.animateDuration,
				'swap1',
				function() {

					_this.selectedMenuLabel.parent().addClass('option_selected')

					_this.slidersContainer.find('.present-slide.' + _this.aSliders[_this.currentSlideIndex].slide).siblings().removeClass('show').end().addClass('show')

					_this.aSliders[_this.currentSlideIndex].func.call()
				}
			)
		}
	})

	this.menuContainer.find('.option > LABEL[for!=]').click(function( evt, client ) {

		if (client != 'autopilot') {

			_this.mainContainer.stopTime()
			_this.mainContainer.unbind('clickNextMenuLabel')
			_this.onTimer = false
		}
		else {

			evt.stopPropagation()
		}
	})

	this.mainContainer.click(function() {

			_this.mainContainer.stopTime()
			_this.mainContainer.unbind('clickNextMenuLabel')
			_this.onTimer = false
	})

	if (this.onTimer) {

		this.mainContainer.bind('clickNextMenuLabel', function() {

			if (_this.currentSlideIndex < _this.aSliders.length - 1)
				_this.mainContainer.find('.option > LABEL[for=' + _this.aSliders[_this.currentSlideIndex + 1].label + ']').eq(0).trigger('click', 'autopilot')
			else
				_this.mainContainer.find('.option > LABEL[for=' + _this.aSliders[0].label + ']').eq(0).trigger('click', 'autopilot')
		})

		this.mainContainer.everyTime(this.timerInterval, function() {

			_this.mainContainer.trigger('clickNextMenuLabel')
		})
	}
}

Slider.prototype.HasSlide = function() {

	return (this.currentSlideIndex === null) ? false : true
}

Slider.prototype.BindCurrentSlide = function() {

	var _this = this

	this.UnbindCurrentSlide()

	$.each(this.aSliders, function(index, item) {

		if (_this.selectedMenuLabel.attr('for') == item.label) {

			_this.currentSlideIndex = index
		}
	})
}

Slider.prototype.UnbindCurrentSlide = function() {

	this.currentSlideIndex = null
}

Slider.prototype.OnError = function() {

	this.mainContainer.hide()
	return false
}
