// construct
var CSlider = function(options, scrollOptions)
{
	this.options = {}; $.extend(this.options, this.defaultOptions, options||{});
	this.scrollOptions = {}; $.extend(this.scrollOptions, this['defaultOptions_'+this.options.scrollType], scrollOptions||{});
	this.currentIndex = 0;
	this.slideItems = $(this.options.slideItems);
	this.currentItem = $(this.slideItems.get(this.currentIndex));
	this.lastIndex = this.slideItems.length-1;
	this.contentStripe = $(this.options.contentStripe);

	// init scroll style
	this['initScroll_'+this.options.scrollType]();
	this._scroll = this['scroll_'+this.options.scrollType];
	
	// init prev-next controlls and show next (optional)
	this.scrollPrevControl = $(this.options.scrollPrevControl).click(this.onScrollPrev.scope(this));
	this.scrollNextControl = $(this.options.scrollNextControl).click(this.onScrollNext.scope(this)).removeClass('hidden');
	
	// init previews controls (optional)
	this.previewItems = $(this.options.previewItems).click(this.onPreviewClick.scope(this));
	
	// init scroll on idle (optional) 
	this.options.idleScroll && this.initIdleScroll();
}
	// 'roll' type scroll performer
	CSlider.prototype.initScroll_roll = function(){
		this.lastIndex -= (this.scrollOptions.showSlides-1);
		this.stepWidth = this.currentItem.outerWidth(true);
	}
	// 'fade' type scroll performer
	CSlider.prototype.initScroll_fade = function(){
		$(this.slideItems.css('opacity',0).get(this.currentIndex)).css('opacity',1);
	}

// init scroll on idle 
CSlider.prototype.initIdleScroll = function()
{
	this.idleSlideTimeout = null;
	this.setIdleSlideTimeout();
	this.contentStripe
		.mouseenter(this.clearIdleSlideTimeout.scope(this))
		.mouseleave(this.setIdleSlideTimeout.scope(this));

	if(this.options.idleScrollIntroOnly){
		this.previewItems.one('click', function(){
			this.contentStripe
				.unbind('mouseenter', this.clearIdleSlideTimeout)
				.unbind('mouseleave', this.setIdleSlideTimeout);
		}.scope(this));
	}
}
	CSlider.prototype.clearIdleSlideTimeout = function(){
		clearTimeout(this.idleSlideTimeout);
	}
	CSlider.prototype.setIdleSlideTimeout = function(){
		this.idleSlideTimeout = setTimeout(this.idleSlide.scope(this),this.options.idleScrollInterval);
	}
	CSlider.prototype.idleSlide = function(){
		var toIndex = this.currentIndex+1;
		if(toIndex > this.lastIndex)
			toIndex = 0;
		this.scroll(toIndex);
		this.setIdleSlideTimeout();
	}

// prev-next controls handlers
CSlider.prototype.onScrollPrev = function(){
	this.scroll(this.currentIndex-1);
}
CSlider.prototype.onScrollNext = function(){
	this.scroll(this.currentIndex+1);
}

// previews handlers
CSlider.prototype.onPreviewClick = function(event){ 
	this.scroll(this.previewItems.index($(event.target)));
}

// common scroll performer
CSlider.prototype.scroll = function(toIndex){
	clearTimeout(this.idleSlideTimeout)
	if(toIndex!=this.currentIndex && toIndex >=0 && toIndex <= this.lastIndex && !this.contentStripe.is(':animated')){
		this._scroll(toIndex);
		this.currentIndex = toIndex;
		this.currentItem = $(this.slideItems.get(this.currentIndex));
		this.update();
	}
}
	// 'roll' type scroll performer 
	CSlider.prototype.scroll_roll = function(toIndex){
		this.contentStripe.animate({left: -toIndex*this.stepWidth}, {queue: false, duration: this.options.duration});
	}
	// 'fade' type scroll performer
	CSlider.prototype.scroll_fade = function(toIndex){
		$(this.slideItems.get(this.currentIndex)).animate({opacity:0},{queue: false, duration: this.options.duration});
		$(this.slideItems.get(toIndex)).animate({opacity:1},{queue: false, duration: this.options.duration});
	}

// gui updater
CSlider.prototype.update = function(){
	this.scrollPrevControl[this.currentIndex<=0?'addClass':'removeClass']('hidden');
	this.scrollNextControl[this.currentIndex>=this.lastIndex?'addClass':'removeClass']('hidden');
	$(this.previewItems.removeClass('selected').get(this.currentIndex)).addClass('selected');
	if(typeof this.options.onScroll == 'function') this.options.onScroll();
}

// default CSlider options
CSlider.prototype.defaultOptions = {
	scrollType: 'roll'				// sroll type ('roll', 'fade')
	, idleScroll: false
	, idleScrollInterval: 4000
	, idleScrollIntroOnly: true
	, onClick: null
};
CSlider.prototype.defaultOptions_roll = {
	showSlides: 1
};
CSlider.prototype.defaultOptions_fade = {};


$(function(){
	new CSlider({
		scrollType: 'roll'
		, duration: 300
		, contentStripe: $('#option-slider')
		, slideItems: $('#option-slider>div')
		, scrollPrevControl: $('#option-slider-left')
		, scrollNextControl: $('#option-slider-right')
	},{
		showSlides: 6
	});
});




/* Rechagre option item */

$(function(){
	var block = $('#recharge-item'),
		plate = block.find('.recharge-plate:first');

	block.hover(function(){plate.show()}, function(){plate.hide()});
});
