function initTMBannerProduct(selCont, selSlider, selItem, selButton, config, counter) {
	if (typeof counter == 'undefined') counter=0;
	var conts = $$(selCont);
	if (!conts.length) {
		if (counter<5)
			setTimeout(function(){initTMBannerProduct(selCont, selItem, selButton, config, counter+1);},250);
		return;
	}
	for (var i=0;i<conts.length;i++) {
		if (typeof conts[i]._tmBannerProduct != 'undefined')
			continue;
		conts[i]._tmBannerProduct = new TMBannerProduct(conts[i], selSlider, selItem, selButton, config);
	}
}
function TMBannerProduct(container, selSlider, selItem, selButton, config) {
	this._container = container;
	var sliders = $(container).getElementsBySelector(selSlider);
	if (!sliders.length) return;
	this._slider = sliders[0];
	this._slider._banner = this;
	this._items = $(container).getElementsBySelector(selItem);
	this._buttons = $(container).getElementsBySelector(selButton);
	if ((this._items.length != this._buttons.length) || (this._buttons.length<2)) return;
	this._current = 0;
	this._slider.style.width = (this._items.length * jQuery(container).width()).toString()+'px';
	for (var i=0;i<this._buttons.length;i++) {
		this._buttons[i]._num = i;
		this._buttons[i]._banner = this;
		this._buttons[i]._tmBannerProductOnclick = (typeof this._buttons[i].onclick == 'function')?this._buttons[i].onclick:false;
		this._buttons[i]._tmBannerProductSwitch = TMBannerProduct.switchImage;
		this._buttons[i].onclick = function(isAuto) {
			isAuto=((typeof isAuto != 'undefined') && (isAuto===true))?true:false;
			if (this._tmBannerProductOnclick) this._tmBannerProductOnclick();
			this._tmBannerProductSwitch(isAuto);
			return false;
		}
	}
	if ((typeof config.movie != 'undefined') && (config.movie)) {
		this._movieBanner = new TMBannerProductMovie(this, config);
	}
	this.setActiveButtonClass();
}
TMBannerProduct.switchImage = function(isAuto) {
	isAuto=((typeof isAuto != 'undefined') && (isAuto===true))?true:false;
	if (this._banner._current == this._num)
		return false;
	
	var banner = this._banner;
	banner._nextNum = this._num;
	var marginLeft = 0;
	for (var i=0;i<this._num;i++) {
		marginLeft = -1 * (jQuery(banner._container).width() * this._num);
	}
	
	jQuery(banner._slider).stop().animate({'margin-left':marginLeft},'slow',function(){
		var banner = this._banner;
		banner._current = banner._nextNum;
		banner.setActiveButtonClass();
	});
	
	if (!isAuto && (typeof banner._movieBanner!='undefined')) {
		if (typeof banner._movieBanner.refreshTimeout == 'function') {
			banner._movieBanner.refreshTimeout(banner._movieBanner);
		}
	}
}
TMBannerProduct.prototype.setActiveButtonClass = function() {
	for (var i=0;i<this._buttons.length;i++) {
		if (i == this._current) {
			jQuery(this._buttons[i]).addClass('active');
		} else {
			jQuery(this._buttons[i]).removeClass('active');
		}
	}
	this._buttons[this._current];
}
TMBannerProduct.prototype.navigationSimple = function(isAuto) {
	if (this._items.length<2)
		return false;
	if (this._buttons.length==2) {
		if (this._current==0)
			this._buttons[1].onclick(isAuto);
		else
			this._buttons[0].onclick(isAuto);
		return false;
	}
	return true;
}
TMBannerProduct.prototype.prev = function(isAuto) {
	isAuto=((typeof isAuto != 'undefined') && (isAuto===true))?true:false;
	if (!this.navigationSimple(isAuto))
		return false;
}
TMBannerProduct.prototype.next = function(isAuto) {
	isAuto=((typeof isAuto != 'undefined') && (isAuto===true))?true:false;
	if (!this.navigationSimple(isAuto))
		return false;
	if (this._current+1 >= this._buttons.length) {
		var tempFirst = this._items[0];
		var tempHtml = tempFirst.innerHTML;
		var tempNext = document.createElement('div');
		tempNext.className = tempFirst.className;
		tempNext.innerHTML = tempHtml;
		tempFirst.parentNode.appendChild(tempNext);
		this._temporaryHtml = tempNext;
		var banner = this;
		var tempWidth = (jQuery(banner._container).width() * this._buttons.length);
		banner._slider.style.width = (tempWidth+jQuery(banner._container).width()).toString()+'px';
		jQuery(banner._slider).stop().animate({'margin-left':(-1*tempWidth)},'slow',function(){
			var banner = this._banner;
			banner._current = 0;
			banner.setActiveButtonClass();
			jQuery(banner._slider).css({'margin-left':0});
			banner._temporaryHtml.parentNode.removeChild(banner._temporaryHtml);
		});

		return false;
	}
	this._buttons[this._current+1].onclick(true);
	return false;
}

TMBannerProductMovie = function(banner, config) {
	if (typeof config.movie_time == 'undefined') config.movie_time = 3000;
	this._banner = banner;
	this._config = config;
	var el = this;
	el.addTimeout(el);
}
TMBannerProductMovie.prototype.movie = function(el) {
	el._banner.next(true);
	el.addTimeout(el);
}
TMBannerProductMovie.prototype.addTimeout = function(el) {
	el._timeout = setTimeout(function(){el.movie(el)}, el._config.movie_time);
}
TMBannerProductMovie.prototype.clearTimeout = function(el) {
	clearTimeout(el._timeout);
	el._timeout = null;
}
TMBannerProductMovie.prototype.refreshTimeout = function(el) {
	el.clearTimeout(el);
	el.addTimeout(el);
}
